summaryrefslogtreecommitdiffstats
path: root/ksokoban/MainWindow.cpp
blob: 626aded8654ce197aa4223da32ad4133476fbb51 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *  ksokoban - a Sokoban game for TDE
 *  Copyright (C) 1998  Anders Widell  <awl@hem.passagen.se>
 *
 *  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 <stdio.h>

#include <kapplication.h>
#include <tdeconfig.h>
#include <kmenubar.h>
#include <tqpopupmenu.h>
#include <tqkeycode.h>
#include <assert.h>
#include <klocale.h>
#include <tqstring.h>
#include <tdefiledialog.h>
#include <tqframe.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <tdeio/netaccess.h>
#include <kiconloader.h>
#include <tqiconset.h>
#include <tqdragobject.h>
#include <kpopupmenu.h>
#include <kurldrag.h>
#include <kstdaccel.h>

#include "MainWindow.h"
#include "PlayField.h"
#include "LevelCollection.h"

#include "MainWindow.moc"

void
MainWindow::createCollectionMenu() {
  collection_ = new TQPopupMenu(0,"collection menu");
  collection_->setCheckable(true);
  //connect(collection_, TQT_SIGNAL(activated(int)), playField_, TQT_SLOT(changeCollection(int)));
  connect(collection_, TQT_SIGNAL(activated(int)), this, TQT_SLOT(changeCollection(int)));

  for (int i=0; i<internalCollections_.collections(); i++) {
    collection_->insertItem(internalCollections_[i]->name(), i);
  }
  checkedCollection_ = 0;

  TDEConfig *cfg=(TDEApplication::kApplication())->config();
  cfg->setGroup("settings");
  int id = cfg->readNumEntry("collection", 10);

  currentCollection_ = 0;
  for (int i=0; i<internalCollections_.collections(); i++) {
    if (internalCollections_[i]->id() == id) currentCollection_ = i;
  }

  changeCollection(currentCollection_);
}


