2 Copyright (c) 2006-2008 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006-2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "mainwindow.h"
12 #include <boost/scoped_ptr.hpp>
14 #include <QKeySequence>
15 #include <QStackedWidget>
16 #include <QDockWidget>
17 #include <QCloseEvent>
18 #include <QTextStream>
22 #include <KActionCollection>
24 #include <KFileDialog>
27 #include <kio/netaccess.h>
28 #include <KMessageBox>
30 #include <KStandardAction>
31 #include <KTemporaryFile>
33 #include "actioncollection.h"
34 #include "chesstable.h"
40 #include "controllers/editgame.h"
41 #include "controllers/editposition.h"
42 #include "engineinfo.h"
43 #include "movelist_table.h"
44 #include "icsconnection.h"
46 #include "mastersettings.h"
49 #include "pgnparser.h"
50 #include "pref_highlight.h"
51 #include "pref_preferences.h"
52 #include "tabwidget.h"
55 using namespace boost
;
57 MainWindow::~MainWindow() {
62 MainWindow::MainWindow(const QString
& variant
)
64 , m_ui(actionCollection()) {
65 setObjectName("tagua_main");
66 m_main
= new TabWidget(this);
67 m_main
->setTabBarHidden(true);
68 setCentralWidget(m_main
);
70 m_movelist_stack
= new QStackedWidget
;
72 connect(m_main
, SIGNAL(currentChanged(int)),
73 this, SLOT(changeTab(int)));
74 connect(m_main
, SIGNAL(closeTab()),
75 this, SLOT(closeTab()));
77 movelist_dock
= new QDockWidget(this);
78 movelist_dock
->setWidget(m_movelist_stack
);
79 movelist_dock
->setWindowTitle(i18n("Move list"));
80 movelist_dock
->setObjectName("move_list");
81 addDockWidget(Qt::LeftDockWidgetArea
, movelist_dock
, Qt::Vertical
);
82 movelist_dock
->show();
84 ChessTable
* board
= new ChessTable
;
88 console_dock
= new QDockWidget(this);
89 console
= new Console(0, i18n("FICS Connection"));
90 console_dock
->setWidget(console
);
91 console_dock
->setFocusProxy(console
);
92 console_dock
->setWindowTitle(i18n("Console"));
93 console_dock
->setObjectName("console");
94 addDockWidget(Qt::BottomDockWidgetArea
, console_dock
, Qt::Horizontal
);
95 console_dock
->setWindowFlags(console_dock
->windowFlags() & ~Qt::WindowStaysOnTopHint
);
98 settings().onChange(this, "settingsChanged");
100 connect(board
, SIGNAL(error(ErrorCode
)), this, SLOT(displayErrorMessage(ErrorCode
)));
101 //BROKEN connect(board->clock(), SIGNAL(labelClicked(int)), &ui(), SLOT(setTurn(int)));
107 // start in edit game mode
108 newGame(variant
, AbstractPosition::Ptr(), true);
109 updateVariantActions();
112 ChessTable
* MainWindow::table() {
113 return qobject_cast
<ChessTable
*>(m_main
->currentWidget());
116 KAction
* MainWindow::installRegularAction(const QString
& name
, const KIcon
& icon
, const QString
& text
, QObject
* obj
, const char* slot
) {
117 KAction
* temp
= new KAction(icon
, text
, this);
118 actionCollection()->addAction(name
, temp
);
119 connect(temp
, SIGNAL(triggered(bool)), obj
, slot
);
123 void MainWindow::setupEngineMenu() {
124 QMenu
* engine_menu
= 0;
125 SettingArray engine_settings
= settings().group("engines").array("engine");
126 foreach (Settings s
, engine_settings
) {
128 // this way the menu is created only if there is at least one engine
129 engine_menu
= menuBar()->addMenu(i18n("E&ngines"));
133 EngineDetails engine_details
;
134 engine_details
.load(s
);
136 EngineInfo
* engine
= new EngineInfo(engine_details
, ui());
138 m_engines
.push_back(engine
);
140 QMenu
* menu
= engine_menu
->addMenu(engine_details
.name
);
143 KAction
* play_white
= new KAction(i18n("Play as &white"), this);
144 play_white
->setCheckable(true);
145 connect(play_white
, SIGNAL(triggered()), engine
, SLOT(playAsWhite()));
146 menu
->addAction(play_white
);
149 KAction
* play_black
= new KAction(i18n("Play as &black"), this);
150 play_black
->setCheckable(true);
151 connect(play_black
, SIGNAL(triggered()), engine
, SLOT(playAsBlack()));
152 menu
->addAction(play_black
);
154 #if 0 // disable analysis for the moment
156 KAction
* analyze
= new KAction(i18n("&Analyze"), this);
157 analyze
->setCheckable(true);
158 connect(analyze
, SIGNAL(triggered()), engine
, SLOT(analyze()));
159 menu
->addAction(analyze
);
165 void MainWindow::setupActions() {
168 KStandardAction::openNew(this, SLOT(newGame()), actionCollection());
169 KStandardAction::open(this, SLOT(loadGame()), actionCollection());
170 KStandardAction::save(this, SLOT(saveGame()), actionCollection());
171 KStandardAction::saveAs(this, SLOT(saveGameAs()), actionCollection());
172 KStandardAction::quit(this, SLOT(quit()), actionCollection());
173 KStandardAction::preferences(this, SLOT(preferences()), actionCollection());
175 installRegularAction("back", KIcon("go-previous"), i18n("&Back"), &ui(), SLOT(back()));
176 installRegularAction("forward", KIcon("go-next"), i18n("&Forward"), &ui(), SLOT(forward()));
177 installRegularAction("begin", KIcon("go-first"), i18n("Be&gin"), &ui(), SLOT(gotoFirst()));
178 installRegularAction("end", KIcon("go-last"), i18n("&End"), &ui(), SLOT(gotoLast()));
179 installRegularAction("connect", KIcon("network-connect"), i18n("&Connect"), this, SLOT(icsConnect()));
180 installRegularAction("disconnect", KIcon("network-disconnect"),
181 i18n("&Disconnect"), this, SLOT(icsDisconnect()));
183 KStandardAction::undo(&ui(), SLOT(undo()), actionCollection());
184 KStandardAction::redo(&ui(), SLOT(redo()), actionCollection());
186 // installRegularAction("pgnCopy", KIcon("edit-copy"), i18n("Copy PGN"), this, SLOT(pgnCopy()));
187 // installRegularAction("pgnPaste", KIcon("edit-paste"), i18n("Paste PGN"), this, SLOT(pgnPaste()));
188 installRegularAction("editPosition", KIcon("edit"), i18n("&Edit position"), this, SLOT(editPosition()));
189 installRegularAction("clearBoard", KIcon("edit-delete"), i18n("&Clear board"), &ui(), SLOT(clearBoard()));
190 installRegularAction("setStartingPosition", KIcon("contents"), i18n("&Set starting position"),
191 &ui(), SLOT(setStartingPosition()));
192 // installRegularAction("copyPosition", KIcon(), i18n("&Copy position"), &ui(), SLOT(copyPosition()));
193 // installRegularAction("pastePosition", KIcon(), i18n("&Paste position"), &ui(), SLOT(pastePosition()));
194 tmp
= installRegularAction("flip", KIcon("object-rotate-left"), i18n("&Flip view"), this, SLOT(flipView()));
195 tmp
->setShortcut(Qt::CTRL
+ Qt::Key_F
);
196 installRegularAction("toggleConsole", KIcon("utilities-terminal"), i18n("Toggle &console"), this, SLOT(toggleConsole()));
197 installRegularAction("toggleMoveList", KIcon("view-list-tree"), i18n("Toggle &move list"), this, SLOT(toggleMoveList()));
200 void MainWindow::updateVariantActions(bool unplug
) {
201 ActionCollection
* variant_actions
= m_ui
.variantActions();
203 unplugActionList("variantActions");
204 if (variant_actions
) {
205 plugActionList("variantActions", variant_actions
->actions());
208 kWarning() << "No variant actions";
212 void MainWindow::readSettings() { }
213 void MainWindow::writeSettings() { }
215 void MainWindow::closeEvent(QCloseEvent
* e
) {
222 void MainWindow::keyPressEvent(QKeyEvent
* event
) {
223 if (event
->key() == Qt::Key_F5
) {
224 ui().createCtrlAction();
228 void MainWindow::keyReleaseEvent(QKeyEvent
* event
) {
229 if (event
->key() == Qt::Key_F5
) {
230 ui().destroyCtrlAction();
234 void MainWindow::changeTab(int index
) {
235 m_ui
.setCurrentTab(m_main
->currentWidget());
236 m_movelist_stack
->setCurrentIndex(index
);
237 updateVariantActions();
240 void MainWindow::closeTab() {
241 if (m_main
->count() > 1) {
242 int old_index
= m_main
->currentIndex();
243 ChessTable
* old_board
= table();
245 int new_index
= old_index
- 1;
247 new_index
= old_index
+ 1;
248 m_main
->setCurrentIndex(new_index
);
250 m_main
->removeTab(old_index
);
251 m_movelist_stack
->removeWidget(m_movelist_stack
->widget(old_index
));
252 m_ui
.removeController(old_board
);
254 if (m_main
->count() <= 1) {
255 m_main
->setTabBarHidden(true);
258 #if 0 // this doesn't work... why?
259 ChessTable
* old_board
= table();
260 m_ui
.removeController(old_board
);
261 m_movelist_stack
->removeWidget(
262 m_movelist_stack
->widget(m_main
->currentIndex()));
263 m_main
->removeTab(m_main
->currentIndex());
267 if (m_main
->count() <= 1) {
268 m_main
->hideTabBar();
271 // update ui controller (just in case...)
272 m_ui
.setCurrentTab(m_main
->currentWidget());
277 void MainWindow::createTab(ChessTable
* board
, const shared_ptr
<Controller
>& controller
,
278 const QString
& caption
, int index
) {
280 index
= m_main
->addTab(board
, caption
);
282 m_main
->insertTab(index
, board
, caption
);
284 m_ui
.addController(board
, controller
);
285 // m_ui.setCurrentTab(board);
286 m_movelist_stack
->addWidget(board
->moveListTable());
288 m_main
->setCurrentIndex(index
);
289 m_movelist_stack
->setCurrentIndex(index
);
291 if (m_main
->count() > 1) m_main
->setTabBarHidden(false);
295 void MainWindow::cleanupGame(const QString
& what
, const QString
& result
) {
302 void MainWindow::cleanupGame() {
307 bool MainWindow::newGame(const QString
& variantName
, AbstractPosition::Ptr startingPos
,
309 VariantPtr variant
= Variants::instance().get(variantName
);
311 kWarning() << "no variant" << variantName
<< "found";
312 variant
= Variants::instance().get("chess");
316 ChessTable
* board
= newTab
? new ChessTable
: table();
317 QString text
= QString("Editing %1 game").arg(variant
->name().toLower());
319 shared_ptr
<Controller
> controller(new EditGameController(
320 board
, variant
, startingPos
));
322 createTab(board
, controller
, text
);
325 unplugActionList("variantActions");
326 ui().setController(controller
);
327 table()->setPlayers(Player(), Player());
328 m_main
->setTabText(m_main
->currentIndex(), text
);
329 updateVariantActions(false);
334 kError() << "Could not find the chess variant";
341 void MainWindow::editPosition() {
344 shared_ptr
<Controller
> controller(new EditPositionController(table(), ChessVariant::info()));
345 m_main
->setTabText(m_main
->currentIndex(), "Editing chess position");
346 ui().setController(controller
);
350 void MainWindow::setupGame(const GameInfo
* gameInfo
, const PositionInfo
& style12
) {
351 QString variantCode
= gameInfo
->variant();
352 VariantPtr variant
= Variants::instance().get(variantCode
);
353 shared_ptr
<EditGameController
> controller(new EditGameController(
355 Q_ASSERT(style12
.relation
== PositionInfo::NotMyMove
||
356 style12
.relation
== PositionInfo::MyMove
);
359 int side
= (style12
.relation
== PositionInfo::MyMove
^ style12
.turn
== 0) ? 0 : 1;
361 if (controller
->addICSPlayer(side
, style12
.gameNumber
, m_connection
)) {
362 ui().setController(controller
);
363 table()->setPlayers(gameInfo
->white(), gameInfo
->black());
364 m_main
->setTabText(m_main
->currentIndex(),
365 QString("FICS Game - %1 vs %2").arg(style12
.whitePlayer
)
366 .arg(style12
.blackPlayer
));
370 void MainWindow::setupExaminedGame(const GameInfo
* gameInfo
, const PositionInfo
& style12
) {
371 shared_ptr
<EditGameController
> controller(
372 new EditGameController(table(), Variants::instance().get(gameInfo
->variant())));
373 if (controller
->setExaminationMode(style12
.gameNumber
, m_connection
)) {
374 table()->setPlayers(Player(style12
.whitePlayer
, -1),
375 Player(style12
.blackPlayer
, -1));
376 ui().setController(controller
);
377 m_main
->setTabText(m_main
->currentIndex(),
378 QString("Examining - %1 vs %2").arg(style12
.whitePlayer
)
379 .arg(style12
.blackPlayer
));
384 void MainWindow::setupObservedGame(const GameInfo
* g
, const PositionInfo
& style12
) {
385 std::auto_ptr
<ChessTable
> board(new ChessTable
);
387 shared_ptr
<EditGameController
> controller(new EditGameController(
388 board
.get(), Variants::instance().get(g
->variant())));
389 if (controller
->setObserveMode(style12
.gameNumber
, m_connection
)) {
390 board
->setPlayers(Player(style12
.whitePlayer
, -1),
391 Player(style12
.blackPlayer
, -1));
392 // ui().setController(controller);
393 //kDebug() << "adding tab";
394 createTab(board
.get(), controller
,
395 QString("Observing - %1 vs %2").arg(style12
.whitePlayer
)
396 .arg(style12
.blackPlayer
));
401 void MainWindow::setupPGN(const QString
& s
) {
404 std::map
<QString
, QString
>::const_iterator var
= pgn
.m_tags
.find("Variant");
407 if (var
== pgn
.m_tags
.end())
410 variant
= var
->second
;
412 newGame(variant
, PositionPtr(), false);
416 bool MainWindow::openFile(const QString
& filename
) {
417 QFileInfo
info(filename
);
420 KMessageBox::sorry(this, i18n("You have specified a folder"), i18n("Error"));
424 if(!info
.exists() || !info
.isFile()) {
425 KMessageBox::sorry(this, i18n("The specified file does not exist"), i18n("Error"));
429 QFile
file(filename
);
431 if(!file
.open(QIODevice::ReadOnly
)) {
432 KMessageBox::sorry(this, i18n("You do not have read permission to this file."), i18n("Error"));
436 QTextStream
stream(&file
);
438 codec
= QTextCodec::codecForLocale();
439 stream
.setCodec(codec
);
441 setupPGN(stream
.readAll());
442 //ui().pgnPaste(stream.readAll());
446 void MainWindow::loadGame() {
447 KUrl url
= KFileDialog::getOpenUrl(KUrl(), "*.pgn", this, i18n("Open PGN file"));
453 if (KIO::NetAccess::download(url
, tmp_file
, this)) {
455 KIO::NetAccess::removeTempFile(tmp_file
);
458 KMessageBox::error(this, KIO::NetAccess::lastErrorString());
461 void MainWindow::saveGame() {
462 if (ui().url().isEmpty())
465 ui().setUrl(saveGame(ui().url()));
468 void MainWindow::saveGameAs() {
469 ui().setUrl(saveGame(KFileDialog::getSaveUrl(KUrl(), "*.pgn", this, i18n("Save PGN file"))));
472 bool MainWindow::checkOverwrite(const KUrl
& url
) {
473 if (!url
.isLocalFile())
476 QFileInfo
info(url
.path());
480 return KMessageBox::Cancel
!= KMessageBox::warningContinueCancel(this,
481 i18n("A file named \"%1\" already exists. "
482 "Are you sure you want to overwrite it?" , info
.fileName()),
483 i18n("Overwrite File?"),
484 KGuiItem(i18n("&Overwrite")));
487 KUrl
MainWindow::saveGame(const KUrl
& url
) {
488 if (!checkOverwrite(url
))
494 if (!url
.isLocalFile()) {
495 // save in a temporary file
496 KTemporaryFile tmp_file
;
499 if (!KIO::NetAccess::upload(tmp_file
.fileName(), url
, this))
503 QFile
file(url
.path());
504 if (!file
.open(QIODevice::WriteOnly
))
512 void MainWindow::saveFile(QFile
& file
) {
513 QTextStream
stream(&file
);
515 codec
= QTextCodec::codecForLocale();
516 stream
.setCodec(codec
);
517 stream
<< ui().currentPGN() << "\n";
520 void MainWindow::createConnection(const QString
& username
, const QString
& password
,
521 const QString
& host
, quint16 port
,
522 const QString
& timeseal
, const QString
& timeseal_cmd
) {
523 m_connection
= shared_ptr
<ICSConnection
>(new ICSConnection
);
525 connect(m_connection
.get(), SIGNAL(established()), this, SLOT(onEstablishConnection()));
526 connect(m_connection
.get(), SIGNAL(hostLookup()), this, SLOT(onHostLookup()));
527 connect(m_connection
.get(), SIGNAL(hostFound()), this, SLOT(onHostFound()));
529 connect(m_connection
.get(), SIGNAL(receivedLine(QString
, int)), console
, SLOT(displayText(QString
, int)));
530 connect(m_connection
.get(), SIGNAL(receivedText(QString
, int)), console
, SLOT(displayText(QString
, int)));
532 connect(console
, SIGNAL(receivedInput(const QString
&)), m_connection
.get(), SLOT(sendText(const QString
&)));
533 connect(console
, SIGNAL(notify()), this, SLOT(flash()));
535 connect(m_connection
.get(), SIGNAL(loginPrompt()), this, SLOT(sendLogin()));
536 connect(m_connection
.get(), SIGNAL(registeredNickname()), this, SLOT(sendBlankPassword()));
537 connect(m_connection
.get(), SIGNAL(prompt()), this, SLOT(prompt()));
540 connect(m_connection
.get(), SIGNAL(creatingExaminedGame(const GameInfo
*, const PositionInfo
&)),
541 this, SLOT(setupExaminedGame(const GameInfo
*, const PositionInfo
&)));
542 connect(m_connection
.get(), SIGNAL(endingExaminedGame(int)), this, SLOT(cleanupGame()));
544 connect(m_connection
.get(), SIGNAL(creatingObservedGame(const GameInfo
*, const PositionInfo
&)),
545 this, SLOT(setupObservedGame(const GameInfo
*, const PositionInfo
&)));
546 connect(m_connection
.get(), SIGNAL(endingObservedGame(int)), this, SLOT(cleanupGame()));
549 connect(m_connection
.get(), SIGNAL(creatingGame(const GameInfo
*, const PositionInfo
&)),
550 this, SLOT(setupGame(const GameInfo
*, const PositionInfo
&)));
551 connect(m_connection
.get(), SIGNAL(endingGame(const QString
&, const QString
&)),
552 this, SLOT(cleanupGame(const QString
&, const QString
&)));
553 connect(m_connection
.get(), SIGNAL(notification()), this, SLOT(flash()));
555 m_connection
->establish(host
.toAscii().constData(), port
, timeseal
, timeseal_cmd
);
558 // send username / password combination
559 if (!username
.isEmpty()) {
560 m_connection
->sendText(username
);
561 m_connection
->sendText(password
);
564 quickConnectDialog
.reset();
567 void MainWindow::icsConnect() {
568 quickConnectDialog
= shared_ptr
<QConnect
>(new QConnect(this));
569 connect(quickConnectDialog
.get(),
570 SIGNAL(acceptConnection(const QString
&,
577 SLOT(createConnection(const QString
&,
583 quickConnectDialog
->show();
586 void MainWindow::destroyConnection() {
587 m_connection
.reset();
590 void MainWindow::icsDisconnect() {
596 void MainWindow::testConnect() {
597 Settings s_ics
= settings().group("ics");
598 if (s_ics
["username"]) {
599 QString username
= s_ics
["username"].value
<QString
>();
600 QString password
= (s_ics
["password"] | "");
601 QString host
= (s_ics
["icsHost"] | "freechess.org");
602 quint16 port
= (s_ics
["icsPort"] | 5000);
603 createConnection(username
, password
, host
, port
, QString(), QString() );
609 void MainWindow::onEstablishConnection() {
610 // kDebug() << "connection established";
613 void MainWindow::onConnectionError(int ) {
614 // kDebug() << "connection error (" << code << ")";
617 void MainWindow::onHostLookup() {
618 // kDebug() << "hostLookup..." << std::flush;
621 void MainWindow::onHostFound() {
622 // kDebug() << "found";
626 void MainWindow::sendLogin() {
627 // kDebug() << "sending username";
628 // connection->sendText(connection->username());
631 void MainWindow::sendBlankPassword() {
632 m_connection
->sendText("");
635 void MainWindow::prompt() {
636 if (!m_connection
->initialized()) {
637 m_connection
->startup();
638 m_connection
->setInitialized();
642 void MainWindow::newGame() {
643 AbstractPosition::Ptr pos
= ui().position();
644 scoped_ptr
<NewGame
> dialog(new NewGame(this));
645 if (dialog
->exec() == QDialog::Accepted
) {
646 if (!newGame(dialog
->variant(), PositionPtr(), dialog
->newTab()))
647 QMessageBox::information(this, i18n("Error"), i18n("Error creating game"));
651 void MainWindow::quit() {
655 void MainWindow::flipView() {
659 void MainWindow::toggleConsole() {
660 if (console_dock
->isVisible())
661 console_dock
->hide();
663 console_dock
->show();
664 console_dock
->setFocus(Qt::MouseFocusReason
665 /*Qt::ActiveWindowFocusReason*/ /*Qt::OtherFocusReason*/);
669 void MainWindow::toggleMoveList() {
670 if (movelist_dock
->isVisible())
671 movelist_dock
->hide();
673 movelist_dock
->show();
674 movelist_dock
->setFocus(Qt::OtherFocusReason
);
679 void MainWindow::displayMessage(const QString
& msg
) {
680 Q_UNUSED(msg
); // TODO
681 // statusBar()->message(msg, 2000);
684 void MainWindow::displayErrorMessage(ErrorCode code
) {
687 displayMessage(i18n("Illegal move"));
692 void MainWindow::flash() {
693 if( !isAncestorOf(QApplication::focusWidget()) )
698 void MainWindow::prefHighlight() {
699 PrefHighlight dialog
;
700 int result
= dialog
.exec();
701 if (result
== QDialog::Accepted
) {
707 void MainWindow::preferences() {
708 Preferences
dialog(ui().currentVariant());
709 int result
= dialog
.exec();
710 if (result
== QDialog::Accepted
)
714 void MainWindow::settingsChanged() {
715 ui().reloadSettings();