• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • xmlgui
ktoolbarhandler.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "ktoolbarhandler_p.h"
20
21#include <QtXml/QDomDocument>
22
23#include <kaction.h>
24#include <kactioncollection.h>
25#include <kactionmenu.h>
26#include <kauthorized.h>
27#include <kguiitem.h>
28#include <klocale.h>
29#include <kxmlguiwindow.h>
30#include <kmenu.h>
31#include <ktoggletoolbaraction.h>
32#include <ktoolbar.h>
33#include <kxmlguifactory.h>
34#include <kdebug.h>
35#include <kstandardaction_p.h>
36
37namespace
38{
39 const char *actionListName = "show_menu_and_toolbar_actionlist";
40
41 const char *guiDescription = ""
42 "<!DOCTYPE kpartgui><kpartgui name=\"StandardToolBarMenuHandler\">"
43 "<MenuBar>"
44 " <Menu name=\"settings\">"
45 " <ActionList name=\"%1\" />"
46 " </Menu>"
47 "</MenuBar>"
48 "</kpartgui>";
49
50 class BarActionBuilder
51 {
52 public:
53 BarActionBuilder( KActionCollection *actionCollection, KXmlGuiWindow *mainWindow,
54 QLinkedList<KToolBar*> &oldToolBarList )
55 : m_actionCollection( actionCollection ), m_mainWindow( mainWindow ), m_needsRebuild( false )
56 {
57 QList<KToolBar*> toolBars = qFindChildren<KToolBar*>( m_mainWindow );
58
59 foreach( KToolBar * toolBar, toolBars) {
60 if ( toolBar->mainWindow() != m_mainWindow )
61 continue;
62
63 if ( !oldToolBarList.contains( toolBar ) )
64 m_needsRebuild = true;
65
66 m_toolBars.append( toolBar );
67 }
68
69 if ( !m_needsRebuild )
70 m_needsRebuild = ( oldToolBarList.count() != m_toolBars.count() );
71 }
72
73 bool needsRebuild() const
74 {
75 return m_needsRebuild;
76 }
77
78 QList<QAction*> create()
79 {
80 QList<QAction*> actions;
81
82 if ( !m_needsRebuild )
83 return actions;
84
85 foreach ( KToolBar* bar, m_toolBars )
86 handleToolBar( bar );
87
88 if ( m_toolBarActions.count() == 0 )
89 return actions;
90
91 if ( m_toolBarActions.count() == 1 ) {
92 const KStandardAction::KStandardActionInfo* pInfo = KStandardAction::infoPtr(KStandardAction::ShowToolbar);
93 KToggleToolBarAction* action = static_cast<KToggleToolBarAction *>( m_toolBarActions.first() );
94 action->setText( i18n( pInfo->psLabel ) );
95 return m_toolBarActions;
96 }
97
98 KActionMenu *menuAction = new KActionMenu(i18n( "Toolbars Shown" ), m_actionCollection);
99 m_actionCollection->addAction("toolbars_submenu_action", menuAction);
100
101 foreach ( QAction* action, m_toolBarActions )
102 menuAction->menu()->addAction( action );
103
104 actions.append( menuAction );
105
106 return actions;
107 }
108
109 const QLinkedList<KToolBar*> &toolBars() const
110 {
111 return m_toolBars;
112 }
113
114 private:
115 void handleToolBar( KToolBar *toolBar )
116 {
117 KToggleToolBarAction *action = new KToggleToolBarAction(
118 toolBar,
119 toolBar->windowTitle(),
120 m_actionCollection);
121 m_actionCollection->addAction(toolBar->objectName(), action);
122
123 // ## tooltips, whatsthis?
124 m_toolBarActions.append( action );
125 }
126
127 KActionCollection *m_actionCollection;
128 KXmlGuiWindow *m_mainWindow;
129
130 QLinkedList<KToolBar*> m_toolBars;
131 QList<QAction*> m_toolBarActions;
132
133 bool m_needsRebuild : 1;
134 };
135}
136
137using namespace KDEPrivate;
138
139class ToolBarHandler::Private
140{
141 public:
142 Private( ToolBarHandler *_parent )
143 : parent( _parent )
144 {
145 }
146
147 void clientAdded( KXMLGUIClient *client )
148 {
149 Q_UNUSED(client)
150 parent->setupActions();
151 }
152
153 void init( KXmlGuiWindow *mainWindow );
154 void connectToActionContainers();
155 void connectToActionContainer( QAction *action );
156 void connectToActionContainer( QWidget *container );
157
158 ToolBarHandler *parent;
159 QPointer<KXmlGuiWindow> mainWindow;
160 QList<QAction*> actions;
161 QLinkedList<KToolBar*> toolBars;
162};
163
164void ToolBarHandler::Private::init( KXmlGuiWindow *mw )
165{
166 mainWindow = mw;
167
168 QObject::connect( mainWindow->guiFactory(), SIGNAL(clientAdded(KXMLGUIClient*)),
169 parent, SLOT(clientAdded(KXMLGUIClient*)) );
170
171 if ( parent->domDocument().documentElement().isNull() ) {
172
173 QString completeDescription = QString::fromLatin1( guiDescription )
174 .arg( actionListName );
175
176 parent->setXML( completeDescription, false /*merge*/ );
177 }
178}
179
180void ToolBarHandler::Private::connectToActionContainers()
181{
182 foreach ( QAction* action, actions )
183 connectToActionContainer( action );
184}
185
186void ToolBarHandler::Private::connectToActionContainer( QAction *action )
187{
188 uint containerCount = action->associatedWidgets().count();
189
190 for ( uint i = 0; i < containerCount; ++i )
191 connectToActionContainer( action->associatedWidgets().value( i ) );
192}
193
194void ToolBarHandler::Private::connectToActionContainer( QWidget *container )
195{
196 QMenu *popupMenu = qobject_cast<QMenu *>( container );
197 if ( !popupMenu )
198 return;
199
200 connect( popupMenu, SIGNAL(aboutToShow()),
201 parent, SLOT(setupActions()) );
202}
203
204ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow )
205 : QObject( mainWindow ), KXMLGUIClient( mainWindow ),
206 d( new Private( this ) )
207{
208 d->init( mainWindow );
209}
210
211ToolBarHandler::ToolBarHandler( KXmlGuiWindow *mainWindow, QObject *parent )
212 : QObject( parent ), KXMLGUIClient( mainWindow ),
213 d( new Private( this ) )
214{
215 d->init( mainWindow );
216}
217
218ToolBarHandler::~ToolBarHandler()
219{
220 qDeleteAll( d->actions );
221 d->actions.clear();
222
223 delete d;
224}
225
226QAction *ToolBarHandler::toolBarMenuAction()
227{
228 Q_ASSERT( d->actions.count() == 1 );
229 return d->actions.first();
230}
231
232void ToolBarHandler::setupActions()
233{
234 if ( !factory() || !d->mainWindow )
235 return;
236
237 BarActionBuilder builder( actionCollection(), d->mainWindow, d->toolBars );
238
239 if ( !builder.needsRebuild() )
240 return;
241
242 unplugActionList( actionListName );
243
244 qDeleteAll( d->actions );
245 d->actions.clear();
246
247 d->actions = builder.create();
248
249 d->toolBars = builder.toolBars();
250
251 // We have no XML file associated with our action collection, so load settings from KConfig
252 actionCollection()->readSettings(); // #233712
253
254 if ( KAuthorized::authorizeKAction( "options_show_toolbar" ) )
255 plugActionList( actionListName, d->actions );
256
257 d->connectToActionContainers();
258}
259
260#include "ktoolbarhandler_p.moc"
KActionCollection
A container for a set of QAction objects.
Definition: kactioncollection.h:57
KActionCollection::readSettings
void readSettings(KConfigGroup *config=0)
Read all key associations from config.
Definition: kactioncollection.cpp:413
KActionMenu
A KActionMenu is an action that has several properties specific to holding a sub-menu of other action...
Definition: kactionmenu.h:48
KActionMenu::menu
KMenu * menu()
Returns this action's menu as a KMenu, if it is one.
Definition: kactionmenu.cpp:162
KToggleToolBarAction
An action that takes care of everything associated with showing or hiding a toolbar by a menu action.
Definition: ktoggletoolbaraction.h:47
KToolBar
Floatable toolbar with auto resize.
Definition: ktoolbar.h:54
KToolBar::mainWindow
KMainWindow * mainWindow() const
Returns the main window that this toolbar is docked with.
Definition: ktoolbar.cpp:1068
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
Definition: kxmlguiclient.h:47
KXmlGuiWindow
KDE top level main window with predefined action layout
Definition: kxmlguiwindow.h:62
KXmlGuiWindow::guiFactory
virtual KXMLGUIFactory * guiFactory()
Definition: kxmlguiwindow.cpp:150
QAction
QList
QMenu
QObject
QWidget
kaction.h
kactioncollection.h
kactionmenu.h
kauthorized.h
kdebug.h
kguiitem.h
klocale.h
i18n
QString i18n(const char *text)
kmenu.h
ktoggletoolbaraction.h
ktoolbar.h
kxmlguifactory.h
kxmlguiwindow.h
KAuthorized::authorizeKAction
bool authorizeKAction(const QString &action)
KDEPrivate
Definition: kcolorchoosermode.cpp:24
KStandardAction::create
KAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
Creates an action corresponding to one of the KStandardAction::StandardAction actions,...
Definition: kstandardaction.cpp:82
KStandardAction::ShowToolbar
@ ShowToolbar
Definition: kstandardaction.h:151
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal