• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal