summaryrefslogtreecommitdiffabout
path: root/microkde/kdeui/ktoolbarhandler.cpp
Unidiff
Diffstat (limited to 'microkde/kdeui/ktoolbarhandler.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdeui/ktoolbarhandler.cpp253
1 files changed, 253 insertions, 0 deletions
diff --git a/microkde/kdeui/ktoolbarhandler.cpp b/microkde/kdeui/ktoolbarhandler.cpp
new file mode 100644
index 0000000..7b97233
--- a/dev/null
+++ b/microkde/kdeui/ktoolbarhandler.cpp
@@ -0,0 +1,253 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 2002 Simon Hausmann <hausmann@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA.
17*/
18
19#include "ktoolbarhandler.h"
20
21#include <qpopupmenu.h>
22#include <kapplication.h>
23#include <ktoolbar.h>
24#include <kmainwindow.h>
25#include <klocale.h>
26#include <kaction.h>
27#include <assert.h>
28
29namespace
30{
31 const char *actionListName = "show_menu_and_toolbar_actionlist";
32
33 const char *guiDescription = ""
34 "<!DOCTYPE kpartgui><kpartgui name=\"StandardToolBarMenuHandler\">"
35 "<MenuBar>"
36 " <Menu name=\"settings\">"
37 " <ActionList name=\"%1\" />"
38 " </Menu>"
39 "</MenuBar>"
40 "</kpartgui>";
41
42 const char *resourceFileName = "barhandler.rc";
43
44 class BarActionBuilder
45 {
46 public:
47 BarActionBuilder( KActionCollection *actionCollection, KMainWindow *mainWindow, QPtrList<KToolBar> &oldToolBarList )
48 : m_actionCollection( actionCollection ), m_mainWindow( mainWindow ), m_needsRebuild( false )
49 {
50/*US
51 QPtrList<QDockWindow> dockWindows = m_mainWindow->dockWindows();
52 QPtrListIterator<QDockWindow> dockWindowIt( dockWindows );
53 for ( ; dockWindowIt.current(); ++dockWindowIt ) {
54
55//US KToolBar *toolBar = dynamic_cast<KToolBar *>( dockWindowIt.current() );
56 KToolBar *toolBar = (KToolBar *)( dockWindowIt.current() );
57 if ( !toolBar )
58 continue;
59
60 if ( oldToolBarList.findRef( toolBar ) == -1 )
61 m_needsRebuild = true;
62
63 m_toolBars.append( toolBar );
64 }
65*/
66 if ( !m_needsRebuild )
67 m_needsRebuild = ( oldToolBarList.count() != m_toolBars.count() );
68
69 }
70
71 bool needsRebuild() const { return m_needsRebuild; }
72
73 QPtrList<KAction> create()
74 {
75 if ( !m_needsRebuild )
76 return QPtrList<KAction>();
77
78 QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
79 for ( ; toolBarIt.current(); ++toolBarIt )
80 handleToolBar( toolBarIt.current() );
81
82 QPtrList<KAction> actions;
83
84 if ( m_toolBarActions.count() == 0 )
85 return actions;
86
87 if ( m_toolBarActions.count() == 1 ) {
88 m_toolBarActions.getFirst()->setText( i18n( "Show Toolbar" ) );
89 return m_toolBarActions;
90 }
91
92 KActionMenu *menuAction = new KActionMenu( i18n( "Toolbars" ), m_actionCollection, "toolbars_submenu_action" );
93
94 QPtrListIterator<KAction> actionIt( m_toolBarActions );
95 for ( ; actionIt.current(); ++actionIt )
96 menuAction->insert( actionIt.current() );
97
98 actions.append( menuAction );
99 return actions;
100 }
101
102 const QPtrList<KToolBar> &toolBars() const { return m_toolBars; }
103
104 private:
105 void handleToolBar( KToolBar *toolBar )
106 {
107 KAction *action = new KToggleToolBarAction( toolBar,
108 i18n( "Show %1" ).arg( toolBar->label() ),
109 m_actionCollection,
110 toolBar->name() );
111
112 m_toolBarActions.append( action );
113 }
114
115 KActionCollection *m_actionCollection;
116 KMainWindow *m_mainWindow;
117
118 QPtrList<KToolBar> m_toolBars;
119 QPtrList<KAction> m_toolBarActions;
120
121 bool m_needsRebuild : 1;
122 };
123}
124
125using namespace KDEPrivate;
126
127ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, const char *name )
128 : QObject( mainWindow, name ), KXMLGUIClient( mainWindow )
129{
130 init( mainWindow );
131}
132
133ToolBarHandler::ToolBarHandler( KMainWindow *mainWindow, QObject *parent, const char *name )
134 : QObject( parent, name ), KXMLGUIClient( mainWindow )
135{
136 init( mainWindow );
137}
138
139ToolBarHandler::~ToolBarHandler()
140{
141 m_actions.setAutoDelete( true );
142 m_actions.clear();
143}
144
145KAction *ToolBarHandler::toolBarMenuAction()
146{
147 assert( m_actions.count() == 1 );
148 return m_actions.getFirst();
149}
150
151void ToolBarHandler::setupActions()
152{
153//US if ( !factory() || !m_mainWindow )
154 if ( !m_mainWindow )
155 return;
156
157 BarActionBuilder builder( actionCollection(), m_mainWindow, m_toolBars );
158
159 if ( !builder.needsRebuild() )
160 return;
161
162 unplugActionList( actionListName );
163
164 m_actions.setAutoDelete( true );
165
166 m_actions.clear();
167 m_actions.setAutoDelete( false );
168
169 m_actions = builder.create();
170
171 /*
172 for ( QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
173 toolBarIt.current(); ++toolBarIt )
174 toolBarIt.current()->disconnect( this );
175 */
176
177 m_toolBars = builder.toolBars();
178
179 /*
180 for ( QPtrListIterator<KToolBar> toolBarIt( m_toolBars );
181 toolBarIt.current(); ++toolBarIt )
182 connect( toolBarIt.current(), SIGNAL( destroyed() ),
183 this, SLOT( setupActions() ) );
184 */
185
186//US if (kapp && kapp->authorizeKAction("options_show_toolbar"))
187 plugActionList( actionListName, m_actions );
188
189 connectToActionContainers();
190}
191
192/*US
193void ToolBarHandler::clientAdded( KXMLGUIClient *client )
194{
195 if ( client == this )
196 setupActions();
197}
198*/
199
200void ToolBarHandler::init( KMainWindow *mainWindow )
201{
202 d = 0;
203 m_mainWindow = mainWindow;
204
205/*US
206 connect( m_mainWindow->guiFactory(), SIGNAL( clientAdded( KXMLGUIClient * ) ),
207 this, SLOT( clientAdded( KXMLGUIClient * ) ) );
208*/
209 /* re-use an existing resource file if it exists. can happen if the user launches the
210 * toolbar editor */
211 /*
212 setXMLFile( resourceFileName );
213 */
214/*US
215 if ( domDocument().documentElement().isNull() ) {
216
217 QString completeDescription = QString::fromLatin1( guiDescription )
218 .arg( actionListName );
219
220 setXML( completeDescription, false*/ /*merge*/ /*);
221 }
222*/
223}
224
225void ToolBarHandler::connectToActionContainers()
226{
227 QPtrListIterator<KAction> actionIt( m_actions );
228 for ( ; actionIt.current(); ++actionIt )
229 connectToActionContainer( actionIt.current() );
230}
231
232void ToolBarHandler::connectToActionContainer( KAction *action )
233{
234 uint containerCount = action->containerCount();
235 for ( uint i = 0; i < containerCount; ++i )
236 connectToActionContainer( action->container( i ) );
237}
238
239void ToolBarHandler::connectToActionContainer( QWidget *container )
240{
241//US QPopupMenu *popupMenu = dynamic_cast<QPopupMenu *>( container );
242 QPopupMenu *popupMenu = (QPopupMenu *)( container );
243 if ( !popupMenu )
244 return;
245
246 connect( popupMenu, SIGNAL( aboutToShow() ),
247 this, SLOT( setupActions() ) );
248}
249
250//US #include "ktoolbarhandler.moc"
251
252/* vim: et sw=4 ts=4
253 */