1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*****************************************************************************
This file is part of the game 'KJumpingCube'
Copyright (C) 1998-2000 by Matthias Kiefer
<matthias.kiefer@gmx.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**************************************************************************** */
#include "kjumpingcube.h"
#include "kcubeboxwidget.h"
#include "version.h"
// Settings
#include "settings.h"
#include <kconfigdialog.h>
#include "prefs.h"
#include <tqregexp.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <kstdgameaction.h>
#include <kaction.h>
#include <kio/netaccess.h>
#include <kstatusbar.h>
#define ID_STATUS_TURN_TEXT 1000
#define ID_STATUS_TURN 2000
#define MESSAGE_TIME 2000
KJumpingCube::KJumpingCube()
: view(new KCubeBoxWidget(5, this, "KCubeBoxWidget"))
{
connect(view,TQT_SIGNAL(playerChanged(int)),TQT_SLOT(changePlayer(int)));
connect(view,TQT_SIGNAL(stoppedMoving()),TQT_SLOT(disableStop()));
connect(view,TQT_SIGNAL(stoppedThinking()),TQT_SLOT(disableStop()));
connect(view,TQT_SIGNAL(startedMoving()),TQT_SLOT(enableStop_Moving()));
connect(view,TQT_SIGNAL(startedThinking()),TQT_SLOT(enableStop_Thinking()));
connect(view,TQT_SIGNAL(playerWon(int)),TQT_SLOT(showWinner(int)));
// tell the KMainWindow that this is indeed the main widget
setCentralWidget(view);
// init statusbar
TQString s = i18n("Current player:");
statusBar()->insertItem(s,ID_STATUS_TURN_TEXT, false);
statusBar()->changeItem(s,ID_STATUS_TURN_TEXT);
statusBar()->setItemAlignment (ID_STATUS_TURN_TEXT, AlignLeft | AlignVCenter);
statusBar()->setFixedHeight( statusBar()->sizeHint().height() );
currentPlayer = new TQWidget(this, "currentPlayer");
currentPlayer->setFixedWidth(40);
statusBar()->addWidget(currentPlayer, ID_STATUS_TURN, false);
statusBar()->setItemAlignment(ID_STATUS_TURN, AlignLeft | AlignVCenter);
initKAction();
changePlayer(1);
}
void KJumpingCube::initKAction() {
KStdGameAction::gameNew(this, TQT_SLOT(newGame()), actionCollection());
KStdGameAction::load(this, TQT_SLOT(openGame()), actionCollection());
KStdGameAction::save(this, TQT_SLOT(save()), actionCollection());
KStdGameAction::saveAs(this, TQT_SLOT(saveAs()), actionCollection());
KStdGameAction::quit(this, TQT_SLOT(close()), actionCollection());
hintAction = KStdGameAction::hint(view, TQT_SLOT(getHint()), actionCollection());
stopAction = new KAction(i18n("Stop &Thinking"), "stop",
Qt::Key_Escape, this, TQT_SLOT(stop()), actionCollection(), "game_stop");
stopAction->setEnabled(false);
undoAction = KStdGameAction::undo(this, TQT_SLOT(undo()), actionCollection());
undoAction->setEnabled(false);
KStdAction::preferences(this, TQT_SLOT(showOptions()), actionCollection());
setupGUI();
}
void KJumpingCube::newGame(){
undoAction->setEnabled(false);
view->reset();
statusBar()->message(i18n("New Game"),MESSAGE_TIME);
}
void KJumpingCube::saveGame(bool saveAs)
{
if(saveAs || gameURL.isEmpty())
{
int result=0;
KURL url;
do
{
url = KFileDialog::getSaveURL(gameURL.url(),"*.kjc",this,0);
if(url.isEmpty())
return;
// check filename
TQRegExp pattern("*.kjc",true,true);
if(!pattern.exactMatch(url.filename()))
{
url.setFileName( url.filename()+".kjc" );
}
if(KIO::NetAccess::exists(url,false,this))
{
TQString mes=i18n("The file %1 exists.\n"
"Do you want to overwrite it?").arg(url.url());
result = KMessageBox::warningContinueCancel(this, mes, TQString::null, i18n("Overwrite"));
if(result==KMessageBox::Cancel)
return;
}
}
while(result==KMessageBox::No);
gameURL=url;
}
KTempFile tempFile;
tempFile.setAutoDelete(true);
KSimpleConfig config(tempFile.name());
config.setGroup("KJumpingCube");
config.writeEntry("Version",KJC_VERSION);
config.setGroup("Game");
view->saveGame(&config);
config.sync();
if(KIO::NetAccess::upload( tempFile.name(),gameURL,this ))
{
TQString s=i18n("game saved as %1");
s=s.arg(gameURL.url());
statusBar()->message(s,MESSAGE_TIME);
}
else
{
KMessageBox::sorry(this,i18n("There was an error in saving file\n%1").arg(gameURL.url()));
}
}
void KJumpingCube::openGame()
{
bool fileOk=true;
KURL url;
do
{
url = KFileDialog::getOpenURL( gameURL.url(), "*.kjc", this, 0 );
if( url.isEmpty() )
return;
if(!KIO::NetAccess::exists(url,true,this))
{
TQString mes=i18n("The file %1 does not exist!").arg(url.url());
KMessageBox::sorry(this,mes);
fileOk=false;
}
}
while(!fileOk);
TQString tempFile;
if( KIO::NetAccess::download( url, tempFile, this ) )
{
KSimpleConfig config(tempFile,true);
config.setGroup("KJumpingCube");
if(!config.hasKey("Version"))
{
TQString mes=i18n("The file %1 isn't a KJumpingCube gamefile!")
.arg(url.url());
KMessageBox::sorry(this,mes);
return;
}
gameURL=url;
config.setGroup("Game");
view->restoreGame(&config);
undoAction->setEnabled(false);
KIO::NetAccess::removeTempFile( tempFile );
}
else
KMessageBox::sorry(this,i18n("There was an error loading file\n%1").arg( url.url() ));
}
void KJumpingCube::stop()
{
if(view->isMoving())
undoAction->setEnabled(true);
view->stopActivities();
statusBar()->message(i18n("stopped activity"),MESSAGE_TIME);
}
void KJumpingCube::undo()
{
if(view->isActive())
return;
view->undo();
undoAction->setEnabled(false);
}
void KJumpingCube::changePlayer(int newPlayer)
{
undoAction->setEnabled(true);
currentPlayer->setBackgroundColor(newPlayer == 1 ? Prefs::color1() : Prefs::color2());
currentPlayer->repaint();
}
void KJumpingCube::showWinner(int player) {
TQString s=i18n("Winner is Player %1!").arg(player);
KMessageBox::information(this,s,i18n("Winner"));
view->reset();
}
void KJumpingCube::disableStop()
{
// toolBar()->setItemEnabled(ID_GAME_STOP_HINT,false);
// game->setItemEnabled(ID_GAME_STOP_HINT,false);
// toolBar()->setItemEnabled(ID_GAME_HINT,true);
// game->setItemEnabled(ID_GAME_HINT,true);
stopAction->setEnabled(false);
hintAction->setEnabled(true);
statusBar()->clear();
}
void KJumpingCube::enableStop_Moving()
{
// toolBar()->setItemEnabled(ID_GAME_STOP_HINT,true);
// game->setItemEnabled(ID_GAME_STOP_HINT,true);
// toolBar()->setItemEnabled(ID_GAME_HINT,false);
// game->setItemEnabled(ID_GAME_HINT,false);
stopAction->setEnabled(true);
hintAction->setEnabled(false);
statusBar()->message(i18n("Performing move."));
}
void KJumpingCube::enableStop_Thinking(){
stopAction->setEnabled(true);
hintAction->setEnabled(false);
statusBar()->message(i18n("Computing next move."));
}
/**
* Show Configure dialog.
*/
void KJumpingCube::showOptions(){
if(KConfigDialog::showDialog("settings"))
return;
KConfigDialog *dialog = new KConfigDialog(this, "settings", Prefs::self(), KDialogBase::Swallow);
dialog->addPage(new Settings(0, "General"), i18n("General"), "package_settings");
connect(dialog, TQT_SIGNAL(settingsChanged()), view, TQT_SLOT(loadSettings()));
dialog->show();
}
#include "kjumpingcube.moc"
|