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

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

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