File indexing completed on 2024-04-28 04:05:04
0001 /* 0002 This file is part of the KDE games library 0003 SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de> 0004 SPDX-FileCopyrightText: 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch> 0005 0006 SPDX-License-Identifier: LGPL-2.0-only 0007 0008 This class was shamelessly stolen from kdelibs/kdeui/kstdction.[cpp|h] and 0009 after that just edited for our needs 0010 */ 0011 0012 #ifndef KGAMESTANDARDACTION_H 0013 #define KGAMESTANDARDACTION_H 0014 0015 // own 0016 #include "kdegames_export.h" 0017 // KF 0018 #include <KRecentFilesAction> 0019 #include <KToggleAction> 0020 // Qt 0021 #include <QAction> 0022 // std 0023 #include <type_traits> 0024 0025 /** 0026 * Extension for KStandardAction in KDE Games 0027 * 0028 * This class is an extension to the usual KStandardAction class which provides 0029 * easy access to often used KDE actions. 0030 * 0031 * Using these actions helps maintaining consistency among the games. 0032 * 0033 * Games often use different menu entries than other programs, e.g. games use 0034 * the menu "game" instead of "file". This class provides the entries which 0035 * differ from the usual KStandardAction entries. 0036 * 0037 * @see <tt>KStandardAction</tt> 0038 * 0039 * @author Andreas Beckermann <b_mann@gmx.de> 0040 */ 0041 namespace KGameStandardAction 0042 { 0043 /** 0044 * The standard menubar and toolbar actions. 0045 */ 0046 enum GameStandardAction { 0047 // Game menu 0048 New = 1, 0049 Load, 0050 LoadRecent, 0051 Save, 0052 SaveAs, 0053 End, 0054 Pause, 0055 Highscores, 0056 Statistics, 0057 Print, 0058 Quit, 0059 // Move menu 0060 Repeat, 0061 Undo, 0062 Redo, 0063 Roll, 0064 EndTurn, 0065 // Settings menu 0066 Carddecks, 0067 ConfigureHighscores, 0068 ClearHighscores, 0069 ClearStatistics, 0070 Restart, 0071 Hint, 0072 Demo, 0073 Solve, 0074 ActionNone 0075 }; 0076 0077 /** 0078 * Creates an action corresponding to the 0079 * KStandardAction::StandardAction enum. 0080 */ 0081 KDEGAMES_EXPORT QAction *create(GameStandardAction id, const QObject *recvr, const char *slot, QObject *parent); 0082 0083 /** 0084 * @internal 0085 */ 0086 KDEGAMES_EXPORT QAction *_k_createInternal(GameStandardAction id, QObject *parent); 0087 0088 /** 0089 * This overloads create() to allow using the new connect syntax. 0090 * 0091 * @note If you use @c LoadRecent as @p id, you should manually connect to the urlSelected(const QUrl &) 0092 * signal of the returned KRecentFilesAction instead or use KGameStandardAction::loadRecent(Receiver *, Func, QObject*). 0093 * 0094 * @see create(GameStandardAction, const QObject *, const char *, QObject *) 0095 * @since 7.3 0096 */ 0097 #ifdef K_DOXYGEN 0098 inline QAction *create(GameStandardAction id, const QObject *recvr, Func slot, QObject *parent) 0099 #else 0100 template<class Receiver, class Func> 0101 inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type * 0102 create(GameStandardAction id, const Receiver *recvr, Func slot, QObject *parent) 0103 #endif 0104 { 0105 QAction *action = _k_createInternal(id, parent); 0106 QObject::connect(action, &QAction::triggered, recvr, slot); 0107 return action; 0108 } 0109 0110 /** 0111 * This will return the internal name of a given standard action. 0112 */ 0113 KDEGAMES_EXPORT QString name(GameStandardAction id); 0114 0115 // we have to disable the templated function for const char* as Func, since it is ambiguous otherwise 0116 // TODO: KF6: unify const char* version and new style by removing std::enable_if 0117 #ifdef K_DOXYGEN 0118 #define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) inline QAction *name(const QObject *recvr, Func slot, QObject *parent); 0119 #define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) inline KToggleAction *name(const QObject *recvr, Func slot, QObject *parent); 0120 #else 0121 #define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \ 0122 template<class Receiver, class Func> \ 0123 inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, QAction>::type *name(const Receiver *recvr, Func slot, QObject *parent) \ 0124 { \ 0125 return create(enumValue, recvr, slot, parent); \ 0126 } 0127 #define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \ 0128 template<class Receiver, class Func> \ 0129 inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, KToggleAction>::type *name(const Receiver *recvr, \ 0130 Func slot, \ 0131 QObject *parent) \ 0132 { \ 0133 QAction *ret = create(enumValue, recvr, slot, parent); \ 0134 Q_ASSERT(qobject_cast<KToggleAction *>(ret)); \ 0135 return static_cast<KToggleAction *>(ret); \ 0136 } 0137 #endif 0138 0139 /** 0140 * Start a new game. 0141 */ 0142 KDEGAMES_EXPORT QAction *gameNew(const QObject *recvr, const char *slot, QObject *parent); 0143 /** 0144 * Start a new game. 0145 * @since 7.3 0146 */ 0147 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(gameNew, New) 0148 0149 /** 0150 * Load a previously saved game. 0151 */ 0152 KDEGAMES_EXPORT QAction *load(const QObject *recvr, const char *slot, QObject *parent); 0153 0154 /** 0155 * Load a previously saved game. 0156 * @since 7.3 0157 */ 0158 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(load, Load) 0159 0160 // FIXME why not to delete this and use just KStandardAction::openRecent??? 0161 // loadRecent seems to mimic its behaviour 0162 /** 0163 * Load a recently loaded game. 0164 * The signature of slot is of the form slotURLSelected(const QUrl&) 0165 */ 0166 KDEGAMES_EXPORT KRecentFilesAction *loadRecent(const QObject *recvr, const char *slot, QObject *parent); 0167 /** 0168 * Load a recently loaded game. 0169 * @since 7.3 0170 */ 0171 #ifdef K_DOXYGEN 0172 inline KRecentFilesAction *loadRecent(const QObject *recvr, Func slot, QObject *parent) 0173 #else 0174 template<class Receiver, class Func> 0175 inline typename std::enable_if<!std::is_convertible<Func, const char *>::value, KRecentFilesAction>::type * 0176 loadRecent(const Receiver *recvr, Func slot, QObject *parent) 0177 #endif 0178 { 0179 QAction *action = _k_createInternal(LoadRecent, parent); 0180 KRecentFilesAction *recentAction = qobject_cast<KRecentFilesAction *>(action); 0181 Q_ASSERT(recentAction); 0182 QObject::connect(recentAction, &KRecentFilesAction::urlSelected, recvr, slot); 0183 return recentAction; 0184 } 0185 0186 /** 0187 * Save the current game. 0188 */ 0189 KDEGAMES_EXPORT QAction *save(const QObject *recvr, const char *slot, QObject *parent); 0190 0191 /** 0192 * Save the current game. 0193 * @since 7.3 0194 */ 0195 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(save, Save) 0196 0197 /** 0198 * Save the current game under a different filename. 0199 */ 0200 KDEGAMES_EXPORT QAction *saveAs(const QObject *recvr, const char *slot, QObject *parent); 0201 0202 /** 0203 * Save the current game under a different filename. 0204 * @since 7.3 0205 */ 0206 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(saveAs, SaveAs) 0207 0208 /** 0209 * Pause the game. 0210 */ 0211 KDEGAMES_EXPORT KToggleAction *pause(const QObject *recvr, const char *slot, QObject *parent); 0212 0213 /** 0214 * Pause the game. 0215 * @since 7.3 0216 */ 0217 KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(pause, Pause) 0218 0219 /** 0220 * Show the highscores. 0221 */ 0222 KDEGAMES_EXPORT QAction *highscores(const QObject *recvr, const char *slot, QObject *parent); 0223 0224 /** 0225 * Show the highscores. 0226 * @since 7.3 0227 */ 0228 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(highscores, Highscores) 0229 0230 /** 0231 * Clear highscores. 0232 */ 0233 KDEGAMES_EXPORT QAction *clearHighscores(const QObject *recvr, const char *slot, QObject *parent); 0234 0235 /** 0236 * Clear highscores. 0237 * @since 7.3 0238 */ 0239 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearHighscores, ClearHighscores) 0240 0241 /** 0242 * Show the statistics. 0243 */ 0244 KDEGAMES_EXPORT QAction *statistics(const QObject *recvr, const char *slot, QObject *parent); 0245 0246 /** 0247 * Show the statistics. 0248 * @since 7.3 0249 */ 0250 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(statistics, Statistics) 0251 0252 /** 0253 * Clear statistics. 0254 */ 0255 KDEGAMES_EXPORT QAction *clearStatistics(const QObject *recvr, const char *slot, QObject *parent); 0256 0257 /** 0258 * Clear statistics. 0259 * @since 7.3 0260 */ 0261 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearStatistics, ClearStatistics) 0262 0263 /** 0264 * End the current game, but do not quit the program. 0265 * Think of a "close" entry. 0266 */ 0267 KDEGAMES_EXPORT QAction *end(const QObject *recvr, const char *slot, QObject *parent); 0268 0269 /** 0270 * End the current game, but do not quit the program. 0271 * Think of a "close" entry. 0272 * @since 7.3 0273 */ 0274 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(end, End) 0275 0276 /** 0277 * Print current game. 0278 * Not useful in all games. 0279 */ 0280 KDEGAMES_EXPORT QAction *print(const QObject *recvr, const char *slot, QObject *parent); 0281 0282 /** 0283 * Print current game. 0284 * Not useful in all games. 0285 * @since 7.3 0286 */ 0287 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(print, Print) 0288 0289 /** 0290 * Quit the game. 0291 */ 0292 KDEGAMES_EXPORT QAction *quit(const QObject *recvr, const char *slot, QObject *parent); 0293 0294 /** 0295 * Quit the game. 0296 * @since 7.3 0297 */ 0298 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(quit, Quit) 0299 0300 /** 0301 * Repeat the last move. 0302 */ 0303 KDEGAMES_EXPORT QAction *repeat(const QObject *recvr, const char *slot, QObject *parent); 0304 0305 /** 0306 * Repeat the last move. 0307 * @since 7.3 0308 */ 0309 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(repeat, Repeat) 0310 0311 /** 0312 * Undo the last move. 0313 */ 0314 KDEGAMES_EXPORT QAction *undo(const QObject *recvr, const char *slot, QObject *parent); 0315 0316 /** 0317 * Undo the last move. 0318 * @since 7.3 0319 */ 0320 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(undo, Undo) 0321 0322 /** 0323 * Redo the last move (which has been undone). 0324 */ 0325 KDEGAMES_EXPORT QAction *redo(const QObject *recvr, const char *slot, QObject *parent); 0326 0327 /** 0328 * Redo the last move (which has been undone). 0329 * @since 7.3 0330 */ 0331 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(redo, Redo) 0332 0333 /** 0334 * Roll die or dice. 0335 */ 0336 KDEGAMES_EXPORT QAction *roll(const QObject *recvr, const char *slot, QObject *parent); 0337 0338 /** 0339 * Roll die or dice. 0340 * @since 7.3 0341 */ 0342 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(roll, Roll) 0343 0344 /** 0345 * End the current turn (not the game). 0346 * Usually to let the next player start. 0347 */ 0348 KDEGAMES_EXPORT QAction *endTurn(const QObject *recvr, const char *slot, QObject *parent); 0349 0350 /** 0351 * End the current turn (not the game). 0352 * Usually to let the next player start. 0353 * @since 7.3 0354 */ 0355 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(endTurn, EndTurn) 0356 0357 /** 0358 * Display configure carddecks dialog. 0359 */ 0360 KDEGAMES_EXPORT QAction *carddecks(const QObject *recvr, const char *slot, QObject *parent); 0361 0362 /** 0363 * Display configure carddecks dialog. 0364 * @since 7.3 0365 */ 0366 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(carddecks, Carddecks) 0367 0368 /** 0369 * Display configure highscores dialog. 0370 */ 0371 KDEGAMES_EXPORT QAction *configureHighscores(const QObject *recvr, const char *slot, QObject *parent); 0372 0373 /** 0374 * Display configure highscores dialog. 0375 * @since 7.3 0376 */ 0377 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(configureHighscores, ConfigureHighscores) 0378 0379 /** 0380 * Give an advice/hint. 0381 */ 0382 KDEGAMES_EXPORT QAction *hint(const QObject *recvr, const char *slot, QObject *parent); 0383 0384 /** 0385 * Give an advice/hint. 0386 * @since 7.3 0387 */ 0388 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(hint, Hint) 0389 0390 /** 0391 * Show a demo. 0392 */ 0393 KDEGAMES_EXPORT KToggleAction *demo(const QObject *recvr, const char *slot, QObject *parent); 0394 0395 /** 0396 * Show a demo. 0397 * @since 7.3 0398 */ 0399 KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(demo, Demo) 0400 0401 /** 0402 * Solve the game. 0403 */ 0404 KDEGAMES_EXPORT QAction *solve(const QObject *recvr, const char *slot, QObject *parent); 0405 0406 /** 0407 * Solve the game. 0408 * @since 7.3 0409 */ 0410 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(solve, Solve) 0411 0412 /** 0413 * Restart the game. 0414 */ 0415 KDEGAMES_EXPORT QAction *restart(const QObject *recvr, const char *slot, QObject *parent); 0416 /** 0417 * Restart the game. 0418 * @since 7.3 0419 */ 0420 KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(restart, Restart) 0421 } 0422 0423 #endif