~weinzh/qfm/devel

« back to all changes in this revision

Viewing changes to gui/qfmmain.cpp

  • Committer: Heinrich Weinz
  • Date: 2008-01-29 20:40:46 UTC
  • Revision ID: weinzh@googlemail.com-20080129204046-bvadhg6o8ce7o67r
Gamestar creation moved to qfmgame. Class qfmMain renamed in QfmMainWindow. Turn over implemented.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2008 by Heinrich Weinz                                  *
3
 
 *   weinzh@googlemail.com                                                 *
4
 
 *                                                                         *
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.                                   *
9
 
 *                                                                         *
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.                          *
14
 
 *                                                                         *
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
 
 ***************************************************************************/
20
 
 
21
 
#include "qfmmain.h"
22
 
#include <QtGui/QFileDialog>
23
 
#include <QtCore/QTextStream>
24
 
#include <QtXml/QDomDocument>
25
 
#include "qfmcountry.h"
26
 
#include <iostream>
27
 
 
28
 
enum configstring {WINDOWMODE};
29
 
 
30
 
configstring string2int(QString str) {
31
 
    if (!str.compare("windowmode"))
32
 
        return WINDOWMODE;
33
 
 
34
 
};
35
 
 
36
 
/**
37
 
*
38
 
* Constructor
39
 
*
40
 
**/
41
 
QfmMain::QfmMain(QWidget *parent) {
42
 
    setupUi(this);
43
 
    menubar->setVisible(false);
44
 
    game = new QfmGame(m_countryList);
45
 
 
46
 
    newGamePage->setCountryList(game->countryList());
47
 
 
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");
54
 
 
55
 
    readConfig();
56
 
 
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 )));
61
 
 
62
 
    //Menubar visibility
63
 
    QObject::connect(mainStackedWidget, SIGNAL(currentChanged( int )), this, SLOT(setMenuBarVisibility(int)));
64
 
    QObject::connect(this, SIGNAL(setMenuBarVisibility(bool)), menubar, SLOT(setVisible( bool )));
65
 
 
66
 
    //Menubar connections
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()) );
71
 
 
72
 
    //Open, save, and quit
73
 
    QObject::connect(startPage, SIGNAL( openGame() ), this, SLOT( openGame()) );
74
 
    QObject::connect(this, SIGNAL( quitGameSignal() ), qApp, SLOT( quit()) );
75
 
 
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()) );
80
 
}
81
 
 
82
 
QfmMain::~QfmMain() {}
83
 
 
84
 
/**
85
 
* new game
86
 
*/
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) {
91
 
        // Clear All
92
 
                m_countryList.clear();
93
 
        game = new QfmGame(m_countryList);
94
 
        newGamePage->setCountryList(game->countryList());
95
 
        emit setStackedWidgetIndex( 1 );
96
 
    }
97
 
}
98
 
 
99
 
/**
100
 
* Open saved game
101
 
*/
102
 
void QfmMain::openGame() {
103
 
    QString fileString = QFileDialog::getOpenFileName(this, tr("Open saved game"), qfmUserDir.path() + "/save", tr("QFM save file (*.qfm)"));
104
 
}
105
 
 
106
 
/**
107
 
* Save current game
108
 
*/
109
 
void QfmMain::saveGame() {
110
 
    QString fileString = QFileDialog::getSaveFileName(this, tr("Save game"), qfmUserDir.path() + "/save", tr("QFM save file (*.qfm)"));
111
 
}
112
 
 
113
 
/**
114
 
* End game
115
 
*/
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) {
120
 
        saveConfig();
121
 
        emit quitGameSignal();
122
 
    }
123
 
}
124
 
 
125
 
/**
126
 
* Set menubar visibility
127
 
* @param currentIndex an integer
128
 
*/
129
 
void QfmMain::setMenuBarVisibility(int currentIndex) {
130
 
    if (currentIndex==2) emit setMenuBarVisibility(true);
131
 
    else emit setMenuBarVisibility(false);
132
 
}
133
 
 
134
 
/**
135
 
* Create menubar actions
136
 
*/
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");
142
 
}
143
 
 
144
 
/**
145
 
* save config
146
 
*/
147
 
void QfmMain::saveConfig() {
148
 
    QDomDocument config( "QFM" );//Configuration
149
 
    QFile configFile(qfmUserDir.path() + "/config");// Configuration file
150
 
 
151
 
    if ( !configFile.open(QIODevice::WriteOnly) ) {
152
 
        QMessageBox::warning( this,tr("Warning"), tr("Can not write configuration file!") );
153
 
    };
154
 
 
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 );
159
 
 
160
 
    // window mode
161
 
    QDomElement fs = config.createElement("windowmode");
162
 
    root.appendChild(fs);
163
 
    QString text;
164
 
    if ( this->isFullScreen() ) text ="fullscreen";
165
 
    else text ="windowed";
166
 
    QDomText t = config.createTextNode(text);
167
 
    fs.appendChild(t);
168
 
 
169
 
    QTextStream ts( &configFile );
170
 
    ts << config.toString();
171
 
    configFile.close(); ///Close configuration file
172
 
}
173
 
 
174
 
/**
175
 
* load config
176
 
*/
177
 
void QfmMain::readConfig() {
178
 
    // Configuration file
179
 
    QDomDocument config( "QFM" );
180
 
    QFile configFile(qfmUserDir.path() + "/config");
181
 
 
182
 
    if ( !configFile.open(QIODevice::ReadOnly) )
183
 
        readConfig();
184
 
    if ( !config.setContent( &configFile ) ) configFile.close();
185
 
 
186
 
    QDomElement root = config.documentElement();
187
 
    if ( root.tagName() !="config" )
188
 
        QMessageBox::warning( this,tr("Loading configuration"), tr("Invalid file!") );
189
 
 
190
 
    QDomNode n = root.firstChild();
191
 
    while ( !n.isNull() ) {
192
 
        QDomElement e = n.toElement();
193
 
        if ( !e.isNull() ) {
194
 
            QDomNode n1 = e.firstChild();
195
 
            if (!n1.isNull()) {
196
 
                QString s = n1.toCharacterData().data();
197
 
//                 QMessageBox::warning( this,tr("Loading configuration"), s );
198
 
            }
199
 
            switch ( string2int(e.tagName()) ) {
200
 
            case WINDOWMODE: {
201
 
// QMessageBox::warning( this,tr("Loading configuration"), "s" );
202
 
                break;
203
 
            }
204
 
            }
205
 
        }
206
 
        n = n.nextSibling();
207
 
    }
208
 
}
209