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 <qpopupmenu.h>
00022 #include <kapplication.h>
00023 #include <ktoolbar.h>
00024 #include <kmainwindow.h>
00025 #include <klocale.h>
00026 #include <kaction.h>
00027 #include <assert.h>
00028
00029 namespace
00030 {
00031 const char *actionListName = "show_menu_and_toolbar_actionlist";
00032
00033 const char *guiDescription = ""
00034 "<!DOCTYPE kpartgui><kpartgui name=\"StandardToolBarMenuHandler\">"
00035 "<MenuBar>"
00036 " <Menu name=\"settings\">"
00037 " <ActionList name=\"%1\" />"
00038 " </Menu>"
00039 "</MenuBar>"
00040 "</kpartgui>";
00041
00042 const char *resourceFileName = "barhandler.rc";
00043
00044 class BarActionBuilder
00045 {
00046 public:
00047 BarActionBuilder( KActionCollection *actionCollection, KMainWindow *mainWindow, QPtrList<KToolBar> &oldToolBarList )
00048 : m_actionCollection( actionCollection ), m_mainWindow( mainWindow ), m_needsRebuild( false )
00049 {
00050 QPtrList<QDockWindow> dockWindows = m_mainWindow->dockWindows();
00051 QPtrListIterator<QDockWindow> dockWindowIt( dockWindows );
00052 for ( ; dockWindowIt.current(); ++dockWindowIt ) {
00053
00054 KToolBar *toolBar = dynamic_cast<KToolBar *>( dockWindowIt.current() );
00055 if ( !toolBar )
00056 continue;
00057
00058 if ( oldToolBarList.findRef( toolBar ) == -1 )
00059 m_needsRebuild = true;
00060
00061 m_toolBars.append( toolBar );
00062 }
00063
00064 if ( !m_needsRebuild )
00065 m_needsRebuild = ( oldToolBarList.count() != m_toolBars.count() );
00066 }
00067
00068 bool needsRebuild() const { return m_needsRebuild; }
00069
00070 QPtrList<KAction> create()
00071 {
00072 if ( !m_needsRebuild )
00073 return QPtrList<KAction>();
00074
00075 QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
00076 for ( ; toolBarIt.current(); ++toolBarIt )
00077 handleToolBar( toolBarIt.current() );
00078
00079 QPtrList<KAction> actions;
00080
00081 if ( m_toolBarActions.count() == 0 )
00082 return actions;
00083
00084 if ( m_toolBarActions.count() == 1 ) {
00085 KToggleToolBarAction* action = static_cast<KToggleToolBarAction *>( m_toolBarActions.getFirst() );
00086 action->setText( i18n( "Show Toolbar" ) );
00087 action->setCheckedState( i18n( "Hide Toolbar" ) );
00088 return m_toolBarActions;
00089 }
00090
00091 KActionMenu *menuAction = new KActionMenu( i18n( "Toolbars" ), m_actionCollection, "toolbars_submenu_action" );
00092
00093 QPtrListIterator<KAction> actionIt( m_toolBarActions );
00094 for ( ; actionIt.current(); ++actionIt )
00095 menuAction->insert( actionIt.current() );
00096
00097 actions.append( menuAction );
00098 return actions;
00099 }
00100
00101 const QPtrList<KToolBar> &toolBars() const { return m_toolBars; }
00102
00103 private:
00104 void handleToolBar( KToolBar *toolBar )
00105 {
00106 KToggleToolBarAction *action = new KToggleToolBarAction(
00107 toolBar,
00108 toolBar->label(),
00109 m_actionCollection,
00110 toolBar->name() );
00111
00112 m_toolBarActions.append( action );
00113 }
00114
00115 KActionCollection *m_actionCollection;
00116 KMainWindow *m_mainWindow;
00117
00118 QPtrList<KToolBar> m_toolBars;
00119 QPtrList<KAction> m_toolBarActions;
00120
00121 bool m_needsRebuild : 1;
00122 };
00123 }
00124
00125 using namespace KDEPrivate;
00126
00127 ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, const char *name )
00128 : QObject( mainWindow, name ), KXMLGUIClient( mainWindow )
00129 {
00130 init( mainWindow );
00131 }
00132
00133 ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, QObject *parent, const char *name )
00134 : QObject( parent, name ), KXMLGUIClient( mainWindow )
00135 {
00136 init( mainWindow );
00137 }
00138
00139 ToolBarHandler::~ToolBarHandler()
00140 {
00141 m_actions.setAutoDelete( true );
00142 m_actions.clear();
00143 }
00144
00145 KAction *ToolBarHandler::toolBarMenuAction()
00146 {
00147 assert( m_actions.count() == 1 );
00148 return m_actions.getFirst();
00149 }
00150
00151 void ToolBarHandler::setupActions()
00152 {
00153 if ( !factory() || !m_mainWindow )
00154 return;
00155
00156 BarActionBuilder builder( actionCollection(), m_mainWindow, m_toolBars );
00157
00158 if ( !builder.needsRebuild() )
00159 return;
00160
00161 unplugActionList( actionListName );
00162
00163 m_actions.setAutoDelete( true );
00164 m_actions.clear();
00165 m_actions.setAutoDelete( false );
00166
00167 m_actions = builder.create();
00168
00169
00170
00171
00172
00173
00174
00175 m_toolBars = builder.toolBars();
00176
00177
00178
00179
00180
00181
00182
00183
00184 if (kapp && kapp->authorizeKAction("options_show_toolbar"))
00185 plugActionList( actionListName, m_actions );
00186
00187 connectToActionContainers();
00188 }
00189
00190 void ToolBarHandler::clientAdded( KXMLGUIClient *client )
00191 {
00192 if ( client == this )
00193 setupActions();
00194 }
00195
00196 void ToolBarHandler::init( KMainWindow *mainWindow )
00197 {
00198 d = 0;
00199 m_mainWindow = mainWindow;
00200
00201 connect( m_mainWindow->guiFactory(), SIGNAL( clientAdded( KXMLGUIClient * ) ),
00202 this, SLOT( clientAdded( KXMLGUIClient * ) ) );
00203
00204
00205
00206
00207
00208
00209
00210 if ( domDocument().documentElement().isNull() ) {
00211
00212 QString completeDescription = QString::fromLatin1( guiDescription )
00213 .arg( actionListName );
00214
00215 setXML( completeDescription, false );
00216 }
00217 }
00218
00219 void ToolBarHandler::connectToActionContainers()
00220 {
00221 QPtrListIterator<KAction> actionIt( m_actions );
00222 for ( ; actionIt.current(); ++actionIt )
00223 connectToActionContainer( actionIt.current() );
00224 }
00225
00226 void ToolBarHandler::connectToActionContainer( KAction *action )
00227 {
00228 uint containerCount = action->containerCount();
00229 for ( uint i = 0; i < containerCount; ++i )
00230 connectToActionContainer( action->container( i ) );
00231 }
00232
00233 void ToolBarHandler::connectToActionContainer( QWidget *container )
00234 {
00235 QPopupMenu *popupMenu = dynamic_cast<QPopupMenu *>( container );
00236 if ( !popupMenu )
00237 return;
00238
00239 connect( popupMenu, SIGNAL( aboutToShow() ),
00240 this, SLOT( setupActions() ) );
00241 }
00242
00243 #include "ktoolbarhandler.moc"
00244
00245
00246