MainWindow::MainWindow() : KMainWindow(0), externalCollection_(0) {
  int i;
  TQPixmap pixmap;

  setEraseColor(TQColor(0,0,0));

  TDEConfig *cfg=(TDEApplication::kApplication())->config();
  cfg->setGroup("Geometry");
  int width = cfg->readNumEntry("width", 750);
  int height = cfg->readNumEntry("height", 562);
  resize(width, height);

  playField_ = new PlayField(this, "playfield");
  setCentralWidget(playField_);
  playField_->show();

  menu_ = new KMenuBar(this, "menubar" );

  game_ = new TQPopupMenu(0,"game menu");
  pixmap = SmallIcon("fileopen");
  game_->insertItem(TQIconSet(pixmap), i18n("&Load Levels..."), this, TQT_SLOT(loadLevels()));
  pixmap = SmallIcon("forward");
  game_->insertItem(TQIconSet(pixmap), i18n("&Next Level"), playField_, TQT_SLOT(nextLevel()), Key_N);
  pixmap = SmallIcon("back");
  game_->insertItem(TQIconSet(pixmap), i18n("&Previous Level"), playField_, TQT_SLOT(previousLevel()), Key_P);
  pixmap = SmallIcon("reload");
  game_->insertItem(TQIconSet(pixmap), i18n("Re&start Level"), playField_, TQT_SLOT(restartLevel()), Key_Escape);

  createCollectionMenu();
  game_->insertItem(i18n("&Level Collection"), collection_);

  pixmap = SmallIcon("undo");
  game_->insertItem(TQIconSet(pixmap), i18n("&Undo"), playField_, TQT_SLOT(undo()),TQKeySequence( (KStdAccel::undo()).toString()));
  pixmap = SmallIcon("redo");
  game_->insertItem(TQIconSet(pixmap), i18n("&Redo"), playField_, TQT_SLOT(redo()), TQKeySequence( (KStdAccel::redo()).toString()));
  game_->insertSeparator();
  pixmap = SmallIcon("exit");
  game_->insertItem(TQIconSet(pixmap), i18n("&Quit"), TDEApplication::kApplication(), TQT_SLOT(closeAllWindows()), TQKeySequence( (KStdAccel::quit()).toString()));
  menu_->insertItem(i18n("&Game"), game_);

  animation_ = new TQPopupMenu(0,"animation menu");
  animation_->setCheckable(true);
  connect(animation_, TQT_SIGNAL(activated(int)), this, TQT_SLOT(updateAnimMenu(int)));
  connect(animation_, TQT_SIGNAL(activated(int)), playField_, TQT_SLOT(changeAnim(int)));
  animation_->insertItem(i18n("&Slow"), 3);
  animation_->insertItem(i18n("&Medium"), 2);
  animation_->insertItem(i18n("&Fast"), 1);
  animation_->insertItem(i18n("&Off"), 0);
  checkedAnim_ = playField_->animDelay();
  updateAnimMenu(checkedAnim_);
  menu_->insertItem(i18n("&Animation"), animation_);

  pixmap = SmallIcon("bookmark_add");
  bookmarkMenu_ = new TQPopupMenu(0,"bookmarks menu");
  setBM_ = new TQPopupMenu(0, "set bookmark menu");
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 1);
  setBM_->setAccel(CTRL+Key_1, 1);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 2);
  setBM_->setAccel(CTRL+Key_2, 2);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 3);
  setBM_->setAccel(CTRL+Key_3, 3);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 4);
  setBM_->setAccel(CTRL+Key_4, 4);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 5);
  setBM_->setAccel(CTRL+Key_5, 5);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 6);
  setBM_->setAccel(CTRL+Key_6, 6);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 7);
  setBM_->setAccel(CTRL+Key_7, 7);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 8);
  setBM_->setAccel(CTRL+Key_8, 8);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 9);
  setBM_->setAccel(CTRL+Key_9, 9);
  setBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 10);
  setBM_->setAccel(CTRL+Key_0, 10);
  connect(setBM_, TQT_SIGNAL(activated(int)), this, TQT_SLOT(setBookmark(int)));
  bookmarkMenu_->insertItem(i18n("&Set Bookmark"), setBM_);

  pixmap = SmallIcon("bookmark");
  goToBM_ = new TQPopupMenu(0, "go to bookmark menu");
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 1);
  goToBM_->setAccel(Key_1, 1);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 2);
  goToBM_->setAccel(Key_2, 2);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 3);
  goToBM_->setAccel(Key_3, 3);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 4);
  goToBM_->setAccel(Key_4, 4);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 5);
  goToBM_->setAccel(Key_5, 5);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 6);
  goToBM_->setAccel(Key_6, 6);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 7);
  goToBM_->setAccel(Key_7, 7);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 8);
  goToBM_->setAccel(Key_8, 8);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 9);
  goToBM_->setAccel(Key_9, 9);
  goToBM_->insertItem(TQIconSet(pixmap), i18n("(unused)"), 10);
  goToBM_->setAccel(Key_0, 10);
  connect(goToBM_, TQT_SIGNAL(activated(int)), this, TQT_SLOT(goToBookmark(int)));
  bookmarkMenu_->insertItem(i18n("&Go to Bookmark"), goToBM_);
  menu_->insertItem(i18n("&Bookmarks"), bookmarkMenu_);

  for (i=1; i<=10; i++) {
    bookmarks_[i-1] = new Bookmark(i);
    updateBookmark(i);
  }

  help_ = helpMenu(TQString(), false);
  menu_->insertSeparator();
  menu_->insertItem(i18n("&Help"), help_);

  menu_->show();

  setAcceptDrops(true);
}

MainWindow::~MainWindow()
{
  TDEConfig *cfg=(TDEApplication::kApplication())->config();

  cfg->setGroup("Geometry");
  cfg->writeEntry("width", width());
  cfg->writeEntry("height", height());

  cfg->setGroup("settings");
  cfg->writeEntry("collection", internalCollections_[checkedCollection_]->id());

  for (int i=1; i<=10; i++) {
    delete bookmarks_[i-1];
  }


  delete externalCollection_;

  // The following line segfaults when linked against qt 1.44
  //delete help_;
  delete goToBM_;
  delete setBM_;
  delete bookmarkMenu_;
  delete animation_;
  delete collection_;
  delete game_;
  //delete menu_;

  //delete playField_;
}



void
MainWindow::focusInEvent(TQFocusEvent *) {
  playField_->setFocus();
}

