1 | /* |
2 | This file is part of the KDE games library |
3 | Copyright (C) 2001 Andreas Beckermann <b_mann@gmx.de> |
4 | Copyright (C) 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch> |
5 | |
6 | This library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Library General Public |
8 | License version 2 as published by the Free Software Foundation. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "kstandardgameaction.h" |
22 | |
23 | #include <klocale.h> |
24 | #include <kaction.h> |
25 | #include <kactioncollection.h> |
26 | #include <kstandardshortcut.h> |
27 | #include <ktoggleaction.h> |
28 | #include <kconfig.h> |
29 | #include <kdebug.h> |
30 | #include <kicon.h> |
31 | #include <krecentfilesaction.h> |
32 | |
33 | #undef I18N_NOOP2 |
34 | #define I18N_NOOP2(ctx,txt) ctx, txt |
35 | |
36 | struct KStandardGameActionInfo |
37 | { |
38 | KStandardGameAction::StandardGameAction id; |
39 | KStandardShortcut::StandardShortcut globalAccel; // if we reuse a global accel |
40 | int shortcut; // specific shortcut (NH: should be configurable) |
41 | const char* psName; |
42 | const char* psLabelContext; |
43 | const char* psLabel; |
44 | const char* psWhatsThis; |
45 | const char* psIconName; |
46 | const char* psToolTip; |
47 | }; |
48 | |
49 | const KStandardGameActionInfo g_rgActionInfo[] = { |
50 | // "game" menu |
51 | { 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" ) }, |
52 | { KStandardGameAction::Load, KStandardShortcut::Open, 0, "game_load" , 0, I18N_NOOP("&Load..." ), 0, "document-open" , I18N_NOOP("Open a saved game..." ) }, |
53 | { KStandardGameAction::LoadRecent, KStandardShortcut::AccelNone, 0, "game_load_recent" , 0, I18N_NOOP("Load &Recent" ), 0, 0, I18N_NOOP("Open a recently saved game..." ) }, |
54 | { KStandardGameAction::Restart, KStandardShortcut::Reload, 0, "game_restart" , 0, I18N_NOOP("Restart &Game" ), 0, "view-refresh" , I18N_NOOP("Restart the game" ) }, |
55 | { KStandardGameAction::Save, KStandardShortcut::Save, 0, "game_save" , 0, I18N_NOOP("&Save" ), 0, "document-save" , I18N_NOOP("Save the current game" ) }, |
56 | { KStandardGameAction::SaveAs, KStandardShortcut::AccelNone, 0, "game_save_as" , 0, I18N_NOOP("Save &As..." ), 0, "document-save-as" , I18N_NOOP("Save the current game to another file" ) }, |
57 | { KStandardGameAction::End, KStandardShortcut::End, 0, "game_end" , 0, I18N_NOOP("&End Game" ), 0, "window-close" , I18N_NOOP("End the current game" ) }, |
58 | { KStandardGameAction::Pause, KStandardShortcut::AccelNone, Qt::Key_P, "game_pause" , 0, I18N_NOOP("Pa&use" ), 0, "media-playback-pause" , I18N_NOOP("Pause the game" ) }, |
59 | { KStandardGameAction::Highscores, KStandardShortcut::AccelNone, Qt::CTRL+Qt::Key_H, "game_highscores" , 0, I18N_NOOP("Show &High Scores" ), 0, "games-highscores" , I18N_NOOP("Show high scores" ) }, |
60 | { KStandardGameAction::ClearHighscores, KStandardShortcut::AccelNone, 0, "game_clear_highscores" , 0, I18N_NOOP("&Clear High Scores" ), 0, "clear_highscore" , I18N_NOOP("Clear high scores" ) }, |
61 | { KStandardGameAction::Statistics, KStandardShortcut::AccelNone, 0, "game_statistics" , 0, I18N_NOOP("Show Statistics" ), 0, "highscore" , I18N_NOOP("Show statistics" ) }, |
62 | { KStandardGameAction::ClearStatistics, KStandardShortcut::AccelNone, 0, "game_clear_statistics" , 0, I18N_NOOP("&Clear Statistics" ), 0, "flag" , I18N_NOOP("Delete all-time statistics." ) }, |
63 | { KStandardGameAction::Print, KStandardShortcut::Print, 0, "game_print" , 0, I18N_NOOP("&Print..." ), 0, "document-print" , 0 }, |
64 | { KStandardGameAction::Quit, KStandardShortcut::Quit, 0, "game_quit" , 0, I18N_NOOP("&Quit" ), 0, "application-exit" , I18N_NOOP("Quit the program" ) }, |
65 | // "move" menu |
66 | { KStandardGameAction::Repeat, KStandardShortcut::AccelNone, 0, "move_repeat" , 0, I18N_NOOP("Repeat" ), 0, 0, I18N_NOOP("Repeat the last move" ) }, |
67 | { KStandardGameAction::Undo, KStandardShortcut::Undo, 0, "move_undo" , 0, I18N_NOOP("Und&o" ), 0, "edit-undo" , I18N_NOOP("Undo the last move" ) }, |
68 | { KStandardGameAction::Redo, KStandardShortcut::Redo, 0, "move_redo" , 0, I18N_NOOP("Re&do" ), 0, "edit-redo" , I18N_NOOP("Redo the latest move" ) }, |
69 | { KStandardGameAction::Roll, KStandardShortcut::AccelNone, Qt::CTRL+Qt::Key_R, "move_roll" , 0, I18N_NOOP("&Roll Dice" ), 0, "roll" , I18N_NOOP("Roll the dice" ) }, |
70 | { KStandardGameAction::EndTurn, KStandardShortcut::AccelNone, 0, "move_end_turn" , 0, I18N_NOOP("End Turn" ), 0, "games-endturn" , 0 }, |
71 | { KStandardGameAction::Hint, KStandardShortcut::AccelNone, Qt::Key_H, "move_hint" , 0, I18N_NOOP("&Hint" ), 0, "games-hint" , I18N_NOOP("Give a hint" ) }, |
72 | { KStandardGameAction::Demo, KStandardShortcut::AccelNone, Qt::Key_D, "move_demo" , 0, I18N_NOOP("&Demo" ), 0, "media-playback-start" , I18N_NOOP("Play a demo" ) }, |
73 | { KStandardGameAction::Solve, KStandardShortcut::AccelNone, 0, "move_solve" , 0, I18N_NOOP("&Solve" ), 0, "games-solve" , I18N_NOOP("Solve the game" ) }, |
74 | // "settings" menu |
75 | { KStandardGameAction::ChooseGameType, KStandardShortcut::AccelNone, 0, "options_choose_game_type" , 0, I18N_NOOP("Choose Game &Type" ), 0, 0, 0 }, |
76 | { KStandardGameAction::Carddecks, KStandardShortcut::AccelNone, 0, "options_configure_carddecks" , 0, I18N_NOOP("Configure &Carddecks..." ), 0, 0, 0 }, |
77 | { KStandardGameAction::ConfigureHighscores, KStandardShortcut::AccelNone, 0, "options_configure_highscores" , 0, I18N_NOOP("Configure &High Scores..." ), 0, 0, 0 }, |
78 | |
79 | { KStandardGameAction::ActionNone, KStandardShortcut::AccelNone, 0, 0, 0, 0, 0, 0, 0 } |
80 | }; |
81 | |
82 | static const KStandardGameActionInfo* infoPtr( KStandardGameAction::StandardGameAction id ) |
83 | { |
84 | for (uint i = 0; g_rgActionInfo[i].id!=KStandardGameAction::ActionNone; i++) { |
85 | if( g_rgActionInfo[i].id == id ) |
86 | return &g_rgActionInfo[i]; |
87 | } |
88 | return 0; |
89 | } |
90 | |
91 | |
92 | KAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot, |
93 | QObject* parent ) |
94 | { |
95 | KAction* pAction = 0; |
96 | const KStandardGameActionInfo* pInfo = infoPtr( id ); |
97 | kDebug(125) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)0) << "," << parent << " )" ; |
98 | if( pInfo ) { |
99 | QString sLabel = i18nc(pInfo->psLabelContext, pInfo->psLabel); |
100 | bool do_connect = (recvr && slot); //both not 0 |
101 | switch( id ) { |
102 | case LoadRecent: |
103 | pAction = new KRecentFilesAction(sLabel, parent); |
104 | if(do_connect) |
105 | QObject::connect( pAction, SIGNAL(urlSelected(KUrl)), recvr, slot); |
106 | break; |
107 | case Pause: |
108 | case Demo: |
109 | pAction = new KToggleAction(KIcon(QLatin1String( pInfo->psIconName )), sLabel, parent); |
110 | if(do_connect) |
111 | QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot); |
112 | break; |
113 | case ChooseGameType: |
114 | pAction = new KSelectAction( KIcon(QLatin1String( pInfo->psIconName )), sLabel, parent); |
115 | if(do_connect) |
116 | QObject::connect( pAction, SIGNAL(triggered(int)), recvr, slot ); |
117 | break; |
118 | default: |
119 | pAction = new KAction(KIcon(QLatin1String( pInfo->psIconName )), sLabel, parent); |
120 | if(do_connect) |
121 | QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot); |
122 | break; |
123 | } |
124 | |
125 | KShortcut cut = (pInfo->globalAccel==KStandardShortcut::AccelNone |
126 | ? KShortcut(pInfo->shortcut) |
127 | : KStandardShortcut::shortcut(pInfo->globalAccel)); |
128 | pAction->setShortcut(cut); |
129 | if (pInfo->psToolTip) |
130 | pAction->setToolTip(i18n(pInfo->psToolTip)); |
131 | if (pInfo->psWhatsThis) |
132 | pAction->setWhatsThis(i18n(pInfo->psWhatsThis)); |
133 | else if (pInfo->psToolTip) |
134 | pAction->setWhatsThis(i18n(pInfo->psToolTip)); |
135 | |
136 | pAction->setObjectName(QLatin1String( pInfo->psName )); |
137 | } |
138 | |
139 | KActionCollection *collection = qobject_cast<KActionCollection *>(parent); |
140 | if (collection && pAction) |
141 | collection->addAction(pAction->objectName(), pAction); |
142 | |
143 | return pAction; |
144 | } |
145 | |
146 | const char* KStandardGameAction::name( StandardGameAction id ) |
147 | { |
148 | const KStandardGameActionInfo* pInfo = infoPtr( id ); |
149 | return (pInfo) ? pInfo->psName : 0; |
150 | } |
151 | |
152 | KAction *KStandardGameAction::gameNew(const QObject *recvr, const char *slot, |
153 | QObject *parent) |
154 | { return KStandardGameAction::create(New, recvr, slot, parent); } |
155 | KAction *KStandardGameAction::load(const QObject *recvr, const char *slot, |
156 | QObject *parent) |
157 | { return KStandardGameAction::create(Load, recvr, slot, parent); } |
158 | KRecentFilesAction *KStandardGameAction::loadRecent(const QObject *recvr, const char *slot, |
159 | QObject *parent) |
160 | { return static_cast<KRecentFilesAction *>(KStandardGameAction::create(LoadRecent, recvr, slot, parent)); } |
161 | KAction *KStandardGameAction::save(const QObject *recvr, const char *slot, |
162 | QObject *parent) |
163 | { return KStandardGameAction::create(Save, recvr, slot, parent); } |
164 | KAction *KStandardGameAction::saveAs(const QObject *recvr, const char *slot, |
165 | QObject *parent) |
166 | { return KStandardGameAction::create(SaveAs, recvr, slot, parent); } |
167 | KAction *KStandardGameAction::end(const QObject *recvr, const char *slot, |
168 | QObject *parent) |
169 | { return KStandardGameAction::create(End, recvr, slot, parent); } |
170 | KToggleAction *KStandardGameAction::pause(const QObject *recvr, const char *slot, |
171 | QObject *parent) |
172 | { return static_cast<KToggleAction *>(KStandardGameAction::create(Pause, recvr, slot, parent)); } |
173 | KAction *KStandardGameAction::highscores(const QObject *recvr, const char *slot, |
174 | QObject *parent) |
175 | { return KStandardGameAction::create(Highscores, recvr, slot, parent); } |
176 | KAction *KStandardGameAction::statistics(const QObject *recvr, const char *slot, |
177 | QObject *parent) |
178 | { return KStandardGameAction::create(Highscores, recvr, slot, parent); } |
179 | KAction *KStandardGameAction::clearStatistics(const QObject *recvr, const char *slot, |
180 | QObject *parent) |
181 | { return KStandardGameAction::create(ClearStatistics, recvr, slot, parent); } |
182 | KAction *KStandardGameAction::print(const QObject *recvr, const char *slot, |
183 | QObject *parent) |
184 | { return KStandardGameAction::create(Print, recvr, slot, parent); } |
185 | KAction *KStandardGameAction::quit(const QObject *recvr, const char *slot, |
186 | QObject *parent) |
187 | { return KStandardGameAction::create(Quit, recvr, slot, parent); } |
188 | |
189 | KAction *KStandardGameAction::repeat(const QObject *recvr, const char *slot, |
190 | QObject *parent) |
191 | { return KStandardGameAction::create(Repeat, recvr, slot, parent); } |
192 | KAction *KStandardGameAction::undo(const QObject *recvr, const char *slot, |
193 | QObject *parent) |
194 | { return KStandardGameAction::create(Undo, recvr, slot, parent); } |
195 | |
196 | KAction *KStandardGameAction::redo(const QObject *recvr, const char *slot, |
197 | QObject *parent) |
198 | { return KStandardGameAction::create(Redo, recvr, slot, parent); } |
199 | |
200 | KAction *KStandardGameAction::roll(const QObject *recvr, const char *slot, |
201 | QObject *parent) |
202 | { return KStandardGameAction::create(Roll, recvr, slot, parent); } |
203 | KAction *KStandardGameAction::endTurn(const QObject *recvr, const char *slot, |
204 | QObject *parent) |
205 | { return KStandardGameAction::create(EndTurn, recvr, slot, parent); } |
206 | |
207 | KAction *KStandardGameAction::carddecks(const QObject *recvr, const char *slot, |
208 | QObject *parent) |
209 | { return KStandardGameAction::create(Carddecks, recvr, slot, parent); } |
210 | KAction *KStandardGameAction::configureHighscores(const QObject*recvr, const char *slot, |
211 | QObject *parent) |
212 | { return KStandardGameAction::create(ConfigureHighscores, recvr, slot, parent); } |
213 | KAction *KStandardGameAction::hint(const QObject*recvr, const char *slot, |
214 | QObject *parent) |
215 | { return KStandardGameAction::create(Hint, recvr, slot, parent); } |
216 | KToggleAction *KStandardGameAction::demo(const QObject*recvr, const char *slot, |
217 | QObject *parent) |
218 | { return static_cast<KToggleAction *>(KStandardGameAction::create(Demo, recvr, slot, parent)); } |
219 | KAction *KStandardGameAction::solve(const QObject*recvr, const char *slot, |
220 | QObject *parent) |
221 | { return KStandardGameAction::create(Solve, recvr, slot, parent); } |
222 | KSelectAction *KStandardGameAction::chooseGameType(const QObject*recvr, const char *slot, |
223 | QObject *parent) |
224 | { return static_cast<KSelectAction *>(KStandardGameAction::create(ChooseGameType, recvr, slot, parent)); } |
225 | KAction *KStandardGameAction::restart(const QObject*recvr, const char *slot, |
226 | QObject *parent) |
227 | { return KStandardGameAction::create(Restart, recvr, slot, parent); } |
228 | |