KDEUI
ktoolbarhandler.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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 parent->setupActions();
00149 }
00150
00151 void init( KXmlGuiWindow *mainWindow );
00152 void connectToActionContainers();
00153 void connectToActionContainer( QAction *action );
00154 void connectToActionContainer( QWidget *container );
00155
00156 ToolBarHandler *parent;
00157 QPointer<KXmlGuiWindow> mainWindow;
00158 QList<QAction*> actions;
00159 QLinkedList<KToolBar*> toolBars;
00160 };
00161
00162 void ToolBarHandler::Private::init( KXmlGuiWindow *mw )
00163 {
00164 mainWindow = mw;
00165
00166 QObject::connect( mainWindow->guiFactory(), SIGNAL( clientAdded( KXMLGUIClient * ) ),
00167 parent, SLOT( clientAdded( KXMLGUIClient * ) ) );
00168
00169 if ( parent->domDocument().documentElement().isNull() ) {
00170
00171 QString completeDescription = QString::fromLatin1( guiDescription )
00172 .arg( actionListName );
00173
00174 parent->setXML( completeDescription, false );
00175 }
00176 }
00177
00178 void ToolBarHandler::Private::connectToActionContainers()
00179 {
00180 foreach ( QAction* action, actions )
00181 connectToActionContainer( action );
00182 }
00183
00184 void ToolBarHandler::Private::connectToActionContainer( QAction *action )
00185 {
00186 uint containerCount = action->associatedWidgets().count();
00187
00188 for ( uint i = 0; i < containerCount; ++i )
00189 connectToActionContainer( action->associatedWidgets().value( i ) );
00190 }
00191
00192 void ToolBarHandler::Private::connectToActionContainer( QWidget *container )
00193 {
00194 QMenu *popupMenu = qobject_cast<QMenu *>( container );
00195 if ( !popupMenu )
00196 return;
00197
00198 connect( popupMenu, SIGNAL( aboutToShow() ),
00199 parent, SLOT( setupActions() ) );
00200 }
00201
00202 ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow )
00203 : QObject( mainWindow ), KXMLGUIClient( mainWindow ),
00204 d( new Private( this ) )
00205 {
00206 d->init( mainWindow );
00207 }
00208
00209 ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow, QObject *parent )
00210 : QObject( parent ), KXMLGUIClient( mainWindow ),
00211 d( new Private( this ) )
00212 {
00213 d->init( mainWindow );
00214 }
00215
00216 ToolBarHandler::~ToolBarHandler()
00217 {
00218 qDeleteAll( d->actions );
00219 d->actions.clear();
00220
00221 delete d;
00222 }
00223
00224 QAction *ToolBarHandler::toolBarMenuAction()
00225 {
00226 Q_ASSERT( d->actions.count() == 1 );
00227 return d->actions.first();
00228 }
00229
00230 void ToolBarHandler::setupActions()
00231 {
00232 if ( !factory() || !d->mainWindow )
00233 return;
00234
00235 BarActionBuilder builder( actionCollection(), d->mainWindow, d->toolBars );
00236
00237 if ( !builder.needsRebuild() )
00238 return;
00239
00240 unplugActionList( actionListName );
00241
00242 qDeleteAll( d->actions );
00243 d->actions.clear();
00244
00245 d->actions = builder.create();
00246
00247 d->toolBars = builder.toolBars();
00248
00249 if ( KAuthorized::authorizeKAction( "options_show_toolbar" ) )
00250 plugActionList( actionListName, d->actions );
00251
00252 d->connectToActionContainers();
00253 }
00254
00255 #include "ktoolbarhandler.moc"