summaryrefslogtreecommitdiffstats
path: root/kmines/main.cpp
blob: 9cdcec4dbddca51898f668f95d3c7e4a3c45afe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (c) 1996-2004 Nicolas HADACEK (hadacek@kde.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "main.h"
#include "main.moc"

#include <tqptrvector.h>

#include <kaccel.h>
#include <kapplication.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <kstdaction.h>
#include <kkeydialog.h>
#include <kstdgameaction.h>
#include <kaction.h>
#include <kdebug.h>
#include <knotifyclient.h>
#include <knotifydialog.h>
#include <khighscore.h>
#include <kconfigdialog.h>

#include "settings.h"
#include "status.h"
#include "highscores.h"
#include "version.h"
#include "dialogs.h"

const MainWidget::KeyData MainWidget::KEY_DATA[NB_KEYS] = {
{I18N_NOOP("Move Up"),     "keyboard_moveup",    Key_Up,    TQT_SLOT(moveUp())},
{I18N_NOOP("Move Down"),   "keyboard_movedown",  Key_Down,  TQT_SLOT(moveDown())},
{I18N_NOOP("Move Right"),  "keyboard_moveright", Key_Right, TQT_SLOT(moveRight())},
{I18N_NOOP("Move Left"),   "keyboard_moveleft",  Key_Left,  TQT_SLOT(moveLeft())},
{I18N_NOOP("Move at Left Edge"), "keyboard_leftedge", Key_Home, TQT_SLOT(moveLeftEdge())},
{I18N_NOOP("Move at Right Edge"), "keyboard_rightedge", Key_End, TQT_SLOT(moveRightEdge())},
{I18N_NOOP("Move at Top Edge"), "keyboard_topedge", Key_PageUp, TQT_SLOT(moveTop())},
{I18N_NOOP("Move at Bottom Edge"), "keyboard_bottomedge", Key_PageDown, TQT_SLOT(moveBottom())},
{I18N_NOOP("Reveal Mine"), "keyboard_revealmine", Key_Space, TQT_SLOT(reveal())},
{I18N_NOOP("Mark Mine"),   "keyboard_markmine",  Key_W,     TQT_SLOT(mark())},
{I18N_NOOP("Automatic Reveal"), "keyboard_autoreveal", Key_Return, TQT_SLOT(autoReveal())}
};


MainWidget::MainWidget()
  : KZoomMainWindow(4, 100, 1, "kmines")
{
    KNotifyClient::startDaemon();

    _status = new Status(this);
    connect(_status, TQT_SIGNAL(gameStateChangedSignal(KMines::GameState)),
            TQT_SLOT(gameStateChanged(KMines::GameState)));
    connect(_status, TQT_SIGNAL(pause()), TQT_SLOT(pause()));

	// Game & Popup
	KStdGameAction::gameNew(TQT_TQOBJECT(_status), TQT_SLOT(restartGame()), actionCollection());
	_pause = KStdGameAction::pause(TQT_TQOBJECT(_status), TQT_SLOT(pauseGame()),
                                  actionCollection());
	KStdGameAction::highscores(TQT_TQOBJECT(this), TQT_SLOT(showHighscores()),
                               actionCollection());
	KStdGameAction::quit(TQT_TQOBJECT(tqApp), TQT_SLOT(quit()), actionCollection());

	// keyboard
    _keybCollection = new KActionCollection(this);
    for (uint i=0; i<NB_KEYS; i++) {
        const KeyData &d = KEY_DATA[i];
        (void)new KAction(i18n(d.label), d.keycode, TQT_TQOBJECT(_status),
                          d.slot, _keybCollection, d.name);
    }

	// Settings
	KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(configureSettings()),
                            actionCollection());
	KStdAction::keyBindings(TQT_TQOBJECT(this), TQT_SLOT(configureKeys()), actionCollection());
    KStdAction::configureNotifications(TQT_TQOBJECT(this), TQT_SLOT(configureNotifications()),
                                       actionCollection());
    KStdGameAction::configureHighscores(TQT_TQOBJECT(this), TQT_SLOT(configureHighscores()),
                                        actionCollection());
	// Levels
    _levels = KStdGameAction::chooseGameType(0, 0, actionCollection());
    TQStringList list;
    for (uint i=0; i<=Level::NB_TYPES; i++)
        list += i18n(Level::LABELS[i]);
    _levels->setItems(list);
    connect(_levels, TQT_SIGNAL(activated(int)), TQT_TQOBJECT(_status), TQT_SLOT(newGame(int)));

    // Adviser
    _advise =
        KStdGameAction::hint(TQT_TQOBJECT(_status), TQT_SLOT(advise()), actionCollection());
    _solve = KStdGameAction::solve(TQT_TQOBJECT(_status), TQT_SLOT(solve()), actionCollection());
    (void)new KAction(i18n("Solving Rate..."), 0, TQT_TQOBJECT(_status), TQT_SLOT(solveRate()),
                      actionCollection(), "solve_rate");

    // Log
    (void)new KAction(KGuiItem(i18n("View Log"), "viewmag"), 0,
                      TQT_TQOBJECT(_status), TQT_SLOT(viewLog()),
                      actionCollection(), "log_view");
    (void)new KAction(KGuiItem(i18n("Replay Log"), "player_play"),
                      0, TQT_TQOBJECT(_status), TQT_SLOT(replayLog()),
                      actionCollection(), "log_replay");
    (void)new KAction(KGuiItem(i18n("Save Log..."), "filesave"), 0,
                      TQT_TQOBJECT(_status), TQT_SLOT(saveLog()),
                      actionCollection(), "log_save");
    (void)new KAction(KGuiItem(i18n("Load Log..."), "fileopen"), 0,
                      TQT_TQOBJECT(_status), TQT_SLOT(loadLog()),
                      actionCollection(), "log_load");

	setupGUI( KMainWindow::Save | Create );
	readSettings();
        setCentralWidget(_status);
        init("popup");
        addWidget(_status->field());
}

