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

KDEUI

ktoolbarhandler.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002 Simon Hausmann <hausmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "ktoolbarhandler.h"
00020 
00021 #include <QtXml/QDomDocument>
00022 
00023 #include <kaction.h>
00024 #include <kactioncollection.h>
00025 #include <kactionmenu.h>
00026 #include <kauthorized.h>
00027 #include <kguiitem.h>
00028 #include <klocale.h>
00029 #include <kxmlguiwindow.h>
00030 #include <kmenu.h>
00031 #include <ktoggletoolbaraction.h>
00032 #include <ktoolbar.h>
00033 #include <kxmlguifactory.h>
00034 #include <kstandardaction_p.h>
00035 
00036 namespace
00037 {
00038   const char *actionListName = "show_menu_and_toolbar_actionlist";
00039 
00040   const char *guiDescription = ""
00041     "<!DOCTYPE kpartgui><kpartgui name=\"StandardToolBarMenuHandler\">"
00042     "<MenuBar>"
00043     "    <Menu name=\"settings\">"
00044     "        <ActionList name=\"%1\" />"
00045     "    </Menu>"
00046     "</MenuBar>"
00047     "</kpartgui>";
00048 
00049   class BarActionBuilder
00050   {
00051     public:
00052       BarActionBuilder( KActionCollection *actionCollection, KXmlGuiWindow *mainWindow,
00053                         QLinkedList<KToolBar*> &oldToolBarList )
00054         : m_actionCollection( actionCollection ), m_mainWindow( mainWindow ), m_needsRebuild( false )
00055       {
00056         QList<KToolBar*> toolBars = qFindChildren<KToolBar*>( m_mainWindow );
00057 
00058         foreach( KToolBar * toolBar, toolBars) {
00059           if ( toolBar->mainWindow() != m_mainWindow )
00060             continue;
00061 
00062           if ( !oldToolBarList.contains( toolBar ) )
00063             m_needsRebuild = true;
00064 
00065           m_toolBars.append( toolBar );
00066         }
00067 
00068         if ( !m_needsRebuild )
00069           m_needsRebuild = ( oldToolBarList.count() != m_toolBars.count() );
00070       }
00071 
00072       bool needsRebuild() const
00073       {
00074         return m_needsRebuild;
00075       }
00076 
00077       QList<QAction*> create()
00078       {
00079         QList<QAction*> actions;
00080 
00081         if ( !m_needsRebuild )
00082           return actions;
00083 
00084         foreach ( KToolBar* bar, m_toolBars )
00085           handleToolBar( bar );
00086 
00087         if ( m_toolBarActions.count() == 0 )
00088           return actions;
00089 
00090         if ( m_toolBarActions.count() == 1 ) {
00091           const KStandardAction::KStandardActionInfo* pInfo = KStandardAction::infoPtr(KStandardAction::ShowToolbar);
00092           KToggleToolBarAction* action = static_cast<KToggleToolBarAction *>( m_toolBarActions.first() );
00093           action->setText( i18n( pInfo->psLabel ) );
00094           return m_toolBarActions;
00095         }
00096 
00097         KActionMenu *menuAction = new KActionMenu(i18n( "Toolbars" ), m_actionCollection);
00098         m_actionCollection->addAction("toolbars_submenu_action", menuAction);
00099 
00100         foreach ( QAction* action, m_toolBarActions )
00101           menuAction->menu()->addAction( action );
00102 
00103         actions.append( menuAction );
00104 
00105         return actions;
00106       }
00107 
00108       const QLinkedList<KToolBar*> &toolBars() const
00109       {
00110         return m_toolBars;
00111       }
00112 
00113     private:
00114       void handleToolBar( KToolBar *toolBar )
00115       {
00116         KToggleToolBarAction *action = new KToggleToolBarAction(
00117               toolBar,
00118               toolBar->windowTitle(),
00119               m_actionCollection);
00120         m_actionCollection->addAction(toolBar->objectName(), action);
00121 
00122         // ## tooltips, whatsthis?
00123         m_toolBarActions.append( action );
00124       }
00125 
00126       KActionCollection *m_actionCollection;
00127       KXmlGuiWindow *m_mainWindow;
00128 
00129       QLinkedList<KToolBar*> m_toolBars;
00130       QList<QAction*> m_toolBarActions;
00131 
00132       bool m_needsRebuild : 1;
00133   };
00134 }
00135 
00136 using namespace KDEPrivate;
00137 
00138 class ToolBarHandler::Private
00139 {
00140   public:
00141     Private( ToolBarHandler *_parent )
00142       : parent( _parent )
00143     {
00144     }
00145 
00146     void clientAdded( KXMLGUIClient *client )
00147     {
00148       if ( client == parent )
00149         parent->setupActions();
00150     }
00151 
00152     void init( KXmlGuiWindow *mainWindow );
00153     void connectToActionContainers();
00154     void connectToActionContainer( QAction *action );
00155     void connectToActionContainer( QWidget *container );
00156 
00157     ToolBarHandler *parent;
00158     QPointer<KXmlGuiWindow> mainWindow;
00159     QList<QAction*> actions;
00160     QLinkedList<KToolBar*> toolBars;
00161 };
00162 
00163 void ToolBarHandler::Private::init( KXmlGuiWindow *mw )
00164 {
00165   mainWindow = mw;
00166 
00167   QObject::connect( mainWindow->guiFactory(), SIGNAL( clientAdded( KXMLGUIClient * ) ),
00168                     parent, SLOT( clientAdded( KXMLGUIClient * ) ) );
00169 
00170   if ( parent->domDocument().documentElement().isNull() ) {
00171 
00172     QString completeDescription = QString::fromLatin1( guiDescription )
00173                                   .arg( actionListName );
00174 
00175     parent->setXML( completeDescription, false /*merge*/ );
00176   }
00177 }
00178 
00179 void ToolBarHandler::Private::connectToActionContainers()
00180 {
00181   foreach ( QAction* action, actions )
00182     connectToActionContainer( action );
00183 }
00184 
00185 void ToolBarHandler::Private::connectToActionContainer( QAction *action )
00186 {
00187   uint containerCount = action->associatedWidgets().count();
00188 
00189   for ( uint i = 0; i < containerCount; ++i )
00190     connectToActionContainer( action->associatedWidgets().value( i ) );
00191 }
00192 
00193 void ToolBarHandler::Private::connectToActionContainer( QWidget *container )
00194 {
00195   QMenu *popupMenu = qobject_cast<QMenu *>( container );
00196   if ( !popupMenu )
00197     return;
00198 
00199   connect( popupMenu, SIGNAL( aboutToShow() ),
00200            parent, SLOT( setupActions() ) );
00201 }
00202 
00203 ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow )
00204   : QObject( mainWindow ), KXMLGUIClient( mainWindow ),
00205     d( new Private( this ) )
00206 {
00207   d->init( mainWindow );
00208 }
00209 
00210 ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow, QObject *parent )
00211   : QObject( parent ), KXMLGUIClient( mainWindow ),
00212     d( new Private( this ) )
00213 {
00214   d->init( mainWindow );
00215 }
00216 
00217 ToolBarHandler::~ToolBarHandler()
00218 {
00219   qDeleteAll( d->actions );
00220   d->actions.clear();
00221 
00222   delete d;
00223 }
00224 
00225 QAction *ToolBarHandler::toolBarMenuAction()
00226 {
00227   Q_ASSERT( d->actions.count() == 1 );
00228   return d->actions.first();
00229 }
00230 
00231 void ToolBarHandler::setupActions()
00232 {
00233   if ( !factory() || !d->mainWindow )
00234       return;
00235 
00236   BarActionBuilder builder( actionCollection(), d->mainWindow, d->toolBars );
00237 
00238   if ( !builder.needsRebuild() )
00239       return;
00240 
00241   unplugActionList( actionListName );
00242 
00243   qDeleteAll( d->actions );
00244   d->actions.clear();
00245 
00246   d->actions = builder.create();
00247 
00248   d->toolBars = builder.toolBars();
00249 
00250   if ( KAuthorized::authorizeKAction( "options_show_toolbar" ) )
00251     plugActionList( actionListName, d->actions );
00252 
00253   d->connectToActionContainers();
00254 }
00255 
00256 #include "ktoolbarhandler.moc"

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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