summaryrefslogtreecommitdiffstats
path: root/ktuberling/toplevel.cpp
blob: eadedb09aac7bd330ab7d9d3b3960e1fdb9f9a5d (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/* -------------------------------------------------------------
   KDE Tuberling
   Top level window
   mailto:e.bischoff@noos.fr
 ------------------------------------------------------------- */

#include <tdeapplication.h>
#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <tdeio/netaccess.h>
#include <tdeaction.h>
#include <kstdaction.h>
#include <kstdgameaction.h>

#include <kprinter.h>
#include <tqclipboard.h>

#include "toplevel.moc"
#include "playground.h"
#include "soundfactory.h"

// Constructor
TopLevel::TopLevel()
  : TDEMainWindow(0)
{
  readOptions();

  gameboards = 0;
  languages = 0;
  playGround = new PlayGround(this, "playground", selectedGameboard);
  soundFactory = new SoundFactory(this, "sounds", selectedLanguage);

  setCentralWidget(playGround);

  setupTDEAction();
}

// Destructor
TopLevel::~TopLevel()
{
}

// Enable or disable "undo" button and menu item
void TopLevel::enableUndo(bool enable) const
{
  actionCollection()->action(KStdAction::stdName(KStdAction::Undo))->setEnabled(enable);
}

// Enable or disable "redo" button and menu item
void TopLevel::enableRedo(bool enable) const
{
  actionCollection()->action(KStdAction::stdName(KStdAction::Redo))->setEnabled(enable);
}

// Register an available gameboard
void TopLevel::registerGameboard(const TQString &menuItem, const char *actionId)
{
  TDEToggleAction *t = 0;

  switch (gameboards)
  {
  	case 0: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard0()), actionCollection(), actionId);
		break;
  	case 1: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard1()), actionCollection(), actionId);
		break;
  	case 2: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard2()), actionCollection(), actionId);
		break;
  	case 3: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard3()), actionCollection(), actionId);
		break;
  	case 4: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard4()), actionCollection(), actionId);
		break;
  	case 5: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard5()), actionCollection(), actionId);
		break;
  	case 6: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard6()), actionCollection(), actionId);
		break;
  	case 7: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(gameboard7()), actionCollection(), actionId);
		break;
  }

  if( t ) {
      if (gameboards == selectedGameboard) t->setChecked(true);
      gameboardActions[gameboards] = actionId;
      gameboards++;
  }
}

// Register an available language
void TopLevel::registerLanguage(const TQString &menuItem, const char *actionId, bool enabled)
{
  TDEToggleAction *t = 0;

  switch (languages)
  {
  	case 0: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language0()), actionCollection(), actionId);
		break;
  	case 1: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language1()), actionCollection(), actionId);
		break;
  	case 2: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language2()), actionCollection(), actionId);
		break;
  	case 3: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language3()), actionCollection(), actionId);
		break;
  	case 4: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language4()), actionCollection(), actionId);
		break;
  	case 5: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language5()), actionCollection(), actionId);
		break;
  	case 6: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language6()), actionCollection(), actionId);
		break;
  	case 7: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language7()), actionCollection(), actionId);
		break;
  	case 8: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language8()), actionCollection(), actionId);
		break;
  	case 9: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language9()), actionCollection(), actionId);
		break;
  	case 10: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language10()), actionCollection(), actionId);
		break;
  	case 11: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language11()), actionCollection(), actionId);
		break;
  	case 12: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language12()), actionCollection(), actionId);
		break;
  	case 13: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language13()), actionCollection(), actionId);
		break;
  	case 14: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language14()), actionCollection(), actionId);
		break;
  	case 15: t = new TDEToggleAction(i18n(menuItem.latin1()), 0, TQT_TQOBJECT(this), TQT_SLOT(language15()), actionCollection(), actionId);
		break;
  }

  if( t ) {
      if (languages == selectedLanguage) t->setChecked(true);
      t->setEnabled(enabled);
      languageActions[languages] = actionId;
      languages++;
  }
}

