Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ZSameWidget.cpp

Go to the documentation of this file.
00001 /* Yo Emacs, this is -*- C++ -*- */
00002 /*
00003  *   ksame 0.4 - simple Game
00004  *   Copyright (C) 1997,1998  Marcus Kreutzberger
00005  *
00006  *   This program is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with this program; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 #include <opie2/oapplicationfactory.h>
00023 #include <opie2/oresource.h>
00024 
00025 #include <qtoolbar.h>
00026 #include <qmenubar.h>
00027 #include <qapplication.h>
00028 #include <qaction.h>
00029 #include <qmessagebox.h>
00030 
00031 #include <kapplication.h>
00032 
00033 #include "ZSameWidget.h"
00034 
00035 static int default_colors=3;
00036 
00037 #define i18n tr
00038 
00039 
00040 using namespace Opie::Core;
00041 OPIE_EXPORT_APP( OApplicationFactory<ZSameWidget> )
00042 
00043 
00044 
00045 ZSameWidget::ZSameWidget( QWidget* parent, const char* name,  WFlags fl )
00046     : QMainWindow( parent, name, fl )
00047 {
00048     setCaption(tr("ZSame"));
00049 
00050     setToolBarsMovable( false );
00051     QToolBar* con = new QToolBar( this );
00052     con->setHorizontalStretchable( true );
00053     QMenuBar* mb = new QMenuBar( con );
00054     QToolBar* tb = new QToolBar( this );
00055 
00056     QPopupMenu* fileMenu = new QPopupMenu( this );
00057 
00058     QAction* a = new QAction(tr("New Game"), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
00059                              QString::null, 0, this, "new_icon");
00060     a->addTo( fileMenu );
00061     a->addTo( tb );
00062     connect(a, SIGNAL(activated()), this, SLOT(m_new()));
00063 
00064     a = new QAction(tr("Restart This Board"), Opie::Core::OResource::loadPixmap( "redo", Opie::Core::OResource::SmallIcon ),
00065                               QString::null, 0, this, "restart_board" );
00066     a->addTo( fileMenu );
00067     connect( a, SIGNAL(activated()), this, SLOT(m_restart()));
00068     restart = a;
00069 
00070     a = new QAction( tr("Undo"), Opie::Core::OResource::loadPixmap( "undo", Opie::Core::OResource::SmallIcon ),
00071                      QString::null, 0, this, "undo_action" );
00072     a->addTo( fileMenu );
00073     a->addTo( tb );
00074     connect( a, SIGNAL(activated()), this, SLOT(m_undo()));
00075 
00076     a = new QAction(tr("Quit"), Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon ),
00077                               QString::null, 0, this, "quit_action");
00078     a->addTo( fileMenu );
00079     a->addTo( tb );
00080     connect(a, SIGNAL(activated()), this, SLOT(m_quit()));
00081 
00082     mb->insertItem(tr("Game" ), fileMenu );
00083 
00084     int foo[2];
00085     desktop_widget(foo);
00086     stone = new StoneWidget(this,foo[0],foo[1]);
00087 
00088     connect( stone, SIGNAL(s_gameover()), this, SLOT(gameover()));
00089 
00090     connect( stone, SIGNAL(s_colors(int)), this, SLOT(setColors(int)));
00091     connect( stone, SIGNAL(s_board(int)), this, SLOT(setBoard(int)));
00092     connect( stone, SIGNAL(s_marked(int)), this, SLOT(setMarked(int)));
00093     connect( stone, SIGNAL(s_score(int)), this, SLOT(setScore(int)));
00094     connect( stone, SIGNAL(s_remove(int,int)), this, SLOT(stonesRemoved(int,int)));
00095 
00096     connect(stone, SIGNAL(s_sizechanged()), this, SLOT(sizeChanged()));
00097 
00098     sizeChanged();
00099     setCentralWidget(stone);
00100 
00101 
00102     setScore(0);
00103 }
00104 
00105 ZSameWidget::~ZSameWidget() {
00106 
00107 }
00108 
00109 void ZSameWidget::readProperties(Config *) {
00110 /*
00111   Q_ASSERT(conf);
00112   stone->readProperties(conf);
00113 */
00114 }
00115 
00116 void ZSameWidget::saveProperties(Config *) {
00117 /*
00118   Q_ASSERT(conf);
00119   stone->saveProperties(conf);
00120   conf->sync();
00121 */
00122 }
00123 
00124 void ZSameWidget::sizeChanged() {
00125 //      stone->setFixedSize(stone->sizeHint());
00126 }
00127 
00128 void ZSameWidget::newGame(unsigned int board,int colors) {
00129         while (board>=1000000) board-=1000000;
00130         // kdDebug() << "newgame board " << board << " colors " << colors << endl;
00131         stone->newGame(board,colors);
00132         setScore(0);
00133 }
00134 
00135 bool ZSameWidget::confirmAbort() {
00136     return stone->isGameover() ||
00137         stone->isOriginalBoard() ||
00138         (QMessageBox::warning(this, i18n("Resign"), i18n("<qt>Do you want to resign?</qt>"),
00139                               QMessageBox::Yes,
00140                               QMessageBox::No|QMessageBox::Default|QMessageBox::Escape, 0) == QMessageBox::Yes );
00141 }
00142 
00143 void ZSameWidget::m_new() {
00144     if (confirmAbort())
00145         newGame(_random(),default_colors);
00146 
00147 }
00148 
00149 void ZSameWidget::m_restart() {
00150         if (confirmAbort())
00151                 newGame(stone->board(),default_colors);
00152 }
00153 
00154 void ZSameWidget::m_load() {
00155 //  kdDebug() << "menu load not supported" << endl;
00156 }
00157 
00158 void ZSameWidget::m_save() {
00159 //  kdDebug() << "menu save not supported" << endl;
00160 }
00161 
00162 void ZSameWidget::m_undo() {
00163 //      Q_ASSERT(stone);
00164         stone->undo();
00165 }
00166 
00167 
00168 void ZSameWidget::m_showhs() {
00169 /*  Q_ASSERT(stone);
00170   stone->unmark();
00171   KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
00172   d.addField(Board, i18n("Board"), "Board");
00173   d.exec();
00174 */
00175 }
00176 
00177 void ZSameWidget::m_quit() {
00178 //  Q_ASSERT(stone);
00179   stone->unmark();
00180   qApp->quit();
00181 //  delete this;
00182 }
00183 
00184 void ZSameWidget::m_tglboard() {
00185 //      kdDebug() << "toggled" << endl;
00186 }
00187 
00188 
00189 void ZSameWidget::setColors(int ) {
00190 //      status->changeItem(i18n("%1 Colors").arg(colors),1);
00191 }
00192 
00193 void ZSameWidget::setBoard(int ) {
00194 //      status->changeItem(i18n("Board: %1").arg(board, 6), 2);
00195 }
00196 
00197 void ZSameWidget::setMarked(int ) {
00198 //  status->changeItem(i18n("Marked: %1").arg(m, 6),3);
00199 }
00200 
00201 void ZSameWidget::stonesRemoved(int,int) {
00202 //      KNotifyClient::event("stones removed",
00203 //         i18n("%1 stones removed.").arg(stone->marked()));
00204 }
00205 
00206 void ZSameWidget::setScore(int ) {
00207 //  status->changeItem(i18n("Score: %1").arg(score, 6),4);
00208 //  undo->setEnabled(stone->undoPossible());
00209 //  restart->setEnabled(!stone->isOriginalBoard());
00210 }
00211 
00212 void ZSameWidget::gameover() {
00213 //  kdDebug() << "GameOver" << endl;
00214   if (stone->hasBonus()) {
00215           QMessageBox::information(this,i18n("Game won"),
00216                   i18n("<qt>You even removed the last stone, great job! "
00217                            "This gave you a score of %1 in total.</qt>").arg(stone->score()));
00218           stone->clearBonus();
00219   } else {
00220           QMessageBox::information(this,i18n("Game over"),
00221                   i18n("<qt>There are no more removeable stones. "
00222                            "You got a score of %1 in total.</qt>").arg(stone->score()));
00223   }
00224   stone->unmark();
00225 }
00226 
00227 void ZSameWidget::desktop_widget(int *f)const{
00228 
00229     QWidget* wid = QApplication::desktop();
00230     /*  width > height landscape mode */
00231     if ( wid->width() > wid->height() ) {
00232         f[0]=15;
00233         f[1]=9;
00234     }
00235     /* normal */
00236     else{
00237         f[0]=12;
00238         f[1]=13;
00239     }
00240 }
00241 
00242 

Generated on Sat Nov 5 16:17:28 2005 for OPIE by  doxygen 1.4.2