kstandardgameaction.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Andreas Beckermann <b_mann@gmx.de>
00004     Copyright (C) 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kstandardgameaction.h"
00022 
00023 #include <klocale.h>
00024 #include <kaction.h>
00025 #include <kactioncollection.h>
00026 #include <kstandardshortcut.h>
00027 #include <ktoggleaction.h>
00028 #include <kconfig.h>
00029 #include <kdebug.h>
00030 #include <kicon.h>
00031 #include <krecentfilesaction.h>
00032 
00033 
00034 struct KStandardGameActionInfo
00035 {
00036     KStandardGameAction::StandardGameAction id;
00037     KStandardShortcut::StandardShortcut globalAccel; // if we reuse a global accel
00038     int shortcut; // specific shortcut (NH: should be configurable)
00039     const char* psName;
00040     const char* psLabel;
00041     const char* psWhatsThis;
00042     const char* psIconName;
00043     const char* psToolTip;
00044 };
00045 
00046 const KStandardGameActionInfo g_rgActionInfo[] = {
00047 // "game" menu
00048     { 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") },
00049     { KStandardGameAction::Load, KStandardShortcut::Open, 0, "game_load", I18N_NOOP("&Load..."), 0, "document-open", I18N_NOOP("Open a saved game...") },
00050     { KStandardGameAction::LoadRecent, KStandardShortcut::AccelNone, 0, "game_load_recent", I18N_NOOP("Load &Recent"), 0, 0, I18N_NOOP("Open a recently saved game...") },
00051     { KStandardGameAction::Restart, KStandardShortcut::Reload, 0, "game_restart", I18N_NOOP("Restart &Game"), 0, "view-refresh", I18N_NOOP("Restart the game") },
00052     { KStandardGameAction::Save, KStandardShortcut::Save, 0, "game_save", I18N_NOOP("&Save"), 0, "document-save", I18N_NOOP("Save the current game") },
00053     { KStandardGameAction::SaveAs, KStandardShortcut::AccelNone, 0, "game_save_as", I18N_NOOP("Save &As..."), 0, "document-save-as", I18N_NOOP("Save the current game to another file") },
00054     { KStandardGameAction::End, KStandardShortcut::End, 0, "game_end", I18N_NOOP("&End Game"), 0, "window-close", I18N_NOOP("End the current game") },
00055     { KStandardGameAction::Pause, KStandardShortcut::AccelNone, Qt::Key_P, "game_pause", I18N_NOOP("Pa&use"), 0, "media-playback-pause", I18N_NOOP("Pause the game") },
00056     { KStandardGameAction::Highscores, KStandardShortcut::AccelNone, Qt::CTRL+Qt::Key_H, "game_highscores", I18N_NOOP("Show &High Scores"), 0, "games-highscores", I18N_NOOP("Show high scores") },
00057     { KStandardGameAction::ClearHighscores, KStandardShortcut::AccelNone, 0, "game_clear_highscores", I18N_NOOP("&Clear High Scores"), 0, "clear_highscore", I18N_NOOP("Clear high scores") },
00058     { KStandardGameAction::Statistics, KStandardShortcut::AccelNone, 0, "game_statistics", I18N_NOOP("Show Statistics"), 0, "highscore", I18N_NOOP("Show statistics") },
00059     { KStandardGameAction::ClearStatistics, KStandardShortcut::AccelNone, 0, "game_clear_statistics", I18N_NOOP("&Clear Statistics"), 0, "flag", I18N_NOOP("Delete all time statistics.") },
00060     { KStandardGameAction::Print, KStandardShortcut::Print, 0, "game_print", I18N_NOOP("&Print..."), 0, "document-print", 0 },
00061     { KStandardGameAction::Quit, KStandardShortcut::Quit, 0, "game_quit", I18N_NOOP("&Quit"), 0, "application-exit", I18N_NOOP("Quit the program") },
00062 // "move" menu
00063     { KStandardGameAction::Repeat, KStandardShortcut::AccelNone, 0, "move_repeat", I18N_NOOP("Repeat"), 0, 0, I18N_NOOP("Repeat the last move") },
00064     { KStandardGameAction::Undo, KStandardShortcut::Undo, 0, "move_undo", I18N_NOOP("Und&o"), 0, "edit-undo", I18N_NOOP("Undo the last move") },
00065     { KStandardGameAction::Redo, KStandardShortcut::Redo, 0, "move_redo", I18N_NOOP("Re&do"), 0, "edit-redo", I18N_NOOP("Redo the latest move") },
00066     { KStandardGameAction::Roll, KStandardShortcut::AccelNone, Qt::CTRL+Qt::Key_R, "move_roll", I18N_NOOP("&Roll Dice"), 0, "roll", I18N_NOOP("Roll the dice") },
00067     { KStandardGameAction::EndTurn, KStandardShortcut::AccelNone, 0, "move_end_turn", I18N_NOOP("End Turn"), 0, "games-endturn", 0  },
00068     { KStandardGameAction::Hint, KStandardShortcut::AccelNone, Qt::Key_H, "move_hint", I18N_NOOP("&Hint"), 0, "games-hint", I18N_NOOP("Give a hint") },
00069     { KStandardGameAction::Demo, KStandardShortcut::AccelNone, Qt::Key_D, "move_demo", I18N_NOOP("&Demo"), 0, "media-playback-start", I18N_NOOP("Play a demo") },
00070     { KStandardGameAction::Solve, KStandardShortcut::AccelNone, 0, "move_solve", I18N_NOOP("&Solve"), 0, "games-solve", I18N_NOOP("Solve the game") },
00071 // "settings" menu
00072     { KStandardGameAction::ChooseGameType, KStandardShortcut::AccelNone, 0, "options_choose_game_type", I18N_NOOP("Choose Game &Type"), 0, 0, 0 },
00073     { KStandardGameAction::Carddecks, KStandardShortcut::AccelNone, 0, "options_configure_carddecks", I18N_NOOP("Configure &Carddecks..."), 0, 0, 0  },
00074     { KStandardGameAction::ConfigureHighscores, KStandardShortcut::AccelNone, 0, "options_configure_highscores", I18N_NOOP("Configure &High Scores..."), 0, 0, 0  },
00075 
00076     { KStandardGameAction::ActionNone, KStandardShortcut::AccelNone, 0, 0, 0, 0, 0, 0  }
00077 };
00078 
00079 static const KStandardGameActionInfo* infoPtr( KStandardGameAction::StandardGameAction id )
00080 {
00081     for (uint i = 0; g_rgActionInfo[i].id!=KStandardGameAction::ActionNone; i++) {
00082       if( g_rgActionInfo[i].id == id )
00083         return &g_rgActionInfo[i];
00084     }
00085     return 0;
00086 }
00087 
00088 
00089 KAction* KStandardGameAction::create(StandardGameAction id, const QObject *recvr, const char *slot,
00090                                      QObject* parent )
00091 {
00092     KAction* pAction = 0;
00093     const KStandardGameActionInfo* pInfo = infoPtr( id );
00094     kDebug(125) << "KStandardGameAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)0) << "," << parent << " )";
00095     if( pInfo ) {
00096         QString sLabel = i18n(pInfo->psLabel);
00097         bool do_connect = (recvr && slot); //both not 0
00098         switch( id ) {
00099         case LoadRecent:
00100             pAction = new KRecentFilesAction(sLabel, parent);
00101             if(do_connect)
00102                 QObject::connect( pAction, SIGNAL(urlSelected(const KUrl&)), recvr, slot);
00103             break;
00104         case Pause:
00105         case Demo:
00106             pAction = new KToggleAction(KIcon(pInfo->psIconName), sLabel, parent);
00107             if(do_connect)
00108                 QObject::connect(pAction, SIGNAL(triggered(bool) ), recvr, slot);
00109             break;
00110         case ChooseGameType:
00111             pAction = new KSelectAction( KIcon(pInfo->psIconName), sLabel, parent);
00112             if(do_connect)
00113                 QObject::connect( pAction, SIGNAL( triggered(int) ), recvr, slot );
00114             break;
00115         default:
00116             pAction = new KAction(KIcon(pInfo->psIconName), sLabel, parent);
00117             if(do_connect)
00118                 QObject::connect(pAction, SIGNAL(triggered(bool) ), recvr, slot);
00119             break;
00120         }
00121 
00122         KShortcut cut = (pInfo->globalAccel==KStandardShortcut::AccelNone
00123                          ? KShortcut(pInfo->shortcut)
00124                          : KStandardShortcut::shortcut(pInfo->globalAccel));
00125         pAction->setShortcut(cut);
00126         if (pInfo->psToolTip)
00127                 pAction->setToolTip(i18n(pInfo->psToolTip));
00128         if (pInfo->psWhatsThis)
00129                 pAction->setWhatsThis(i18n(pInfo->psWhatsThis));
00130         else if (pInfo->psToolTip)
00131                 pAction->setWhatsThis(i18n(pInfo->psToolTip));
00132 
00133         pAction->setObjectName(pInfo->psName);
00134     }
00135 
00136     KActionCollection *collection = qobject_cast<KActionCollection *>(parent);
00137     if (collection && pAction)
00138         collection->addAction(pAction->objectName(), pAction);
00139 
00140     return pAction;
00141 }
00142 
00143 const char* KStandardGameAction::name( StandardGameAction id )
00144 {
00145     const KStandardGameActionInfo* pInfo = infoPtr( id );
00146     return (pInfo) ? pInfo->psName : 0;
00147 }
00148 
00149 KAction *KStandardGameAction::gameNew(const QObject *recvr, const char *slot,
00150                                       QObject *parent)
00151 { return KStandardGameAction::create(New, recvr, slot, parent); }
00152 KAction *KStandardGameAction::load(const QObject *recvr, const char *slot,
00153                                    QObject *parent)
00154 { return KStandardGameAction::create(Load, recvr, slot, parent); }
00155 KRecentFilesAction *KStandardGameAction::loadRecent(const QObject *recvr, const char *slot,
00156                                                     QObject *parent)
00157 { return static_cast<KRecentFilesAction *>(KStandardGameAction::create(LoadRecent, recvr, slot, parent)); }
00158 KAction *KStandardGameAction::save(const QObject *recvr, const char *slot,
00159                                    QObject *parent)
00160 { return KStandardGameAction::create(Save, recvr, slot, parent); }
00161 KAction *KStandardGameAction::saveAs(const QObject *recvr, const char *slot,
00162                                      QObject *parent)
00163 { return KStandardGameAction::create(SaveAs, recvr, slot, parent); }
00164 KAction *KStandardGameAction::end(const QObject *recvr, const char *slot,
00165                                   QObject *parent)
00166 { return KStandardGameAction::create(End, recvr, slot, parent); }
00167 KToggleAction *KStandardGameAction::pause(const QObject *recvr, const char *slot,
00168                                           QObject *parent)
00169 { return static_cast<KToggleAction *>(KStandardGameAction::create(Pause, recvr, slot, parent)); }
00170 KAction *KStandardGameAction::highscores(const QObject *recvr, const char *slot,
00171                                          QObject *parent)
00172 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
00173 KAction *KStandardGameAction::statistics(const QObject *recvr, const char *slot,
00174                                          QObject *parent)
00175 { return KStandardGameAction::create(Highscores, recvr, slot, parent); }
00176 KAction *KStandardGameAction::clearStatistics(const QObject *recvr, const char *slot,
00177                                          QObject *parent)
00178 { return KStandardGameAction::create(ClearStatistics, recvr, slot, parent); }
00179 KAction *KStandardGameAction::print(const QObject *recvr, const char *slot,
00180                                     QObject *parent)
00181 { return KStandardGameAction::create(Print, recvr, slot, parent); }
00182 KAction *KStandardGameAction::quit(const QObject *recvr, const char *slot,
00183                                    QObject *parent)
00184 { return KStandardGameAction::create(Quit, recvr, slot, parent); }
00185 
00186 KAction *KStandardGameAction::repeat(const QObject *recvr, const char *slot,
00187                                      QObject *parent)
00188 { return KStandardGameAction::create(Repeat, recvr, slot, parent); }
00189 KAction *KStandardGameAction::undo(const QObject *recvr, const char *slot,
00190                                    QObject *parent)
00191 { return KStandardGameAction::create(Undo, recvr, slot, parent); }
00192 
00193 KAction *KStandardGameAction::redo(const QObject *recvr, const char *slot,
00194                                    QObject *parent)
00195 { return KStandardGameAction::create(Redo, recvr, slot, parent); }
00196 
00197 KAction *KStandardGameAction::roll(const QObject *recvr, const char *slot,
00198                                    QObject *parent)
00199 { return KStandardGameAction::create(Roll, recvr, slot, parent); }
00200 KAction *KStandardGameAction::endTurn(const QObject *recvr, const char *slot,
00201                                       QObject *parent)
00202 { return KStandardGameAction::create(EndTurn, recvr, slot, parent); }
00203 
00204 KAction *KStandardGameAction::carddecks(const QObject *recvr, const char *slot,
00205                                         QObject *parent)
00206 { return KStandardGameAction::create(Carddecks, recvr, slot, parent); }
00207 KAction *KStandardGameAction::configureHighscores(const QObject*recvr, const char *slot,
00208                                                   QObject *parent)
00209 { return KStandardGameAction::create(ConfigureHighscores, recvr, slot, parent); }
00210 KAction *KStandardGameAction::hint(const QObject*recvr, const char *slot,
00211                                    QObject *parent)
00212 { return KStandardGameAction::create(Hint, recvr, slot, parent); }
00213 KToggleAction *KStandardGameAction::demo(const QObject*recvr, const char *slot,
00214                                          QObject *parent)
00215 { return static_cast<KToggleAction *>(KStandardGameAction::create(Demo, recvr, slot, parent)); }
00216 KAction *KStandardGameAction::solve(const QObject*recvr, const char *slot,
00217                                     QObject *parent)
00218 { return KStandardGameAction::create(Solve, recvr, slot, parent); }
00219 KSelectAction *KStandardGameAction::chooseGameType(const QObject*recvr, const char *slot,
00220                                                    QObject *parent)
00221 { return static_cast<KSelectAction *>(KStandardGameAction::create(ChooseGameType, recvr, slot, parent)); }
00222 KAction *KStandardGameAction::restart(const QObject*recvr, const char *slot,
00223                                       QObject *parent)
00224 { return KStandardGameAction::create(Restart, recvr, slot, parent); }

Generated on Sun Mar 16 08:02:53 2008 for Libkdegames by  doxygen 1.5.3