Debian Codegraph @

src/toplevel.cc

raw symbols SCIP
1 // Version: $Id$
2 /***************************************************************************
3 * Copyright (C) 2002-2003 Andi Peredri *
4 * [email protected] *
5 * Copyright (C) 2004-2005 Artur Wiebe *
6 * [email protected] *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #include <QLayout>
25 #include <QApplication>
26 #include <QMenuBar>
27 #include <QToolBar>
28 #include <QDir>
29 #include <QMessageBox>
30 #include <QFileDialog>
31 #include <QCloseEvent>
32 #include <QFontDialog>
33 #include <QDebug>
34
35 #include "pdn.h"
36 #include "toplevel.h"
37 #include "newgamedlg.h"
38 #include "history.h"
39 #include "view.h"
40 #include "common.h"
41
42 #define CFG_THEME_PATH CFG_KEY"ThemePath"
43 #define CFG_FILENAME CFG_KEY"Filename"
44 #define CFG_KEEPDIALOG CFG_KEY"ShowKeepDialog"
45 #define CFG_NOTATION CFG_KEY"Notation"
46 #define CFG_NOT_ABOVE CFG_KEY"NotationAbove"
47 #define CFG_NOT_FONT CFG_KEY"NotationFont"
48 #define CFG_CLEAR_LOG CFG_KEY"ClearLogOnNewRound"
49 #define CFG_GEOMETRY CFG_KEY"WindowGeometry"
50 #define CFG_STATE CFG_KEY"WindowState"
51
52
53 myTopLevel::myTopLevel()
54 {
55 setWindowTitle(APPNAME);
56 setWindowIcon(QIcon(":/icons/logo.svg"));
57
58 m_newgame = new myNewGameDlg(this);
59 make_central_widget();
60 make_docks();
61 make_actions();
62 restore_settings();
63
64 /*
65 * new computer game.
66 */
67 m_view->newGame(m_newgame->rules(), m_newgame->freePlacement(),
68 m_newgame->name(),
69 m_newgame->isWhite(), m_newgame->opponent(),
70 m_newgame->opponentName(), m_newgame->skill());
71
72 if(layout()) {
73 //layout()->setSizeConstraint(QLayout::SetFixedSize);
74 }
75 }
76
77 void myTopLevel::make_docks() {
78 m_history = new myHistory(this);
79
80 connect(m_history, SIGNAL(previewGame(int)),
81 m_view, SLOT(slot_preview_game(int)));
82 connect(m_history, SIGNAL(applyMoves(const QString&)),
83 m_view, SLOT(slot_apply_moves(const QString&)));
84 connect(m_history, SIGNAL(newMode(bool, bool)),
85 m_view, SLOT(slot_new_mode(bool, bool)));
86 connect(m_view, SIGNAL(working(bool)),
87 m_history, SLOT(slotWorking(bool)));
88
89 m_history_dock = make_dock("history", tr("History"), Qt::RightDockWidgetArea, m_history);
90
91 m_log = new QTextEdit(this);
92 //m_log->setFixedHeight(100); //FIXME
93 m_log->setReadOnly(true);
94
95 m_log_dock = make_dock("log", tr("Log"), Qt::BottomDockWidgetArea, m_log);
96
97 }
98
99 QDockWidget* myTopLevel::make_dock(const QString& name, const QString& title, Qt::DockWidgetArea area, QWidget* widget) {
100 QDockWidget* dock = new QDockWidget(title, this);
101 dock->setAllowedAreas(Qt::AllDockWidgetAreas);
102 dock->setWidget(widget);
103 dock->setObjectName(name);
104 addDockWidget(area, dock);
105 return dock;
106 }
107
108
109 void myTopLevel::make_actions()
110 {
111 // game menu actions
112 gameNew = new QAction(QIcon(":/icons/logo.svg"), tr("&New..."), this);
113 gameNew->setShortcut(tr("CTRL+N", "File|New"));
114 connect(gameNew, SIGNAL(triggered()), this, SLOT(slot_new_game()));
115
116 gameNextRound = new QAction(QIcon(":/icons/next.svg"),
117 tr("&Next Round"), this);
118 connect(gameNextRound, SIGNAL(triggered()),
119 this, SLOT(slot_next_round()));
120
121 gameStop = new QAction(QIcon(":/icons/stop.svg"), tr("&Stop"), this);
122 connect(gameStop, SIGNAL(triggered()), m_view, SLOT(slotStopGame()));
123
124 gameOpen = new QAction(QIcon(":/icons/fileopen.svg"), tr("&Open..."),
125 this);
126 gameOpen->setShortcut(tr("CTRL+O", "File|Open"));
127 connect(gameOpen, SIGNAL(triggered()), this, SLOT(slot_open_game()));
128
129 gameSave = new QAction(QIcon(":/icons/filesave.svg"), tr("&Save..."),
130 this);
131 gameSave->setShortcut(tr("CTRL+S", "File|Save"));
132 connect(gameSave, SIGNAL(triggered()), this, SLOT(slot_save_game()));
133
134 QAction* gameQuit = new QAction(QIcon(":/icons/exit.svg"), tr("&Quit"),
135 this);
136 gameQuit->setShortcut(tr("CTRL+Q", "File|Quit"));
137 connect(gameQuit, SIGNAL(triggered()),
138 this, SLOT(close()));
139 // QApplication::instance(), SLOT(quit()));
140
141 // view menu actions
142 viewNotation = new QAction(tr("&Show Notation"), this);
143 viewNotation->setCheckable(true);
144 connect(viewNotation, SIGNAL(toggled(bool)),
145 this, SLOT(slot_notation(bool)));
146
147 viewNotationAbove = new QAction(tr("Show notation &above men"), this);
148 viewNotationAbove->setCheckable(true);
149 connect(viewNotationAbove, SIGNAL(toggled(bool)),
150 this, SLOT(slot_notation(bool)));
151
152 // settings menu
153 settingsKeep = new QAction(tr("&Confirm aborting current game"), this);
154 settingsKeep->setCheckable(true);
155 //
156 settingsClearLog = new QAction(tr("Clear &log on new round"), this);
157 settingsClearLog->setCheckable(true);
158 connect(settingsClearLog, SIGNAL(toggled(bool)),
159 m_view, SLOT(slotClearLog(bool)));
160 //
161 QAction* settingsNotationFont = new QAction(tr("&Notation font..."),
162 this);
163 connect(settingsNotationFont, SIGNAL(triggered()),
164 this, SLOT(slot_notation_font()));
165
166 // help menu actions
167 QAction* helpRules = new QAction(tr("&Rules of Play"), this);
168 helpRules->setShortcut(tr("F1", "Help|Help"));
169 connect(helpRules, SIGNAL(triggered()), this, SLOT(slot_help()));
170
171 QAction* helpAbout = new QAction(QIcon(":/icons/logo.svg"),
172 tr("&About")+" "APPNAME, this);
173 connect(helpAbout, SIGNAL(triggered()), this, SLOT(slot_about()));
174
175 QAction* helpAboutQt = new QAction(tr("About &Qt"), this);
176 connect(helpAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
177
178
179 /*
180 * toolbar
181 */
182 QToolBar* tb = addToolBar(tr("&Toolbar"));
183 tb->setMovable(false);
184 tb->addAction(gameNew);
185 tb->addAction(gameOpen);
186 tb->addAction(gameSave);
187 tb->addSeparator();
188 tb->addAction(gameNextRound);
189 tb->addAction(gameStop);
190
191
192 /*
193 * menus
194 */
195 QMenu* gameMenu = menuBar()->addMenu(myTopLevel::tr("&Game"));
196 gameMenu->addAction(gameNew);
197 gameMenu->addAction(gameOpen);
198 gameMenu->addAction(gameSave);
199 gameMenu->addSeparator();
200 gameMenu->addAction(gameNextRound);
201 gameMenu->addAction(gameStop);
202 gameMenu->addSeparator();
203 gameMenu->addAction(gameQuit);
204
205 viewMenu = menuBar()->addMenu(tr("&View"));
206 viewMenu->addAction(tb->toggleViewAction());
207 viewMenu->addSeparator();
208 viewMenu->addAction(viewNotation);
209 viewMenu->addAction(viewNotationAbove);
210 viewMenu->addAction(m_history_dock->toggleViewAction());
211 viewMenu->addAction(m_log_dock->toggleViewAction());
212
213 QMenu* settingsMenu = menuBar()->addMenu(tr("&Settings"));
214 settingsMenu->addAction(settingsKeep);
215 settingsMenu->addSeparator();
216 settingsMenu->addAction(settingsNotationFont);
217 settingsMenu->addSeparator();
218 settingsMenu->addAction(settingsClearLog);
219
220 QMenu* helpMenu = menuBar()->addMenu(tr("&Help"));
221 helpMenu->addAction(helpRules);
222 helpMenu->addSeparator();
223 helpMenu->addAction(helpAbout);
224 helpMenu->addAction(helpAboutQt);
225
226
227 /*
228 * THEMES. create a default theme.
229 */
230 QActionGroup* themes_grp = new QActionGroup(this);
231 themes_grp->setExclusive(true);
232
233 QAction* default_theme = new QAction(tr(DEFAULT_THEME), themes_grp);
234 default_theme->setCheckable(true);
235 m_themes[default_theme] = DEFAULT_THEME;
236 default_theme->setChecked(true);
237 set_theme(default_theme);//FIXME TODO
238 themes_grp->addAction(default_theme);
239
240 viewMenu->addSeparator();
241 viewMenu->addAction(default_theme);
242 read_themes(themes_grp,
243 viewMenu,QDir::homePath()+"/"USER_PATH"/"THEME_DIR);
244 //TODO-hardcoded
245 read_themes(themes_grp,
246 viewMenu, PREFIX"/share/qcheckers/"THEME_DIR);
247
248 connect(themes_grp, SIGNAL(triggered(QAction*)),
249 this, SLOT(set_theme(QAction*)));
250 }
251
252
253 void myTopLevel::read_themes(QActionGroup* menu_grp, QMenu* menu,
254 const QString& path)
255 {
256 QDir sharedir(path);
257 QStringList themes = sharedir.entryList(QDir::Dirs|QDir::Readable);
258 themes.removeAll(".");
259 themes.removeAll("..");
260
261 for(QStringList::iterator it=themes.begin(); it!=themes.end(); ++it) {
262 sharedir.cd(*it);
263
264 QString theme_dir = sharedir.absolutePath();
265 qInfo("Check: %s", qUtf8Printable(theme_dir));
266 Theme* theme = new Theme(this, theme_dir, 0);
267 if (! theme->isValid()) {
268 qWarning() << "Theme in "
269 << sharedir.absolutePath() << " is invalid";
270 } else {
271 QString themename = theme->name();
272
273 QAction* action = new QAction(themename, this);
274 menu_grp->addAction(action);
275 menu->addAction(action);
276 action->setCheckable(true);
277 m_themes[action] = theme_dir;
278 }
279
280 sharedir.cdUp();
281 }
282 }
283
284
285 void myTopLevel::restore_settings()
286 {
287 QSettings cfg(APPNAME, APPNAME);
288
289 QString theme_path = cfg.value(CFG_THEME_PATH, DEFAULT_THEME)
290 .toString();
291 for(myThemeMap::iterator it = m_themes.begin(); it!=m_themes.end();
292 it++) {
293 if(it.value() == theme_path) {
294 it.key()->setChecked(true);
295 set_theme(it.key());
296 break;
297 }
298 }
299
300 filename = cfg.value(CFG_FILENAME).toString();
301
302 viewNotation->setChecked(cfg.value(CFG_NOTATION, false).toBool());
303 viewNotationAbove->setChecked(cfg.value(CFG_NOT_ABOVE, true).toBool());
304 slot_notation(true); // here: arg is ignored by the function
305
306 bool clear_log = cfg.value(CFG_CLEAR_LOG, true).toBool();
307 settingsClearLog->setChecked(clear_log);
308 m_view->slotClearLog(clear_log);
309
310 //
311 settingsKeep->setChecked(cfg.value(CFG_KEEPDIALOG, true).toBool());
312
313 QFont cfont;
314 if(cfont.fromString(cfg.value(CFG_NOT_FONT, "").toString()))
315 m_view->setNotationFont(cfont);
316
317 // new game
318 m_newgame->readSettings(&cfg);
319
320 restoreGeometry(cfg.value(CFG_GEOMETRY).toByteArray());
321 restoreState(cfg.value(CFG_STATE).toByteArray());
322 }
323
324
325 void myTopLevel::make_central_widget()
326 {
327 m_view = new myView(this);
328
329 connect(m_view, SIGNAL(working(bool)),
330 this, SLOT(slot_working(bool)));
331
332 setCentralWidget(m_view);
333 }
334
335
336 void myTopLevel::warning(const QString& text)
337 {
338 QMessageBox::warning(this, tr("Error")+" - "APPNAME, text);
339 }
340
341
342 void myTopLevel::information(const QString& caption, const QString& text)
343 {
344 QDialog* dlg = new QDialog(this);
345 dlg->setModal(true);
346 dlg->setWindowTitle(caption+" - "APPNAME);
347
348 // logo left
349 QLabel* logo = new QLabel(dlg);
350 logo->setPixmap(QPixmap(":/icons/dialog.png"));
351
352 // text editor
353 QTextEdit* te = new QTextEdit(text, dlg);
354 te->setReadOnly(true);
355 te->setMinimumWidth(m_view->width()-100);
356 te->setMinimumHeight(m_view->height()-200);
357
358 // close button
359 QPushButton* button = new QPushButton(tr("&Close"), dlg);
360 connect(button, SIGNAL(clicked()), dlg, SLOT(accept()));
361
362 // Layout.
363 QHBoxLayout* hb = new QHBoxLayout();
364 hb->addWidget(logo, 0, Qt::AlignTop);
365 hb->addWidget(te, 1);
366
367 QHBoxLayout* hb_button = new QHBoxLayout();
368 hb_button->addStretch();
369 hb_button->addWidget(button);
370
371 QVBoxLayout* vb = new QVBoxLayout(dlg);
372 vb->addLayout(hb, 1);
373 vb->addSpacing(5);
374 vb->addLayout(hb_button);
375
376 // go
377 dlg->exec();
378 delete dlg;
379 }
380
381 myHistory* myTopLevel::get_history() const {
382 return m_history;
383 }
384
385 void myTopLevel::clear_log() {
386 m_log->clear();
387 }
388
389 void myTopLevel::append_log(const QString& text) {
390 m_log->append(text);
391 m_log->ensureCursorVisible();
392 }
393
394 void myTopLevel::slot_save_game()
395 {
396 QString fn = QFileDialog::getSaveFileName(this,
397 tr("Save Game")+" - "APPNAME, filename, "PDN Files (*."EXT")");
398 if(!fn.isEmpty()) {
399 if(fn.right(3)!=EXT) {
400 fn += "."EXT;
401 }
402
403 if(m_view->savePdn(fn)) {
404 filename = fn;
405 } else {
406 warning(tr("Could not save: ")+filename);
407 }
408 }
409 }
410
411
412 void myTopLevel::slot_open_game()
413 {
414 QString fn = QFileDialog::getOpenFileName(this,
415 tr("Open Game")+" - "APPNAME, filename, "PDN Files (*."EXT")");
416 if(!fn.isEmpty()) {
417 open(fn);
418 }
419 }
420
421
422 void myTopLevel::open(const QString& fn)
423 {
424 if(m_view->openPdn(fn)) {
425 filename = fn;
426 }
427 }
428
429
430 void myTopLevel::closeEvent(QCloseEvent* e)
431 {
432 if(!keep_game()) {
433 store_settings();
434 e->accept();
435 } else {
436 e->ignore();
437 }
438 }
439
440
441 void myTopLevel::store_settings()
442 {
443 QSettings config(APPNAME, APPNAME);
444
445 for(myThemeMap::iterator it=m_themes.begin(); it!=m_themes.end();
446 it++) {
447 if(it.key()->isChecked()) {
448 config.setValue(CFG_THEME_PATH, it.value());
449 break;
450 }
451 }
452
453 config.setValue(CFG_FILENAME, filename);
454
455 config.setValue(CFG_NOTATION, viewNotation->isChecked());
456 config.setValue(CFG_NOT_ABOVE, viewNotationAbove->isChecked());
457
458 config.setValue(CFG_KEEPDIALOG, settingsKeep->isChecked());
459 config.setValue(CFG_NOT_FONT, m_view->notationFont().toString());
460 config.setValue(CFG_CLEAR_LOG, settingsClearLog->isChecked());
461
462 // new game
463 m_newgame->writeSettings(&config);
464
465 config.setValue(CFG_GEOMETRY, saveGeometry());
466 config.setValue(CFG_STATE, saveState());
467 }
468
469
470 void myTopLevel::set_theme(QAction* action)
471 {
472 QString path = m_themes[action];
473 m_view->setTheme(path);
474 }
475
476
477 void myTopLevel::slot_help()
478 {
479 QString text =
480 tr("<p>In the beginning of game you have 12 checkers (men). "
481 "The men move forward only. The men can capture:"
482 "<ul>"
483 "<li>by jumping forward only (english rules);"
484 "<li>by jumping forward or backward (russian rules)."
485 "</ul>"
486 "<p>A man which reaches the far side of the board becomes a king. "
487 "The kings move forward or backward:"
488 "<ul>"
489 "<li>to one square only (english rules);"
490 "<li>to any number of squares (russian rules)."
491 "</ul>"
492 "<p>The kings capture by jumping forward or backward. "
493 "Whenever a player is able to make a capture he must do so.");
494
495 information(tr("Rules of Play"), text);
496 }
497
498
499 void myTopLevel::slot_about()
500 {
501 QString text =
502 APPNAME", a board game.<br>Version "VERSION"<br><br>"
503 COPYRIGHT"<br>"
504 "<a href=\""HOMEPAGE"\">"HOMEPAGE"</a><br><br>"
505 "Contributors:<br>"CONTRIBS"<br><br>"
506 "This program is distributed under the terms "
507 "of the GNU General Public License.";
508
509 information(tr("About"), text);
510 }
511
512
513 void myTopLevel::slot_new_game()
514 {
515 if(keep_game())
516 return;
517
518 if(m_newgame->exec()==QDialog::Accepted) {
519 m_view->newGame(m_newgame->rules(), m_newgame->freePlacement(),
520 m_newgame->name(),
521 m_newgame->isWhite(), m_newgame->opponent(),
522 m_newgame->opponentName(), m_newgame->skill());
523 }
524 }
525
526
527 void myTopLevel::slot_working(bool working)
528 {
529 bool disable = !working;
530
531 gameNew->setEnabled(disable);
532 gameSave->setEnabled(disable);
533
534 gameNextRound->setEnabled(disable);// FIXME !m_view->isAborted());
535 gameOpen->setEnabled(disable);
536 gameStop->setEnabled(!disable);
537
538 m_view->setEnabled(disable);
539 }
540
541
542 bool myTopLevel::keep_game()
543 {
544 if(!settingsKeep->isChecked() || m_view->isAborted()) {
545 return false;
546 }
547
548 int answer = QMessageBox::question(this, tr("Abort Game?")+" - "APPNAME,
549 tr("Current game will be lost if you continue.\n"
550 "Do you really want to discard it?"),
551 QMessageBox::Yes, QMessageBox::No);
552
553 if(answer == QMessageBox::Yes) {
554 return false;
555 }
556
557 return true;
558 }
559
560
561 void myTopLevel::slot_next_round()
562 {
563 if(!keep_game()) {
564 m_view->slotNextRound();
565 }
566 }
567
568
569 void myTopLevel::slot_notation_font()
570 {
571 bool ok;
572 QFont font = QFontDialog::getFont(&ok, m_view->notationFont(), this);
573 if(ok) {
574 m_view->setNotationFont(font);
575 }
576 }
577
578
579 void myTopLevel::slot_notation(bool)
580 {
581 m_view->setNotation(viewNotation->isChecked(),
582 viewNotationAbove->isChecked());
583 }
584