// Switch to another gameboard
void TopLevel::changeGameboard(uint newGameboard)
{
  // Do not accept to switch to same gameboard
  if (newGameboard == selectedGameboard) {
    // select this gameboard again
    ((TDEToggleAction*) actionCollection()->action(gameboardActions[newGameboard].latin1()))->setChecked(true);
    return;
  }

  // Unselect preceding gameboard
  ((TDEToggleAction*) actionCollection()->action(gameboardActions[selectedGameboard].latin1()))->setChecked(false);

  // Change gameboard in the remembered options
  selectedGameboard = newGameboard;
  writeOptions();

  if( !((TDEToggleAction*) actionCollection()->action(gameboardActions[selectedGameboard].latin1()))->isChecked() )
    ((TDEToggleAction*) actionCollection()->action(gameboardActions[selectedGameboard].latin1()))->setChecked(true);
  
  // Change gameboard effectively
  playGround->change(newGameboard);

  // Disable undo and redo actions
  enableUndo(false);
  enableRedo(false);
}

// Switch to another language
void TopLevel::changeLanguage(uint newLanguage)
{
  // Do not accept to switch to same language
  if (newLanguage == selectedLanguage && soundEnabled) {
    // newLanguage should stay checked
    ((TDEToggleAction*) actionCollection()->action(languageActions[newLanguage].latin1()))->setChecked(true);
    return;
  }

  // Unselect preceding language
  if (!soundEnabled) ((TDEToggleAction*) actionCollection()->action("speech_no_sound"))->setChecked(false);
  ((TDEToggleAction*) actionCollection()->action(languageActions[selectedLanguage].latin1()))->setChecked(false);

  // Change language in the remembered options
  soundEnabled = true;
  selectedLanguage = newLanguage;
  writeOptions();

  // Change language effectively
  soundFactory->change(newLanguage);
}

// Load the layouts file
bool TopLevel::loadLayout(TQDomDocument &layoutDocument)
{
  TQFile layoutFile(TQFile::encodeName(locate("data", "ktuberling/pics/layout.xml")));
  if (!layoutFile.open(IO_ReadOnly))
     return false;

  if (!layoutDocument.setContent(&layoutFile))
  {
     layoutFile.close();
     return false;
  }
  layoutFile.close();

  return true;
}

// Play a sound
void TopLevel::playSound(const TQString &ref) const
{
  soundFactory->playSound(ref);
}

// Read options from preferences file
void TopLevel::readOptions()
{
  TDEConfig *config;
  TQString option;

  config = TDEApplication::kApplication()->config();

  config->setGroup("General");
  option = config->readEntry("Sound", "on");
  soundEnabled = option.find("on") == 0;

  option = config->readEntry("GameboardNumber", "0");
  selectedGameboard = option.toInt();
  if (selectedGameboard <= 0) selectedGameboard = 0;
  if (selectedGameboard > 7) selectedGameboard = 7;

  option = config->readEntry("LanguageNumber", "2");
  selectedLanguage = option.toInt();
  if (selectedLanguage <= 0) selectedLanguage = 0;
  if (selectedLanguage > 15) selectedLanguage = 15;

}

// Write options to preferences file
void TopLevel::writeOptions()
{
  TDEConfig *config;

  config = TDEApplication::kApplication()->config();

  config->setGroup("General");
  config->writeEntry("Sound", soundEnabled? "on": "off");

  config->writeEntry("GameboardNumber", selectedGameboard);

  config->writeEntry("LanguageNumber", selectedLanguage);

  config->sync();
}

