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 <KConfig>
16 // Qt
17 #include <QIcon>
18 
19 #undef I18N_NOOP2
20 #define I18N_NOOP2(ctx,txt) ctx, txt
21 
22 Q_LOGGING_CATEGORY(GAMES_UI, "org.kde.games.ui", QtWarningMsg)
23 
24 struct KStandardGameActionInfo
25 {
27  KStandardShortcut::StandardShortcut globalAccel; // if we reuse a global accel
28  int shortcut; // specific shortcut (NH: should be configurable)
29  const char* psName;
30  const char* psLabelContext;
31  const char* psLabel;
32  const char* psWhatsThis;
33  const char* psIconName;
34  const char* psToolTip;
35 };
36 
37 const KStandardGameActionInfo g_rgActionInfo[] = {
38 // "game" menu
39  { 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") },
40  { KStandardGameAction::Load, KStandardShortcut::Open, 0, "game_load", nullptr, I18N_NOOP("&Load..."), nullptr, "document-open", I18N_NOOP("Open a saved game...") },
41  { KStandardGameAction::LoadRecent, KStandardShortcut::AccelNone, 0, "game_load_recent", nullptr, I18N_NOOP("Load &Recent"), nullptr, nullptr, I18N_NOOP("Open a recently saved game...") },
42  { KStandardGameAction::Restart, KStandardShortcut::Reload, 0, "game_restart", nullptr, I18N_NOOP("Restart &Game"), nullptr, "view-refresh", I18N_NOOP("Restart the game") },
43  { KStandardGameAction::Save, KStandardShortcut::Save, 0, "game_save", nullptr, I18N_NOOP("&Save"), nullptr, "document-save", I18N_NOOP("Save the current game") },
44  { 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") },
45  { KStandardGameAction::End, KStandardShortcut::End, 0, "game_end", nullptr, I18N_NOOP("&End Game"), nullptr, "window-close", I18N_NOOP("End the current game") },
46  { KStandardGameAction::Pause, KStandardShortcut::AccelNone, Qt::Key_P, "game_pause", nullptr, I18N_NOOP("Pa&use"), nullptr, "media-playback-pause", I18N_NOOP("Pause the game") },
47  { 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") },
48  { KStandardGameAction::ClearHighscores, KStandardShortcut::AccelNone, 0, "game_clear_highscores", nullptr, I18N_NOOP("&Clear High Scores"), nullptr, "clear_highscore", I18N_NOOP("Clear high scores") },
49  { KStandardGameAction::Statistics, KStandardShortcut::AccelNone, 0, "game_statistics", nullptr, I18N_NOOP("Show Statistics"), nullptr, "highscore", I18N_NOOP("Show statistics") },
50  { KStandardGameAction::ClearStatistics, KStandardShortcut::AccelNone, 0, "game_clear_statistics", nullptr, I18N_NOOP("&Clear Statistics"), nullptr, "flag", I18N_NOOP("Delete all-time statistics.") },
51  { KStandardGameAction::Print, KStandardShortcut::Print, 0, "game_print", nullptr, I18N_NOOP("&Print..."), nullptr, "document-print", nullptr },
52  { KStandardGameAction::Quit, KStandardShortcut::Quit, 0, "game_quit", nullptr, I18N_NOOP("&Quit"), nullptr, "application-exit", I18N_NOOP("Quit the program") },
53 // "move" menu
54  { KStandardGameAction::Repeat, KStandardShortcut::AccelNone, 0, "move_repeat", nullptr, I18N_NOOP("Repeat"), nullptr, nullptr, I18N_NOOP("Repeat the last move") },
55  { KStandardGameAction::Undo, KStandardShortcut::Undo, 0, "move_undo", nullptr, I18N_NOOP("Und&o"), nullptr, "edit-undo", I18N_NOOP("Undo the last move") },
56  { KStandardGameAction::Redo, KStandardShortcut::Redo, 0, "move_redo", nullptr, I18N_NOOP("Re&do"), nullptr, "edit-redo", I18N_NOOP("Redo the latest move") },
57  { KStandardGameAction::Roll, KStandardShortcut::AccelNone, Qt::CTRL | Qt::Key_R, "move_roll", nullptr, I18N_NOOP("&Roll Dice"), nullptr, "roll", I18N_NOOP("Roll the dice") },
58  { KStandardGameAction::EndTurn, KStandardShortcut::AccelNone, 0, "move_end_turn", nullptr, I18N_NOOP("End Turn"), nullptr, "games-endturn", nullptr },
59  { KStandardGameAction::Hint, KStandardShortcut::AccelNone, Qt::Key_H, "move_hint", nullptr, I18N_NOOP("&Hint"), nullptr, "games-hint", I18N_NOOP("Give a hint") },
60  { KStandardGameAction::Demo, KStandardShortcut::AccelNone, Qt::Key_D, "move_demo", nullptr, I18N_NOOP("&Demo"), nullptr, "media-playback-start", I18N_NOOP("Play a demo") },
61  { KStandardGameAction::Solve, KStandardShortcut::AccelNone, 0, "move_solve", nullptr, I18N_NOOP("&Solve"), nullptr, "games-solve", I18N_NOOP("Solve the game") },
62 // "settings" menu
63  { KStandardGameAction::ChooseGameType, KStandardShortcut::AccelNone, 0, "options_choose_game_type", nullptr, I18N_NOOP("Choose Game &Type"), nullptr, nullptr, nullptr },
64  { KStandardGameAction::Carddecks, KStandardShortcut::AccelNone, 0, "options_configure_carddecks", nullptr, I18N_NOOP("Configure &Carddecks..."), nullptr, nullptr, nullptr },
65  { KStandardGameAction::ConfigureHighscores, KStandardShortcut::AccelNone, 0, "options_configure_highscores", nullptr, I18N_NOOP("Configure &High Scores..."), nullptr, nullptr, nullptr },
66 
67  { KStandardGameAction::ActionNone, KStandardShortcut::AccelNone, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }
68 };
69 
70 static const KStandardGameActionInfo* infoPtr( KStandardGameAction::StandardGameAction id )
71 {
72  for (uint i = 0; g_rgActionInfo[i].id!=KStandardGameAction::ActionNone; i++) {
73  if( g_rgActionInfo[i].id == id )
74  return &g_rgActionInfo[i];
75  }
76  return nullptr;
77 }
78 
80 {
81  QAction* pAction = nullptr;
82  const KStandardGameActionInfo* pInfo = infoPtr( id );
83 // qCDebug(GAMES_UI) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)nullptr) << "," << parent << " )";
84  if( pInfo ) {
85  QString sLabel = i18nc(pInfo->psLabelContext, pInfo->psLabel);
86  switch( id ) {
87  case LoadRecent:
88  pAction = new KRecentFilesAction(sLabel, parent);
89  break;
90  case Pause:
91  case Demo:
92  pAction = new KToggleAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
93  break;
94  case ChooseGameType:
95  pAction = new KSelectAction( QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
96  break;
97  default:
98  pAction = new QAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
99  break;
100  }
101 
102  QList<QKeySequence> cut = (pInfo->globalAccel==KStandardShortcut::AccelNone
103  ? QList<QKeySequence>() << QKeySequence(pInfo->shortcut)
104  : KStandardShortcut::shortcut(pInfo->globalAccel));
105  if (!cut.isEmpty()) {
106  pAction->setShortcuts(cut);
107  pAction->setProperty("defaultShortcuts", QVariant::fromValue(cut));
108  }
109  if (pInfo->psToolTip)
110  pAction->setToolTip(i18n(pInfo->psToolTip));
111  if (pInfo->psWhatsThis)
112  pAction->setWhatsThis(i18n(pInfo->psWhatsThis));
113  else if (pInfo->psToolTip)
114  pAction->setWhatsThis(i18n(pInfo->psToolTip));
115 
116  pAction->setObjectName(QLatin1String( pInfo->psName ));
117  }
118 
119  KActionCollection *collection = qobject_cast<KActionCollection *>(parent);
120  if (collection && pAction)
121  collection->addAction(pAction->objectName(), pAction);
122 
123  return pAction;
124 }
125 
126 QAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot,
127  QObject* parent )
128 {
129  QAction* pAction = _k_createInternal(id, parent);
130  if (recvr && slot) {
131  switch( id ) {
132  case LoadRecent:
133  QObject::connect(pAction, SIGNAL(urlSelected(QUrl)), recvr, slot);
134  break;
135  case ChooseGameType:
136  QObject::connect(pAction, SIGNAL(indexTriggered(int)), recvr, slot);
137  break;
138  default:
139  QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
140  break;
141  }
142  }
143  return pAction;
144 
145 }
146 
148 {
149  const KStandardGameActionInfo* pInfo = infoPtr( id );
150  return (pInfo) ? pInfo->psName : nullptr;
151 }
152 
153 QAction *KStandardGameAction::gameNew(const QObject *recvr, const char *slot,
154  QObject *parent)
155 { return KStandardGameAction::create(New, recvr, slot, parent); }
156 QAction *KStandardGameAction::load(const QObject *recvr, const char *slot,
157  QObject *parent)
158 { return KStandardGameAction::create(Load, recvr, slot, parent); }
160  QObject *parent)
161 { return static_cast<KRecentFilesAction *>(KStandardGameAction::create(LoadRecent, recvr, slot, parent)); }
162 QAction *KStandardGameAction::save(const QObject *recvr, const char *slot,
163  QObject *parent)
164 { return KStandardGameAction::create(Save, recvr, slot, parent); }
165 QAction *KStandardGameAction::saveAs(const QObject *recvr, const char *slot,
166  QObject *parent)
167 { return KStandardGameAction::create(SaveAs, recvr, slot, parent); }
168 QAction *KStandardGameAction::end(const QObject *recvr, const char *slot,
169  QObject *parent)
170 { return KStandardGameAction::create(End, recvr, slot, parent); }
171 KToggleAction *KStandardGameAction::pause(const QObject *recvr, const char *slot,
172  QObject *parent)
173 { return static_cast<KToggleAction *>(KStandardGameAction::create(Pause, recvr, slot, parent)); }
174 QAction *KStandardGameAction::highscores(const QObject *recvr, const char *slot,
175  QObject *parent)
176 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
177 QAction *KStandardGameAction::statistics(const QObject *recvr, const char *slot,
178  QObject *parent)
179 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
180 QAction *KStandardGameAction::clearStatistics(const QObject *recvr, const char *slot,
181  QObject *parent)
182 { return KStandardGameAction::create(ClearStatistics, recvr, slot, parent); }
183 QAction *KStandardGameAction::print(const QObject *recvr, const char *slot,
184  QObject *parent)
185 { return KStandardGameAction::create(Print, recvr, slot, parent); }
186 QAction *KStandardGameAction::quit(const QObject *recvr, const char *slot,
187  QObject *parent)
188 { return KStandardGameAction::create(Quit, recvr, slot, parent); }
189 
190 QAction *KStandardGameAction::repeat(const QObject *recvr, const char *slot,
191  QObject *parent)
192 { return KStandardGameAction::create(Repeat, recvr, slot, parent); }
193 QAction *KStandardGameAction::undo(const QObject *recvr, const char *slot,
194  QObject *parent)
195 { return KStandardGameAction::create(Undo, recvr, slot, parent); }
196 
197 QAction *KStandardGameAction::redo(const QObject *recvr, const char *slot,
198  QObject *parent)
199 { return KStandardGameAction::create(Redo, recvr, slot, parent); }
200 
201 QAction *KStandardGameAction::roll(const QObject *recvr, const char *slot,
202  QObject *parent)
203 { return KStandardGameAction::create(Roll, recvr, slot, parent); }
204 QAction *KStandardGameAction::endTurn(const QObject *recvr, const char *slot,
205  QObject *parent)
206 { return KStandardGameAction::create(EndTurn, recvr, slot, parent); }
207 
208 QAction *KStandardGameAction::carddecks(const QObject *recvr, const char *slot,
209  QObject *parent)
210 { return KStandardGameAction::create(Carddecks, recvr, slot, parent); }
212  QObject *parent)
213 { return KStandardGameAction::create(ConfigureHighscores, recvr, slot, parent); }
214 QAction *KStandardGameAction::hint(const QObject*recvr, const char *slot,
215  QObject *parent)
216 { return KStandardGameAction::create(Hint, recvr, slot, parent); }
217 KToggleAction *KStandardGameAction::demo(const QObject*recvr, const char *slot,
218  QObject *parent)
219 { return static_cast<KToggleAction *>(KStandardGameAction::create(Demo, recvr, slot, parent)); }
220 QAction *KStandardGameAction::solve(const QObject*recvr, const char *slot,
221  QObject *parent)
222 { return KStandardGameAction::create(Solve, recvr, slot, parent); }
224  QObject *parent)
225 { return static_cast<KSelectAction *>(KStandardGameAction::create(ChooseGameType, recvr, slot, parent)); }
226 QAction *KStandardGameAction::restart(const QObject*recvr, const char *slot,
227  QObject *parent)
228 { 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 Tue Nov 30 2021 22:34:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.