ktoolbarhandler.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# ktoolbarhandler.cpp

    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., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    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                 m_toolBarActions.getFirst()->setText( i18n( "Show Toolbar" ) );
    00086                 return m_toolBarActions;
    00087             }
    00088
    00089             KActionMenu *menuAction = new KActionMenu( i18n( "Toolbars" ), m_actionCollection, "toolbars_submenu_action" );
    00090
    00091             QPtrListIterator<KAction> actionIt( m_toolBarActions );
    00092             for ( ; actionIt.current(); ++actionIt )
    00093                 menuAction->insert( actionIt.current() );
    00094
    00095             actions.append( menuAction );
    00096             return actions;
    00097         }
    00098
    00099         const QPtrList<KToolBar> &toolBars() const { return m_toolBars; }
    00100
    00101     private:
    00102         void handleToolBar( KToolBar *toolBar )
    00103         {
    00104             KAction *action = new KToggleToolBarAction( toolBar,
    00105                                                         i18n( "Show %1" ).arg( toolBar->label() ),
    00106                                                         m_actionCollection,
    00107                                                         toolBar->name() );
    00108
    00109             m_toolBarActions.append( action );
    00110         }
    00111
    00112         KActionCollection *m_actionCollection;
    00113         KMainWindow *m_mainWindow;
    00114
    00115         QPtrList<KToolBar> m_toolBars;
    00116         QPtrList<KAction> m_toolBarActions;
    00117
    00118         bool m_needsRebuild : 1;
    00119     };
    00120 }
    00121
    00122 using namespace KDEPrivate;
    00123
    00124 ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, const char *name )
    00125     : QObject( mainWindow, name ), KXMLGUIClient( mainWindow )
    00126 {
    00127     init( mainWindow );
    00128 }
    00129
    00130 ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, QObject *parent, const char *name )
    00131     : QObject( parent, name ), KXMLGUIClient( mainWindow )
    00132 {
    00133     init( mainWindow );
    00134 }
    00135
    00136 ToolBarHandler::~ToolBarHandler()
    00137 {
    00138     m_actions.setAutoDelete( true );
    00139     m_actions.clear();
    00140 }
    00141
    00142 KAction *ToolBarHandler::toolBarMenuAction()
    00143 {
    00144     assert( m_actions.count() == 1 );
    00145     return m_actions.getFirst();
    00146 }
    00147
    00148 void ToolBarHandler::setupActions()
    00149 {
    00150     if ( !factory() || !m_mainWindow )
    00151         return;
    00152
    00153     BarActionBuilder builder( actionCollection(), m_mainWindow, m_toolBars );
    00154
    00155     if ( !builder.needsRebuild() )
    00156         return;
    00157
    00158     unplugActionList( actionListName );
    00159
    00160     m_actions.setAutoDelete( true );
    00161     m_actions.clear();
    00162     m_actions.setAutoDelete( false );
    00163
    00164     m_actions = builder.create();
    00165
    00166     /*
    00167     for (  QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
    00168            toolBarIt.current(); ++toolBarIt )
    00169         toolBarIt.current()->disconnect( this );
    00170         */
    00171
    00172     m_toolBars = builder.toolBars();
    00173
    00174     /*
    00175     for (  QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
    00176            toolBarIt.current(); ++toolBarIt )
    00177         connect( toolBarIt.current(), SIGNAL( destroyed() ),
    00178                  this, SLOT( setupActions() ) );
    00179                  */
    00180
    00181     if (kapp && kapp->authorizeKAction("options_show_toolbar"))
    00182         plugActionList( actionListName, m_actions );
    00183
    00184     connectToActionContainers();
    00185 }
    00186
    00187 void ToolBarHandler::clientAdded( KXMLGUIClient *client )
    00188 {
    00189     if ( client == this )
    00190         setupActions();
    00191 }
    00192
    00193 void ToolBarHandler::init( KMainWindow *mainWindow )
    00194 {
    00195     d = 0;
    00196     m_mainWindow = mainWindow;
    00197
    00198     connect( m_mainWindow->guiFactory(), SIGNAL( clientAdded( KXMLGUIClient * ) ),
    00199              this, SLOT( clientAdded( KXMLGUIClient * ) ) );
    00200
    00201     /* re-use an existing resource file if it exists. can happen if the user launches the
    00202      * toolbar editor */
    00203     /*
    00204     setXMLFile( resourceFileName );
    00205     */
    00206
    00207     if ( domDocument().documentElement().isNull() ) {
    00208
    00209         QString completeDescription = QString::fromLatin1( guiDescription )
    00210             .arg( actionListName );
    00211
    00212         setXML( completeDescription, false /*merge*/ );
    00213     }
    00214 }
    00215
    00216 void ToolBarHandler::connectToActionContainers()
    00217 {
    00218     QPtrListIterator<KAction> actionIt( m_actions );
    00219     for ( ; actionIt.current(); ++actionIt )
    00220         connectToActionContainer( actionIt.current() );
    00221 }
    00222
    00223 void ToolBarHandler::connectToActionContainer( KAction *action )
    00224 {
    00225     uint containerCount = action->containerCount();
    00226     for ( uint i = 0; i < containerCount; ++i )
    00227         connectToActionContainer( action->container( i ) );
    00228 }
    00229
    00230 void ToolBarHandler::connectToActionContainer( QWidget *container )
    00231 {
    00232     QPopupMenu *popupMenu = dynamic_cast<QPopupMenu *>( container );
    00233     if ( !popupMenu )
    00234         return;
    00235
    00236     connect( popupMenu, SIGNAL( aboutToShow() ),
    00237              this, SLOT( setupActions() ) );
    00238 }
    00239
    00240 #include "ktoolbarhandler.moc"
    00241
    00242 /* vim: et sw=4 ts=4
    00243  */

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:58 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001