// TDEAction initialization (aka menubar + toolbar init)
void TopLevel::setupTDEAction()
{
//Game
  KStdGameAction::gameNew(TQT_TQOBJECT(this), TQT_SLOT(fileNew()), actionCollection());
  KStdGameAction::load(TQT_TQOBJECT(this), TQT_SLOT(fileOpen()), actionCollection());
  KStdGameAction::save(TQT_TQOBJECT(this), TQT_SLOT(fileSave()), actionCollection());
  KStdGameAction::print(TQT_TQOBJECT(this), TQT_SLOT(filePrint()), actionCollection());
  KStdGameAction::quit(TQT_TQOBJECT(kapp), TQT_SLOT(quit()), actionCollection());
  (void) new TDEAction(i18n("Save &as Picture..."), 0, TQT_TQOBJECT(this), TQT_SLOT(filePicture()), actionCollection(), "game_save_picture");

//Edit
  KStdAction::copy(TQT_TQOBJECT(this), TQT_SLOT(editCopy()), actionCollection());
  KStdAction::undo(TQT_TQOBJECT(this), TQT_SLOT(editUndo()), actionCollection());
  KStdAction::redo(TQT_TQOBJECT(this), TQT_SLOT(editRedo()), actionCollection());
  enableUndo(false);
  enableRedo(false);

//Speech
  TDEToggleAction* t = new TDEToggleAction(i18n("&No Sound"), 0, TQT_TQOBJECT(this), TQT_SLOT(soundOff()), actionCollection(), "speech_no_sound");
  if (!soundEnabled) t->setChecked(true);

  setupGUI();
}

// Reset gameboard
void TopLevel::fileNew()
{
  playGround->reset();

  enableUndo(false);
  enableRedo(false);

  playGround->repaintAll();
}

// Load gameboard
void TopLevel::fileOpen()
{
  TQString dir = locate("data", "ktuberling/museum/miss.tuberling");
  dir.truncate(dir.findRev('/') + 1);

  KURL url = KFileDialog::getOpenURL(dir, "*.tuberling");

  open(url);
}

void TopLevel::open(const KURL &url)
{
  if (url.isEmpty())
    return;

  TQString name;

  TDEIO::NetAccess::download(url, name, this);

  playGround->reset();

  if (!playGround->loadFrom(name))
    KMessageBox::error(this, i18n("Could not load file."));

  enableUndo(!playGround->isFirstAction());
  enableRedo(false);

  playGround->repaintAll();

  TDEIO::NetAccess::removeTempFile( name );
}

// Save gameboard
void TopLevel::fileSave()
{
  KURL url = KFileDialog::getSaveURL
                ( TQString(),
		 "*.tuberling");

  if (url.isEmpty())
    return;

  if( !url.isLocalFile() )
  {
    KMessageBox::sorry(this,
                       i18n("Only saving to local files is currently "
                            "supported."));
    return;
  }

  TQString name = url.path();
  int suffix;

  suffix = name.findRev('.');
  if (suffix == -1)
  {
    name += ".tuberling";
  }

  if( !playGround->saveAs( name ) )
    KMessageBox::error(this, i18n("Could not save file."));
}

// Save gameboard as picture
void TopLevel::filePicture()
{
  TQPixmap picture(playGround->getPicture());

  KURL url = KFileDialog::getSaveURL
                ( TQString(),
                 i18n(  "*.xpm|UNIX Pixmaps (*.xpm)\n"
                        "*.jpg|JPEG Compressed Files (*.jpg)\n"
                        "*.png|Next Generation Pictures (*.png)\n"
                        "*.bmp|Windows Bitmaps (*.bmp)\n"
                        "*|All Picture Formats"));

  if( url.isEmpty() )
    return;

  if( !url.isLocalFile() )
  {
    KMessageBox::sorry(this,
                       i18n("Only saving to local files is currently "
                            "supported."));
    return;
  }

  TQString name = url.path();
  const char *format;
  int suffix;
  TQString end;

  suffix = name.findRev('.');
  if (suffix == -1)
  {
    name += ".xpm";
    end = "xpm";
  }
  else end = name.mid(suffix + 1, name.length());

  if (end == "xpm") format = "XPM";
  else if (end == "jpg") format = "JPEG";
  else if (end == "png") format = "PNG";
  else if (end == "bmp") format = "BMP";
  else
  {
    KMessageBox::error(this, i18n("Unknown picture format."));
    return;
  }

  if (!picture.save(name, format))
    KMessageBox::error
      (this, i18n("Could not save file."));
}

