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 if (pInfo->psToolTip) { 00193 pAction->setToolTip(i18n(pInfo->psToolTip)); 00194 } 00195 pAction->setIcon(icon); 00196 00197 KShortcut cut = KStandardShortcut::shortcut(pInfo->idAccel); 00198 if (!cut.isEmpty()) 00199 pAction->setShortcut(cut); 00200 00201 pAction->setObjectName(pInfo->psName); 00202 } 00203 00204 if (recvr && slot) { 00205 if (id == OpenRecent) { 00206 // FIXME KAction port: probably a good idea to find a cleaner way to do this 00207 // Open Recent is a special case - provide the selected URL 00208 QObject::connect(pAction, SIGNAL(urlSelected(KUrl)), recvr, slot); 00209 } else if (id == ConfigureToolbars) { // #200815 00210 QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot, Qt::QueuedConnection); 00211 } else { 00212 QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot); 00213 } 00214 } 00215 00216 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00217 if (pAction && collection) 00218 collection->addAction(pAction->objectName(), pAction); 00219 00220 return pAction; 00221 } 00222 00223 const char* name( StandardAction id ) 00224 { 00225 const KStandardActionInfo* pInfo = infoPtr( id ); 00226 return (pInfo) ? pInfo->psName : 0; 00227 } 00228 00229 KAction *openNew(const QObject *recvr, const char *slot, QObject *parent) 00230 { 00231 return KStandardAction::create(New, recvr, slot, parent); 00232 } 00233 00234 KAction *open(const QObject *recvr, const char *slot, QObject *parent) 00235 { 00236 return KStandardAction::create(Open, recvr, slot, parent); 00237 } 00238 00239 KRecentFilesAction *openRecent(const QObject *recvr, const char *slot, QObject *parent) 00240 { 00241 return (KRecentFilesAction*) KStandardAction::create( OpenRecent, recvr, slot, parent ); 00242 } 00243 00244 KAction *save(const QObject *recvr, const char *slot, QObject *parent) 00245 { 00246 return KStandardAction::create(Save, recvr, slot, parent); 00247 } 00248 00249 KAction *saveAs(const QObject *recvr, const char *slot, QObject *parent) 00250 { 00251 return KStandardAction::create(SaveAs, recvr, slot, parent); 00252 } 00253 00254 KAction *revert(const QObject *recvr, const char *slot, QObject *parent) 00255 { 00256 return KStandardAction::create(Revert, recvr, slot, parent); 00257 } 00258 00259 KAction *print(const QObject *recvr, const char *slot, QObject *parent) 00260 { 00261 return KStandardAction::create(Print, recvr, slot, parent); 00262 } 00263 00264 KAction *printPreview( const QObject *recvr, const char *slot, QObject *parent ) 00265 { 00266 return KStandardAction::create( PrintPreview, recvr, slot, parent ); 00267 } 00268 00269 KAction *close( const QObject *recvr, const char *slot, QObject *parent ) 00270 { 00271 return KStandardAction::create( Close, recvr, slot, parent ); 00272 } 00273 00274 KAction *mail( const QObject *recvr, const char *slot, QObject *parent ) 00275 { 00276 return KStandardAction::create( Mail, recvr, slot, parent ); 00277 } 00278 00279 KAction *quit( const QObject *recvr, const char *slot, QObject *parent ) 00280 { 00281 return KStandardAction::create( Quit, recvr, slot, parent ); 00282 } 00283 00284 KAction *undo( const QObject *recvr, const char *slot, QObject *parent ) 00285 { 00286 return KStandardAction::create( Undo, recvr, slot, parent ); 00287 } 00288 00289 KAction *redo( const QObject *recvr, const char *slot, QObject *parent ) 00290 { 00291 return KStandardAction::create( Redo, recvr, slot, parent ); 00292 } 00293 00294 KAction *cut( const QObject *recvr, const char *slot, QObject *parent ) 00295 { 00296 return KStandardAction::create( Cut, recvr, slot, parent ); 00297 } 00298 00299 KAction *copy( const QObject *recvr, const char *slot, QObject *parent ) 00300 { 00301 return KStandardAction::create( Copy, recvr, slot, parent ); 00302 } 00303 00304 KAction *paste( const QObject *recvr, const char *slot, QObject *parent ) 00305 { 00306 return KStandardAction::create( Paste, recvr, slot, parent ); 00307 } 00308 00309 KAction *pasteText( const QObject *recvr, const char *slot, QObject *parent ) 00310 { 00311 return KStandardAction::create( PasteText, recvr, slot, parent ); 00312 } 00313 00314 KAction *clear( const QObject *recvr, const char *slot, QObject *parent ) 00315 { 00316 return KStandardAction::create( Clear, recvr, slot, parent ); 00317 } 00318 00319 KAction *selectAll( const QObject *recvr, const char *slot, QObject *parent ) 00320 { 00321 return KStandardAction::create( SelectAll, recvr, slot, parent ); 00322 } 00323 00324 KAction *deselect( const QObject *recvr, const char *slot, QObject *parent ) 00325 { 00326 return KStandardAction::create( Deselect, recvr, slot, parent ); 00327 } 00328 00329 KAction *find( const QObject *recvr, const char *slot, QObject *parent ) 00330 { 00331 return KStandardAction::create( Find, recvr, slot, parent ); 00332 } 00333 00334 KAction *findNext( const QObject *recvr, const char *slot, QObject *parent ) 00335 { 00336 return KStandardAction::create( FindNext, recvr, slot, parent ); 00337 } 00338 00339 KAction *findPrev( const QObject *recvr, const char *slot, QObject *parent ) 00340 { 00341 return KStandardAction::create( FindPrev, recvr, slot, parent ); 00342 } 00343 00344 KAction *replace( const QObject *recvr, const char *slot, QObject *parent ) 00345 { 00346 return KStandardAction::create( Replace, recvr, slot, parent ); 00347 } 00348 00349 KAction *actualSize( const QObject *recvr, const char *slot, QObject *parent ) 00350 { 00351 return KStandardAction::create( ActualSize, recvr, slot, parent ); 00352 } 00353 00354 KAction *fitToPage( const QObject *recvr, const char *slot, QObject *parent ) 00355 { 00356 return KStandardAction::create( FitToPage, recvr, slot, parent ); 00357 } 00358 00359 KAction *fitToWidth( const QObject *recvr, const char *slot, QObject *parent ) 00360 { 00361 return KStandardAction::create( FitToWidth, recvr, slot, parent ); 00362 } 00363 00364 KAction *fitToHeight( const QObject *recvr, const char *slot, QObject *parent ) 00365 { 00366 return KStandardAction::create( FitToHeight, recvr, slot, parent ); 00367 } 00368 00369 KAction *zoomIn( const QObject *recvr, const char *slot, QObject *parent ) 00370 { 00371 return KStandardAction::create( ZoomIn, recvr, slot, parent ); 00372 } 00373 00374 KAction *zoomOut( const QObject *recvr, const char *slot, QObject *parent ) 00375 { 00376 return KStandardAction::create( ZoomOut, recvr, slot, parent ); 00377 } 00378 00379 KAction *zoom( const QObject *recvr, const char *slot, QObject *parent ) 00380 { 00381 return KStandardAction::create( Zoom, recvr, slot, parent ); 00382 } 00383 00384 KAction *redisplay( const QObject *recvr, const char *slot, QObject *parent ) 00385 { 00386 return KStandardAction::create( Redisplay, recvr, slot, parent ); 00387 } 00388 00389 KAction *up( const QObject *recvr, const char *slot, QObject *parent ) 00390 { 00391 return KStandardAction::create( Up, recvr, slot, parent ); 00392 } 00393 00394 KAction *back( const QObject *recvr, const char *slot, QObject *parent ) 00395 { 00396 return KStandardAction::create( Back, recvr, slot, parent ); 00397 } 00398 00399 KAction *forward( const QObject *recvr, const char *slot, QObject *parent ) 00400 { 00401 return KStandardAction::create( Forward, recvr, slot, parent ); 00402 } 00403 00404 KAction *home( const QObject *recvr, const char *slot, QObject *parent ) 00405 { 00406 return KStandardAction::create( Home, recvr, slot, parent ); 00407 } 00408 00409 KAction *prior( const QObject *recvr, const char *slot, QObject *parent ) 00410 { 00411 return KStandardAction::create( Prior, recvr, slot, parent ); 00412 } 00413 00414 KAction *next( const QObject *recvr, const char *slot, QObject *parent ) 00415 { 00416 return KStandardAction::create( Next, recvr, slot, parent ); 00417 } 00418 00419 KAction *goTo( const QObject *recvr, const char *slot, QObject *parent ) 00420 { 00421 return KStandardAction::create( Goto, recvr, slot, parent ); 00422 } 00423 00424 KAction *gotoPage( const QObject *recvr, const char *slot, QObject *parent ) 00425 { 00426 return KStandardAction::create( GotoPage, recvr, slot, parent ); 00427 } 00428 00429 KAction *gotoLine( const QObject *recvr, const char *slot, QObject *parent ) 00430 { 00431 return KStandardAction::create( GotoLine, recvr, slot, parent ); 00432 } 00433 00434 KAction *firstPage( const QObject *recvr, const char *slot, QObject *parent ) 00435 { 00436 return KStandardAction::create( FirstPage, recvr, slot, parent ); 00437 } 00438 00439 KAction *lastPage( const QObject *recvr, const char *slot, QObject *parent ) 00440 { 00441 return KStandardAction::create( LastPage, recvr, slot, parent ); 00442 } 00443 00444 KAction *documentBack( const QObject *recvr, const char *slot, QObject *parent ) 00445 { 00446 return KStandardAction::create( DocumentBack, recvr, slot, parent ); 00447 } 00448 00449 KAction *documentForward( const QObject *recvr, const char *slot, QObject *parent ) 00450 { 00451 return KStandardAction::create( DocumentForward, recvr, slot, parent ); 00452 } 00453 00454 KAction *addBookmark( const QObject *recvr, const char *slot, QObject *parent ) 00455 { 00456 return KStandardAction::create( AddBookmark, recvr, slot, parent ); 00457 } 00458 00459 KAction *editBookmarks( const QObject *recvr, const char *slot, QObject *parent ) 00460 { 00461 return KStandardAction::create( EditBookmarks, recvr, slot, parent ); 00462 } 00463 00464 KAction *spelling( const QObject *recvr, const char *slot, QObject *parent ) 00465 { 00466 return KStandardAction::create( Spelling, recvr, slot, parent ); 00467 } 00468 00469 static KAction *buildAutomaticAction( QObject* parent, StandardAction id, const char* slot ) 00470 { 00471 const KStandardActionInfo* p = infoPtr( id ); 00472 if ( !p ) 00473 return 0; 00474 00475 AutomaticAction *action = new AutomaticAction( 00476 KIcon( p->psIconName ), 00477 i18n(p->psLabel), 00478 KStandardShortcut::shortcut( p->idAccel ), 00479 slot, 00480 parent); 00481 00482 action->setObjectName(p->psName); 00483 if (p->psToolTip) { 00484 action->setToolTip(i18n(p->psToolTip)); 00485 } 00486 00487 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00488 if (collection) 00489 collection->addAction(action->objectName(), action); 00490 00491 return action; 00492 } 00493 00494 KAction *cut( QObject* parent ) 00495 { 00496 return buildAutomaticAction( parent, Cut, SLOT(cut()) ); 00497 } 00498 00499 KAction *copy( QObject* parent ) 00500 { 00501 return buildAutomaticAction( parent, Copy, SLOT(copy()) ); 00502 } 00503 00504 KAction *paste( QObject* parent ) 00505 { 00506 return buildAutomaticAction( parent, Paste, SLOT(paste()) ); 00507 } 00508 00509 KAction *clear( QObject* parent ) 00510 { 00511 return buildAutomaticAction( parent, Clear, SLOT(clear()) ); 00512 } 00513 00514 KAction *selectAll( QObject* parent ) 00515 { 00516 return buildAutomaticAction( parent, SelectAll, SLOT(selectAll()) ); 00517 } 00518 00519 KToggleAction *showMenubar(const QObject *recvr, const char *slot, QObject *parent) 00520 { 00521 KToggleAction *ret = new KToggleAction(i18n( "Show &Menubar" ), parent); 00522 ret->setObjectName(name(ShowMenubar)); 00523 ret->setIcon( KIcon( "show-menu" ) ); 00524 00525 ret->setShortcut( KStandardShortcut::shortcut( KStandardShortcut::ShowMenubar ) ); 00526 00527 ret->setWhatsThis( i18n( "Show Menubar<p>" 00528 "Shows the menubar again after it has been hidden</p>" ) ); 00529 00530 ret->setChecked( true ); 00531 00532 if ( recvr && slot ) 00533 QObject::connect( ret, SIGNAL(triggered(bool)), recvr, slot ); 00534 00535 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00536 if (collection) 00537 collection->addAction(ret->objectName(), ret); 00538 00539 return ret; 00540 } 00541 00542 KToggleAction *showStatusbar(const QObject *recvr, const char *slot, QObject *parent) 00543 { 00544 KToggleAction *ret = new KToggleAction(i18n( "Show St&atusbar" ), parent); 00545 ret->setObjectName(name(ShowStatusbar)); 00546 00547 ret->setWhatsThis( i18n( "Show Statusbar<br /><br />" 00548 "Shows the statusbar, which is the bar at the bottom of the window used for status information." ) ); 00549 00550 ret->setChecked( true ); 00551 00552 if ( recvr && slot ) 00553 QObject::connect( ret, SIGNAL(triggered(bool)), recvr, slot ); 00554 00555 KActionCollection *collection = qobject_cast<KActionCollection *>(parent); 00556 if (collection) 00557 collection->addAction(ret->objectName(), ret); 00558 00559 return ret; 00560 } 00561 00562 KToggleFullScreenAction *fullScreen(const QObject *recvr, const char *slot, QWidget* window, QObject *parent) 00563 { 00564 KToggleFullScreenAction *ret; 00565 ret = static_cast< KToggleFullScreenAction* >( KStandardAction::create( FullScreen, recvr, slot, parent ) ); 00566 ret->setWindow( window ); 00567 00568 return ret; 00569 } 00570 00571 KAction *saveOptions( const QObject *recvr, const char *slot, QObject *parent ) 00572 { 00573 return KStandardAction::create( SaveOptions, recvr, slot, parent ); 00574 } 00575 00576 KAction *keyBindings( const QObject *recvr, const char *slot, QObject *parent ) 00577 { 00578 return KStandardAction::create( KeyBindings, recvr, slot, parent ); 00579 } 00580 00581 KAction *preferences( const QObject *recvr, const char *slot, QObject *parent ) 00582 { 00583 return KStandardAction::create( Preferences, recvr, slot, parent ); 00584 } 00585 00586 KAction *configureToolbars( const QObject *recvr, const char *slot, QObject *parent ) 00587 { 00588 return KStandardAction::create( ConfigureToolbars, recvr, slot, parent ); 00589 } 00590 00591 KAction *configureNotifications( const QObject *recvr, const char *slot, QObject *parent ) 00592 { 00593 return KStandardAction::create( ConfigureNotifications, recvr, slot, parent ); 00594 } 00595 00596 KAction *help( const QObject *recvr, const char *slot, QObject *parent ) 00597 { 00598 return KStandardAction::create( Help, recvr, slot, parent ); 00599 } 00600 00601 KAction *helpContents( const QObject *recvr, const char *slot, QObject *parent ) 00602 { 00603 return KStandardAction::create( HelpContents, recvr, slot, parent ); 00604 } 00605 00606 KAction *whatsThis( const QObject *recvr, const char *slot, QObject *parent ) 00607 { 00608 return KStandardAction::create( WhatsThis, recvr, slot, parent ); 00609 } 00610 00611 KAction *tipOfDay( const QObject *recvr, const char *slot, QObject *parent ) 00612 { 00613 return KStandardAction::create( TipofDay, recvr, slot, parent ); 00614 } 00615 00616 KAction *reportBug( const QObject *recvr, const char *slot, QObject *parent ) 00617 { 00618 return KStandardAction::create( ReportBug, recvr, slot, parent ); 00619 } 00620 00621 KAction *switchApplicationLanguage( const QObject *recvr, const char *slot, QObject *parent ) 00622 { 00623 return KStandardAction::create( SwitchApplicationLanguage, recvr, slot, parent ); 00624 } 00625 00626 KAction *aboutApp( const QObject *recvr, const char *slot, QObject *parent ) 00627 { 00628 return KStandardAction::create( AboutApp, recvr, slot, parent ); 00629 } 00630 00631 KAction *aboutKDE( const QObject *recvr, const char *slot, QObject *parent ) 00632 { 00633 return KStandardAction::create( AboutKDE, recvr, slot, parent ); 00634 } 00635 00636 } 00637
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:32:43 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:32:43 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.