bool MainWidget::queryExit()
{
    _status->checkBlackMark();
    return KZoomMainWindow::queryExit();
}

void MainWidget::readSettings()
{
    settingsChanged();
    Level::Type type = (Level::Type) Settings::level();
    _levels->setCurrentItem(type);
    _status->newGame(type);
}

void MainWidget::showHighscores()
{
    KExtHighscore::show(this);
}

void MainWidget::focusOutEvent(TQFocusEvent *e)
{
    if ( Settings::pauseFocus() && e->reason()==TQFocusEvent::ActiveWindow
          && _status->isPlaying() ) pause();
    KMainWindow::focusOutEvent(e);
}

void MainWidget::configureSettings()
{
    if ( KConfigDialog::showDialog("settings") ) return;

    KConfigDialog *dialog = new KConfigDialog(this, "settings", Settings::self());
    GameConfig *gc = new GameConfig;
    dialog->addPage(gc, i18n("Game"), "package_system");
    dialog->addPage(new AppearanceConfig, i18n("Appearance"), "style");
    CustomConfig *cc = new CustomConfig;
    dialog->addPage(cc, i18n("Custom Game"), "package_settings");
    connect(dialog, TQT_SIGNAL(settingsChanged()), TQT_SLOT(settingsChanged()));
    dialog->show();
    cc->init();
    gc->init();
}

void MainWidget::configureHighscores()
{
    KExtHighscore::configure(this);
}

void MainWidget::settingsChanged()
{
    bool enabled = Settings::keyboardGame();
    TQValueList<KAction *> list = _keybCollection->actions();
    TQValueList<KAction *>::Iterator it;
    for (it = list.begin(); it!=list.end(); ++it)
        (*it)->setEnabled(enabled);
    _status->settingsChanged();
}

void MainWidget::configureKeys()
{
    KKeyDialog d(true, this);
    d.insert(_keybCollection, i18n("Keyboard game"));
    d.insert(actionCollection(), i18n("General"));
    d.configure();
}

void MainWidget::configureNotifications()
{
    KNotifyDialog::configure(this);
}

void MainWidget::gameStateChanged(KMines::GameState state)
{
    stateChanged(KMines::STATES[state]);
    if ( state==Playing ) setFocus();
}

void MainWidget::pause()
{
    _pause->activate();
}

void MainWidget::writeZoomSetting(uint zoom)
{
  Settings::setCaseSize(zoom);
  Settings::writeConfig();
}

uint MainWidget::readZoomSetting() const
{
  return Settings::caseSize();
}

void MainWidget::writeMenubarVisibleSetting(bool visible)
{
  Settings::setMenubarVisible(visible);
  Settings::writeConfig();
}

bool MainWidget::menubarVisibleSetting() const
{
  return Settings::menubarVisible();
}

//----------------------------------------------------------------------------
static const char *DESCRIPTION
    = I18N_NOOP("KMines is a classic mine sweeper game");

int main(int argc, char **argv)
{
    KHighscore::init("kmines");

    KAboutData aboutData("kmines", I18N_NOOP("KMines"), LONG_VERSION,
						 DESCRIPTION, KAboutData::License_GPL,
						 COPYLEFT, 0, HOMEPAGE);
    aboutData.addAuthor("Nicolas Hadacek", 0, EMAIL);
	aboutData.addCredit("Andreas Zehender", I18N_NOOP("Smiley pixmaps"));
    aboutData.addCredit("Mikhail Kourinny", I18N_NOOP("Solver/Adviser"));
    aboutData.addCredit("Thomas Capricelli", I18N_NOOP("Magic reveal mode"));
    KCmdLineArgs::init(argc, argv, &aboutData);

    KApplication a;
    KGlobal::locale()->insertCatalogue("libtdegames");
    KExtHighscore::ExtManager manager;

    if ( a.isRestored() ) RESTORE(MainWidget)
    else {
        MainWidget *mw = new MainWidget;
        mw->show();
    }
    return a.exec();
}