1
/***************************************************************************
2
* Copyright (C) 2008 by Heinrich Weinz *
3
* weinzh@googlemail.com *
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 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
22
#include <QtGui/QFileDialog>
23
#include <QtCore/QTextStream>
24
#include <QtXml/QDomDocument>
25
#include "qfmcountry.h"
28
enum configstring {WINDOWMODE};
30
configstring string2int(QString str) {
31
if (!str.compare("windowmode"))
41
QfmMain::QfmMain(QWidget *parent) {
43
menubar->setVisible(false);
44
game = new QfmGame(m_countryList);
46
newGamePage->setCountryList(game->countryList());
48
// Create configurations and save direcories
49
qfmUserDir.mkpath(QDir::homePath() + "/.qfm/icons");
50
qfmUserDir.mkpath(QDir::homePath() + "/.qfm/save");
51
qfmUserDir.mkpath(QDir::homePath() + "/.qfm/definitions/countries");
52
qfmUserDir.mkpath(QDir::homePath() + "/.qfm/definitions/countries/teams");
53
qfmUserDir.setPath(QDir::homePath() + "/.qfm");
57
//Main stacked widget browsing
58
QObject::connect(startPage, SIGNAL(newGame( int )), mainStackedWidget, SLOT(setCurrentIndex( int )));
59
QObject::connect(newGamePage, SIGNAL(stackedWidgetIndexChanged( int )), mainStackedWidget, SLOT(setCurrentIndex( int )));
60
QObject::connect(this, SIGNAL(setStackedWidgetIndex( int )), mainStackedWidget, SLOT(setCurrentIndex( int )));
63
QObject::connect(mainStackedWidget, SIGNAL(currentChanged( int )), this, SLOT(setMenuBarVisibility(int)));
64
QObject::connect(this, SIGNAL(setMenuBarVisibility(bool)), menubar, SLOT(setVisible( bool )));
67
QObject::connect(actionNew_Game, SIGNAL(triggered()), this, SLOT(newGame()));
68
QObject::connect(actionLoad_Game, SIGNAL(triggered()), this, SLOT(openGame()));
69
QObject::connect(actionSave_Game, SIGNAL( triggered() ), this, SLOT( saveGame()) );
70
QObject::connect(actionQuit, SIGNAL( triggered() ), this, SLOT( quitGame()) );
72
//Open, save, and quit
73
QObject::connect(startPage, SIGNAL( openGame() ), this, SLOT( openGame()) );
74
QObject::connect(this, SIGNAL( quitGameSignal() ), qApp, SLOT( quit()) );
76
// toggle Fullscreen, Window mode
77
QObject::connect(gamePage, SIGNAL( fullscreen() ), this, SLOT( showFullScreen()) );
78
QObject::connect(gamePage, SIGNAL( windowMode() ), this, SLOT( showNormal()) );
79
QObject::connect(this, SIGNAL( setFS() ), this, SLOT( showFullScreen()) );
82
QfmMain::~QfmMain() {}
87
void QfmMain::newGame() {
88
int ans = QMessageBox::warning(this, tr("New game?"), tr("Start new game?\n"
89
"All changes will be lost"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
90
if (ans==0x00000400) {
92
m_countryList.clear();
93
game = new QfmGame(m_countryList);
94
newGamePage->setCountryList(game->countryList());
95
emit setStackedWidgetIndex( 1 );
102
void QfmMain::openGame() {
103
QString fileString = QFileDialog::getOpenFileName(this, tr("Open saved game"), qfmUserDir.path() + "/save", tr("QFM save file (*.qfm)"));
109
void QfmMain::saveGame() {
110
QString fileString = QFileDialog::getSaveFileName(this, tr("Save game"), qfmUserDir.path() + "/save", tr("QFM save file (*.qfm)"));
116
void QfmMain::quitGame() {
117
int ans = QMessageBox::warning(this, tr("End game?"), tr("End current game?\n"
118
"All changes will be lost"), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
119
if (ans==0x00000400) {
121
emit quitGameSignal();
126
* Set menubar visibility
127
* @param currentIndex an integer
129
void QfmMain::setMenuBarVisibility(int currentIndex) {
130
if (currentIndex==2) emit setMenuBarVisibility(true);
131
else emit setMenuBarVisibility(false);
135
* Create menubar actions
137
void QfmMain::createActions() {
138
actionNew_Game->setStatusTip("Start a new game");
139
actionSave_Game->setStatusTip("Save current game");
140
actionLoad_Game->setStatusTip("Load a saved game");
141
actionQuit->setStatusTip("Quit the game");
147
void QfmMain::saveConfig() {
148
QDomDocument config( "QFM" );//Configuration
149
QFile configFile(qfmUserDir.path() + "/config");// Configuration file
151
if ( !configFile.open(QIODevice::WriteOnly) ) {
152
QMessageBox::warning( this,tr("Warning"), tr("Can not write configuration file!") );
155
QDomProcessingInstruction header = config.createProcessingInstruction( "xml","version=\"1.0\" encoding=\"UTF-8\"" );
156
config.appendChild( header );
157
QDomElement root = config.createElement( "config" );
158
config.appendChild( root );
161
QDomElement fs = config.createElement("windowmode");
162
root.appendChild(fs);
164
if ( this->isFullScreen() ) text ="fullscreen";
165
else text ="windowed";
166
QDomText t = config.createTextNode(text);
169
QTextStream ts( &configFile );
170
ts << config.toString();
171
configFile.close(); ///Close configuration file
177
void QfmMain::readConfig() {
178
// Configuration file
179
QDomDocument config( "QFM" );
180
QFile configFile(qfmUserDir.path() + "/config");
182
if ( !configFile.open(QIODevice::ReadOnly) )
184
if ( !config.setContent( &configFile ) ) configFile.close();
186
QDomElement root = config.documentElement();
187
if ( root.tagName() !="config" )
188
QMessageBox::warning( this,tr("Loading configuration"), tr("Invalid file!") );
190
QDomNode n = root.firstChild();
191
while ( !n.isNull() ) {
192
QDomElement e = n.toElement();
194
QDomNode n1 = e.firstChild();
196
QString s = n1.toCharacterData().data();
197
// QMessageBox::warning( this,tr("Loading configuration"), s );
199
switch ( string2int(e.tagName()) ) {
201
// QMessageBox::warning( this,tr("Loading configuration"), "s" );