source: trunk/tools/shared/qttoolbardialog/qttoolbardialog.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 60.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qttoolbardialog.h"
43#include "ui_qttoolbardialog.h"
44
45#include <QtCore/QSet>
46#include <QtGui/QtEvents>
47#include <QtGui/QAction>
48#include <QtGui/QToolBar>
49#include <QtGui/QMainWindow>
50#include <QtGui/QHeaderView>
51#include <QtGui/QPushButton>
52
53QT_BEGIN_NAMESPACE
54
55class QtFullToolBarManagerPrivate;
56
57class QtFullToolBarManager : public QObject
58{
59 Q_OBJECT
60public:
61 QtFullToolBarManager(QObject *parent);
62 ~QtFullToolBarManager();
63
64 void setMainWindow(QMainWindow *mainWindow);
65 QMainWindow *mainWindow() const;
66
67 void addCategory(const QString &category);
68 bool hasCategory(const QString &category) const;
69 QStringList categories() const;
70 QList<QAction *> categoryActions(const QString &category) const;
71 QString actionCategory(QAction *action) const;
72
73 // only non-separator
74 void addAction(QAction *action, const QString &category);
75
76 void removeAction(QAction *action);
77
78 QSet<QAction *> actions() const;
79 bool isWidgetAction(QAction *action) const;
80
81 /*
82 Adds (registers) toolBar. Adds (registers) actions that already exists in toolBar.
83 Remembers toolbar and its actions as a default.
84 */
85 void addDefaultToolBar(QToolBar *toolBar, const QString &category);
86
87 void removeDefaultToolBar(QToolBar *toolBar);
88 // NULL on action list means separator.
89 QMap<QToolBar *, QList<QAction *> > defaultToolBars() const;
90 bool isDefaultToolBar(QToolBar *toolBar) const;
91
92 QToolBar *createToolBar(const QString &toolBarName);
93 void deleteToolBar(QToolBar *toolBar); // only those which were created, not added
94
95 QList<QAction *> actions(QToolBar *toolBar) const;
96
97 void setToolBars(const QMap<QToolBar *, QList<QAction *> > &actions);
98 void setToolBar(QToolBar *toolBar, const QList<QAction *> &actions);
99
100 QMap<QToolBar *, QList<QAction *> > toolBarsActions() const;
101 QByteArray saveState(int version = 0) const;
102 bool restoreState(const QByteArray &state, int version = 0);
103
104public slots:
105
106 void resetToolBar(QToolBar *toolBar);
107 void resetAllToolBars();
108
109signals:
110 void toolBarCreated(QToolBar *toolBar);
111 void toolBarRemoved(QToolBar *toolBar);
112
113 /*
114 If QToolBarWidgetAction was in another tool bar and is inserted into
115 this toolBar, toolBarChanged is first emitted for other toolbar - without
116 that action. (Another approach may be that user first must call setToolBar
117 without that action for old tool bar)
118 */
119 void toolBarChanged(QToolBar *toolBar, const QList<QAction *> &actions);
120
121private:
122 QScopedPointer<QtFullToolBarManagerPrivate> d_ptr;
123 Q_DECLARE_PRIVATE(QtFullToolBarManager)
124 Q_DISABLE_COPY(QtFullToolBarManager)
125};
126
127class QtFullToolBarManagerPrivate
128{
129 class QtFullToolBarManager *q_ptr;
130 Q_DECLARE_PUBLIC(QtFullToolBarManager)
131
132public:
133
134 QToolBar *toolBarWidgetAction(QAction *action) const;
135 void removeWidgetActions(const QMap<QToolBar *, QList<QAction *> > &actions);
136
137 enum {
138 VersionMarker = 0xff,
139 ToolBarMarker = 0xfe,
140 CustomToolBarMarker = 0xfd,
141 };
142
143 void saveState(QDataStream &stream) const;
144 bool restoreState(QDataStream &stream) const;
145 QToolBar *findDefaultToolBar(const QString &objectName) const;
146 QAction *findAction(const QString &actionName) const;
147
148 QToolBar *toolBarByName(const QString &toolBarName) const;
149
150 QtFullToolBarManagerPrivate();
151
152 QMap<QString, QList<QAction *> > categoryToActions;
153 QMap<QAction *, QString> actionToCategory;
154
155 QSet<QAction *> allActions;
156 QMap<QAction *, QToolBar *> widgetActions;
157 QSet<QAction *> regularActions;
158 QMap<QAction *, QList<QToolBar *> > actionToToolBars;
159
160 QMap<QToolBar *, QList<QAction *> > toolBars;
161 QMap<QToolBar *, QList<QAction *> > toolBarsWithSeparators;
162 QMap<QToolBar *, QList<QAction *> > defaultToolBars;
163 QList<QToolBar *> customToolBars;
164
165 QMainWindow *theMainWindow;
166};
167
168
169
170
171QtFullToolBarManagerPrivate::QtFullToolBarManagerPrivate()
172 : theMainWindow(0)
173{
174}
175
176QToolBar *QtFullToolBarManagerPrivate::toolBarWidgetAction(QAction *action) const
177{
178 if (widgetActions.contains(action))
179 return widgetActions.value(action);
180 return 0;
181}
182
183void QtFullToolBarManagerPrivate::removeWidgetActions(const QMap<QToolBar *, QList<QAction *> >
184 &actions)
185{
186 QMap<QToolBar *, QList<QAction *> >::ConstIterator itToolBar = actions.constBegin();
187 while (itToolBar != actions.constEnd()) {
188 QToolBar *toolBar = itToolBar.key();
189 QList<QAction *> newActions = toolBars.value(toolBar);
190 QList<QAction *> newActionsWithSeparators = toolBarsWithSeparators.value(toolBar);
191
192 QList<QAction *> removedActions;
193 QList<QAction *> actionList = itToolBar.value();
194 QListIterator<QAction *> itAction(actionList);
195 while (itAction.hasNext()) {
196 QAction *action = itAction.next();
197 if (newActions.contains(action) && toolBarWidgetAction(action) == toolBar) {
198 newActions.removeAll(action);
199 newActionsWithSeparators.removeAll(action);
200 removedActions.append(action);
201 }
202 }
203
204 //emit q_ptr->toolBarChanged(toolBar, newActions);
205
206 toolBars.insert(toolBar, newActions);
207 toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
208 QListIterator<QAction *> itRemovedAction(removedActions);
209 while (itRemovedAction.hasNext()) {
210 QAction *oldAction = itRemovedAction.next();
211 widgetActions.insert(oldAction, 0);
212 actionToToolBars[oldAction].removeAll(toolBar);
213 }
214
215 ++itToolBar;
216 }
217}
218
219void QtFullToolBarManagerPrivate::saveState(QDataStream &stream) const
220{
221 stream << (uchar) ToolBarMarker;
222 stream << defaultToolBars.size();
223 QMap<QToolBar *, QList<QAction *> >::ConstIterator itToolBar =
224 defaultToolBars.constBegin();
225 while (itToolBar != defaultToolBars.constEnd()) {
226 QToolBar *tb = itToolBar.key();
227 if (tb->objectName().isEmpty()) {
228 qWarning("QtToolBarManager::saveState(): 'objectName' not set for QToolBar "
229 "%p '%s', using 'windowTitle' instead",
230 tb, tb->windowTitle().toLocal8Bit().constData());
231 stream << tb->windowTitle();
232 } else {
233 stream << tb->objectName();
234 }
235
236 stream << toolBars[tb].size();
237 QListIterator<QAction *> itAction(toolBars[tb]);
238 while (itAction.hasNext()) {
239 QAction *action = itAction.next();
240
241 if (action) {
242 if (action->objectName().isEmpty()) {
243 qWarning("QtToolBarManager::saveState(): 'objectName' not set for QAction "
244 "%p '%s', using 'text' instead",
245 action, action->text().toLocal8Bit().constData());
246 stream << action->text();
247 } else {
248 stream << action->objectName();
249 }
250 } else {
251 stream << QString();
252 }
253 }
254 ++itToolBar;
255 }
256
257
258 stream << (uchar) CustomToolBarMarker;
259 stream << toolBars.size() - defaultToolBars.size();
260 itToolBar = toolBars.constBegin();
261 while (itToolBar != toolBars.constEnd()) {
262 QToolBar *tb = itToolBar.key();
263 if (!defaultToolBars.contains(tb)) {
264 stream << tb->objectName();
265 stream << tb->windowTitle();
266
267 stream << toolBars[tb].size();
268 QListIterator<QAction *> itAction(toolBars[tb]);
269 while (itAction.hasNext()) {
270 QAction *action = itAction.next();
271
272 if (action) {
273 if (action->objectName().isEmpty()) {
274 qWarning("QtToolBarManager::saveState(): 'objectName' not set for QAction "
275 "%p '%s', using 'text' instead",
276 action, action->text().toLocal8Bit().constData());
277 stream << action->text();
278 } else {
279 stream << action->objectName();
280 }
281 } else {
282 stream << QString();
283 }
284 }
285 }
286 ++itToolBar;
287 }
288}
289
290bool QtFullToolBarManagerPrivate::restoreState(QDataStream &stream) const
291{
292 uchar tmarker;
293 stream >> tmarker;
294 if (tmarker != ToolBarMarker)
295 return false;
296
297 int toolBars;
298 stream >> toolBars;
299 for (int i = 0; i < toolBars; i++) {
300 QString objectName;
301 stream >> objectName;
302 int actionCount;
303 stream >> actionCount;
304 QList<QAction *> actions;
305 for (int j = 0; j < actionCount; j++) {
306 QString actionName;
307 stream >> actionName;
308
309 if (actionName.isEmpty())
310 actions.append(0);
311 else {
312 QAction *action = findAction(actionName);
313 if (action)
314 actions.append(action);
315 }
316 }
317
318 QToolBar *toolBar = findDefaultToolBar(objectName);
319 if (toolBar)
320 q_ptr->setToolBar(toolBar, actions);
321 }
322
323
324
325 uchar ctmarker;
326 stream >> ctmarker;
327 if (ctmarker != CustomToolBarMarker)
328 return false;
329
330 QList<QToolBar *> oldCustomToolBars = customToolBars;
331
332 stream >> toolBars;
333 for (int i = 0; i < toolBars; i++) {
334 QString objectName;
335 QString toolBarName;
336 int actionCount;
337 stream >> objectName;
338 stream >> toolBarName;
339 stream >> actionCount;
340 QList<QAction *> actions;
341 for (int j = 0; j < actionCount; j++) {
342 QString actionName;
343 stream >> actionName;
344
345 if (actionName.isEmpty())
346 actions.append(0);
347 else {
348 QAction *action = findAction(actionName);
349 if (action)
350 actions.append(action);
351 }
352 }
353
354 QToolBar *toolBar = toolBarByName(objectName);
355 if (toolBar) {
356 toolBar->setWindowTitle(toolBarName);
357 oldCustomToolBars.removeAll(toolBar);
358 }
359 else
360 toolBar = q_ptr->createToolBar(toolBarName);
361 if (toolBar) {
362 toolBar->setObjectName(objectName);
363 q_ptr->setToolBar(toolBar, actions);
364 }
365 }
366 QListIterator<QToolBar *> itToolBar(oldCustomToolBars);
367 while (itToolBar.hasNext())
368 q_ptr->deleteToolBar(itToolBar.next());
369 return true;
370}
371
372QToolBar *QtFullToolBarManagerPrivate::findDefaultToolBar(const QString &objectName) const
373{
374 QMap<QToolBar *, QList<QAction *> >::ConstIterator itToolBar =
375 defaultToolBars.constBegin();
376 while (itToolBar != defaultToolBars.constEnd()) {
377 QToolBar *tb = itToolBar.key();
378 if (tb->objectName() == objectName)
379 return tb;
380
381 ++itToolBar;
382 }
383
384 qWarning("QtToolBarManager::restoreState(): cannot find a QToolBar named "
385 "'%s', trying to match using 'windowTitle' instead.",
386 objectName.toLocal8Bit().constData());
387
388 itToolBar = defaultToolBars.constBegin();
389 while (itToolBar != defaultToolBars.constEnd()) {
390 QToolBar *tb = itToolBar.key();
391 if (tb->windowTitle() == objectName)
392 return tb;
393
394 ++itToolBar;
395 }
396 qWarning("QtToolBarManager::restoreState(): cannot find a QToolBar with "
397 "matching 'windowTitle' (looking for '%s').",
398 objectName.toLocal8Bit().constData());
399
400 return 0;
401}
402
403QAction *QtFullToolBarManagerPrivate::findAction(const QString &actionName) const
404{
405 QSetIterator<QAction *> itAction(allActions);
406 while (itAction.hasNext()) {
407 QAction *action = itAction.next();
408
409 if (action->objectName() == actionName)
410 return action;
411 }
412 qWarning("QtToolBarManager::restoreState(): cannot find a QAction named "
413 "'%s', trying to match using 'text' instead.",
414 actionName.toLocal8Bit().constData());
415
416 itAction.toFront();
417 while (itAction.hasNext()) {
418 QAction *action = itAction.next();
419
420 if (action->text() == actionName)
421 return action;
422 }
423 qWarning("QtToolBarManager::restoreState(): cannot find a QAction with "
424 "matching 'text' (looking for '%s').",
425 actionName.toLocal8Bit().constData());
426
427 return 0;
428}
429
430QToolBar *QtFullToolBarManagerPrivate::toolBarByName(const QString &toolBarName) const
431{
432 QMap<QToolBar *, QList<QAction *> >::ConstIterator itToolBar = toolBars.constBegin();
433 while (itToolBar != toolBars.constEnd()) {
434 QToolBar *toolBar = itToolBar.key();
435 if (toolBar->objectName() == toolBarName)
436 return toolBar;
437
438 ++itToolBar;
439 }
440 return 0;
441}
442
443//////////////////////////////
444
445QtFullToolBarManager::QtFullToolBarManager(QObject *parent)
446 : QObject(parent), d_ptr(new QtFullToolBarManagerPrivate)
447{
448 d_ptr->q_ptr = this;
449}
450
451QtFullToolBarManager::~QtFullToolBarManager()
452{
453}
454
455void QtFullToolBarManager::setMainWindow(QMainWindow *mainWindow)
456{
457 d_ptr->theMainWindow = mainWindow;
458}
459
460QMainWindow *QtFullToolBarManager::mainWindow() const
461{
462 return d_ptr->theMainWindow;
463}
464
465void QtFullToolBarManager::addCategory(const QString &category)
466{
467 d_ptr->categoryToActions[category] = QList<QAction *>();
468}
469
470bool QtFullToolBarManager::hasCategory(const QString &category) const
471{
472 return d_ptr->categoryToActions.contains(category);
473}
474
475QStringList QtFullToolBarManager::categories() const
476{
477 return d_ptr->categoryToActions.keys();
478}
479
480QList<QAction *> QtFullToolBarManager::categoryActions(const QString &category) const
481{
482 QMap<QString, QList<QAction *> >::ConstIterator it =
483 d_ptr->categoryToActions.find(category);
484 if (it != d_ptr->categoryToActions.constEnd())
485 return it.value();
486 return QList<QAction *>();
487}
488
489QString QtFullToolBarManager::actionCategory(QAction *action) const
490{
491 QMap<QAction *, QString>::ConstIterator it = d_ptr->actionToCategory.find(action);
492 if (it != d_ptr->actionToCategory.constEnd())
493 return it.value();
494 return QString();
495}
496
497void QtFullToolBarManager::addAction(QAction *action, const QString &category)
498{
499 if (!action)
500 return;
501 if (action->isSeparator())
502 return;
503 if (d_ptr->allActions.contains(action))
504 return;
505 if (QLatin1String(action->metaObject()->className()) ==
506 QLatin1String("QToolBarWidgetAction"))
507 d_ptr->widgetActions.insert(action, 0);
508 else
509 d_ptr->regularActions.insert(action);
510 d_ptr->allActions.insert(action);
511 d_ptr->categoryToActions[category].append(action);
512 d_ptr->actionToCategory[action] = category;
513}
514
515void QtFullToolBarManager::removeAction(QAction *action)
516{
517 if (!d_ptr->allActions.contains(action))
518 return;
519
520 QList<QToolBar *> toolBars = d_ptr->actionToToolBars[action];
521 QListIterator<QToolBar *> itToolBar(toolBars);
522 while (itToolBar.hasNext()) {
523 QToolBar *toolBar = itToolBar.next();
524
525 d_ptr->toolBars[toolBar].removeAll(action);
526 d_ptr->toolBarsWithSeparators[toolBar].removeAll(action);
527
528 toolBar->removeAction(action);
529 }
530
531 QMap<QToolBar *, QList<QAction *> >::ConstIterator itDefault =
532 d_ptr->defaultToolBars.constBegin();
533 while (itDefault != d_ptr->defaultToolBars.constEnd()) {
534 if (itDefault.value().contains(action))
535 d_ptr->defaultToolBars[itDefault.key()].removeAll(action);
536
537 itDefault++;
538 }
539
540 d_ptr->allActions.remove(action);
541 d_ptr->widgetActions.remove(action);
542 d_ptr->regularActions.remove(action);
543 d_ptr->actionToToolBars.remove(action);
544
545 QString category = d_ptr->actionToCategory.value(action);
546 d_ptr->actionToCategory.remove(action);
547 d_ptr->categoryToActions[category].removeAll(action);
548
549 if (d_ptr->categoryToActions[category].isEmpty())
550 d_ptr->categoryToActions.remove(category);
551}
552
553QSet<QAction *> QtFullToolBarManager::actions() const
554{
555 return d_ptr->allActions;
556}
557
558bool QtFullToolBarManager::isWidgetAction(QAction *action) const
559{
560 if (d_ptr->widgetActions.contains(action))
561 return true;
562 return false;
563}
564
565void QtFullToolBarManager::addDefaultToolBar(QToolBar *toolBar, const QString &category)
566{
567 if (!toolBar)
568 return;
569 if (d_ptr->toolBars.contains(toolBar))
570 return;
571 // could be also checked if toolBar belongs to mainwindow
572
573 QList<QAction *> newActionsWithSeparators;
574 QList<QAction *> newActions;
575 QList<QAction *> actions = toolBar->actions();
576 QListIterator<QAction *> itAction(actions);
577 while (itAction.hasNext()) {
578 QAction *action = itAction.next();
579 addAction(action, category);
580 if (d_ptr->widgetActions.contains(action))
581 d_ptr->widgetActions.insert(action, toolBar);
582 newActionsWithSeparators.append(action);
583 if (action->isSeparator())
584 action = 0;
585 else
586 d_ptr->actionToToolBars[action].append(toolBar);
587 newActions.append(action);
588 }
589 d_ptr->defaultToolBars.insert(toolBar, newActions);
590 //Below could be done by call setToolBar() if we want signal emission here.
591 d_ptr->toolBars.insert(toolBar, newActions);
592 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
593}
594
595void QtFullToolBarManager::removeDefaultToolBar(QToolBar *toolBar)
596{
597 if (!d_ptr->defaultToolBars.contains(toolBar))
598 return;
599
600 QList<QAction *> defaultActions = d_ptr->defaultToolBars[toolBar];
601 setToolBar(toolBar, QList<QAction *>());
602 QListIterator<QAction *> itAction(defaultActions);
603 while (itAction.hasNext())
604 removeAction(itAction.next());
605
606 d_ptr->toolBars.remove(toolBar);
607 d_ptr->toolBarsWithSeparators.remove(toolBar);
608 d_ptr->defaultToolBars.remove(toolBar);
609
610 itAction.toFront();
611 while (itAction.hasNext()) {
612 QAction *action = itAction.next();
613 if (action)
614 toolBar->insertAction(0, action);
615 else
616 toolBar->insertSeparator(0);
617 }
618}
619
620QMap<QToolBar *, QList<QAction *> > QtFullToolBarManager::defaultToolBars() const
621{
622 return d_ptr->defaultToolBars;
623}
624
625bool QtFullToolBarManager::isDefaultToolBar(QToolBar *toolBar) const
626{
627 if (d_ptr->defaultToolBars.contains(toolBar))
628 return true;
629 return false;
630}
631
632QToolBar *QtFullToolBarManager::createToolBar(const QString &toolBarName)
633{
634 if (!mainWindow())
635 return 0;
636 QToolBar *toolBar = new QToolBar(toolBarName, mainWindow());
637 int i = 1;
638 const QString prefix = QLatin1String("_Custom_Toolbar_%1");
639 QString name = prefix.arg(i);
640 while (d_ptr->toolBarByName(name))
641 name = prefix.arg(++i);
642 toolBar->setObjectName(name);
643 mainWindow()->addToolBar(toolBar);
644 d_ptr->customToolBars.append(toolBar);
645 d_ptr->toolBars.insert(toolBar, QList<QAction *>());
646 d_ptr->toolBarsWithSeparators.insert(toolBar, QList<QAction *>());
647 return toolBar;
648}
649
650void QtFullToolBarManager::deleteToolBar(QToolBar *toolBar)
651{
652 if (!d_ptr->toolBars.contains(toolBar))
653 return;
654 if (d_ptr->defaultToolBars.contains(toolBar))
655 return;
656 setToolBar(toolBar, QList<QAction *>());
657 d_ptr->customToolBars.removeAll(toolBar);
658 d_ptr->toolBars.remove(toolBar);
659 d_ptr->toolBarsWithSeparators.remove(toolBar);
660 delete toolBar;
661}
662
663QList<QAction *> QtFullToolBarManager::actions(QToolBar *toolBar) const
664{
665 if (d_ptr->toolBars.contains(toolBar))
666 return d_ptr->toolBars.value(toolBar);
667 return QList<QAction *>();
668}
669
670void QtFullToolBarManager::setToolBars(const QMap<QToolBar *, QList<QAction *> > &actions)
671{
672 QMap<QToolBar *, QList<QAction *> >::ConstIterator it = actions.constBegin();
673 while (it != actions.constEnd()) {
674 setToolBar(it.key(), it.value());
675 ++it;
676 }
677}
678
679void QtFullToolBarManager::setToolBar(QToolBar *toolBar, const QList<QAction *> &actions)
680{
681 if (!toolBar)
682 return;
683 if (!d_ptr->toolBars.contains(toolBar))
684 return;
685
686 if (actions == d_ptr->toolBars[toolBar])
687 return;
688
689 QMap<QToolBar *, QList<QAction *> > toRemove;
690
691 QList<QAction *> newActions;
692 QListIterator<QAction *> itAction(actions);
693 while (itAction.hasNext()) {
694 QAction *action = itAction.next();
695 if (!action || (!newActions.contains(action) && d_ptr->allActions.contains(action)))
696 newActions.append(action);
697
698 QToolBar *oldToolBar = d_ptr->toolBarWidgetAction(action);
699 if (oldToolBar && oldToolBar != toolBar)
700 toRemove[oldToolBar].append(action);
701 }
702
703 d_ptr->removeWidgetActions(toRemove);
704
705 QList<QAction *> oldActions = d_ptr->toolBarsWithSeparators.value(toolBar);
706 QListIterator<QAction *> itOldAction(oldActions);
707 while (itOldAction.hasNext()) {
708 QAction *action = itOldAction.next();
709 /*
710 When addDefaultToolBar() separator actions could be checked if they are
711 inserted in other toolbars - if yes then create new one.
712 */
713 if (d_ptr->toolBarWidgetAction(action) == toolBar)
714 d_ptr->widgetActions.insert(action, 0);
715 toolBar->removeAction(action);
716 if (action->isSeparator())
717 delete action;
718 else
719 d_ptr->actionToToolBars[action].removeAll(toolBar);
720 }
721
722 QList<QAction *> newActionsWithSeparators;
723 QListIterator<QAction *> itNewActions(newActions);
724 while (itNewActions.hasNext()) {
725 QAction *action = itNewActions.next();
726 QAction *newAction = 0;
727 if (!action)
728 newAction = toolBar->insertSeparator(0);
729 if (d_ptr->allActions.contains(action)) {
730 toolBar->insertAction(0, action);
731 newAction = action;
732 d_ptr->actionToToolBars[action].append(toolBar);
733 }
734 newActionsWithSeparators.append(newAction);
735 }
736 d_ptr->toolBars.insert(toolBar, newActions);
737 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
738}
739
740QMap<QToolBar *, QList<QAction *> > QtFullToolBarManager::toolBarsActions() const
741{
742 return d_ptr->toolBars;
743}
744
745void QtFullToolBarManager::resetToolBar(QToolBar *toolBar)
746{
747 if (!isDefaultToolBar(toolBar))
748 return;
749 setToolBar(toolBar, defaultToolBars().value(toolBar));
750}
751
752void QtFullToolBarManager::resetAllToolBars()
753{
754 setToolBars(defaultToolBars());
755 QList<QToolBar *> oldCustomToolBars = d_ptr->customToolBars;
756 QListIterator<QToolBar *> itToolBar(oldCustomToolBars);
757 while (itToolBar.hasNext()) {
758 deleteToolBar(itToolBar.next());
759 }
760}
761
762QByteArray QtFullToolBarManager::saveState(int version) const
763{
764 QByteArray data;
765 QDataStream stream(&data, QIODevice::WriteOnly);
766 stream << QtFullToolBarManagerPrivate::VersionMarker;
767 stream << version;
768 d_ptr->saveState(stream);
769 return data;
770}
771
772bool QtFullToolBarManager::restoreState(const QByteArray &state, int version)
773{
774 QByteArray sd = state;
775 QDataStream stream(&sd, QIODevice::ReadOnly);
776 int marker, v;
777 stream >> marker;
778 stream >> v;
779 if (marker != QtFullToolBarManagerPrivate::VersionMarker || v != version)
780 return false;
781 return d_ptr->restoreState(stream);
782}
783
784
785class QtToolBarManagerPrivate
786{
787 class QtToolBarManager *q_ptr;
788 Q_DECLARE_PUBLIC(QtToolBarManager)
789public:
790 QtFullToolBarManager *manager;
791};
792
793//////////////////////////////////////
794
795/*! \class QtToolBarManager
796 \internal
797 \inmodule QtDesigner
798 \since 4.4
799
800 \brief The QtToolBarManager class provides toolbar management for
801 main windows.
802
803 The QtToolBarManager is typically used with a QtToolBarDialog
804 which allows the user to customize the toolbars for a given main
805 window. The QtToolBarDialog class's functionality is controlled by
806 an instance of the QtToolBarManager class, and the main window is
807 specified using the QtToolBarManager class's setMainWindow()
808 function.
809
810 The currently specified main window can be retrieved using the
811 mainWindow() function.
812
813 The toolbar manager holds lists of the given main window's actions
814 and toolbars, and can add actions and toolbars to these
815 lists using the addAction() and addToolBar() functions
816 respectively. The actions can in addition be categorized
817 acccording to the user's preferences. The toolbar manager can also
818 remove custom actions and toolbars using the removeAction() and
819 removeToolBar() functions.
820
821 Finally, the QtToolBarManager is able to save the customized state
822 of its toolbars using the saveState() function as well as restore
823 the toolbars' saved state using restoreState() function.
824
825 \sa QtToolBarDialog
826*/
827
828/*!
829 Creates a toolbar manager with the given \a parent.
830*/
831QtToolBarManager::QtToolBarManager(QObject *parent)
832 : QObject(parent), d_ptr(new QtToolBarManagerPrivate)
833{
834 d_ptr->q_ptr = this;
835
836 d_ptr->manager = new QtFullToolBarManager(this);
837}
838
839/*!
840 Destroys the toolbar manager.
841*/
842QtToolBarManager::~QtToolBarManager()
843{
844}
845
846/*!
847 Sets the main window upon which the toolbar manager operates, to
848 be the given \a mainWindow.
849*/
850void QtToolBarManager::setMainWindow(QMainWindow *mainWindow)
851{
852 d_ptr->manager->setMainWindow(mainWindow);
853}
854
855/*!
856 Returns the main window associated this toolbar manager.
857*/
858QMainWindow *QtToolBarManager::mainWindow() const
859{
860 return d_ptr->manager->mainWindow();
861}
862
863/*!
864 Adds the given \a action to the given \a category in the manager's
865 list of actions. If the \a category doesn't exist it is created.
866 Only non separator actions can be added. If the action is already
867 added to the list, the function doesn't do anything.
868
869 \sa removeAction()
870*/
871void QtToolBarManager::addAction(QAction *action, const QString &category)
872{
873 d_ptr->manager->addAction(action, category);
874}
875
876/*!
877 Removes the specified \a action from the manager's list of
878 actions. The action is also removed from all the registered
879 toolbars. If the specified \a action is the only action in its
880 category, that category is removed as well.
881
882 \sa addAction()
883*/
884void QtToolBarManager::removeAction(QAction *action)
885{
886 d_ptr->manager->removeAction(action);
887}
888
889/*!
890 Adds the given \a toolBar to the manager's toolbar list.
891
892 All the \a toolBar's actions are automatically added to the given
893 \a category in the manager's list of actions if they're not
894 already there. The manager remembers which toolbar the actions
895 belonged to, so, when the \a toolBar is removed, its actions will
896 be removed as well.
897
898 Custom toolbars are created with the main window returned by
899 the mainWindow() function, as its parent.
900
901 \sa removeToolBar()
902*/
903void QtToolBarManager::addToolBar(QToolBar *toolBar, const QString &category)
904{
905 d_ptr->manager->addDefaultToolBar(toolBar, category);
906}
907
908/*!
909 Removes the specified \a toolBar from the manager's list. All the
910 actions that existed in the specified \a toolBar when it was
911 added are removed as well.
912
913 \sa addToolBar()
914*/
915void QtToolBarManager::removeToolBar(QToolBar *toolBar)
916{
917 d_ptr->manager->removeDefaultToolBar(toolBar);
918}
919
920/*!
921 Returns the manager's toolbar list.
922*/
923QList<QToolBar *> QtToolBarManager::toolBars() const
924{
925 return d_ptr->manager->toolBarsActions().keys();
926}
927
928/*
929void QtToolBarManager::resetToolBar(QToolBar *toolBar)
930{
931 d_ptr->manager->resetToolBar(toolBar);
932}
933
934void QtToolBarManager::resetAllToolBars()
935{
936 d_ptr->manager->resetAllToolBars();
937}
938*/
939
940/*!
941 Saves the state of the toolbar manager's toolbars. The \a version
942 number is stored as part of the data.
943
944 Identifies all the QToolBar and QAction objects by their object
945 name property. Ensure that this property is unique for each
946 QToolBar and QAction that you add using the QtToolBarManager.
947
948 Returns an identifier for the state which can be passed along with
949 the version number to the restoreState() function to restore the
950 saved state.
951
952 \sa restoreState()
953*/
954QByteArray QtToolBarManager::saveState(int version) const
955{
956 return d_ptr->manager->saveState(version);
957}
958
959/*!
960 Restores the saved state of the toolbar manager's toolbars. The
961 \a version number is compared with the version number of the
962 stored \a state.
963
964 Returns true if the version numbers are matching and the toolbar
965 manager's state is restored; otherwise the toolbar manager's state
966 is left unchanged and the function returns false.
967
968 Note that the state of the toolbar manager's toolbars should be
969 restored before restoring the state of the main window's toolbars
970 and dockwidgets using the QMainWindow::restoreState() function. In
971 that way the restoreState() function can create the custom
972 toolbars before the QMainWindow::restoreState() function restores
973 the custom toolbars' positions.
974
975 \sa saveState()
976*/
977bool QtToolBarManager::restoreState(const QByteArray &state, int version)
978{
979 return d_ptr->manager->restoreState(state, version);
980}
981
982//////////////////////
983
984class ToolBarItem {
985public:
986 ToolBarItem() : tb(0) {}
987 ToolBarItem(QToolBar *toolBar) : tb(toolBar) {}
988 ToolBarItem(QToolBar *toolBar, const QString &toolBarName)
989 : tb(toolBar), tbName(toolBarName) {}
990 ToolBarItem(const QString &toolBarName) : tb(0), tbName(toolBarName) {}
991 QToolBar *toolBar() const
992 { return tb; }
993 void setToolBar(QToolBar *toolBar)
994 { tb = toolBar; }
995 QString toolBarName() const
996 { return tbName; }
997 void setToolBarName(const QString &toolBarName)
998 { tbName = toolBarName; }
999private:
1000 QToolBar *tb;
1001 QString tbName;
1002};
1003
1004class QtToolBarDialogPrivate {
1005 QtToolBarDialog *q_ptr;
1006 Q_DECLARE_PUBLIC(QtToolBarDialog)
1007public:
1008 QtToolBarDialogPrivate()
1009 : toolBarManager(0),
1010 currentAction(0),
1011 currentToolBar(0)
1012 { }
1013
1014 ToolBarItem *createItem(QToolBar *toolBar);
1015 ToolBarItem *createItem(const QString &toolBarName);
1016 void deleteItem(ToolBarItem *item);
1017
1018 void newClicked();
1019 void removeClicked();
1020 void defaultClicked();
1021 void okClicked();
1022 void applyClicked();
1023 void cancelClicked();
1024 void upClicked();
1025 void downClicked();
1026 void leftClicked();
1027 void rightClicked();
1028 void renameClicked();
1029 void toolBarRenamed(QListWidgetItem *item);
1030 void currentActionChanged(QTreeWidgetItem *current);
1031 void currentToolBarChanged(QListWidgetItem *current);
1032 void currentToolBarActionChanged(QListWidgetItem *current);
1033
1034 void removeToolBar(ToolBarItem *item);
1035 bool isDefaultToolBar(ToolBarItem *item) const;
1036 void setButtons();
1037 void clearOld();
1038 void fillNew();
1039 QtFullToolBarManager *toolBarManager;
1040 QMap<ToolBarItem *, QList<QAction *> > currentState;
1041 QMap<QToolBar *, ToolBarItem *> toolBarItems;
1042 QSet<ToolBarItem *> createdItems;
1043 QSet<ToolBarItem *> removedItems;
1044
1045 QSet<ToolBarItem *> allToolBarItems;
1046
1047 // static
1048 QTreeWidgetItem *currentAction;
1049 QMap<QAction *, QTreeWidgetItem *> actionToItem;
1050 QMap<QTreeWidgetItem *, QAction *> itemToAction;
1051
1052 // dynamic
1053 ToolBarItem *currentToolBar;
1054 QMap<ToolBarItem *, QListWidgetItem *> toolBarToItem;
1055 QMap<QListWidgetItem *, ToolBarItem *> itemToToolBar;
1056
1057 // dynamic
1058 QMap<QAction *, QListWidgetItem *> actionToCurrentItem;
1059 QMap<QListWidgetItem *, QAction *> currentItemToAction;
1060
1061 QMap<QAction *, ToolBarItem *> widgetActionToToolBar;
1062 QMap<ToolBarItem *, QSet<QAction *> > toolBarToWidgetActions;
1063
1064 QString separatorText;
1065 Ui::QtToolBarDialog ui;
1066};
1067
1068ToolBarItem *QtToolBarDialogPrivate::createItem(QToolBar *toolBar)
1069{
1070 if (!toolBar)
1071 return 0;
1072 ToolBarItem *item = new ToolBarItem(toolBar, toolBar->windowTitle());
1073 allToolBarItems.insert(item);
1074 return item;
1075}
1076
1077ToolBarItem *QtToolBarDialogPrivate::createItem(const QString &toolBarName)
1078{
1079 ToolBarItem *item = new ToolBarItem(toolBarName);
1080 allToolBarItems.insert(item);
1081 return item;
1082}
1083
1084void QtToolBarDialogPrivate::deleteItem(ToolBarItem *item)
1085{
1086 if (!allToolBarItems.contains(item))
1087 return;
1088 allToolBarItems.remove(item);
1089 delete item;
1090}
1091
1092void QtToolBarDialogPrivate::clearOld()
1093{
1094 ui.actionTree->clear();
1095 ui.toolBarList->clear();
1096 ui.currentToolBarList->clear();
1097 ui.removeButton->setEnabled(false);
1098 ui.newButton->setEnabled(false);
1099 ui.upButton->setEnabled(false);
1100 ui.downButton->setEnabled(false);
1101 ui.leftButton->setEnabled(false);
1102 ui.rightButton->setEnabled(false);
1103
1104 actionToItem.clear();
1105 itemToAction.clear();
1106 toolBarToItem.clear();
1107 itemToToolBar.clear();
1108 actionToCurrentItem.clear();
1109 currentItemToAction.clear();
1110 widgetActionToToolBar.clear();
1111 toolBarToWidgetActions.clear();
1112
1113 toolBarItems.clear();
1114 currentState.clear();
1115 createdItems.clear();
1116 removedItems.clear();
1117 QSetIterator<ToolBarItem *> itItem(allToolBarItems);
1118 while (itItem.hasNext())
1119 delete itItem.next();
1120 allToolBarItems.clear();
1121
1122 currentToolBar = 0;
1123 currentAction = 0;
1124}
1125
1126void QtToolBarDialogPrivate::fillNew()
1127{
1128 if (!toolBarManager)
1129 return;
1130
1131 QTreeWidgetItem *item = new QTreeWidgetItem(ui.actionTree);
1132 item->setText(0, separatorText);
1133 ui.actionTree->setCurrentItem(item);
1134 currentAction = item;
1135 actionToItem.insert(0, item);
1136 itemToAction.insert(item, 0);
1137 QStringList categories = toolBarManager->categories();
1138 QStringListIterator itCategory(categories);
1139 while (itCategory.hasNext()) {
1140 QString category = itCategory.next();
1141 QTreeWidgetItem *categoryItem = new QTreeWidgetItem(ui.actionTree);
1142 categoryItem->setText(0, category);
1143 QList<QAction *> actions = toolBarManager->categoryActions(category);
1144 QListIterator<QAction *> itAction(actions);
1145 while (itAction.hasNext()) {
1146 QAction *action = itAction.next();
1147 item = new QTreeWidgetItem(categoryItem);
1148 item->setText(0, action->text());
1149 item->setIcon(0, action->icon());
1150 item->setTextAlignment(0, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic);
1151 actionToItem.insert(action, item);
1152 itemToAction.insert(item, action);
1153 if (toolBarManager->isWidgetAction(action)) {
1154 item->setData(0, Qt::TextColorRole, QColor(Qt::blue));
1155 widgetActionToToolBar.insert(action, 0);
1156 }
1157 item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
1158 }
1159 ui.actionTree->setItemExpanded(categoryItem, true);
1160 }
1161 //ui.actionTree->sortItems(0, Qt::AscendingOrder);
1162
1163 QMap<QToolBar *, QList<QAction *> > toolBars = toolBarManager->toolBarsActions();
1164 QMap<QToolBar *, QList<QAction *> >::ConstIterator it = toolBars.constBegin();
1165 while (it != toolBars.constEnd()) {
1166 QToolBar *toolBar = it.key();
1167 ToolBarItem *tbItem = createItem(toolBar);
1168 toolBarItems.insert(toolBar, tbItem);
1169 QListWidgetItem *item = new QListWidgetItem(toolBar->windowTitle(),
1170 ui.toolBarList);
1171 toolBarToItem.insert(tbItem, item);
1172 itemToToolBar.insert(item, tbItem);
1173 QList<QAction *> actions = it.value();
1174 QListIterator<QAction *> itAction(actions);
1175 while (itAction.hasNext()) {
1176 QAction *action = itAction.next();
1177 if (toolBarManager->isWidgetAction(action)) {
1178 widgetActionToToolBar.insert(action, tbItem);
1179 toolBarToWidgetActions[tbItem].insert(action);
1180 }
1181 }
1182 currentState.insert(tbItem, actions);
1183 if (it == toolBars.constBegin())
1184 ui.toolBarList->setCurrentItem(item);
1185 if (isDefaultToolBar(tbItem))
1186 item->setData(Qt::TextColorRole, QColor(Qt::darkGreen));
1187 else
1188 item->setFlags(item->flags() | Qt::ItemIsEditable);
1189
1190 ++it;
1191 }
1192 ui.toolBarList->sortItems();
1193 setButtons();
1194}
1195
1196bool QtToolBarDialogPrivate::isDefaultToolBar(ToolBarItem *item) const
1197{
1198 if (!item)
1199 return false;
1200 if (!item->toolBar())
1201 return false;
1202 return toolBarManager->isDefaultToolBar(item->toolBar());
1203}
1204
1205void QtToolBarDialogPrivate::setButtons()
1206{
1207 bool newEnabled = false;
1208 bool removeEnabled = false;
1209 bool renameEnabled = false;
1210 bool upEnabled = false;
1211 bool downEnabled = false;
1212 bool leftEnabled = false;
1213 bool rightEnabled = false;
1214
1215 if (toolBarManager) {
1216 newEnabled = true;
1217 removeEnabled = !isDefaultToolBar(currentToolBar);
1218 renameEnabled = removeEnabled;
1219 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1220 if (currentToolBarAction) {
1221 int row = ui.currentToolBarList->row(currentToolBarAction);
1222 upEnabled = row > 0;
1223 downEnabled = row < ui.currentToolBarList->count() - 1;
1224 leftEnabled = true;
1225 }
1226 if (currentAction && currentToolBar)
1227 rightEnabled = true;
1228 }
1229 ui.newButton->setEnabled(newEnabled);
1230 ui.removeButton->setEnabled(removeEnabled);
1231 ui.renameButton->setEnabled(renameEnabled);
1232 ui.upButton->setEnabled(upEnabled);
1233 ui.downButton->setEnabled(downEnabled);
1234 ui.leftButton->setEnabled(leftEnabled);
1235 ui.rightButton->setEnabled(rightEnabled);
1236}
1237
1238void QtToolBarDialogPrivate::newClicked()
1239{
1240 QString toolBarName = QtToolBarDialog::tr("Custom Toolbar"); // = QInputDialog::getString();
1241 // produce unique name
1242 ToolBarItem *item = createItem(toolBarName);
1243 currentState.insert(item, QList<QAction *>());
1244 createdItems.insert(item);
1245 QListWidgetItem *i = new QListWidgetItem(toolBarName, ui.toolBarList);
1246 i->setFlags(i->flags() | Qt::ItemIsEditable);
1247 ui.toolBarList->setCurrentItem(i);
1248 itemToToolBar.insert(i, item);
1249 toolBarToItem.insert(item, i);
1250 ui.toolBarList->sortItems();
1251 ui.toolBarList->setCurrentItem(i);
1252 currentToolBarChanged(i);
1253 renameClicked();
1254}
1255
1256void QtToolBarDialogPrivate::removeToolBar(ToolBarItem *item)
1257{
1258 if (!item)
1259 return;
1260 if (item->toolBar() && toolBarManager->isDefaultToolBar(item->toolBar()))
1261 return;
1262 if (!toolBarToItem.contains(item))
1263 return;
1264 QListWidgetItem *i = toolBarToItem.value(item);
1265 bool wasCurrent = false;
1266 if (i == ui.toolBarList->currentItem())
1267 wasCurrent = true;
1268 int row = ui.toolBarList->row(i);
1269 QMap<ToolBarItem *, QSet<QAction *> >::ConstIterator itToolBar =
1270 toolBarToWidgetActions.find(item);
1271 if (itToolBar != toolBarToWidgetActions.constEnd()) {
1272 QSet<QAction *> actions = itToolBar.value();
1273 QSetIterator<QAction *> itAction(actions);
1274 while (itAction.hasNext()) {
1275 QAction *action = itAction.next();
1276 widgetActionToToolBar.insert(action, 0);
1277 }
1278 toolBarToWidgetActions.remove(item);
1279 }
1280
1281 currentState.remove(item);
1282 createdItems.remove(item);
1283 toolBarToItem.remove(item);
1284 itemToToolBar.remove(i);
1285 delete i;
1286 if (item->toolBar())
1287 removedItems.insert(item);
1288 else
1289 deleteItem(item);
1290 if (wasCurrent) {
1291 if (row == ui.toolBarList->count())
1292 row--;
1293 if (row < 0)
1294 ;
1295 else
1296 ui.toolBarList->setCurrentRow(row);
1297 }
1298 setButtons();
1299}
1300
1301void QtToolBarDialogPrivate::removeClicked()
1302{
1303 QListWidgetItem *i = ui.toolBarList->currentItem();
1304 if (!i)
1305 return;
1306 ToolBarItem *item = itemToToolBar.value(i);
1307 removeToolBar(item);
1308}
1309
1310void QtToolBarDialogPrivate::defaultClicked()
1311{
1312 QMap<QToolBar *, QList<QAction *> > defaultToolBars = toolBarManager->defaultToolBars();
1313 QMap<QToolBar *, QList<QAction *> >::ConstIterator itToolBar = defaultToolBars.constBegin();
1314 while (itToolBar != defaultToolBars.constEnd()) {
1315 QToolBar *toolBar = itToolBar.key();
1316 ToolBarItem *toolBarItem = toolBarItems.value(toolBar);
1317
1318 if (toolBarToWidgetActions.contains(toolBarItem)) {
1319 QSetIterator<QAction *> itAction(toolBarToWidgetActions.value(toolBarItem));
1320 while (itAction.hasNext())
1321 widgetActionToToolBar.insert(itAction.next(), 0);
1322 toolBarToWidgetActions.remove(toolBarItem);
1323 }
1324
1325 currentState.remove(toolBarItem);
1326
1327 QListIterator<QAction *> itAction(itToolBar.value());
1328 while (itAction.hasNext()) {
1329 QAction *action = itAction.next();
1330 if (toolBarManager->isWidgetAction(action)) {
1331 ToolBarItem *otherToolBar = widgetActionToToolBar.value(action);
1332 if (otherToolBar) {
1333 toolBarToWidgetActions[otherToolBar].remove(action);
1334 currentState[otherToolBar].removeAll(action);
1335 }
1336 widgetActionToToolBar.insert(action, toolBarItem);
1337 toolBarToWidgetActions[toolBarItem].insert(action);
1338 }
1339 }
1340 currentState.insert(toolBarItem, itToolBar.value());
1341
1342 ++itToolBar;
1343 }
1344 currentToolBarChanged(toolBarToItem.value(currentToolBar));
1345
1346 QList<ToolBarItem *> toolBars = currentState.keys();
1347 QListIterator<ToolBarItem *> itTb(toolBars);
1348 while (itTb.hasNext())
1349 removeToolBar(itTb.next());
1350}
1351
1352void QtToolBarDialogPrivate::okClicked()
1353{
1354 applyClicked();
1355 q_ptr->accept();
1356}
1357
1358void QtToolBarDialogPrivate::applyClicked()
1359{
1360 QMap<ToolBarItem *, QList<QAction *> > toolBars = currentState;
1361 QMap<ToolBarItem *, QList<QAction *> >::ConstIterator itToolBar = toolBars.constBegin();
1362 while (itToolBar != toolBars.constEnd()) {
1363 ToolBarItem *item = itToolBar.key();
1364 QToolBar *toolBar = item->toolBar();
1365 if (toolBar) {
1366 toolBarManager->setToolBar(toolBar, itToolBar.value());
1367 toolBar->setWindowTitle(item->toolBarName());
1368 }
1369
1370 ++itToolBar;
1371 }
1372
1373 QSet<ToolBarItem *> toRemove = removedItems;
1374 QSetIterator<ToolBarItem *> itRemove(toRemove);
1375 while (itRemove.hasNext()) {
1376 ToolBarItem *item = itRemove.next();
1377 QToolBar *toolBar = item->toolBar();
1378 removedItems.remove(item);
1379 currentState.remove(item);
1380 deleteItem(item);
1381 if (toolBar)
1382 toolBarManager->deleteToolBar(toolBar);
1383 }
1384
1385 QSet<ToolBarItem *> toCreate = createdItems;
1386 QSetIterator<ToolBarItem *> itCreate(toCreate);
1387 while (itCreate.hasNext()) {
1388 ToolBarItem *item = itCreate.next();
1389 QString toolBarName = item->toolBarName();
1390 createdItems.remove(item);
1391 QList<QAction *> actions = currentState.value(item);
1392 QToolBar *toolBar = toolBarManager->createToolBar(toolBarName);
1393 item->setToolBar(toolBar);
1394 toolBarManager->setToolBar(toolBar, actions);
1395 }
1396}
1397
1398void QtToolBarDialogPrivate::upClicked()
1399{
1400 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1401 if (!currentToolBarAction)
1402 return;
1403 int row = ui.currentToolBarList->row(currentToolBarAction);
1404 if (row == 0)
1405 return;
1406 ui.currentToolBarList->takeItem(row);
1407 int newRow = row - 1;
1408 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1409 QList<QAction *> actions = currentState.value(currentToolBar);
1410 QAction *action = actions.at(row);
1411 actions.removeAt(row);
1412 actions.insert(newRow, action);
1413 currentState.insert(currentToolBar, actions);
1414 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1415 setButtons();
1416}
1417
1418void QtToolBarDialogPrivate::downClicked()
1419{
1420 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1421 if (!currentToolBarAction)
1422 return;
1423 int row = ui.currentToolBarList->row(currentToolBarAction);
1424 if (row == ui.currentToolBarList->count() - 1)
1425 return;
1426 ui.currentToolBarList->takeItem(row);
1427 int newRow = row + 1;
1428 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1429 QList<QAction *> actions = currentState.value(currentToolBar);
1430 QAction *action = actions.at(row);
1431 actions.removeAt(row);
1432 actions.insert(newRow, action);
1433 currentState.insert(currentToolBar, actions);
1434 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1435 setButtons();
1436}
1437
1438void QtToolBarDialogPrivate::leftClicked()
1439{
1440 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1441 if (!currentToolBarAction)
1442 return;
1443 int row = ui.currentToolBarList->row(currentToolBarAction);
1444 currentState[currentToolBar].removeAt(row);
1445 QAction *action = currentItemToAction.value(currentToolBarAction);
1446 if (widgetActionToToolBar.contains(action)) {
1447 ToolBarItem *item = widgetActionToToolBar.value(action);
1448 if (item == currentToolBar) { // have to be
1449 toolBarToWidgetActions[item].remove(action);
1450 if (toolBarToWidgetActions[item].empty())
1451 toolBarToWidgetActions.remove(item);
1452 }
1453 widgetActionToToolBar.insert(action, 0);
1454 }
1455 if (action)
1456 actionToCurrentItem.remove(action);
1457 currentItemToAction.remove(currentToolBarAction);
1458 delete currentToolBarAction;
1459 if (row == ui.currentToolBarList->count())
1460 row--;
1461 if (row >= 0) {
1462 QListWidgetItem *item = ui.currentToolBarList->item(row);
1463 ui.currentToolBarList->setCurrentItem(item);
1464 }
1465 setButtons();
1466}
1467
1468void QtToolBarDialogPrivate::rightClicked()
1469{
1470 if (!currentAction)
1471 return;
1472 if (!currentToolBar)
1473 return;
1474 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1475
1476 QAction *action = itemToAction.value(currentAction);
1477 QListWidgetItem *item = 0;
1478 if (action) {
1479 if (currentState[currentToolBar].contains(action)) {
1480 item = actionToCurrentItem.value(action);
1481 if (item == currentToolBarAction)
1482 return;
1483 int row = ui.currentToolBarList->row(item);
1484 ui.currentToolBarList->takeItem(row);
1485 currentState[currentToolBar].removeAt(row);
1486 // only reorder here
1487 } else {
1488 item = new QListWidgetItem(action->text());
1489 item->setIcon(action->icon());
1490 item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic);
1491 currentItemToAction.insert(item, action);
1492 actionToCurrentItem.insert(action, item);
1493 if (widgetActionToToolBar.contains(action)) {
1494 item->setData(Qt::TextColorRole, QColor(Qt::blue));
1495 ToolBarItem *toolBar = widgetActionToToolBar.value(action);
1496 if (toolBar) {
1497 currentState[toolBar].removeAll(action);
1498 toolBarToWidgetActions[toolBar].remove(action);
1499 if (toolBarToWidgetActions[toolBar].empty())
1500 toolBarToWidgetActions.remove(toolBar);
1501 }
1502 widgetActionToToolBar.insert(action, currentToolBar);
1503 toolBarToWidgetActions[currentToolBar].insert(action);
1504 }
1505 }
1506 } else {
1507 item = new QListWidgetItem(separatorText);
1508 currentItemToAction.insert(item, 0);
1509 }
1510
1511 int row = ui.currentToolBarList->count();
1512 if (currentToolBarAction) {
1513 row = ui.currentToolBarList->row(currentToolBarAction) + 1;
1514 }
1515 ui.currentToolBarList->insertItem(row, item);
1516 currentState[currentToolBar].insert(row, action);
1517 ui.currentToolBarList->setCurrentItem(item);
1518
1519 setButtons();
1520}
1521
1522void QtToolBarDialogPrivate::renameClicked()
1523{
1524 if (!currentToolBar)
1525 return;
1526
1527 QListWidgetItem *item = toolBarToItem.value(currentToolBar);
1528 ui.toolBarList->editItem(item);
1529}
1530
1531void QtToolBarDialogPrivate::toolBarRenamed(QListWidgetItem *item)
1532{
1533 if (!currentToolBar)
1534 return;
1535
1536 ToolBarItem *tbItem = itemToToolBar.value(item);
1537 if (!tbItem)
1538 return;
1539 tbItem->setToolBarName(item->text());
1540 //ui.toolBarList->sortItems();
1541}
1542
1543void QtToolBarDialogPrivate::currentActionChanged(QTreeWidgetItem *current)
1544{
1545 if (itemToAction.contains(current))
1546 currentAction = current;
1547 else
1548 currentAction = NULL;
1549 setButtons();
1550}
1551
1552void QtToolBarDialogPrivate::currentToolBarChanged(QListWidgetItem *current)
1553{
1554 currentToolBar = itemToToolBar.value(current);
1555 ui.currentToolBarList->clear();
1556 actionToCurrentItem.clear();
1557 currentItemToAction.clear();
1558 setButtons();
1559 if (!currentToolBar) {
1560 return;
1561 }
1562 QList<QAction *> actions = currentState.value(currentToolBar);
1563 QListIterator<QAction *> itAction(actions);
1564 QListWidgetItem *first = 0;
1565 while (itAction.hasNext()) {
1566 QAction *action = itAction.next();
1567 QString actionName = separatorText;
1568 if (action)
1569 actionName = action->text();
1570 QListWidgetItem *item = new QListWidgetItem(actionName, ui.currentToolBarList);
1571 if (action) {
1572 item->setIcon(action->icon());
1573 item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic);
1574 actionToCurrentItem.insert(action, item);
1575 if (widgetActionToToolBar.contains(action))
1576 item->setData(Qt::TextColorRole, QColor(Qt::blue));
1577 }
1578 currentItemToAction.insert(item, action);
1579 if (!first)
1580 first = item;
1581 }
1582 if (first)
1583 ui.currentToolBarList->setCurrentItem(first);
1584}
1585
1586void QtToolBarDialogPrivate::currentToolBarActionChanged(QListWidgetItem *)
1587{
1588 setButtons();
1589}
1590
1591void QtToolBarDialogPrivate::cancelClicked()
1592{
1593 // just nothing
1594 q_ptr->reject();
1595}
1596
1597//////////////////////
1598/*
1599class FeedbackItemDelegate : public QItemDelegate
1600{
1601 Q_OBJECT
1602public:
1603 FeedbackItemDelegate(QObject *parent = 0) : QItemDelegate(parent) { }
1604
1605 virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
1606 const QModelIndex & index) const;
1607 virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
1608};
1609
1610void FeedbackItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
1611 const QModelIndex &index) const
1612{
1613 if ()
1614 painter->save();
1615 QRect r = option.rect;
1616 float yCentral = r.height() / 2.0;
1617 float margin = 2.0;
1618 float arrowWidth = 5.0;
1619 float width = 20;
1620 qDebug("rect: x %d, y %d, w %d, h %d", r.x(), r.y(), r.width(), r.height());
1621 QLineF lineBase(0.0 + margin, r.y() + yCentral, width - margin, r.y() + yCentral);
1622 QLineF lineArrowLeft(width - margin - arrowWidth, r.y() + yCentral - arrowWidth,
1623 width - margin, r.y() + yCentral);
1624 QLineF lineArrowRight(width - margin - arrowWidth, r.y() + yCentral + arrowWidth,
1625 width - margin, r.y() + yCentral);
1626 painter->drawLine(lineBase);
1627 painter->drawLine(lineArrowLeft);
1628 painter->drawLine(lineArrowRight);
1629 painter->translate(QPoint(width, 0));
1630 QItemDelegate::paint(painter, option, index);
1631 painter->restore();
1632}
1633
1634QSize FeedbackItemDelegate::sizeHint(const QStyleOptionViewItem &option,
1635 const QModelIndex &index) const
1636{
1637 //return QItemDelegate::sizeHint(option, index);
1638 QSize s = QItemDelegate::sizeHint(option, index);
1639 s.setWidth(s.width() - 20);
1640 return s;
1641}
1642
1643class QtToolBarListWidget : public QListWidget
1644{
1645 Q_OBJECT
1646public:
1647 QtToolBarListWidget(QWidget *parent) : QListWidget(parent), actionDrag(false) {}
1648
1649protected:
1650 void startDrag(Qt::DropActions supportedActions);
1651
1652 void dragEnterEvent(QDragEnterEvent *event);
1653 void dragMoveEvent(QDragMoveEvent *event);
1654 void dragLeaveEvent(QDragLeaveEvent *);
1655 void dropEvent(QDropEvent *event);
1656
1657 void setDragAction(const QString *action) { actionName = action; }
1658private:
1659 QPersistentModelIndex lastDropIndicator;
1660 QString actionName;
1661 bool actionDrag;
1662};
1663
1664void QtToolBarListWidget::startDrag(Qt::DropActions supportedActions)
1665{
1666 QListWidgetItem *item = currentItem();
1667 if (item) {
1668 actionName = QString();
1669 emit aboutToDrag(item);
1670 if (!actionName.isEmpty()) {
1671 QDrag *drag = new QDrag(this);
1672 QMimeData *data = new QMimeData;
1673 data->setData("action", actionName.toLocal8Bit().constData());
1674 drag->setMimeData(data);
1675 drag->start(supportedActions);
1676 }
1677 }
1678}
1679
1680void QtToolBarListWidget::dragEnterEvent(QDragEnterEvent *event)
1681{
1682 const QMimeData *mime = event->mimeData();
1683 actionDrag = mime->hasFormat("action");
1684 if (actionDrag)
1685 event->accept();
1686 else
1687 event->ignore();
1688}
1689
1690void QtToolBarListWidget::dragMoveEvent(QDragMoveEvent *event)
1691{
1692 event->ignore();
1693 if (actionDrag) {
1694 QPoint p = event->pos();
1695 QListWidgetItem *item = itemAt(p);
1696 Indicator indic = QtToolBarListWidget::None;
1697 if (item) {
1698 QRect rect = visualItemRect(item);
1699 if (p.y() - rect.top() < rect.height() / 2)
1700 indic = QtToolBarListWidget::Above;
1701 else
1702 indic = QtToolBarListWidget::Below;
1703 }
1704 setIndicator(item, indic);
1705 event->accept();
1706 }
1707}
1708
1709void QtToolBarListWidget::dragLeaveEvent(QDragLeaveEvent *)
1710{
1711 if (actionDrag) {
1712 actionDrag = false;
1713 setIndicator(item, QtToolBarListWidget::None);
1714 }
1715}
1716
1717void QtToolBarListWidget::dropEvent(QDropEvent *event)
1718{
1719 if (actionDrag) {
1720 QListWidgetItem *item = indicatorItem();
1721 Indicator indic = indicator();
1722 QByteArray array = event->mimeData()->data("action");
1723 QDataStream stream(&array, QIODevice::ReadOnly);
1724 QString action;
1725 stream >> action;
1726 emit actionDropped(action, item, );
1727
1728 actionDrag = false;
1729 setIndicator(item, QtToolBarListWidget::None);
1730 }
1731}
1732*/
1733
1734/*! \class QtToolBarDialog
1735 \internal
1736 \inmodule QtDesigner
1737 \since 4.4
1738
1739 \brief The QtToolBarDialog class provides a dialog for customizing
1740 toolbars.
1741
1742 QtToolBarDialog allows the user to customize the toolbars for a
1743 given main window.
1744
1745 \image qttoolbardialog.png
1746
1747 The dialog lets the users add, rename and remove custom toolbars.
1748 Note that built-in toolbars are marked with a green color, and
1749 cannot be removed or renamed.
1750
1751 The users can also add and remove actions from the toolbars. An
1752 action can be added to many toolbars, but a toolbar can only
1753 contain one instance of each action. Actions that contains a
1754 widget are marked with a blue color in the list of actions, and
1755 can only be added to one single toolbar.
1756
1757 Finally, the users can add separators to the toolbars.
1758
1759 The original toolbars can be restored by clicking the \gui
1760 {Restore all} button. All custom toolbars will then be removed,
1761 and all built-in toolbars will be restored to their original state.
1762
1763 The QtToolBarDialog class's functionality is controlled by an
1764 instance of the QtToolBarManager class, and the main window is
1765 specified using the QtToolBarManager::setMainWindow() function.
1766
1767 All you need to do to use QtToolBarDialog is to specify an
1768 QtToolBarManager instance and call the QDialog::exec() slot:
1769
1770 \snippet doc/src/snippets/code/tools_shared_qttoolbardialog_qttoolbardialog.cpp 0
1771
1772 \sa QtToolBarManager
1773*/
1774
1775/*!
1776 Creates a toolbar dialog with the given \a parent and the specified
1777 window \a flags.
1778*/
1779QtToolBarDialog::QtToolBarDialog(QWidget *parent, Qt::WindowFlags flags)
1780 : QDialog(parent, flags), d_ptr(new QtToolBarDialogPrivate)
1781{
1782 d_ptr->q_ptr = this;
1783 d_ptr->ui.setupUi(this);
1784 d_ptr->separatorText = tr("< S E P A R A T O R >");
1785
1786 d_ptr->ui.actionTree->setColumnCount(1);
1787 d_ptr->ui.actionTree->setRootIsDecorated(false);
1788 d_ptr->ui.actionTree->header()->hide();
1789
1790 d_ptr->ui.upButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/up.png")));
1791 d_ptr->ui.downButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/down.png")));
1792 d_ptr->ui.leftButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/back.png")));
1793 d_ptr->ui.rightButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/forward.png")));
1794 d_ptr->ui.newButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/plus.png")));
1795 d_ptr->ui.removeButton->setIcon(QIcon(QLatin1String(":/trolltech/qttoolbardialog/images/minus.png")));
1796
1797 connect(d_ptr->ui.newButton, SIGNAL(clicked()), this, SLOT(newClicked()));
1798 connect(d_ptr->ui.removeButton, SIGNAL(clicked()), this, SLOT(removeClicked()));
1799 connect(d_ptr->ui.renameButton, SIGNAL(clicked()), this, SLOT(renameClicked()));
1800 connect(d_ptr->ui.upButton, SIGNAL(clicked()), this, SLOT(upClicked()));
1801 connect(d_ptr->ui.downButton, SIGNAL(clicked()), this, SLOT(downClicked()));
1802 connect(d_ptr->ui.leftButton, SIGNAL(clicked()), this, SLOT(leftClicked()));
1803 connect(d_ptr->ui.rightButton, SIGNAL(clicked()), this, SLOT(rightClicked()));
1804
1805 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), this, SLOT(defaultClicked()));
1806 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(okClicked()));
1807 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyClicked()));
1808 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancelClicked()));
1809
1810 connect(d_ptr->ui.actionTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
1811 this, SLOT(currentActionChanged(QTreeWidgetItem*)));
1812 connect(d_ptr->ui.toolBarList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
1813 this, SLOT(currentToolBarChanged(QListWidgetItem*)));
1814 connect(d_ptr->ui.currentToolBarList,
1815 SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
1816 this, SLOT(currentToolBarActionChanged(QListWidgetItem*)));
1817
1818 connect(d_ptr->ui.actionTree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
1819 this, SLOT(rightClicked()));
1820 connect(d_ptr->ui.currentToolBarList, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
1821 this, SLOT(leftClicked()));
1822 connect(d_ptr->ui.toolBarList, SIGNAL(itemChanged(QListWidgetItem*)),
1823 this, SLOT(toolBarRenamed(QListWidgetItem*)));
1824}
1825
1826/*!
1827 Destroys the toolbar dialog.
1828*/
1829QtToolBarDialog::~QtToolBarDialog()
1830{
1831 d_ptr->clearOld();
1832}
1833
1834/*!
1835 Connects the toolbar dialog to the given \a toolBarManager. Then,
1836 when exec() is called, the toolbar dialog will operate using the
1837 given \a toolBarManager.
1838*/
1839void QtToolBarDialog::setToolBarManager(QtToolBarManager *toolBarManager)
1840{
1841 if (d_ptr->toolBarManager == toolBarManager->d_ptr->manager)
1842 return;
1843 if (isVisible())
1844 d_ptr->clearOld();
1845 d_ptr->toolBarManager = toolBarManager->d_ptr->manager;
1846 if (isVisible())
1847 d_ptr->fillNew();
1848}
1849
1850/*!
1851 \reimp
1852*/
1853void QtToolBarDialog::showEvent(QShowEvent *event)
1854{
1855 if (!event->spontaneous())
1856 d_ptr->fillNew();
1857}
1858
1859/*!
1860 \reimp
1861*/
1862void QtToolBarDialog::hideEvent(QHideEvent *event)
1863{
1864 if (!event->spontaneous())
1865 d_ptr->clearOld();
1866}
1867
1868QT_END_NAMESPACE
1869
1870#include "moc_qttoolbardialog.cpp"
1871#include "qttoolbardialog.moc"
Note: See TracBrowser for help on using the repository browser.