KDEGames

kstandardgameaction.h
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  This class was shamelessly stolen from kdelibs/kdeui/kstdction.[cpp|h] and
9  after that just edited for our needs
10 */
11 
12 #ifndef KSTANDARDGAMEACTION_H
13 #define KSTANDARDGAMEACTION_H
14 
15 // own
16 #include <libkdegames_export.h>
17 // KF
18 #include <kwidgetsaddons_version.h>
19 #include <KRecentFilesAction>
20 #include <KToggleAction>
21 #include <KSelectAction>
22 // Qt
23 #include <QAction>
24 #include <QLoggingCategory>
25 // std
26 #include <type_traits>
27 
28 Q_DECLARE_LOGGING_CATEGORY(GAMES_UI)
29 
30 /**
31  * Extension for KStandardAction in KDE Games
32  *
33  * This class is an extension to the usual KStandardAction class which provides
34  * easy access to often used KDE actions.
35  *
36  * Using these actions helps maintaining consistency among the games.
37  *
38  * Games often use different menu entries than other programs, e.g. games use
39  * the menu "game" instead of "file". This class provides the entries which
40  * differ from the usual KStandardAction entries.
41  *
42  * @see <tt>KStandardAction</tt>
43  *
44  * @author Andreas Beckermann <[email protected]>
45  */
47 {
48  /**
49  * The standard menubar and toolbar actions.
50  **/
52  // Game menu
53  New=1, Load, LoadRecent, Save, SaveAs, End, Pause, Highscores, Statistics,
54  Print, Quit,
55  // Move menu
56  Repeat, Undo, Redo, Roll, EndTurn,
57  // Settings menu
58  Carddecks,
59  ChooseGameType,
60  ConfigureHighscores,
61  ClearHighscores,
62  ClearStatistics,
63  Restart,
64  Hint,
65  Demo,
66  Solve,
67  ActionNone
68  };
69 
70  /**
71  * Creates an action corresponding to the
72  * KStandardAction::StandardAction enum.
73  */
74  KDEGAMES_EXPORT QAction* create( StandardGameAction id, const QObject *recvr, const char *slot,
75  QObject* parent );
76 
77  /**
78  * @internal
79  */
80  KDEGAMES_EXPORT QAction *_k_createInternal(StandardGameAction id, QObject *parent);
81 
82  /**
83  * This overloads create() to allow using the new connect syntax.
84  *
85  * @note If you use @c LoadRecent as @p id, you should manually connect to the urlSelected(const QUrl &)
86  * signal of the returned KRecentFilesAction instead or use KStandardGameAction::loadRecent(Receiver *, Func, QObject*).
87  * If you use @c ChooseGameType as @p id, you should manually connect to the triggered(int)
88  * signal of the returned KSelectAction instead or use KStandardGameAction::chooseGameType(Receiver *, Func, QObject*).
89  *
90  * @see create(StandardGameAction, const QObject *, const char *, QObject *)
91  * @since 7.3
92  */
93 #ifdef K_DOXYGEN
94  inline QAction *create(StandardGameAction id, const QObject *recvr, Func slot, QObject* parent)
95 #else
96  template<class Receiver, class Func>
97  inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, QAction>::type *create(StandardGameAction id, const Receiver *recvr, Func slot, QObject *parent)
98 #endif
99  {
100  QAction *action = _k_createInternal(id, parent);
101  QObject::connect(action, &QAction::triggered, recvr, slot);
102  return action;
103  }
104 
105  /**
106  * This will return the internal name of a given standard action.
107  */
108  KDEGAMES_EXPORT const char* name( StandardGameAction id );
109 
110 // we have to disable the templated function for const char* as Func, since it is ambiguous otherwise
111 // TODO: KF6: unify const char* version and new style by removing std::enable_if
112 #ifdef K_DOXYGEN
113 #define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
114  inline QAction *name(const QObject *recvr, Func slot, QObject *parent);
115 #define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
116  inline KToggleAction *name(const QObject *recvr, Func slot, QObject *parent);
117 #else
118 #define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
119  template<class Receiver, class Func> \
120  inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, QAction>::type *name(const Receiver *recvr, Func slot, QObject *parent) \
121  { return create(enumValue, recvr, slot, parent); }
122 #define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
123  template<class Receiver, class Func> \
124  inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KToggleAction>::type *name(const Receiver *recvr, Func slot, QObject *parent) \
125  { \
126  QAction* ret = create(enumValue, recvr, slot, parent); \
127  Q_ASSERT(qobject_cast<KToggleAction *>(ret)); \
128  return static_cast<KToggleAction *>(ret); \
129  }
130 #endif
131 
132  /**
133  * Start a new game.
134  **/
135  KDEGAMES_EXPORT QAction *gameNew(const QObject *recvr, const char *slot,
136  QObject *parent );
137  /**
138  * Start a new game.
139  * @since 7.3
140  */
141  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(gameNew, New)
142 
143  /**
144  * Load a previously saved game.
145  */
146  KDEGAMES_EXPORT QAction *load(const QObject *recvr, const char *slot,
147  QObject *parent );
148 
149  /**
150  * Load a previously saved game.
151  * @since 7.3
152  */
153  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(load, Load)
154 
155  // FIXME why not to delete this and use just KStandardAction::openRecent???
156  // loadRecent seems to mimic its behaviour
157  /**
158  * Load a recently loaded game.
159  * The signature of slot is of the form slotURLSelected(const QUrl&)
160  */
161  KDEGAMES_EXPORT KRecentFilesAction *loadRecent(const QObject *recvr, const char *slot,
162  QObject *parent );
163  /**
164  * Load a recently loaded game.
165  * @since 7.3
166  */
167 #ifdef K_DOXYGEN
168  inline KRecentFilesAction *loadRecent(const QObject *recvr, Func slot, QObject *parent)
169 #else
170  template<class Receiver, class Func>
171  inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KRecentFilesAction>::type *loadRecent(const Receiver *recvr, Func slot, QObject *parent)
172 #endif
173  {
174  QAction* action = _k_createInternal(LoadRecent, parent);
175  KRecentFilesAction* recentAction = qobject_cast<KRecentFilesAction*>(action);
176  Q_ASSERT(recentAction);
177  QObject::connect(recentAction, &KRecentFilesAction::urlSelected, recvr, slot);
178  return recentAction;
179  }
180 
181  /**
182  * Save the current game.
183  */
184  KDEGAMES_EXPORT QAction *save(const QObject *recvr, const char *slot,
185  QObject *parent);
186 
187  /**
188  * Save the current game.
189  * @since 7.3
190  */
191  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(save, Save)
192 
193  /**
194  * Save the current game under a different filename.
195  */
196  KDEGAMES_EXPORT QAction *saveAs(const QObject *recvr, const char *slot,
197  QObject *parent );
198 
199  /**
200  * Save the current game under a different filename.
201  * @since 7.3
202  */
203  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(saveAs, SaveAs)
204 
205  /**
206  * Pause the game.
207  **/
208  KDEGAMES_EXPORT KToggleAction *pause(const QObject *recvr, const char *slot,
209  QObject *parent );
210 
211  /**
212  * Pause the game.
213  * @since 7.3
214  */
215  KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(pause, Pause)
216 
217  /**
218  * Show the highscores.
219  */
220  KDEGAMES_EXPORT QAction *highscores(const QObject *recvr, const char *slot,
221  QObject *parent );
222 
223  /**
224  * Show the highscores.
225  * @since 7.3
226  */
227  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(highscores, Highscores)
228 
229  /**
230  * Clear highscores.
231  */
232  KDEGAMES_EXPORT QAction *clearHighscores(const QObject *recvr, const char *slot,
233  QObject *parent );
234 
235  /**
236  * Clear highscores.
237  * @since 7.3
238  */
239  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearHighscores, ClearHighscores)
240 
241  /**
242  * Show the statistics.
243  */
244  KDEGAMES_EXPORT QAction *statistics(const QObject *recvr, const char *slot,
245  QObject *parent );
246 
247  /**
248  * Show the statistics.
249  * @since 7.3
250  */
251  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(statistics, Statistics)
252 
253  /**
254  * Clear statistics.
255  */
256  KDEGAMES_EXPORT QAction *clearStatistics(const QObject *recvr, const char *slot,
257  QObject *parent );
258 
259  /**
260  * Clear statistics.
261  * @since 7.3
262  */
263  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearStatistics, ClearStatistics)
264 
265 
266  /**
267  * End the current game, but do not quit the program.
268  * Think of a "close" entry.
269  */
270  KDEGAMES_EXPORT QAction *end(const QObject *recvr, const char *slot,
271  QObject *parent );
272 
273  /**
274  * End the current game, but do not quit the program.
275  * Think of a "close" entry.
276  * @since 7.3
277  */
278  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(end, End)
279 
280  /**
281  * Print current game.
282  * Not useful in all games.
283  */
284  KDEGAMES_EXPORT QAction *print(const QObject *recvr, const char *slot,
285  QObject *parent );
286 
287  /**
288  * Print current game.
289  * Not useful in all games.
290  * @since 7.3
291  */
292  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(print, Print)
293 
294  /**
295  * Quit the game.
296  */
297  KDEGAMES_EXPORT QAction *quit(const QObject *recvr, const char *slot,
298  QObject *parent );
299 
300  /**
301  * Quit the game.
302  * @since 7.3
303  */
304  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(quit, Quit)
305 
306  /**
307  * Repeat the last move.
308  **/
309  KDEGAMES_EXPORT QAction *repeat(const QObject *recvr, const char *slot,
310  QObject *parent );
311 
312  /**
313  * Repeat the last move.
314  * @since 7.3
315  */
316  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(repeat, Repeat)
317 
318  /**
319  * Undo the last move.
320  **/
321  KDEGAMES_EXPORT QAction *undo(const QObject *recvr, const char *slot,
322  QObject *parent );
323 
324  /**
325  * Undo the last move.
326  * @since 7.3
327  */
328  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(undo, Undo)
329 
330  /**
331  * Redo the last move (which has been undone).
332  **/
333  KDEGAMES_EXPORT QAction *redo(const QObject *recvr, const char *slot,
334  QObject *parent );
335 
336  /**
337  * Redo the last move (which has been undone).
338  * @since 7.3
339  */
340  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(redo, Redo)
341 
342  /**
343  * Roll die or dice.
344  **/
345  KDEGAMES_EXPORT QAction *roll(const QObject *recvr, const char *slot,
346  QObject *parent );
347 
348  /**
349  * Roll die or dice.
350  * @since 7.3
351  */
352  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(roll, Roll)
353 
354  /**
355  * End the current turn (not the game).
356  * Usually to let the next player start.
357  **/
358  KDEGAMES_EXPORT QAction *endTurn(const QObject *recvr, const char *slot,
359  QObject *parent );
360 
361  /**
362  * End the current turn (not the game).
363  * Usually to let the next player start.
364  * @since 7.3
365  */
366  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(endTurn, EndTurn)
367 
368  /**
369  * Display configure carddecks dialog.
370  */
371  KDEGAMES_EXPORT QAction *carddecks(const QObject *recvr, const char *slot,
372  QObject *parent );
373 
374  /**
375  * Display configure carddecks dialog.
376  * @since 7.3
377  */
378  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(carddecks, Carddecks)
379 
380  /**
381  * Display configure highscores dialog.
382  */
383  KDEGAMES_EXPORT QAction *configureHighscores(const QObject *recvr, const char *slot,
384  QObject *parent );
385 
386  /**
387  * Display configure highscores dialog.
388  * @since 7.3
389  */
390  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(configureHighscores, ConfigureHighscores)
391 
392  /**
393  * Give an advice/hint.
394  */
395  KDEGAMES_EXPORT QAction *hint(const QObject *recvr, const char *slot,
396  QObject *parent );
397 
398  /**
399  * Give an advice/hint.
400  * @since 7.3
401  */
402  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(hint, Hint)
403 
404  /**
405  * Show a demo.
406  */
407  KDEGAMES_EXPORT KToggleAction *demo(const QObject *recvr, const char *slot,
408  QObject *parent );
409 
410  /**
411  * Show a demo.
412  * @since 7.3
413  */
414  KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(demo, Demo)
415 
416  /**
417  * Solve the game.
418  */
419  KDEGAMES_EXPORT QAction *solve(const QObject *recvr, const char *slot,
420  QObject *parent );
421 
422  /**
423  * Solve the game.
424  * @since 7.3
425  */
426  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(solve, Solve)
427 
428  /**
429  * Choose game type.
430  * The signature of slot is of the form slotGameTypeChosen(int)
431  */
432  KDEGAMES_EXPORT KSelectAction *chooseGameType(const QObject *recvr, const char *slot,
433  QObject *parent );
434 
435  /**
436  * Choose game type.
437  * @since 7.3
438  */
439 #ifdef K_DOXYGEN
440  inline KSelectAction *chooseGameType(const QObject *recvr, Func slot, QObject *parent)
441 #else
442  template<class Receiver, class Func>
443  inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KSelectAction>::type *chooseGameType(const Receiver *recvr, Func slot, QObject *parent)
444 #endif
445  {
446  QAction* action = _k_createInternal(ChooseGameType, parent);
447  KSelectAction* chooseGameTypeAction = qobject_cast<KSelectAction*>(action);
448  Q_ASSERT(chooseGameTypeAction);
449 #if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5, 78, 0)
450  QObject::connect(chooseGameTypeAction, &KSelectAction::indexTriggered, recvr, slot);
451 #else
452  QObject::connect(chooseGameTypeAction, QOverload<int>::of(&KSelectAction::triggered), recvr, slot);
453 #endif
454  return chooseGameTypeAction;
455  }
456 
457  /**
458  * Restart the game.
459  */
460  KDEGAMES_EXPORT QAction *restart(const QObject *recvr, const char *slot,
461  QObject *parent );
462  /**
463  * Restart the game.
464  * @since 7.3
465  */
466  KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(restart, Restart)
467 
468 }
469 
470 #endif
KSelectAction * chooseGameType(const QObject *recvr, const char *slot, QObject *parent)
Choose game type.
void triggered(bool checked)
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.
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 urlSelected(const QUrl &url)
QAction * gameNew(const QObject *recvr, const char *slot, QObject *parent)
Start a new game.
Extension for KStandardAction in KDE Games.
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
Load a previously saved game.
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.
QAction * roll(const QObject *recvr, const char *slot, QObject *parent)
Roll die or dice.
StandardGameAction
The standard menubar and toolbar actions.
QAction * hint(const QObject *recvr, const char *slot, QObject *parent)
Give an advice/hint.
KToggleAction * demo(const QObject *recvr, const char *slot, QObject *parent)
Show a demo.
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.
void indexTriggered(int index)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void triggered(QAction *action)
QAction * clearHighscores(const QObject *recvr, const char *slot, QObject *parent)
Clear highscores.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Mar 4 2021 22:38:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.