File indexing completed on 2024-05-19 07:52:02
0001 /* 0002 This file is part of the game 'KJumpingCube' 0003 0004 SPDX-FileCopyrightText: 1998-2000 Matthias Kiefer <matthias.kiefer@gmx.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "kjumpingcube.h" 0010 #include "kcubeboxwidget.h" 0011 #include "settingswidget.h" 0012 #include "prefs.h" 0013 0014 #include <QSignalMapper> 0015 #include <QStatusBar> 0016 #include <KLocalizedString> 0017 #include <KGameStandardAction> 0018 #include <QAction> 0019 #include <KActionCollection> 0020 #include <KStandardAction> 0021 #include <QWidgetAction> 0022 0023 #include "kjumpingcube_debug.h" 0024 0025 #define MESSAGE_TIME 2000 0026 0027 KJumpingCube::KJumpingCube() 0028 { 0029 // Make a KCubeBoxWidget with the user's currently preferred number of cubes. 0030 qCDebug(KJUMPINGCUBE_LOG) << "KJumpingCube::KJumpingCube() CONSTRUCTOR"; 0031 m_view = new KCubeBoxWidget (Prefs::cubeDim(), this); 0032 m_game = new Game (Prefs::cubeDim(), m_view, this); 0033 m_view->makeStatusPixmaps (30); 0034 0035 connect(m_game,&Game::playerChanged,this, &KJumpingCube::changePlayerColor); 0036 connect(m_game,&Game::buttonChange, 0037 this, &KJumpingCube::changeButton); 0038 connect(m_game,&Game::statusMessage, 0039 this, &KJumpingCube::statusMessage); 0040 0041 // Tell the KMainWindow that this is indeed the main widget. 0042 setCentralWidget (m_view); 0043 0044 // init statusbar 0045 QString s = i18n("Current player:"); 0046 statusBar()->addPermanentWidget (new QLabel (s)); 0047 0048 currentPlayer = new QLabel (); 0049 currentPlayer->setFrameStyle (QFrame::NoFrame); 0050 changePlayerColor(One); 0051 statusBar()->addPermanentWidget (currentPlayer); 0052 0053 initKAction(); 0054 0055 connect (m_game, &Game::setAction, 0056 this, &KJumpingCube::setAction); 0057 m_game->gameActions (NEW); // Start a new game. 0058 } 0059 0060 bool KJumpingCube::queryClose() 0061 { 0062 // Terminate the AI or animation cleanly if either one is active. 0063 // If the AI is active, quitting immediately could cause a crash. 0064 m_game->shutdown(); 0065 return true; 0066 } 0067 0068 void KJumpingCube::initKAction() { 0069 QAction * action; 0070 0071 QSignalMapper * gameMapper = new QSignalMapper (this); 0072 connect (gameMapper, &QSignalMapper::mappedInt, m_game, &Game::gameActions); 0073 0074 action = KGameStandardAction::gameNew(gameMapper, 0075 qOverload<>(&QSignalMapper::map), this); 0076 actionCollection()->addAction (action->objectName(), action); 0077 gameMapper->setMapping (action, NEW); 0078 0079 action = KGameStandardAction::load(gameMapper, 0080 qOverload<>(&QSignalMapper::map), this); 0081 actionCollection()->addAction (action->objectName(), action); 0082 gameMapper->setMapping (action, LOAD); 0083 0084 action = KGameStandardAction::save(gameMapper, 0085 qOverload<>(&QSignalMapper::map), this); 0086 actionCollection()->addAction (action->objectName(), action); 0087 gameMapper->setMapping (action, SAVE); 0088 0089 action = KGameStandardAction::saveAs(gameMapper, 0090 qOverload<>(&QSignalMapper::map), this); 0091 actionCollection()->addAction (action->objectName(), action); 0092 gameMapper->setMapping (action, SAVE_AS); 0093 0094 action = KGameStandardAction::hint(gameMapper, 0095 qOverload<>(&QSignalMapper::map), this); 0096 actionCollection()->addAction (action->objectName(), action); 0097 gameMapper->setMapping (action, HINT); 0098 0099 action = KGameStandardAction::undo(gameMapper, 0100 qOverload<>(&QSignalMapper::map), this); 0101 actionCollection()->addAction (action->objectName(), action); 0102 gameMapper->setMapping (action, UNDO); 0103 action->setEnabled (false); 0104 0105 action = KGameStandardAction::redo(gameMapper, 0106 qOverload<>(&QSignalMapper::map), this); 0107 actionCollection()->addAction (action->objectName(), action); 0108 gameMapper->setMapping (action, REDO); 0109 action->setEnabled (false); 0110 0111 actionButton = new QPushButton (this); 0112 actionButton->setObjectName (QStringLiteral("ActionButton")); 0113 // Action button's style sheet: parameters for red, green and clicked colors. 0114 buttonLook = QStringLiteral( 0115 "QPushButton#ActionButton { color: white; background-color: %1; " 0116 "border-style: outset; border-width: 2px; border-radius: 10px; " 0117 "border-color: beige; font: bold 14px; min-width: 10em; " 0118 "padding: 6px; margin: 5px; margin-left: 10px; } " 0119 "QPushButton#ActionButton:pressed { background-color: %2; " 0120 "border-style: inset; } " 0121 "QPushButton#ActionButton:disabled { color: white;" 0122 "border-color: beige; background-color: steelblue; }"); 0123 gameMapper->setMapping (actionButton, BUTTON); 0124 connect(actionButton, &QAbstractButton::clicked, gameMapper, 0125 qOverload<>(&QSignalMapper::map)); 0126 0127 QWidgetAction *widgetAction = new QWidgetAction(this); 0128 widgetAction->setDefaultWidget(actionButton); 0129 actionCollection()->addAction (QStringLiteral ("action_button"), widgetAction); 0130 0131 changeButton (true, true); // Load the button's style sheet. 0132 changeButton (false); // Set the button to be inactive. 0133 0134 action = KStandardAction::preferences (m_game, [this]() { m_game->showSettingsDialog(true); }, 0135 actionCollection()); 0136 qCDebug(KJUMPINGCUBE_LOG) << "PREFERENCES ACTION is" << action->objectName(); 0137 action->setIconText (i18n("Settings")); 0138 0139 action = KGameStandardAction::quit (this, &KJumpingCube::close, this); 0140 actionCollection()->addAction (action->objectName(), action); 0141 0142 setupGUI(); 0143 } 0144 0145 void KJumpingCube::changeButton (bool enabled, bool stop, 0146 const QString & caption) 0147 { 0148 qCDebug(KJUMPINGCUBE_LOG) << "KJumpingCube::changeButton (" << enabled << stop << caption; 0149 if (enabled && stop) { // Red look (stop something). 0150 actionButton->setStyleSheet (buttonLook.arg(QStringLiteral("rgb(210, 0, 0)")) 0151 .arg(QStringLiteral("rgb(180, 0, 0)"))); 0152 } 0153 else if (enabled) { // Green look (continue something). 0154 actionButton->setStyleSheet (buttonLook.arg(QStringLiteral("rgb(0, 170, 0)")) 0155 .arg(QStringLiteral("rgb(0, 150, 0)"))); 0156 } 0157 actionButton->setText (caption); 0158 actionButton->setEnabled (enabled); 0159 } 0160 0161 void KJumpingCube::changePlayerColor (int newPlayer) 0162 { 0163 currentPlayer->setPixmap (m_view->playerPixmap (newPlayer)); 0164 } 0165 0166 void KJumpingCube::setAction (const Action a, const bool onOff) 0167 { 0168 // These must match enum Action (see file game.h) and be in the same order. 0169 static const QString name [] = { 0170 QStringLiteral("game_new"), 0171 QStringLiteral("move_hint"), 0172 QStringLiteral("action_button"), 0173 QStringLiteral("move_undo"), 0174 QStringLiteral("move_redo"), 0175 QStringLiteral("game_save"), 0176 QStringLiteral("game_saveAs"), 0177 QStringLiteral("game_load")}; 0178 0179 ((QAction *) actionCollection()->action (name [a]))->setEnabled (onOff); 0180 } 0181 0182 void KJumpingCube::statusMessage (const QString & message, bool timed) 0183 { 0184 if (timed) { 0185 statusBar()->showMessage (message, MESSAGE_TIME); 0186 } 0187 else { 0188 statusBar()->showMessage (message); 0189 } 0190 } 0191 0192 #include "moc_kjumpingcube.cpp"