KDEGames

kstandardgameaction.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2001 Andreas Beckermann <[email protected]>
4  SPDX-FileCopyrightText: 2007 Simon Hürlimann <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kstandardgameaction.h"
10 
11 // KF
12 #include <KLocalizedString>
13 #include <KActionCollection>
14 #include <KStandardShortcut>
15 #include <KToggleAction>
16 #include <KConfig>
17 #include <KRecentFilesAction>
18 // Qt
19 #include <QAction>
20 #include <QIcon>
21 
22 #undef I18N_NOOP2
23 #define I18N_NOOP2(ctx,txt) ctx, txt
24 
25 Q_LOGGING_CATEGORY(GAMES_UI, "org.kde.games.ui", QtWarningMsg)
26 
27 struct KStandardGameActionInfo
28 {
30  KStandardShortcut::StandardShortcut globalAccel; // if we reuse a global accel
31  int shortcut; // specific shortcut (NH: should be configurable)
32  const char* psName;
33  const char* psLabelContext;
34  const char* psLabel;
35  const char* psWhatsThis;
36  const char* psIconName;
37  const char* psToolTip;
38 };
39 
40 const KStandardGameActionInfo g_rgActionInfo[] = {
41 // "game" menu
42  { KStandardGameAction::New, KStandardShortcut::New, 0, "game_new", I18N_NOOP2("new game", "&New"), I18N_NOOP("Start a new game."), "document-new", I18N_NOOP("Start a new game") },
43  { KStandardGameAction::Load, KStandardShortcut::Open, 0, "game_load", nullptr, I18N_NOOP("&Load..."), nullptr, "document-open", I18N_NOOP("Open a saved game...") },
44  { KStandardGameAction::LoadRecent, KStandardShortcut::AccelNone, 0, "game_load_recent", nullptr, I18N_NOOP("Load &Recent"), nullptr, nullptr, I18N_NOOP("Open a recently saved game...") },
45  { KStandardGameAction::Restart, KStandardShortcut::Reload, 0, "game_restart", nullptr, I18N_NOOP("Restart &Game"), nullptr, "view-refresh", I18N_NOOP("Restart the game") },
46  { KStandardGameAction::Save, KStandardShortcut::Save, 0, "game_save", nullptr, I18N_NOOP("&Save"), nullptr, "document-save", I18N_NOOP("Save the current game") },
47  { KStandardGameAction::SaveAs, KStandardShortcut::AccelNone, 0, "game_save_as", nullptr, I18N_NOOP("Save &As..."), nullptr, "document-save-as", I18N_NOOP("Save the current game to another file") },
48  { KStandardGameAction::End, KStandardShortcut::End, 0, "game_end", nullptr, I18N_NOOP("&End Game"), nullptr, "window-close", I18N_NOOP("End the current game") },
49  { KStandardGameAction::Pause, KStandardShortcut::AccelNone, Qt::Key_P, "game_pause", nullptr, I18N_NOOP("Pa&use"), nullptr, "media-playback-pause", I18N_NOOP("Pause the game") },
50  { KStandardGameAction::Highscores, KStandardShortcut::AccelNone, Qt::CTRL | Qt::Key_H, "game_highscores", nullptr, I18N_NOOP("Show &High Scores"), nullptr, "games-highscores", I18N_NOOP("Show high scores") },
51  { KStandardGameAction::ClearHighscores, KStandardShortcut::AccelNone, 0, "game_clear_highscores", nullptr, I18N_NOOP("&Clear High Scores"), nullptr, "clear_highscore", I18N_NOOP("Clear high scores") },
52  { KStandardGameAction::Statistics, KStandardShortcut::AccelNone, 0, "game_statistics", nullptr, I18N_NOOP("Show Statistics"), nullptr, "highscore", I18N_NOOP("Show statistics") },
53  { KStandardGameAction::ClearStatistics, KStandardShortcut::AccelNone, 0, "game_clear_statistics", nullptr, I18N_NOOP("&Clear Statistics"), nullptr, "flag", I18N_NOOP("Delete all-time statistics.") },
54  { KStandardGameAction::Print, KStandardShortcut::Print, 0, "game_print", nullptr, I18N_NOOP("&Print..."), nullptr, "document-print", nullptr },
55  { KStandardGameAction::Quit, KStandardShortcut::Quit, 0, "game_quit", nullptr, I18N_NOOP("&Quit"), nullptr, "application-exit", I18N_NOOP("Quit the program") },
56 // "move" menu
57  { KStandardGameAction::Repeat, KStandardShortcut::AccelNone, 0, "move_repeat", nullptr, I18N_NOOP("Repeat"), nullptr, nullptr, I18N_NOOP("Repeat the last move") },
58  { KStandardGameAction::Undo, KStandardShortcut::Undo, 0, "move_undo", nullptr, I18N_NOOP("Und&o"), nullptr, "edit-undo", I18N_NOOP("Undo the last move") },
59  { KStandardGameAction::Redo, KStandardShortcut::Redo, 0, "move_redo", nullptr, I18N_NOOP("Re&do"), nullptr, "edit-redo", I18N_NOOP("Redo the latest move") },
60  { KStandardGameAction::Roll, KStandardShortcut::AccelNone, Qt::CTRL | Qt::Key_R, "move_roll", nullptr, I18N_NOOP("&Roll Dice"), nullptr, "roll", I18N_NOOP("Roll the dice") },
61  { KStandardGameAction::EndTurn, KStandardShortcut::AccelNone, 0, "move_end_turn", nullptr, I18N_NOOP("End Turn"), nullptr, "games-endturn", nullptr },
62  { KStandardGameAction::Hint, KStandardShortcut::AccelNone, Qt::Key_H, "move_hint", nullptr, I18N_NOOP("&Hint"), nullptr, "games-hint", I18N_NOOP("Give a hint") },
63  { KStandardGameAction::Demo, KStandardShortcut::AccelNone, Qt::Key_D, "move_demo", nullptr, I18N_NOOP("&Demo"), nullptr, "media-playback-start", I18N_NOOP("Play a demo") },
64  { KStandardGameAction::Solve, KStandardShortcut::AccelNone, 0, "move_solve", nullptr, I18N_NOOP("&Solve"), nullptr, "games-solve", I18N_NOOP("Solve the game") },
65 // "settings" menu
66  { KStandardGameAction::ChooseGameType, KStandardShortcut::AccelNone, 0, "options_choose_game_type", nullptr, I18N_NOOP("Choose Game &Type"), nullptr, nullptr, nullptr },
67  { KStandardGameAction::Carddecks, KStandardShortcut::AccelNone, 0, "options_configure_carddecks", nullptr, I18N_NOOP("Configure &Carddecks..."), nullptr, nullptr, nullptr },
68  { KStandardGameAction::ConfigureHighscores, KStandardShortcut::AccelNone, 0, "options_configure_highscores", nullptr, I18N_NOOP("Configure &High Scores..."), nullptr, nullptr, nullptr },
69 
70  { KStandardGameAction::ActionNone, KStandardShortcut::AccelNone, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }
71 };
72 
73 static const KStandardGameActionInfo* infoPtr( KStandardGameAction::StandardGameAction id )
74 {
75  for (uint i = 0; g_rgActionInfo[i].id!=KStandardGameAction::ActionNone; i++) {
76  if( g_rgActionInfo[i].id == id )
77  return &g_rgActionInfo[i];
78  }
79  return nullptr;
80 }
81 
83 {
84  QAction* pAction = nullptr;
85  const KStandardGameActionInfo* pInfo = infoPtr( id );
86 // qCDebug(GAMES_UI) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)nullptr) << "," << parent << " )";
87  if( pInfo ) {
88  QString sLabel = i18nc(pInfo->psLabelContext, pInfo->psLabel);
89  switch( id ) {
90  case LoadRecent:
91  pAction = new KRecentFilesAction(sLabel, parent);
92  break;
93  case Pause:
94  case Demo:
95  pAction = new KToggleAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
96  break;
97  case ChooseGameType:
98  pAction = new KSelectAction( QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
99  break;
100  default:
101  pAction = new QAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
102  break;
103  }
104 
105  QList<QKeySequence> cut = (pInfo->globalAccel==KStandardShortcut::AccelNone
106  ? QList<QKeySequence>() << QKeySequence(pInfo->shortcut)
107  : KStandardShortcut::shortcut(pInfo->globalAccel));
108  if (!cut.isEmpty()) {
109  pAction->setShortcuts(cut);
110  pAction->setProperty("defaultShortcuts", QVariant::fromValue(cut));
111  }
112  if (pInfo->psToolTip)
113  pAction->setToolTip(i18n(pInfo->psToolTip));
114  if (pInfo->psWhatsThis)
115  pAction->setWhatsThis(i18n(pInfo->psWhatsThis));
116  else if (pInfo->psToolTip)
117  pAction->setWhatsThis(i18n(pInfo->psToolTip));
118 
119  pAction->setObjectName(QLatin1String( pInfo->psName ));
120  }
121 
122  KActionCollection *collection = qobject_cast<KActionCollection *>(parent);
123  if (collection && pAction)
124  collection->addAction(pAction->objectName(), pAction);
125 
126  return pAction;
127 }
128 
129 QAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot,
130  QObject* parent )
131 {
132  QAction* pAction = _k_createInternal(id, parent);
133  if (recvr && slot) {
134  switch( id ) {
135  case LoadRecent:
136  QObject::connect(pAction, SIGNAL(urlSelected(QUrl)), recvr, slot);
137  break;
138  case ChooseGameType:
139 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 78, 0)
140  QObject::connect(pAction, SIGNAL(indexTriggered(int)), recvr, slot);
141 #else
142  QObject::connect(pAction, SIGNAL(triggered(int)), recvr, slot);
143 #endif
144  break;
145  default:
146  QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
147  break;
148  }
149  }
150  return pAction;
151 
152 }
153 
155 {
156  const KStandardGameActionInfo* pInfo = infoPtr( id );
157  return (pInfo) ? pInfo->psName : nullptr;
158 }
159 
160 QAction *KStandardGameAction::gameNew(const QObject *recvr, const char *slot,
161  QObject *parent)
162 { return KStandardGameAction::create(New, recvr, slot, parent); }
163 QAction *KStandardGameAction::load(const QObject *recvr, const char *slot,
164  QObject *parent)
165 { return KStandardGameAction::create(Load, recvr, slot, parent); }
167  QObject *parent)
168 { return static_cast<KRecentFilesAction *>(KStandardGameAction::create(LoadRecent, recvr, slot, parent)); }
169 QAction *KStandardGameAction::save(const QObject *recvr, const char *slot,
170  QObject *parent)
171 { return KStandardGameAction::create(Save, recvr, slot, parent); }
172 QAction *KStandardGameAction::saveAs(const QObject *recvr, const char *slot,
173  QObject *parent)
174 { return KStandardGameAction::create(SaveAs, recvr, slot, parent); }
175 QAction *KStandardGameAction::end(const QObject *recvr, const char *slot,
176  QObject *parent)
177 { return KStandardGameAction::create(End, recvr, slot, parent); }
178 KToggleAction *KStandardGameAction::pause(const QObject *recvr, const char *slot,
179  QObject *parent)
180 { return static_cast<KToggleAction *>(KStandardGameAction::create(Pause, recvr, slot, parent)); }
181 QAction *KStandardGameAction::highscores(const QObject *recvr, const char *slot,
182  QObject *parent)
183 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
184 QAction *KStandardGameAction::statistics(const QObject *recvr, const char *slot,
185  QObject *parent)
186 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
187 QAction *KStandardGameAction::clearStatistics(const QObject *recvr, const char *slot,
188  QObject *parent)
189 { return KStandardGameAction::create(ClearStatistics, recvr, slot, parent); }
190 QAction *KStandardGameAction::print(const QObject *recvr, const char *slot,
191  QObject *parent)
192 { return KStandardGameAction::create(Print, recvr, slot, parent); }
193 QAction *KStandardGameAction::quit(const QObject *recvr, const char *slot,
194  QObject *parent)
195 { return KStandardGameAction::create(Quit, recvr, slot, parent); }
196 
197 QAction *KStandardGameAction::repeat(const QObject *recvr, const char *slot,
198  QObject *parent)
199 { return KStandardGameAction::create(Repeat, recvr, slot, parent); }
200 QAction *KStandardGameAction::undo(const QObject *recvr, const char *slot,
201  QObject *parent)
202 { return KStandardGameAction::create(Undo, recvr, slot, parent); }
203 
204 QAction *KStandardGameAction::redo(const QObject *recvr, const char *slot,
205  QObject *parent)
206 { return KStandardGameAction::create(Redo, recvr, slot, parent); }
207 
208 QAction *KStandardGameAction::roll(const QObject *recvr, const char *slot,
209  QObject *parent)
210 { return KStandardGameAction::create(Roll, recvr, slot, parent); }
211 QAction *KStandardGameAction::endTurn(const QObject *recvr, const char *slot,
212  QObject *parent)
213 { return KStandardGameAction::create(EndTurn, recvr, slot, parent); }
214 
215 QAction *KStandardGameAction::carddecks(const QObject *recvr, const char *slot,
216  QObject *parent)
217 { return KStandardGameAction::create(Carddecks, recvr, slot, parent); }
219  QObject *parent)
220 { return KStandardGameAction::create(ConfigureHighscores, recvr, slot, parent); }
221 QAction *KStandardGameAction::hint(const QObject*recvr, const char *slot,
222  QObject *parent)
223 { return KStandardGameAction::create(Hint, recvr, slot, parent); }
224 KToggleAction *KStandardGameAction::demo(const QObject*recvr, const char *slot,
225  QObject *parent)
226 { return static_cast<KToggleAction *>(KStandardGameAction::create(Demo, recvr, slot, parent)); }
227 QAction *KStandardGameAction::solve(const QObject*recvr, const char *slot,
228  QObject *parent)
229 { return KStandardGameAction::create(Solve, recvr, slot, parent); }
231  QObject *parent)
232 { return static_cast<KSelectAction *>(KStandardGameAction::create(ChooseGameType, recvr, slot, parent)); }
233 QAction *KStandardGameAction::restart(const QObject*recvr, const char *slot,
234  QObject *parent)
235 { return KStandardGameAction::create(Restart, recvr, slot, parent); }
KSelectAction * chooseGameType(const QObject *recvr, const char *slot, QObject *parent)
Choose game type.
QAction * carddecks(const QObject *recvr, const char *slot, QObject *parent)
Display configure carddecks dialog.
QAction * endTurn(const QObject *recvr, const char *slot, QObject *parent)
End the current turn (not the game).
QAction * highscores(const QObject *recvr, const char *slot, QObject *parent)
Show the highscores.
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
Repeat the last move.
Q_INVOKABLE QAction * addAction(const QString &name, QAction *action)
KToggleAction * pause(const QObject *recvr, const char *slot, QObject *parent)
Pause the game.
QAction * clearStatistics(const QObject *recvr, const char *slot, QObject *parent)
Clear statistics.
void setToolTip(const QString &tip)
QAction * gameNew(const QObject *recvr, const char *slot, QObject *parent)
Start a new game.
QAction * print(const QObject *recvr, const char *slot, QObject *parent)
Print current game.
QAction * saveAs(const QObject *recvr, const char *slot, QObject *parent)
Save the current game under a different filename.
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
Load a previously saved game.
void setShortcuts(const QList< QKeySequence > &shortcuts)
QAction * undo(const QObject *recvr, const char *slot, QObject *parent)
Undo the last move.
QAction * restart(const QObject *recvr, const char *slot, QObject *parent)
Restart the game.
QAction * statistics(const QObject *recvr, const char *slot, QObject *parent)
Show the statistics.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QAction * roll(const QObject *recvr, const char *slot, QObject *parent)
Roll die or dice.
const char * name(StandardGameAction id)
This will return the internal name of a given standard action.
QAction * _k_createInternal(StandardGameAction id, QObject *parent)
bool isEmpty() const const
void setObjectName(const QString &name)
#define I18N_NOOP(text)
StandardGameAction
The standard menubar and toolbar actions.
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
End the current game, but do not quit the program.
QAction * hint(const QObject *recvr, const char *slot, QObject *parent)
Give an advice/hint.
void setWhatsThis(const QString &what)
QAction * create(StandardGameAction id, const QObject *recvr, const char *slot, QObject *parent)
Creates an action corresponding to the KStandardAction::StandardAction enum.
QVariant fromValue(const T &value)
QAction * quit(const QObject *recvr, const char *slot, QObject *parent)
Quit the game.
QAction * save(const QObject *recvr, const char *slot, QObject *parent)
Save the current game.
KToggleAction * demo(const QObject *recvr, const char *slot, QObject *parent)
Show a demo.
QString i18n(const char *text, const TYPE &arg...)
KRecentFilesAction * loadRecent(const QObject *recvr, const char *slot, QObject *parent)
Load a recently loaded game.
QAction * solve(const QObject *recvr, const char *slot, QObject *parent)
Solve the game.
QAction * configureHighscores(const QObject *recvr, const char *slot, QObject *parent)
Display configure highscores dialog.
QAction * redo(const QObject *recvr, const char *slot, QObject *parent)
Redo the last move (which has been undone).
QString fromLatin1(const char *str, int size)
QIcon fromTheme(const QString &name)
bool setProperty(const char *name, const QVariant &value)
const QList< QKeySequence > & shortcut(StandardShortcut id)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jan 20 2021 22:40:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.