| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by the qGo project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #include "defines.h" |
| 24 | #include "mainwindow.h" |
| 25 | #include "boardwindow.h" |
| 26 | #include "connectionwidget.h" |
| 27 | #include "login.h" |
| 28 | #include "sgfpreview.h" |
| 29 | #include "audio.h" |
| 30 | #include "sgfparser.h" |
| 31 | #include "newgamedialog.h" |
| 32 | #include "ui_mainwindow.h" |
| 33 | #include "preferences.h" |
| 34 | |
| 35 | MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags ) |
| 36 | : QMainWindow( parent, flags ), ui(new Ui::MainWindow) |
| 37 | { |
| 38 | qDebug( "Home Path : %s" ,QDir::homePath().toLatin1().constData()); |
| 39 | qDebug( "Current Path : %s" ,QDir::currentPath ().toLatin1().constData()); |
| 40 | |
| 41 | ui->setupUi(this); |
| 42 | this->setAttribute( Qt::WA_DeleteOnClose ); |
| 43 | //hide by default |
| 44 | setWindowTitle(QString(PACKAGE) + " " + QString(VERSION)); |
| 45 | preferences.fill(); |
| 46 | |
| 47 | // connecting the new game button |
| 48 | connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::slot_fileOpen); |
| 49 | connect(ui->actionNew, &QAction::triggered, this, &MainWindow::slot_fileNew); |
| 50 | |
| 51 | |
| 52 | //sound |
| 53 | connectSound = new Sound("static.wav"); |
| 54 | |
| 55 | logindialog = new LoginDialog(this); |
| 56 | |
| 57 | connect( ui->actionConnect, &QAction::triggered, this, &MainWindow::openConnectDialog); |
| 58 | connect(ui->actionPreferences, &QAction::triggered, this, &MainWindow::openPreferences); |
| 59 | } |
| 60 | |
| 61 | MainWindow::~MainWindow() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void MainWindow::closeEvent(QCloseEvent * e) |
| 66 | { |
| 67 | /* Close connection if open */ |
| 68 | if(ui->connectionWidget->closeConnection() < 0) |
| 69 | { |
| 70 | e->ignore(); |
| 71 | return; |
| 72 | } |
| 73 | if(checkForOpenBoards() < 0) |
| 74 | { |
| 75 | e->ignore(); |
| 76 | return; |
| 77 | } |
| 78 | preferences.save(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void MainWindow::slot_fileOpen() |
| 83 | { |
| 84 | QFileDialog *dialog = new QFileDialog(this); |
| 85 | dialog->setOption(QFileDialog::DontUseNativeDialog, true); |
| 86 | SGFPreview *previewWidget = new SGFPreview(dialog); |
| 87 | QGridLayout *layout = (QGridLayout*)dialog->layout(); |
| 88 | layout->addWidget(previewWidget, 1, 3); |
| 89 | connect(dialog, &QFileDialog::currentChanged,previewWidget,&SGFPreview::setPath); |
| 90 | connect(dialog, &QFileDialog::fileSelected,this,&MainWindow::openSGF); |
| 91 | dialog->setNameFilter("Smart Game Format (*.sgf *.SGF)"); |
| 92 | dialog->setFileMode(QFileDialog::ExistingFile); |
| 93 | dialog->exec(); |
| 94 | delete dialog; |
| 95 | } |
| 96 | |
| 97 | void MainWindow::openPreferences() |
| 98 | { |
| 99 | Preferences preferencesDialog(this); |
| 100 | preferencesDialog.exec(); |
| 101 | } |
| 102 | |
| 103 | void MainWindow::openSGF(QString path) |
| 104 | { |
| 105 | SGFParser * MW_SGFparser = new SGFParser(NULL); |
| 106 | QString SGFloaded = MW_SGFparser->loadFile(path); |
| 107 | if (SGFloaded == NULL) |
| 108 | return; |
| 109 | |
| 110 | GameData * GameLoaded = MW_SGFparser->initGame(SGFloaded, path); |
| 111 | if (GameLoaded == NULL) |
| 112 | return; |
| 113 | |
| 114 | GameLoaded->gameMode = modeLocal; |
| 115 | this->addBoardWindow(new BoardWindow(GameLoaded, true, true)); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * The 'New Game' button in 'Go Engine' tab has been pressed. |
| 120 | */ |
| 121 | void MainWindow::slot_fileNew() |
| 122 | { |
| 123 | NewGameDialog * newGameDialog = new NewGameDialog(this); |
| 124 | newGameDialog->exec(); |
| 125 | delete newGameDialog; |
| 126 | } |
| 127 | |
| 128 | void MainWindow::addBoardWindow(BoardWindow * bw) |
| 129 | { |
| 130 | boardWindowList.append(bw); |
| 131 | connect(bw, &BoardWindow::destroyed, this, &MainWindow::removeBoardWindow); |
| 132 | } |
| 133 | |
| 134 | void MainWindow::removeBoardWindow(QObject *bw) |
| 135 | { |
| 136 | for(int i = 0; i < boardWindowList.length(); i++) |
| 137 | { |
| 138 | if(boardWindowList[i] == (BoardWindow*)bw) |
| 139 | { |
| 140 | boardWindowList.removeAt(i); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int MainWindow::checkForOpenBoards(void) |
| 147 | { |
| 148 | for(int i = 0; i < boardWindowList.length(); i++) |
| 149 | { |
| 150 | if(!(boardWindowList[i]->okayToQuit())) |
| 151 | return -1; |
| 152 | } |
| 153 | //close all open, since boardwindow no longer has parent because of windows task bar issue: |
| 154 | qDeleteAll(boardWindowList); |
| 155 | boardWindowList.clear(); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void MainWindow::openConnectDialog(void) |
| 160 | { |
| 161 | /* The login dialog is responsible for creating the connection |
| 162 | *and notifying the connectionWidget about it. */ |
| 163 | if(connectionWidget->isConnected()) |
| 164 | return; |
| 165 | logindialog->exec(); |
| 166 | } |