1/*****************************************************************************
2 This file is part of the game 'KJumpingCube'
3
4 Copyright (C) 1998-2000 by Matthias Kiefer
5 <matthias.kiefer@gmx.de>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21**************************************************************************** */
22
23#include "kjumpingcube.h"
24#include "kcubeboxwidget.h"
25#include "settingswidget.h"
26#include "prefs.h"
27
28#include <QSignalMapper>
29#include <QRegExp>
30
31#include <klocale.h>
32#include <kmessagebox.h>
33#include <kstandardgameaction.h>
34#include <kaction.h>
35#include <kactioncollection.h>
36#include <kstatusbar.h>
37#include <kstandardaction.h>
38#include <kconfigdialog.h>
39#include <kicon.h>
40
41#define MESSAGE_TIME 2000
42
43KJumpingCube::KJumpingCube()
44{
45 // Make a KCubeBoxWidget with the user's currently preferred number of cubes.
46 qDebug() << "KJumpingCube::KJumpingCube() CONSTRUCTOR";
47 m_view = new KCubeBoxWidget (Prefs::cubeDim(), this);
48 m_game = new Game (Prefs::cubeDim(), m_view, this);
49 m_view->makeStatusPixmaps (30);
50
51 connect(m_game,SIGNAL(playerChanged(int)),SLOT(changePlayerColor(int)));
52 connect(m_game,SIGNAL(buttonChange(bool,bool,const QString&)),
53 SLOT(changeButton(bool,bool,const QString&)));
54 connect(m_game,SIGNAL(statusMessage(const QString&, bool)),
55 SLOT(statusMessage(const QString&, bool)));
56
57 // Tell the KMainWindow that this is indeed the main widget.
58 setCentralWidget (m_view);
59
60 // init statusbar
61 QString s = i18n("Current player:");
62 statusBar()->addPermanentWidget (new QLabel (s));
63
64 currentPlayer = new QLabel ();
65 currentPlayer->setFrameStyle (QFrame::NoFrame);
66 changePlayerColor(One);
67 statusBar()->addPermanentWidget (currentPlayer);
68
69 initKAction();
70
71 connect (m_game, SIGNAL (setAction(const Action,const bool)),
72 SLOT (setAction(const Action,const bool)));
73 m_game->gameActions (NEW); // Start a new game.
74}
75
76bool KJumpingCube::queryClose()
77{
78 // Terminate the AI or animation cleanly if either one is active.
79 // If the AI is active, quitting immediately could cause a crash.
80 m_game->shutdown();
81 return true;
82}
83
84void KJumpingCube::initKAction() {
85 QAction * action;
86
87 QSignalMapper * gameMapper = new QSignalMapper (this);
88 connect (gameMapper, SIGNAL (mapped(int)), m_game, SLOT (gameActions(int)));
89
90 action = KStandardGameAction::gameNew (gameMapper, SLOT (map()), this);
91 actionCollection()->addAction (action->objectName(), action);
92 gameMapper->setMapping (action, NEW);
93
94 action = KStandardGameAction::load (gameMapper, SLOT (map()), this);
95 actionCollection()->addAction (action->objectName(), action);
96 gameMapper->setMapping (action, LOAD);
97
98 action = KStandardGameAction::save (gameMapper, SLOT (map()), this);
99 actionCollection()->addAction (action->objectName(), action);
100 gameMapper->setMapping (action, SAVE);
101
102 action = KStandardGameAction::saveAs (gameMapper, SLOT (map()), this);
103 actionCollection()->addAction (action->objectName(), action);
104 gameMapper->setMapping (action, SAVE_AS);
105
106 action = KStandardGameAction::hint (gameMapper, SLOT(map()), this);
107 actionCollection()->addAction (action->objectName(), action);
108 gameMapper->setMapping (action, HINT);
109
110 action = KStandardGameAction::undo (gameMapper, SLOT (map()), this);
111 actionCollection()->addAction (action->objectName(), action);
112 gameMapper->setMapping (action, UNDO);
113 action->setEnabled (false);
114
115 action = KStandardGameAction::redo (gameMapper, SLOT (map()), this);
116 actionCollection()->addAction (action->objectName(), action);
117 gameMapper->setMapping (action, REDO);
118 action->setEnabled (false);
119
120 actionButton = new QPushButton (this);
121 actionButton->setObjectName ("ActionButton");
122 // Action button's style sheet: parameters for red, green and clicked colors.
123 buttonLook =
124 "QPushButton#ActionButton { color: white; background-color: %1; "
125 "border-style: outset; border-width: 2px; border-radius: 10px; "
126 "border-color: beige; font: bold 14px; min-width: 10em; "
127 "padding: 6px; margin: 5px; margin-left: 10px; } "
128 "QPushButton#ActionButton:pressed { background-color: %2; "
129 "border-style: inset; } "
130 "QPushButton#ActionButton:disabled { color: white;"
131 "border-color: beige; background-color: steelblue; }";
132 gameMapper->setMapping (actionButton, BUTTON);
133 connect (actionButton, SIGNAL(clicked()), gameMapper, SLOT(map()));
134
135 KAction * b = actionCollection()->addAction (QLatin1String ("action_button"));
136 b->setDefaultWidget (actionButton); // Show the button on the toolbar.
137 changeButton (true, true); // Load the button's style sheet.
138 changeButton (false); // Set the button to be inactive.
139
140 action = KStandardAction::preferences (m_game, SLOT(showSettingsDialog()),
141 actionCollection());
142 qDebug() << "PREFERENCES ACTION is" << action->objectName();
143 action->setIconText (i18n("Settings"));
144
145 action = KStandardGameAction::quit (this, SLOT (close()), this);
146 actionCollection()->addAction (action->objectName(), action);
147
148 setupGUI();
149}
150
151void KJumpingCube::changeButton (bool enabled, bool stop,
152 const QString & caption)
153{
154 qDebug() << "KJumpingCube::changeButton (" << enabled << stop << caption;
155 if (enabled && stop) { // Red look (stop something).
156 actionButton->setStyleSheet (buttonLook.arg("rgb(210, 0, 0)")
157 .arg("rgb(180, 0, 0)"));
158 }
159 else if (enabled) { // Green look (continue something).
160 actionButton->setStyleSheet (buttonLook.arg("rgb(0, 170, 0)")
161 .arg("rgb(0, 150, 0)"));
162 }
163 actionButton->setText (caption);
164 actionButton->setEnabled (enabled);
165}
166
167void KJumpingCube::changePlayerColor (int newPlayer)
168{
169 currentPlayer->setPixmap (m_view->playerPixmap (newPlayer));
170}
171
172void KJumpingCube::setAction (const Action a, const bool onOff)
173{
174 // These must match enum Action (see file game.h) and be in the same order.
175 const char * name [] = {"game_new", "move_hint", "action_button",
176 "move_undo", "move_redo",
177 "game_save", "game_saveAs", "game_load"};
178
179 ((QAction *) actionCollection()->action (name [a]))->setEnabled (onOff);
180}
181
182void KJumpingCube::statusMessage (const QString & message, bool timed)
183{
184 if (timed) {
185 statusBar()->showMessage (message, MESSAGE_TIME);
186 }
187 else {
188 statusBar()->showMessage (message);
189 }
190}
191
192#include "kjumpingcube.moc"
193