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

KDE's Doxygen guidelines are available online.