Commit a9d14a04 authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

KStandardGameAction: add method overloads for new connect syntax

parent 681d7041
......@@ -89,37 +89,26 @@ static const KStandardGameActionInfo* infoPtr( KStandardGameAction::StandardGame
return nullptr;
}
QAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot,
QObject* parent )
QAction* KStandardGameAction::_k_createInternal(KStandardGameAction::StandardGameAction id, QObject *parent)
{
QAction* pAction = nullptr;
const KStandardGameActionInfo* pInfo = infoPtr( id );
qCDebug(GAMES_UI) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)nullptr) << "," << parent << " )";
// qCDebug(GAMES_UI) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)nullptr) << "," << parent << " )";
if( pInfo ) {
QString sLabel = i18nc(pInfo->psLabelContext, pInfo->psLabel);
bool do_connect = (recvr && slot); //both not 0
switch( id ) {
case LoadRecent:
pAction = new KRecentFilesAction(sLabel, parent);
if(do_connect)
QObject::connect( pAction, SIGNAL(urlSelected(QUrl)), recvr, slot);
break;
case Pause:
case Demo:
pAction = new KToggleAction(QIcon::fromTheme(QLatin1String( pInfo->psIconName )), sLabel, parent);
if(do_connect)
QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
pAction = new KToggleAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
break;
case ChooseGameType:
pAction = new KSelectAction( QIcon::fromTheme(QLatin1String( pInfo->psIconName )), sLabel, parent);
if(do_connect)
QObject::connect( pAction, SIGNAL(triggered(int)), recvr, slot );
pAction = new KSelectAction( QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
break;
default:
pAction = new QAction(QIcon::fromTheme(QLatin1String( pInfo->psIconName )), sLabel, parent);
if(do_connect)
QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
pAction = new QAction(QIcon::fromTheme(QString::fromLatin1(pInfo->psIconName)), sLabel, parent);
break;
}
......@@ -147,6 +136,27 @@ QAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr
return pAction;
}
QAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot,
QObject* parent )
{
QAction* pAction = _k_createInternal(id, parent);
if (recvr && slot) {
switch( id ) {
case LoadRecent:
QObject::connect(pAction, SIGNAL(urlSelected(QUrl)), recvr, slot);
break;
case ChooseGameType:
QObject::connect(pAction, SIGNAL(triggered(int)), recvr, slot);
break;
default:
QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
break;
}
}
return pAction;
}
const char* KStandardGameAction::name( StandardGameAction id )
{
const KStandardGameActionInfo* pInfo = infoPtr( id );
......
......@@ -23,14 +23,15 @@
#define KSTANDARDGAMEACTION_H
#include <libkdegames_export.h>
// KF
#include <KRecentFilesAction>
#include <KToggleAction>
#include <KSelectAction>
// Qt
#include <QAction>
#include <QLoggingCategory>
class QAction;
class KToggleAction;
class QObject;
class KRecentFilesAction;
class KSelectAction;
// std
#include <type_traits>
Q_DECLARE_LOGGING_CATEGORY(GAMES_UI)
......@@ -81,17 +82,71 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction* create( StandardGameAction id, const QObject *recvr, const char *slot,
QObject* parent );
/**
* @internal
*/
KDEGAMES_EXPORT QAction *_k_createInternal(StandardGameAction id, QObject *parent);
/**
* This overloads create() to allow using the new connect syntax.
*
* @note If you use @c LoadRecent as @p id, you should manually connect to the urlSelected(const QUrl &)
* signal of the returned KRecentFilesAction instead or use KStandardGameAction::loadRecent(Receiver *, Func, QObject*).
* If you use @c ChooseGameType as @p id, you should manually connect to the triggered(int)
* signal of the returned KSelectAction instead or use KStandardGameAction::chooseGameType(Receiver *, Func, QObject*).
*
* @see create(StandardGameAction, const QObject *, const char *, QObject *)
* @since 7.3
*/
#ifdef K_DOXYGEN
inline QAction *create(StandardGameAction id, const QObject *recvr, Func slot, QObject* parent)
#else
template<class Receiver, class Func>
inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, QAction>::type *create(StandardGameAction id, const Receiver *recvr, Func slot, QObject *parent)
#endif
{
QAction *action = _k_createInternal(id, parent);
QObject::connect(action, &QAction::triggered, recvr, slot);
return action;
}
/**
* This will return the internal name of a given standard action.
*/
KDEGAMES_EXPORT const char* name( StandardGameAction id );
// we have to disable the templated function for const char* as Func, since it is ambiguous otherwise
// TODO: KF6: unify const char* version and new style by removing std::enable_if
#ifdef K_DOXYGEN
#define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
inline QAction *name(const QObject *recvr, Func slot, QObject *parent);
#define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
inline KToggleAction *name(const QObject *recvr, Func slot, QObject *parent);
#else
#define KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
template<class Receiver, class Func> \
inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, QAction>::type *name(const Receiver *recvr, Func slot, QObject *parent) \
{ return create(enumValue, recvr, slot, parent); }
#define KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(name, enumValue) \
template<class Receiver, class Func> \
inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KToggleAction>::type *name(const Receiver *recvr, Func slot, QObject *parent) \
{ \
QAction* ret = create(enumValue, recvr, slot, parent); \
Q_ASSERT(qobject_cast<KToggleAction *>(ret)); \
return static_cast<KToggleAction *>(ret); \
}
#endif
/**
* Start a new game.
**/
KDEGAMES_EXPORT QAction *gameNew(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Start a new game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(gameNew, New)
/**
* Load a previously saved game.
......@@ -99,14 +154,37 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction *load(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Load a previously saved game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(load, Load)
// FIXME why not to delete this and use just KStandardAction::openRecent???
// loadRecent seems to mimic its behaviour
/**
* Load a recently loaded game.
* The signature of slot is of the form slotURLSelected(const KUrl&)
* The signature of slot is of the form slotURLSelected(const QUrl&)
*/
KDEGAMES_EXPORT KRecentFilesAction *loadRecent(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Load a recently loaded game.
* @since 7.3
*/
#ifdef K_DOXYGEN
inline KRecentFilesAction *loadRecent(const QObject *recvr, Func slot, QObject *parent)
#else
template<class Receiver, class Func>
inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KRecentFilesAction>::type *loadRecent(const Receiver *recvr, Func slot, QObject *parent)
#endif
{
QAction* action = _k_createInternal(LoadRecent, parent);
KRecentFilesAction* recentAction = qobject_cast<KRecentFilesAction*>(action);
Q_ASSERT(recentAction);
QObject::connect(recentAction, &KRecentFilesAction::urlSelected, recvr, slot);
return recentAction;
}
/**
* Save the current game.
......@@ -114,42 +192,84 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction *save(const QObject *recvr, const char *slot,
QObject *parent);
/**
* Save the current game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(save, Save)
/**
* Save the current game under a different filename.
*/
KDEGAMES_EXPORT QAction *saveAs(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Save the current game under a different filename.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(saveAs, SaveAs)
/**
* Pause the game.
**/
KDEGAMES_EXPORT KToggleAction *pause(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Pause the game.
* @since 7.3
*/
KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(pause, Pause)
/**
* Show the highscores.
*/
KDEGAMES_EXPORT QAction *highscores(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Show the highscores.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(highscores, Highscores)
/**
* Clear highscores.
*/
KDEGAMES_EXPORT QAction *clearHighscores(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Clear highscores.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearHighscores, ClearHighscores)
/**
* Show the statistics.
*/
KDEGAMES_EXPORT QAction *statistics(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Show the statistics.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(statistics, Statistics)
/**
* Clear statistics.
*/
KDEGAMES_EXPORT QAction *clearStatistics(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Clear statistics.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(clearStatistics, ClearStatistics)
/**
* End the current game, but do not quit the program.
......@@ -158,6 +278,13 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction *end(const QObject *recvr, const char *slot,
QObject *parent );
/**
* End the current game, but do not quit the program.
* Think of a "close" entry.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(end, End)
/**
* Print current game.
* Not useful in all games.
......@@ -165,36 +292,73 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction *print(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Print current game.
* Not useful in all games.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(print, Print)
/**
* Quit the game.
*/
KDEGAMES_EXPORT QAction *quit(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Quit the game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(quit, Quit)
/**
* Repeat the last move.
**/
KDEGAMES_EXPORT QAction *repeat(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Repeat the last move.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(repeat, Repeat)
/**
* Undo the last move.
**/
KDEGAMES_EXPORT QAction *undo(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Undo the last move.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(undo, Undo)
/**
* Redo the last move (which has been undone).
**/
KDEGAMES_EXPORT QAction *redo(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Redo the last move (which has been undone).
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(redo, Redo)
/**
* Roll die or dice.
**/
KDEGAMES_EXPORT QAction *roll(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Roll die or dice.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(roll, Roll)
/**
* End the current turn (not the game).
* Usually to let the next player start.
......@@ -202,36 +366,73 @@ namespace KStandardGameAction
KDEGAMES_EXPORT QAction *endTurn(const QObject *recvr, const char *slot,
QObject *parent );
/**
* End the current turn (not the game).
* Usually to let the next player start.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(endTurn, EndTurn)
/**
* Display configure carddecks dialog.
*/
KDEGAMES_EXPORT QAction *carddecks(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Display configure carddecks dialog.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(carddecks, Carddecks)
/**
* Display configure highscores dialog.
*/
KDEGAMES_EXPORT QAction *configureHighscores(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Display configure highscores dialog.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(configureHighscores, ConfigureHighscores)
/**
* Give an advice/hint.
*/
KDEGAMES_EXPORT QAction *hint(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Give an advice/hint.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(hint, Hint)
/**
* Show a demo.
*/
KDEGAMES_EXPORT KToggleAction *demo(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Show a demo.
* @since 7.3
*/
KSTANDARDGAMETOGGLEACTION_WITH_NEW_STYLE_CONNECT(demo, Demo)
/**
* Solve the game.
*/
KDEGAMES_EXPORT QAction *solve(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Solve the game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(solve, Solve)
/**
* Choose game type.
* The signature of slot is of the form slotGameTypeChosen(int)
......@@ -239,11 +440,34 @@ namespace KStandardGameAction
KDEGAMES_EXPORT KSelectAction *chooseGameType(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Choose game type.
* @since 7.3
*/
#ifdef K_DOXYGEN
inline KSelectAction *chooseGameType(const QObject *recvr, Func slot, QObject *parent)
#else
template<class Receiver, class Func>
inline typename std::enable_if<!std::is_convertible<Func, const char*>::value, KSelectAction>::type *chooseGameType(const Receiver *recvr, Func slot, QObject *parent)
#endif
{
QAction* action = _k_createInternal(ChooseGameType, parent);
KSelectAction* chooseGameTypeAction = qobject_cast<KSelectAction*>(action);
Q_ASSERT(chooseGameTypeAction);
QObject::connect(chooseGameTypeAction, QOverload<int>::of(&KSelectAction::triggered), recvr, slot);
return chooseGameTypeAction;
}
/**
* Restart the game.
*/
KDEGAMES_EXPORT QAction *restart(const QObject *recvr, const char *slot,
QObject *parent );
/**
* Restart the game.
* @since 7.3
*/
KSTANDARDGAMEACTION_WITH_NEW_STYLE_CONNECT(restart, Restart)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment