KDEUI
kstandardaction.cpp
Go to the documentation of this file.
00001 // vim: sw=2 et 00002 /* This file is part of the KDE libraries 00003 Copyright (C) 1999,2000 Kurt Granroth <granroth@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kstandardaction.h" 00021 #include "kstandardaction_p.h" 00022 #include "kstandardaction_p.moc" 00023 00024 #include <QtCore/QMutableStringListIterator> 00025 #include <QtGui/QToolButton> 00026 00027 #include <kaboutdata.h> 00028 #include <kaction.h> 00029 #include <QtGui/QApplication> 00030 #include <kcomponentdata.h> 00031 #include <kdebug.h> 00032 #include <kglobal.h> 00033 #include <kguiitem.h> 00034 #include <kiconloader.h> 00035 #include <klocale.h> 00036 #include <kstandardshortcut.h> 00037 #include <kmainwindow.h> 00038 #include <kicon.h> 00039 00040 #include "kdualaction.h" 00041 #include "krecentfilesaction.h" 00042 #include "ktogglefullscreenaction.h" 00043 #include "kpastetextaction.h" 00044 #include "kactioncollection.h" 00045 00046 namespace KStandardAction 00047 { 00048 AutomaticAction::AutomaticAction(const KIcon &icon, const QString &text, const KShortcut &shortcut, const char *slot, 00049 QObject *parent) 00050 : KAction(parent) 00051 { 00052 setText(text); 00053 setIcon(icon); 00054 setShortcut(shortcut); 00055 connect(this, SIGNAL(triggered()), this, slot); 00056 } 00057 00058 QStringList stdNames() 00059 { 00060 return internal_stdNames(); 00061 } 00062 00063 QList<StandardAction> actionIds() 00064 { 00065 QList<StandardAction> result; 00066 00067 for ( uint i = 0; g_rgActionInfo[i].id != ActionNone; i++ ) 00068 { 00069 result.append(g_rgActionInfo[i].id); 00070 } 00071 00072 return result; 00073 } 00074 00075 KDEUI_EXPORT KStandardShortcut::StandardShortcut shortcutForActionId(StandardAction id) 00076 { 00077 const KStandardActionInfo* pInfo = infoPtr( id ); 00078 return (pInfo) ? pInfo->idAccel : KStandardShortcut::AccelNone; 00079 } 00080 00081 00082 KAction *create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent) 00083 { 00084 KAction *pAction = 0; 00085 const KStandardActionInfo* pInfo = infoPtr(id); 00086 00087 // kDebug(125) << "KStandardAction::create( " << id << "=" << (pInfo ? pInfo->psName : (const char*)0) << ", " << parent << " )"; // ellis 00088 00089 if ( pInfo ) { 00090 QString sLabel, iconName = pInfo->psIconName; 00091 switch( id ) { 00092 case Back: 00093 sLabel = i18nc( "go back", "&Back"); 00094 if ( QApplication::isRightToLeft() ) 00095 iconName = "go-next"; 00096 break; 00097 00098 case Forward: 00099 sLabel = i18nc( "go forward", "&Forward" ); 00100 if ( QApplication::isRightToLeft() ) 00101 iconName = "go-previous"; 00102 break; 00103 00104 case Home: 00105 sLabel = i18nc( "home page", "&Home" ); 00106 break; 00107 case Help: 00108 sLabel = i18nc( "show help", "&Help" ); 00109 break; 00110 case Preferences: 00111 case AboutApp: 00112 case HelpContents: 00113 { 00114 const KAboutData *aboutData = KGlobal::mainComponent().aboutData(); 00115 /* TODO KDE4 00116 const KAboutData *aboutData; 00117 if ( parent ) 00118 aboutData = parent->componentData().aboutData(); 00119 else 00120 aboutData = KGlobal::aboutData(); 00121 */ 00122 QString appName = (aboutData) ? aboutData->programName() : qApp->applicationName(); 00123 sLabel = i18n( pInfo->psLabel, appName ); 00124 } 00125 break; 00126 default: 00127 sLabel = i18n( pInfo->psLabel ); 00128 } 00129 00130 if ( QApplication::isRightToLeft() ) { 00131 switch ( id ) { 00132 case Prior: iconName = "go-next-view-page"; break; 00133 case Next: iconName = "go-previous-view-page"; break; 00134 case FirstPage: iconName = "go-last-view-page"; break; 00135 case LastPage: iconName = "go-first-view-page"; break; 00136 case DocumentBack: iconName = "go-next"; break; 00137 case DocumentForward: iconName = "go-previous"; break; 00138 default: break; 00139 } 00140 } 00141 00142 QIcon icon = iconName.isEmpty() ? KIcon() : KIcon(iconName); 00143 00144 switch ( id ) { 00145 case OpenRecent: 00146 pAction = new KRecentFilesAction(parent); 00147 break; 00148 case ShowMenubar: 00149 case ShowToolbar: 00150 case ShowStatusbar: 00151 pAction = new KAction(parent); 00152 pAction->setCheckable(true); 00153 pAction->setChecked(true); 00154 break; 00155 case FullScreen: 00156 pAction = new KToggleFullScreenAction(parent); 00157 pAction->setCheckable(true); 00158 break; 00159 case PasteText: 00160 pAction = new KPasteTextAction(parent); 00161 break; 00162 // Same as default, but with the app icon 00163 case AboutApp: 00164 pAction = new KAction(parent); 00165 icon = qApp->windowIcon(); 00166 break; 00167 00168 default: 00169 pAction = new KAction(parent); 00170 break; 00171 } 00172 00173 switch ( id ) { 00174 case Quit: 00175 pAction->setMenuRole(QAction::QuitRole); 00176 break; 00177 00178 case Preferences: 00179 pAction->setMenuRole(QAction::PreferencesRole); 00180 break; 00181 00182 case AboutApp: 00183 pAction->setMenuRole(QAction::AboutRole); 00184 break; 00185 00186 default: 00187 pAction->setMenuRole(QAction::NoRole); 00188 break; 00189 } 00190 00191 pAction->setText(sLabel); 00192 pAction->setIcon(icon); 00193 00194 KShortcut cut = KStandardShortcut::shortcut(pInfo->idAccel); 00195 if (!cut.isEmpty()) 00196 pAction->setShortcut(cut); 00197 00198 pAction->setObjectName(pInfo->psName); 00199 } 00200 00201 if (recvr && slot) { 00202 if (id == OpenRecent) { 00203 // FIXME KAction port: probably a good idea to find a cleaner way to do this 00204 // Open Recent is a special case - provide the selected URL 00205 QObject::connect(pAction, SIGNAL(urlSelected(const KUrl &)), recvr, slot); 00206 } else if (id == ConfigureToolbars) { // #200815 00207 QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot, Qt::QueuedConnection); 00208 } else { 00209 QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot); 00210 } 00211 } 00212 00213 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00214 if (pAction && collection) 00215 collection->addAction(pAction->objectName(), pAction); 00216 00217 return pAction; 00218 } 00219 00220 const char* name( StandardAction id ) 00221 { 00222 const KStandardActionInfo* pInfo = infoPtr( id ); 00223 return (pInfo) ? pInfo->psName : 0; 00224 } 00225 00226 KAction *openNew(const QObject *recvr, const char *slot, QObject *parent) 00227 { 00228 return KStandardAction::create(New, recvr, slot, parent); 00229 } 00230 00231 KAction *open(const QObject *recvr, const char *slot, QObject *parent) 00232 { 00233 return KStandardAction::create(Open, recvr, slot, parent); 00234 } 00235 00236 KRecentFilesAction *openRecent(const QObject *recvr, const char *slot, QObject *parent) 00237 { 00238 return (KRecentFilesAction*) KStandardAction::create( OpenRecent, recvr, slot, parent ); 00239 } 00240 00241 KAction *save(const QObject *recvr, const char *slot, QObject *parent) 00242 { 00243 return KStandardAction::create(Save, recvr, slot, parent); 00244 } 00245 00246 KAction *saveAs(const QObject *recvr, const char *slot, QObject *parent) 00247 { 00248 return KStandardAction::create(SaveAs, recvr, slot, parent); 00249 } 00250 00251 KAction *revert(const QObject *recvr, const char *slot, QObject *parent) 00252 { 00253 return KStandardAction::create(Revert, recvr, slot, parent); 00254 } 00255 00256 KAction *print(const QObject *recvr, const char *slot, QObject *parent) 00257 { 00258 return KStandardAction::create(Print, recvr, slot, parent); 00259 } 00260 00261 KAction *printPreview( const QObject *recvr, const char *slot, QObject *parent ) 00262 { 00263 return KStandardAction::create( PrintPreview, recvr, slot, parent ); 00264 } 00265 00266 KAction *close( const QObject *recvr, const char *slot, QObject *parent ) 00267 { 00268 return KStandardAction::create( Close, recvr, slot, parent ); 00269 } 00270 00271 KAction *mail( const QObject *recvr, const char *slot, QObject *parent ) 00272 { 00273 return KStandardAction::create( Mail, recvr, slot, parent ); 00274 } 00275 00276 KAction *quit( const QObject *recvr, const char *slot, QObject *parent ) 00277 { 00278 return KStandardAction::create( Quit, recvr, slot, parent ); 00279 } 00280 00281 KAction *undo( const QObject *recvr, const char *slot, QObject *parent ) 00282 { 00283 return KStandardAction::create( Undo, recvr, slot, parent ); 00284 } 00285 00286 KAction *redo( const QObject *recvr, const char *slot, QObject *parent ) 00287 { 00288 return KStandardAction::create( Redo, recvr, slot, parent ); 00289 } 00290 00291 KAction *cut( const QObject *recvr, const char *slot, QObject *parent ) 00292 { 00293 return KStandardAction::create( Cut, recvr, slot, parent ); 00294 } 00295 00296 KAction *copy( const QObject *recvr, const char *slot, QObject *parent ) 00297 { 00298 return KStandardAction::create( Copy, recvr, slot, parent ); 00299 } 00300 00301 KAction *paste( const QObject *recvr, const char *slot, QObject *parent ) 00302 { 00303 return KStandardAction::create( Paste, recvr, slot, parent ); 00304 } 00305 00306 KAction *pasteText( const QObject *recvr, const char *slot, QObject *parent ) 00307 { 00308 return KStandardAction::create( PasteText, recvr, slot, parent ); 00309 } 00310 00311 KAction *clear( const QObject *recvr, const char *slot, QObject *parent ) 00312 { 00313 return KStandardAction::create( Clear, recvr, slot, parent ); 00314 } 00315 00316 KAction *selectAll( const QObject *recvr, const char *slot, QObject *parent ) 00317 { 00318 return KStandardAction::create( SelectAll, recvr, slot, parent ); 00319 } 00320 00321 KAction *deselect( const QObject *recvr, const char *slot, QObject *parent ) 00322 { 00323 return KStandardAction::create( Deselect, recvr, slot, parent ); 00324 } 00325 00326 KAction *find( const QObject *recvr, const char *slot, QObject *parent ) 00327 { 00328 return KStandardAction::create( Find, recvr, slot, parent ); 00329 } 00330 00331 KAction *findNext( const QObject *recvr, const char *slot, QObject *parent ) 00332 { 00333 return KStandardAction::create( FindNext, recvr, slot, parent ); 00334 } 00335 00336 KAction *findPrev( const QObject *recvr, const char *slot, QObject *parent ) 00337 { 00338 return KStandardAction::create( FindPrev, recvr, slot, parent ); 00339 } 00340 00341 KAction *replace( const QObject *recvr, const char *slot, QObject *parent ) 00342 { 00343 return KStandardAction::create( Replace, recvr, slot, parent ); 00344 } 00345 00346 KAction *actualSize( const QObject *recvr, const char *slot, QObject *parent ) 00347 { 00348 return KStandardAction::create( ActualSize, recvr, slot, parent ); 00349 } 00350 00351 KAction *fitToPage( const QObject *recvr, const char *slot, QObject *parent ) 00352 { 00353 return KStandardAction::create( FitToPage, recvr, slot, parent ); 00354 } 00355 00356 KAction *fitToWidth( const QObject *recvr, const char *slot, QObject *parent ) 00357 { 00358 return KStandardAction::create( FitToWidth, recvr, slot, parent ); 00359 } 00360 00361 KAction *fitToHeight( const QObject *recvr, const char *slot, QObject *parent ) 00362 { 00363 return KStandardAction::create( FitToHeight, recvr, slot, parent ); 00364 } 00365 00366 KAction *zoomIn( const QObject *recvr, const char *slot, QObject *parent ) 00367 { 00368 return KStandardAction::create( ZoomIn, recvr, slot, parent ); 00369 } 00370 00371 KAction *zoomOut( const QObject *recvr, const char *slot, QObject *parent ) 00372 { 00373 return KStandardAction::create( ZoomOut, recvr, slot, parent ); 00374 } 00375 00376 KAction *zoom( const QObject *recvr, const char *slot, QObject *parent ) 00377 { 00378 return KStandardAction::create( Zoom, recvr, slot, parent ); 00379 } 00380 00381 KAction *redisplay( const QObject *recvr, const char *slot, QObject *parent ) 00382 { 00383 return KStandardAction::create( Redisplay, recvr, slot, parent ); 00384 } 00385 00386 KAction *up( const QObject *recvr, const char *slot, QObject *parent ) 00387 { 00388 return KStandardAction::create( Up, recvr, slot, parent ); 00389 } 00390 00391 KAction *back( const QObject *recvr, const char *slot, QObject *parent ) 00392 { 00393 return KStandardAction::create( Back, recvr, slot, parent ); 00394 } 00395 00396 KAction *forward( const QObject *recvr, const char *slot, QObject *parent ) 00397 { 00398 return KStandardAction::create( Forward, recvr, slot, parent ); 00399 } 00400 00401 KAction *home( const QObject *recvr, const char *slot, QObject *parent ) 00402 { 00403 return KStandardAction::create( Home, recvr, slot, parent ); 00404 } 00405 00406 KAction *prior( const QObject *recvr, const char *slot, QObject *parent ) 00407 { 00408 return KStandardAction::create( Prior, recvr, slot, parent ); 00409 } 00410 00411 KAction *next( const QObject *recvr, const char *slot, QObject *parent ) 00412 { 00413 return KStandardAction::create( Next, recvr, slot, parent ); 00414 } 00415 00416 KAction *goTo( const QObject *recvr, const char *slot, QObject *parent ) 00417 { 00418 return KStandardAction::create( Goto, recvr, slot, parent ); 00419 } 00420 00421 KAction *gotoPage( const QObject *recvr, const char *slot, QObject *parent ) 00422 { 00423 return KStandardAction::create( GotoPage, recvr, slot, parent ); 00424 } 00425 00426 KAction *gotoLine( const QObject *recvr, const char *slot, QObject *parent ) 00427 { 00428 return KStandardAction::create( GotoLine, recvr, slot, parent ); 00429 } 00430 00431 KAction *firstPage( const QObject *recvr, const char *slot, QObject *parent ) 00432 { 00433 return KStandardAction::create( FirstPage, recvr, slot, parent ); 00434 } 00435 00436 KAction *lastPage( const QObject *recvr, const char *slot, QObject *parent ) 00437 { 00438 return KStandardAction::create( LastPage, recvr, slot, parent ); 00439 } 00440 00441 KAction *documentBack( const QObject *recvr, const char *slot, QObject *parent ) 00442 { 00443 return KStandardAction::create( DocumentBack, recvr, slot, parent ); 00444 } 00445 00446 KAction *documentForward( const QObject *recvr, const char *slot, QObject *parent ) 00447 { 00448 return KStandardAction::create( DocumentForward, recvr, slot, parent ); 00449 } 00450 00451 KAction *addBookmark( const QObject *recvr, const char *slot, QObject *parent ) 00452 { 00453 return KStandardAction::create( AddBookmark, recvr, slot, parent ); 00454 } 00455 00456 KAction *editBookmarks( const QObject *recvr, const char *slot, QObject *parent ) 00457 { 00458 return KStandardAction::create( EditBookmarks, recvr, slot, parent ); 00459 } 00460 00461 KAction *spelling( const QObject *recvr, const char *slot, QObject *parent ) 00462 { 00463 return KStandardAction::create( Spelling, recvr, slot, parent ); 00464 } 00465 00466 static KAction *buildAutomaticAction( QObject* parent, StandardAction id, const char* slot ) 00467 { 00468 const KStandardActionInfo* p = infoPtr( id ); 00469 if ( !p ) 00470 return 0; 00471 00472 AutomaticAction *action = new AutomaticAction( 00473 KIcon( p->psIconName ), 00474 i18n(p->psLabel), 00475 KStandardShortcut::shortcut( p->idAccel ), 00476 slot, 00477 parent); 00478 00479 action->setObjectName(p->psName); 00480 action->setWhatsThis( i18n(p->psWhatsThis) ); 00481 00482 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00483 if (collection) 00484 collection->addAction(action->objectName(), action); 00485 00486 return action; 00487 } 00488 00489 KAction *cut( QObject* parent ) 00490 { 00491 return buildAutomaticAction( parent, Cut, SLOT( cut() ) ); 00492 } 00493 00494 KAction *copy( QObject* parent ) 00495 { 00496 return buildAutomaticAction( parent, Copy, SLOT( copy() ) ); 00497 } 00498 00499 KAction *paste( QObject* parent ) 00500 { 00501 return buildAutomaticAction( parent, Paste, SLOT( paste() ) ); 00502 } 00503 00504 KAction *clear( QObject* parent ) 00505 { 00506 return buildAutomaticAction( parent, Clear, SLOT( clear() ) ); 00507 } 00508 00509 KAction *selectAll( QObject* parent ) 00510 { 00511 return buildAutomaticAction( parent, SelectAll, SLOT( selectAll() ) ); 00512 } 00513 00514 KToggleAction *showMenubar(const QObject *recvr, const char *slot, QObject *parent) 00515 { 00516 KToggleAction *ret = new KToggleAction(i18n( "Show &Menubar" ), parent); 00517 ret->setObjectName(name(ShowMenubar)); 00518 ret->setIcon( KIcon( "show-menu" ) ); 00519 00520 ret->setShortcut( KStandardShortcut::shortcut( KStandardShortcut::ShowMenubar ) ); 00521 00522 ret->setWhatsThis( i18n( "Show Menubar<p>" 00523 "Shows the menubar again after it has been hidden</p>" ) ); 00524 00525 ret->setChecked( true ); 00526 00527 if ( recvr && slot ) 00528 QObject::connect( ret, SIGNAL( triggered( bool ) ), recvr, slot ); 00529 00530 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00531 if (collection) 00532 collection->addAction(ret->objectName(), ret); 00533 00534 return ret; 00535 } 00536 00537 KToggleAction *showStatusbar(const QObject *recvr, const char *slot, QObject *parent) 00538 { 00539 KToggleAction *ret = new KToggleAction(i18n( "Show St&atusbar" ), parent); 00540 ret->setObjectName(name(ShowStatusbar)); 00541 00542 ret->setWhatsThis( i18n( "Show Statusbar<br /><br />" 00543 "Shows the statusbar, which is the bar at the bottom of the window used for status information." ) ); 00544 00545 ret->setChecked( true ); 00546 00547 if ( recvr && slot ) 00548 QObject::connect( ret, SIGNAL( triggered( bool ) ), recvr, slot ); 00549 00550 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00551 if (collection) 00552 collection->addAction(ret->objectName(), ret); 00553 00554 return ret; 00555 } 00556 00557 KToggleFullScreenAction *fullScreen(const QObject *recvr, const char *slot, QWidget* window, QObject *parent) 00558 { 00559 KToggleFullScreenAction *ret; 00560 ret = static_cast< KToggleFullScreenAction* >( KStandardAction::create( FullScreen, recvr, slot, parent ) ); 00561 ret->setWindow( window ); 00562 00563 return ret; 00564 } 00565 00566 KAction *saveOptions( const QObject *recvr, const char *slot, QObject *parent ) 00567 { 00568 return KStandardAction::create( SaveOptions, recvr, slot, parent ); 00569 } 00570 00571 KAction *keyBindings( const QObject *recvr, const char *slot, QObject *parent ) 00572 { 00573 return KStandardAction::create( KeyBindings, recvr, slot, parent ); 00574 } 00575 00576 KAction *preferences( const QObject *recvr, const char *slot, QObject *parent ) 00577 { 00578 return KStandardAction::create( Preferences, recvr, slot, parent ); 00579 } 00580 00581 KAction *configureToolbars( const QObject *recvr, const char *slot, QObject *parent ) 00582 { 00583 return KStandardAction::create( ConfigureToolbars, recvr, slot, parent ); 00584 } 00585 00586 KAction *configureNotifications( const QObject *recvr, const char *slot, QObject *parent ) 00587 { 00588 return KStandardAction::create( ConfigureNotifications, recvr, slot, parent ); 00589 } 00590 00591 KAction *help( const QObject *recvr, const char *slot, QObject *parent ) 00592 { 00593 return KStandardAction::create( Help, recvr, slot, parent ); 00594 } 00595 00596 KAction *helpContents( const QObject *recvr, const char *slot, QObject *parent ) 00597 { 00598 return KStandardAction::create( HelpContents, recvr, slot, parent ); 00599 } 00600 00601 KAction *whatsThis( const QObject *recvr, const char *slot, QObject *parent ) 00602 { 00603 return KStandardAction::create( WhatsThis, recvr, slot, parent ); 00604 } 00605 00606 KAction *tipOfDay( const QObject *recvr, const char *slot, QObject *parent ) 00607 { 00608 return KStandardAction::create( TipofDay, recvr, slot, parent ); 00609 } 00610 00611 KAction *reportBug( const QObject *recvr, const char *slot, QObject *parent ) 00612 { 00613 return KStandardAction::create( ReportBug, recvr, slot, parent ); 00614 } 00615 00616 KAction *switchApplicationLanguage( const QObject *recvr, const char *slot, QObject *parent ) 00617 { 00618 return KStandardAction::create( SwitchApplicationLanguage, recvr, slot, parent ); 00619 } 00620 00621 KAction *aboutApp( const QObject *recvr, const char *slot, QObject *parent ) 00622 { 00623 return KStandardAction::create( AboutApp, recvr, slot, parent ); 00624 } 00625 00626 KAction *aboutKDE( const QObject *recvr, const char *slot, QObject *parent ) 00627 { 00628 return KStandardAction::create( AboutKDE, recvr, slot, parent ); 00629 } 00630 00631 } 00632
KDE 4.6 API Reference