void
MainWindow::updateAnimMenu(int id) {
  animation_->setItemChecked(checkedAnim_, false);
  checkedAnim_ = id;
  animation_->setItemChecked(checkedAnim_, true);
}

void
MainWindow::updateBookmark(int num) {
  int col = internalCollections_.toInternalId(bookmarks_[num-1]->collection());
  int lev = bookmarks_[num-1]->level();
  int mov = bookmarks_[num-1]->moves();

  if (col < 0 || lev < 0) return;

  TQString name;
  if (col >= 0 && col < internalCollections_.collections())
    name = internalCollections_[col]->name();
  else
    name = i18n("(invalid)");
  TQString l;
  l.setNum(lev+1);
  name += " #" + l;
  l.setNum(mov);
  name += " (" + l + ")";

  setBM_->changeItem(name, num);
  goToBM_->changeItem(name, num);
}

void
MainWindow::setBookmark(int id) {
  assert(id >= 1 && id <= 10);
  playField_->setBookmark(bookmarks_[id-1]);
  updateBookmark(id);
}

void
MainWindow::goToBookmark(int id) {
  assert(id >= 1 && id <= 10);

  Bookmark *bm = bookmarks_[id-1];
  int collection = internalCollections_.toInternalId(bm->collection());
  int level = bm->level();

  if (collection < 0 || collection >= internalCollections_.collections()) return;
  LevelCollection* colPtr = internalCollections_[collection];
  if (colPtr == 0) return;
  if (level < 0 || level >= colPtr->noOfLevels()) return;
  if (level > colPtr->completedLevels()) return;

  playField_->setUpdatesEnabled(false);
  changeCollection(collection);
  playField_->setUpdatesEnabled(true);
  playField_->goToBookmark(bookmarks_[id-1]);
}

void
MainWindow::changeCollection(int id)
{
  collection_->setItemChecked(checkedCollection_, false);
  checkedCollection_ = id;
  collection_->setItemChecked(checkedCollection_, true);

  delete externalCollection_;
  externalCollection_ = 0;
  playField_->changeCollection(internalCollections_[id]);
}

void
MainWindow::loadLevels() {
  TDEConfig *cfg=(TDEApplication::kApplication())->config();
  cfg->setGroup("settings");
  TQString lastFile = cfg->readPathEntry("lastLevelFile");

  KURL result = KFileDialog::getOpenURL(lastFile, "*", this, i18n("Load Levels From File"));
  if (result.isEmpty()) return;

  openURL(result);
}

void
MainWindow::openURL(KURL _url) {
  TDEConfig *cfg=(TDEApplication::kApplication())->config();

//   int namepos = _url.path().findRev('/') + 1; // NOTE: findRev can return -1
//   TQString levelName = _url.path().mid(namepos);
  TQString levelName = _url.fileName();

  TQString levelFile;
  if (_url.isLocalFile()) {
    levelFile = _url.path();
  } else {
//     levelFile = locateLocal("appdata", "levels/" + levelName);
    if(!TDEIO::NetAccess::download( _url, levelFile ) )
	  return;
  }

  LevelCollection *tmpCollection = new LevelCollection(levelFile, levelName);
  TDEIO::NetAccess::removeTempFile(levelFile );

  if (tmpCollection->noOfLevels() < 1) {
    KMessageBox::sorry(this, i18n("No levels found in file"));
    delete tmpCollection;
    return;
  }
  if (_url.isLocalFile()) {
    cfg->setGroup("settings");
    cfg->writePathEntry("lastLevelFile", _url.path());
  }

  delete externalCollection_;
  externalCollection_ = tmpCollection;

  collection_->setItemChecked(checkedCollection_, false);
  playField_->changeCollection(externalCollection_);


}

void
MainWindow::dragEnterEvent(TQDragEnterEvent* event) {
  event->accept(KURLDrag::canDecode(event));
}

void
MainWindow::dropEvent(TQDropEvent* event) {
  KURL::List urls;
  if (KURLDrag::decode(event, urls)) {
//     kdDebug() << "MainWindow:Handling TQUriDrag..." << endl;
     if (urls.count() > 0) {
         const KURL &url = urls.first();
         openURL(url);
     }
  }
}