// Save gameboard as picture
void TopLevel::filePrint()
{
  KPrinter printer;
  bool ok;

  ok = printer.setup(this, i18n("Print %1").arg(actionCollection()->action(gameboardActions[selectedGameboard].latin1())->plainText()));
  if (!ok) return;
  playGround->repaint(true);
  if (!playGround->printPicture(printer))
    KMessageBox::error(this,
                         i18n("Could not print picture."));
  else
    KMessageBox::information(this,
                             i18n("Picture successfully printed."));
}

// Copy modified area to clipboard
void TopLevel::editCopy()
{
  TQClipboard *clipboard = TQApplication::clipboard();
  TQPixmap picture(playGround->getPicture());

  clipboard->setPixmap(picture);
}

// Undo last action
void TopLevel::editUndo()
{
  if (playGround->isFirstAction()) return;

  if (!playGround->undo()) return;

  if (playGround->isFirstAction()) enableUndo(false);
  enableRedo(true);

  playGround->repaintAll();
}

// Redo last action
void TopLevel::editRedo()
{
  if (playGround->isLastAction()) return;

  if (!playGround->redo()) return;

  if (playGround->isLastAction()) enableRedo(false);
  enableUndo(true);

  playGround->repaintAll();
}

// Switch to gameboard #0
void TopLevel::gameboard0()
{
  changeGameboard(0);
}

// Switch to gameboard #1
void TopLevel::gameboard1()
{
  changeGameboard(1);
}

// Switch to gameboard #2
void TopLevel::gameboard2()
{
  changeGameboard(2);
}

// Switch to gameboard #3
void TopLevel::gameboard3()
{
  changeGameboard(3);
}

// Switch to gameboard #4
void TopLevel::gameboard4()
{
  changeGameboard(4);
}

// Switch to gameboard #5
void TopLevel::gameboard5()
{
  changeGameboard(5);
}

// Switch to gameboard #6
void TopLevel::gameboard6()
{
  changeGameboard(6);
}

// Switch to gameboard #7
void TopLevel::gameboard7()
{
  changeGameboard(7);
}

// Toggle sound off
void TopLevel::soundOff()
{
  if (!soundEnabled) return;

  soundEnabled = false;
  ((TDEToggleAction*) actionCollection()->action(languageActions[selectedLanguage].latin1()))->setChecked(false);
  ((TDEToggleAction*) actionCollection()->action("speech_no_sound"))->setChecked(true);

  writeOptions();
}

// Switch to language #0
void TopLevel::language0()
{
  changeLanguage(0);
}

// Switch to language #1
void TopLevel::language1()
{
  changeLanguage(1);
}

// Switch to language #2
void TopLevel::language2()
{
  changeLanguage(2);
}

// Switch to language #3
void TopLevel::language3()
{
  changeLanguage(3);
}

// Switch to language #4
void TopLevel::language4()
{
  changeLanguage(4);
}

// Switch to language #5
void TopLevel::language5()
{
  changeLanguage(5);
}

// Switch to language #6
void TopLevel::language6()
{
  changeLanguage(6);
}

// Switch to language #7
void TopLevel::language7()
{
  changeLanguage(7);
}

// Switch to language #8
void TopLevel::language8()
{
  changeLanguage(8);
}

// Switch to language #9
void TopLevel::language9()
{
  changeLanguage(9);
}

// Switch to language #10
void TopLevel::language10()
{
  changeLanguage(10);
}

// Switch to language #11
void TopLevel::language11()
{
  changeLanguage(11);
}

// Switch to language #12
void TopLevel::language12()
{
  changeLanguage(12);
}

// Switch to language #13
void TopLevel::language13()
{
  changeLanguage(13);
}

// Switch to language #14
void TopLevel::language14()
{
  changeLanguage(14);
}

// Switch to language #15
void TopLevel::language15()
{
  changeLanguage(15);
}