6#include "ui_qttoolbardialog.h"
8#include <QtWidgets/qmainwindow.h>
9#include <QtWidgets/qpushbutton.h>
10#include <QtWidgets/qtoolbar.h>
12#include <QtGui/qaction.h>
13#include <QtGui/qevent.h>
15#include <QtCore/qmap.h>
16#include <QtCore/qhash.h>
17#include <QtCore/qset.h>
23using namespace Qt::StringLiterals;
44 void addAction(QAction *action,
const QString &category);
52
53
54
67 void setToolBars(
const QHash<QToolBar *, QList<QAction *>> &actions);
68 void setToolBar(QToolBar *toolBar,
const QList<QAction *> &actions);
84
85
86
87
88
92 QScopedPointer<QtFullToolBarManagerPrivate> d_ptr;
100 Q_DECLARE_PUBLIC(QtFullToolBarManager)
138 return widgetActions.value(action,
nullptr);
144 auto itToolBar = actions.constBegin();
145 while (itToolBar != actions.constEnd()) {
146 QToolBar *toolBar = itToolBar.key();
147 auto newActions = toolBars.value(toolBar);
148 auto newActionsWithSeparators = toolBarsWithSeparators.value(toolBar);
150 QList<QAction *> removedActions;
151 const auto actionList = itToolBar.value();
152 for (QAction *action : actionList) {
153 if (newActions.contains(action) && toolBarWidgetAction(action) == toolBar) {
154 newActions.removeAll(action);
155 newActionsWithSeparators.removeAll(action);
156 removedActions.append(action);
162 toolBars.insert(toolBar, newActions);
163 toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
164 for (QAction *oldAction : std::as_const(removedActions)) {
165 widgetActions.insert(oldAction, 0);
166 actionToToolBars[oldAction].removeAll(toolBar);
175 stream << (uchar) ToolBarMarker;
176 stream << defaultToolBars.size();
177 auto itToolBar = defaultToolBars.constBegin();
178 while (itToolBar != defaultToolBars.constEnd()) {
179 QToolBar *tb = itToolBar.key();
180 if (tb->objectName().isEmpty()) {
181 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QToolBar "
182 "%p '%s', using 'windowTitle' instead",
183 tb, tb->windowTitle().toLocal8Bit().constData());
184 stream << tb->windowTitle();
186 stream << tb->objectName();
189 const auto actions = toolBars.value(tb);
190 stream << actions.size();
191 for (QAction *action : actions) {
193 if (action->objectName().isEmpty()) {
194 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QAction "
195 "%p '%s', using 'text' instead",
196 action, action->text().toLocal8Bit().constData());
197 stream << action->text();
199 stream << action->objectName();
209 stream << (uchar) CustomToolBarMarker;
210 stream << toolBars.size() - defaultToolBars.size();
211 itToolBar = toolBars.constBegin();
212 while (itToolBar != toolBars.constEnd()) {
213 QToolBar *tb = itToolBar.key();
214 if (!defaultToolBars.contains(tb)) {
215 stream << tb->objectName();
216 stream << tb->windowTitle();
218 stream << toolBars[tb].size();
220 const auto actions = toolBars.value(tb);
221 for (QAction *action : actions) {
223 if (action->objectName().isEmpty()) {
224 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QAction "
225 "%p '%s', using 'text' instead",
226 action, action->text().toLocal8Bit().constData());
227 stream << action->text();
229 stream << action->objectName();
249 for (
int i = 0; i < toolBars; i++) {
251 stream >> objectName;
253 stream >> actionCount;
254 QList<QAction *> actions;
255 for (
int j = 0; j < actionCount; j++) {
257 stream >> actionName;
259 if (actionName.isEmpty())
260 actions.append(
nullptr);
262 QAction *action = findAction(actionName);
264 actions.append(action);
268 QToolBar *toolBar = findDefaultToolBar(objectName);
270 q_ptr->setToolBar(toolBar, actions);
280 auto oldCustomToolBars = customToolBars;
283 for (
int i = 0; i < toolBars; i++) {
287 stream >> objectName;
288 stream >> toolBarName;
289 stream >> actionCount;
290 QList<QAction *> actions;
291 for (
int j = 0; j < actionCount; j++) {
293 stream >> actionName;
295 if (actionName.isEmpty())
296 actions.append(
nullptr);
298 QAction *action = findAction(actionName);
300 actions.append(action);
304 QToolBar *toolBar = toolBarByName(objectName);
306 toolBar->setWindowTitle(toolBarName);
307 oldCustomToolBars.removeAll(toolBar);
310 toolBar = q_ptr->createToolBar(toolBarName);
312 toolBar->setObjectName(objectName);
313 q_ptr->setToolBar(toolBar, actions);
316 for (QToolBar *toolBar : std::as_const(oldCustomToolBars))
317 q_ptr->deleteToolBar(toolBar);
323 auto itToolBar = defaultToolBars.constBegin();
324 while (itToolBar != defaultToolBars.constEnd()) {
325 QToolBar *tb = itToolBar.key();
326 if (tb->objectName() == objectName)
332 qWarning(
"QtToolBarManager::restoreState(): cannot find a QToolBar named "
333 "'%s', trying to match using 'windowTitle' instead.",
334 objectName.toLocal8Bit().constData());
336 itToolBar = defaultToolBars.constBegin();
337 while (itToolBar != defaultToolBars.constEnd()) {
338 QToolBar *tb = itToolBar.key();
339 if (tb->windowTitle() == objectName)
344 qWarning(
"QtToolBarManager::restoreState(): cannot find a QToolBar with "
345 "matching 'windowTitle' (looking for '%s').",
346 objectName.toLocal8Bit().constData());
354 std::find_if(allActions.cbegin(), allActions.cend(),
355 [&actionName] (
const QAction *a) {
return a->objectName() == actionName; });
356 if (it != allActions.cend())
358 qWarning(
"QtToolBarManager::restoreState(): cannot find a QAction named "
359 "'%s', trying to match using 'text' instead.",
360 actionName.toLocal8Bit().constData());
362 it =
std::find_if(allActions.cbegin(), allActions.cend(),
363 [&actionName] (
const QAction *a) {
return a->text() == actionName; });
364 if (it != allActions.cend())
366 qWarning(
"QtToolBarManager::restoreState(): cannot find a QAction with "
367 "matching 'text' (looking for '%s').",
368 actionName.toLocal8Bit().constData());
375 auto itToolBar = toolBars.constBegin();
376 while (itToolBar != toolBars.constEnd()) {
377 QToolBar *toolBar = itToolBar.key();
378 if (toolBar->objectName() == toolBarName)
389 : QObject(parent), d_ptr(
new QtFullToolBarManagerPrivate)
398 d_ptr->theMainWindow = mainWindow;
403 return d_ptr->theMainWindow;
408 d_ptr->categoryToActions[category] = QList<QAction *>();
413 return d_ptr->categoryToActions.contains(category);
418 return d_ptr->categoryToActions.keys();
423 const auto it = d_ptr->categoryToActions.constFind(category);
424 if (it != d_ptr->categoryToActions.constEnd())
431 return d_ptr->actionToCategory.value(action, {});
438 if (action->isSeparator())
440 if (d_ptr->allActions.contains(action))
442 if (qstrcmp(action->metaObject()->className(),
"QToolBarWidgetAction") == 0)
443 d_ptr->widgetActions.insert(action, 0);
445 d_ptr->regularActions.insert(action);
446 d_ptr->allActions.insert(action);
447 d_ptr->categoryToActions[category].append(action);
448 d_ptr->actionToCategory[action] = category;
453 if (!d_ptr->allActions.contains(action))
456 const auto toolBars = d_ptr->actionToToolBars[action];
457 for (QToolBar *toolBar : toolBars) {
458 d_ptr->toolBars[toolBar].removeAll(action);
459 d_ptr->toolBarsWithSeparators[toolBar].removeAll(action);
461 toolBar->removeAction(action);
464 auto itDefault = d_ptr->defaultToolBars.constBegin();
465 while (itDefault != d_ptr->defaultToolBars.constEnd()) {
466 if (itDefault.value().contains(action))
467 d_ptr->defaultToolBars[itDefault.key()].removeAll(action);
472 d_ptr->allActions.remove(action);
473 d_ptr->widgetActions.remove(action);
474 d_ptr->regularActions.remove(action);
475 d_ptr->actionToToolBars.remove(action);
477 QString category = d_ptr->actionToCategory.value(action);
478 d_ptr->actionToCategory.remove(action);
479 d_ptr->categoryToActions[category].removeAll(action);
481 if (d_ptr->categoryToActions[category].isEmpty())
482 d_ptr->categoryToActions.remove(category);
487 return d_ptr->allActions;
492 return d_ptr->widgetActions.contains(action);
499 if (d_ptr->toolBars.contains(toolBar))
503 QList<QAction *> newActionsWithSeparators;
504 QList<QAction *> newActions;
505 const auto actions = toolBar->actions();
506 for (QAction *action : actions) {
507 addAction(action, category);
508 if (d_ptr->widgetActions.contains(action))
509 d_ptr->widgetActions.insert(action, toolBar);
510 newActionsWithSeparators.append(action);
511 if (action->isSeparator())
514 d_ptr->actionToToolBars[action].append(toolBar);
515 newActions.append(action);
517 d_ptr->defaultToolBars.insert(toolBar, newActions);
519 d_ptr->toolBars.insert(toolBar, newActions);
520 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
525 if (!d_ptr->defaultToolBars.contains(toolBar))
528 const auto defaultActions = d_ptr->defaultToolBars[toolBar];
529 setToolBar(toolBar, QList<QAction *>());
530 for (QAction *action : defaultActions)
531 removeAction(action);
533 d_ptr->toolBars.remove(toolBar);
534 d_ptr->toolBarsWithSeparators.remove(toolBar);
535 d_ptr->defaultToolBars.remove(toolBar);
537 for (QAction *action : defaultActions) {
539 toolBar->insertAction(
nullptr, action);
541 toolBar->insertSeparator(
nullptr);
547 return d_ptr->defaultToolBars;
552 if (d_ptr->defaultToolBars.contains(toolBar))
561 auto *toolBar =
new QToolBar(toolBarName, mainWindow());
563 const QString prefix =
"_Custom_Toolbar_%1"_L1;
564 QString name = prefix.arg(i);
565 while (d_ptr->toolBarByName(name))
566 name = prefix.arg(++i);
567 toolBar->setObjectName(name);
569 d_ptr->customToolBars.append(toolBar);
570 d_ptr->toolBars.insert(toolBar, QList<QAction *>());
571 d_ptr->toolBarsWithSeparators.insert(toolBar, QList<QAction *>());
577 if (!d_ptr->toolBars.contains(toolBar))
579 if (d_ptr->defaultToolBars.contains(toolBar))
581 setToolBar(toolBar, QList<QAction *>());
582 d_ptr->customToolBars.removeAll(toolBar);
583 d_ptr->toolBars.remove(toolBar);
584 d_ptr->toolBarsWithSeparators.remove(toolBar);
590 if (d_ptr->toolBars.contains(toolBar))
591 return d_ptr->toolBars.value(toolBar);
597 auto it = actions.constBegin();
598 while (it != actions.constEnd()) {
599 setToolBar(it.key(), it.value());
608 if (!d_ptr->toolBars.contains(toolBar))
611 if (actions == d_ptr->toolBars[toolBar])
614 QHash<QToolBar *, QList<QAction *>> toRemove;
616 QList<QAction *> newActions;
617 for (QAction *action : actions) {
618 if (!action || (!newActions.contains(action) && d_ptr->allActions.contains(action)))
619 newActions.append(action);
621 QToolBar *oldToolBar = d_ptr->toolBarWidgetAction(action);
622 if (oldToolBar && oldToolBar != toolBar)
623 toRemove[oldToolBar].append(action);
626 d_ptr->removeWidgetActions(toRemove);
628 const auto oldActions = d_ptr->toolBarsWithSeparators.value(toolBar);
629 for (QAction *action : oldActions) {
631
632
633
634 if (d_ptr->toolBarWidgetAction(action) == toolBar)
635 d_ptr->widgetActions.insert(action, 0);
636 toolBar->removeAction(action);
637 if (action->isSeparator())
640 d_ptr->actionToToolBars[action].removeAll(toolBar);
643 QList<QAction *> newActionsWithSeparators;
644 for (QAction *action : std::as_const(newActions)) {
645 QAction *newAction =
nullptr;
647 newAction = toolBar->insertSeparator(
nullptr);
648 if (d_ptr->allActions.contains(action)) {
649 toolBar->insertAction(
nullptr, action);
651 d_ptr->actionToToolBars[action].append(toolBar);
653 newActionsWithSeparators.append(newAction);
655 d_ptr->toolBars.insert(toolBar, newActions);
656 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
661 return d_ptr->toolBars;
668 setToolBar(toolBar, defaultToolBars().value(toolBar));
673 setToolBars(defaultToolBars());
674 const auto oldCustomToolBars = d_ptr->customToolBars;
675 for (QToolBar *tb : oldCustomToolBars)
682 QDataStream stream(&data, QIODevice::WriteOnly);
685 d_ptr->saveState(stream);
691 QByteArray sd = state;
692 QDataStream stream(&sd, QIODevice::ReadOnly);
693 int marker = 0, v = 0;
698 return d_ptr->restoreState(stream);
705 Q_DECLARE_PUBLIC(QtToolBarManager)
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
746
747
749 : QObject(parent), d_ptr(
new QtToolBarManagerPrivate)
753 d_ptr->manager =
new QtFullToolBarManager(
this);
757
758
762
763
764
767 d_ptr->manager->setMainWindow(mainWindow);
771
772
775 return d_ptr->manager->mainWindow();
779
780
781
782
783
784
785
788 d_ptr->manager->addAction(action, category);
792
793
794
795
796
797
798
801 d_ptr->manager->removeAction(action);
805
806
807
808
809
810
811
812
813
814
815
816
817
820 d_ptr->manager->addDefaultToolBar(toolBar, category);
824
825
826
827
828
829
832 d_ptr->manager->removeDefaultToolBar(toolBar);
836
837
840 return d_ptr->manager->toolBarsActions().keys();
844
845
846
847
848
849
850
851
852
853
856
857
858
859
860
861
862
863
864
865
866
867
868
871 return d_ptr->manager->saveState(version);
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
894 return d_ptr->manager->restoreState(state, version);
913 { tbName = toolBarName; }
921 Q_DECLARE_PUBLIC(QtToolBarDialog)
981 auto *item =
new ToolBarItem(toolBar, toolBar->windowTitle());
982 allToolBarItems.insert(item);
989 allToolBarItems.insert(item);
995 if (!allToolBarItems.contains(item))
997 allToolBarItems.remove(item);
1003 ui.actionTree->clear();
1004 ui.toolBarList->clear();
1005 ui.currentToolBarList->clear();
1006 ui.removeButton->setEnabled(
false);
1007 ui.newButton->setEnabled(
false);
1008 ui.upButton->setEnabled(
false);
1009 ui.downButton->setEnabled(
false);
1010 ui.leftButton->setEnabled(
false);
1011 ui.rightButton->setEnabled(
false);
1013 actionToItem.clear();
1014 itemToAction.clear();
1015 toolBarToItem.clear();
1016 itemToToolBar.clear();
1017 actionToCurrentItem.clear();
1018 currentItemToAction.clear();
1019 widgetActionToToolBar.clear();
1020 toolBarToWidgetActions.clear();
1022 toolBarItems.clear();
1023 currentState.clear();
1024 createdItems.clear();
1025 removedItems.clear();
1026 qDeleteAll(allToolBarItems);
1027 allToolBarItems.clear();
1030 currentAction =
nullptr;
1038 auto *item =
new QTreeWidgetItem(ui.actionTree);
1039 item->setText(0, separatorText);
1040 ui.actionTree->setCurrentItem(item);
1041 currentAction = item;
1042 actionToItem.insert(0, item);
1043 itemToAction.insert(item, 0);
1045 for (
const QString &category : categories) {
1046 auto *categoryItem =
new QTreeWidgetItem(ui.actionTree);
1047 categoryItem->setText(0, category);
1048 const auto actions = toolBarManager->categoryActions(category);
1049 for (QAction *action : actions) {
1050 item =
new QTreeWidgetItem(categoryItem);
1051 item->setText(0, action->text());
1052 item->setIcon(0, action->icon());
1053 item->setTextAlignment(0, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1054 actionToItem.insert(action, item);
1055 itemToAction.insert(item, action);
1056 if (toolBarManager->isWidgetAction(action)) {
1057 item->setData(0, Qt::ForegroundRole, QColor(Qt::blue));
1058 widgetActionToToolBar.insert(action, 0);
1060 item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
1062 categoryItem->setExpanded(
true);
1067 auto it = toolBars.constBegin();
1068 while (it != toolBars.constEnd()) {
1069 QToolBar *toolBar = it.key();
1071 toolBarItems.insert(toolBar, tbItem);
1072 auto *item =
new QListWidgetItem(toolBar->windowTitle(), ui.toolBarList);
1073 toolBarToItem.insert(tbItem, item);
1074 itemToToolBar.insert(item, tbItem);
1075 const auto actions = it.value();
1076 for (QAction *action : actions) {
1077 if (toolBarManager->isWidgetAction(action)) {
1078 widgetActionToToolBar.insert(action, tbItem);
1079 toolBarToWidgetActions[tbItem].insert(action);
1082 currentState.insert(tbItem, actions);
1083 if (it == toolBars.constBegin())
1084 ui.toolBarList->setCurrentItem(item);
1086 item->setData(Qt::ForegroundRole, QColor(Qt::darkGreen));
1088 item->setFlags(item->flags() | Qt::ItemIsEditable);
1092 ui.toolBarList->sortItems();
1107 bool newEnabled =
false;
1108 bool removeEnabled =
false;
1109 bool renameEnabled =
false;
1110 bool upEnabled =
false;
1111 bool downEnabled =
false;
1112 bool leftEnabled =
false;
1113 bool rightEnabled =
false;
1118 renameEnabled = removeEnabled;
1119 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1120 if (currentToolBarAction) {
1121 int row = ui.currentToolBarList->row(currentToolBarAction);
1122 upEnabled = row > 0;
1123 downEnabled = row < ui.currentToolBarList->count() - 1;
1126 if (currentAction && currentToolBar)
1127 rightEnabled =
true;
1129 ui.newButton->setEnabled(newEnabled);
1130 ui.removeButton->setEnabled(removeEnabled);
1131 ui.renameButton->setEnabled(renameEnabled);
1132 ui.upButton->setEnabled(upEnabled);
1133 ui.downButton->setEnabled(downEnabled);
1134 ui.leftButton->setEnabled(leftEnabled);
1135 ui.rightButton->setEnabled(rightEnabled);
1140 QString toolBarName = QtToolBarDialog::tr(
"Custom Toolbar");
1143 currentState.insert(item, QList<QAction *>());
1144 createdItems.insert(item);
1145 auto *i =
new QListWidgetItem(toolBarName, ui.toolBarList);
1146 i->setFlags(i->flags() | Qt::ItemIsEditable);
1147 ui.toolBarList->setCurrentItem(i);
1148 itemToToolBar.insert(i, item);
1149 toolBarToItem.insert(item, i);
1150 ui.toolBarList->sortItems();
1151 ui.toolBarList->setCurrentItem(i);
1152 currentToolBarChanged(i);
1162 if (!toolBarToItem.contains(item))
1164 QListWidgetItem *i = toolBarToItem.value(item);
1165 bool wasCurrent =
false;
1166 if (i == ui.toolBarList->currentItem())
1168 int row = ui.toolBarList->row(i);
1169 const auto itToolBar = toolBarToWidgetActions.find(item);
1170 if (itToolBar != toolBarToWidgetActions.end()) {
1171 for (QAction *action : std::as_const(itToolBar.value()))
1172 widgetActionToToolBar.insert(action, 0);
1173 toolBarToWidgetActions.erase(itToolBar);
1176 currentState.remove(item);
1177 createdItems.remove(item);
1178 toolBarToItem.remove(item);
1179 itemToToolBar.remove(i);
1182 removedItems.insert(item);
1186 if (row == ui.toolBarList->count())
1191 ui.toolBarList->setCurrentRow(row);
1198 QListWidgetItem *i = ui.toolBarList->currentItem();
1208 auto itToolBar = defaultToolBars.constBegin();
1209 while (itToolBar != defaultToolBars.constEnd()) {
1210 QToolBar *toolBar = itToolBar.key();
1211 ToolBarItem *toolBarItem = toolBarItems.value(toolBar);
1213 const auto tbwit = toolBarToWidgetActions.find(toolBarItem);
1214 if (tbwit != toolBarToWidgetActions.end()) {
1215 for (QAction *action : std::as_const(tbwit.value()))
1216 widgetActionToToolBar.insert(action, 0);
1217 toolBarToWidgetActions.erase(tbwit);
1220 currentState.remove(toolBarItem);
1222 for (QAction *action : itToolBar.value()) {
1223 if (toolBarManager->isWidgetAction(action)) {
1224 ToolBarItem *otherToolBar = widgetActionToToolBar.value(action);
1226 toolBarToWidgetActions[otherToolBar].remove(action);
1227 currentState[otherToolBar].removeAll(action);
1229 widgetActionToToolBar.insert(action, toolBarItem);
1230 toolBarToWidgetActions[toolBarItem].insert(action);
1233 currentState.insert(toolBarItem, itToolBar.value());
1237 currentToolBarChanged(toolBarToItem.value(currentToolBar));
1239 const auto toolBars = currentState.keys();
1240 for (ToolBarItem *tb : toolBars)
1252 const auto toolBars = currentState;
1253 auto itToolBar = toolBars.constBegin();
1254 while (itToolBar != toolBars.constEnd()) {
1259 toolBar->setWindowTitle(item->toolBarName());
1265 const QSet<ToolBarItem *> toRemove = removedItems;
1266 for (ToolBarItem *item : toRemove) {
1267 QToolBar *toolBar = item->toolBar();
1268 removedItems.remove(item);
1269 currentState.remove(item);
1272 toolBarManager->deleteToolBar(toolBar);
1275 const QSet<ToolBarItem *> toCreate = createdItems;
1276 for (ToolBarItem *item : toCreate) {
1277 QString toolBarName = item->toolBarName();
1278 createdItems.remove(item);
1279 const auto actions = currentState.value(item);
1280 QToolBar *toolBar = toolBarManager->createToolBar(toolBarName);
1281 item->setToolBar(toolBar);
1282 toolBarManager->setToolBar(toolBar, actions);
1288 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1289 if (!currentToolBarAction)
1291 int row = ui.currentToolBarList->row(currentToolBarAction);
1294 ui.currentToolBarList->takeItem(row);
1295 int newRow = row - 1;
1296 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1297 auto actions = currentState.value(currentToolBar);
1298 QAction *action = actions.at(row);
1299 actions.removeAt(row);
1300 actions.insert(newRow, action);
1301 currentState.insert(currentToolBar, actions);
1302 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1308 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1309 if (!currentToolBarAction)
1311 int row = ui.currentToolBarList->row(currentToolBarAction);
1312 if (row == ui.currentToolBarList->count() - 1)
1314 ui.currentToolBarList->takeItem(row);
1315 int newRow = row + 1;
1316 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1317 auto actions = currentState.value(currentToolBar);
1318 QAction *action = actions.at(row);
1319 actions.removeAt(row);
1320 actions.insert(newRow, action);
1321 currentState.insert(currentToolBar, actions);
1322 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1328 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1329 if (!currentToolBarAction)
1331 int row = ui.currentToolBarList->row(currentToolBarAction);
1332 currentState[currentToolBar].removeAt(row);
1333 QAction *action = currentItemToAction.value(currentToolBarAction);
1334 if (widgetActionToToolBar.contains(action)) {
1335 ToolBarItem *item = widgetActionToToolBar.value(action);
1337 toolBarToWidgetActions[item].remove(action);
1338 if (toolBarToWidgetActions[item].isEmpty())
1339 toolBarToWidgetActions.remove(item);
1341 widgetActionToToolBar.insert(action, 0);
1344 actionToCurrentItem.remove(action);
1345 currentItemToAction.remove(currentToolBarAction);
1346 delete currentToolBarAction;
1347 if (row == ui.currentToolBarList->count())
1350 QListWidgetItem *item = ui.currentToolBarList->item(row);
1351 ui.currentToolBarList->setCurrentItem(item);
1362 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1364 QAction *action = itemToAction.value(currentAction);
1365 QListWidgetItem *item =
nullptr;
1367 if (currentState[currentToolBar].contains(action)) {
1368 item = actionToCurrentItem.value(action);
1369 if (item == currentToolBarAction)
1371 int row = ui.currentToolBarList->row(item);
1372 ui.currentToolBarList->takeItem(row);
1373 currentState[currentToolBar].removeAt(row);
1376 item =
new QListWidgetItem(action->text());
1377 item->setIcon(action->icon());
1378 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1379 currentItemToAction.insert(item, action);
1380 actionToCurrentItem.insert(action, item);
1381 if (widgetActionToToolBar.contains(action)) {
1382 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1383 ToolBarItem *toolBar = widgetActionToToolBar.value(action);
1385 currentState[toolBar].removeAll(action);
1386 toolBarToWidgetActions[toolBar].remove(action);
1387 if (toolBarToWidgetActions[toolBar].isEmpty())
1388 toolBarToWidgetActions.remove(toolBar);
1390 widgetActionToToolBar.insert(action, currentToolBar);
1391 toolBarToWidgetActions[currentToolBar].insert(action);
1395 item =
new QListWidgetItem(separatorText);
1396 currentItemToAction.insert(item, 0);
1399 int row = ui.currentToolBarList->count();
1400 if (currentToolBarAction) {
1401 row = ui.currentToolBarList->row(currentToolBarAction) + 1;
1403 ui.currentToolBarList->insertItem(row, item);
1404 currentState[currentToolBar].insert(row, action);
1405 ui.currentToolBarList->setCurrentItem(item);
1415 QListWidgetItem *item = toolBarToItem.value(currentToolBar);
1416 ui.toolBarList->editItem(item);
1427 tbItem->setToolBarName(item->text());
1433 if (itemToAction.contains(current))
1434 currentAction = current;
1436 currentAction =
nullptr;
1442 currentToolBar = itemToToolBar.value(current);
1443 ui.currentToolBarList->clear();
1444 actionToCurrentItem.clear();
1445 currentItemToAction.clear();
1450 const auto actions = currentState.value(currentToolBar);
1451 QListWidgetItem *first =
nullptr;
1452 for (QAction *action : actions) {
1453 QString actionName = separatorText;
1455 actionName = action->text();
1456 auto *item =
new QListWidgetItem(actionName, ui.currentToolBarList);
1458 item->setIcon(action->icon());
1459 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1460 actionToCurrentItem.insert(action, item);
1461 if (widgetActionToToolBar.contains(action))
1462 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1464 currentItemToAction.insert(item, action);
1469 ui.currentToolBarList->setCurrentItem(first);
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1662
1663
1664
1666 : QDialog(parent, flags), d_ptr(
new QtToolBarDialogPrivate)
1668 d_ptr->q_ptr =
this;
1669 d_ptr->ui.setupUi(
this);
1670 d_ptr->separatorText = tr(
"< S E P A R A T O R >");
1672 d_ptr->ui.actionTree->setColumnCount(1);
1673 d_ptr->ui.actionTree->setRootIsDecorated(
false);
1674 d_ptr->ui.actionTree->header()->hide();
1676 d_ptr->ui.upButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/up.png"_L1));
1677 d_ptr->ui.downButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/down.png"_L1));
1678 d_ptr->ui.leftButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/back.png"_L1));
1679 d_ptr->ui.rightButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/forward.png"_L1));
1680 d_ptr->ui.newButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/plus.png"_L1));
1681 d_ptr->ui.removeButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/minus.png"_L1));
1683 connect(d_ptr->ui.newButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->newClicked(); });
1684 connect(d_ptr->ui.removeButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->removeClicked(); });
1685 connect(d_ptr->ui.renameButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->renameClicked(); });
1686 connect(d_ptr->ui.upButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->upClicked(); });
1687 connect(d_ptr->ui.downButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->downClicked(); });
1688 connect(d_ptr->ui.leftButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->leftClicked(); });
1689 connect(d_ptr->ui.rightButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->rightClicked(); });
1691 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::RestoreDefaults),
1692 &QAbstractButton::clicked,
this, [
this] { d_ptr->defaultClicked(); });
1693 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok),
1694 &QAbstractButton::clicked,
this, [
this] { d_ptr->okClicked(); });
1695 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Apply),
1696 &QAbstractButton::clicked,
this, [
this] { d_ptr->applyClicked(); });
1697 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel),
1698 &QAbstractButton::clicked,
this, [
this] { d_ptr->cancelClicked(); });
1700 connect(d_ptr->ui.actionTree, &QTreeWidget::currentItemChanged,
1701 this, [
this](QTreeWidgetItem *current) { d_ptr->currentActionChanged(current); });
1702 connect(d_ptr->ui.currentToolBarList, &QListWidget::currentItemChanged,
1703 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarActionChanged(current); });
1704 connect(d_ptr->ui.toolBarList, &QListWidget::currentItemChanged,
1705 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarChanged(current); });
1707 connect(d_ptr->ui.actionTree, &QTreeWidget::itemDoubleClicked,
1708 this, [
this] { d_ptr->rightClicked(); });
1709 connect(d_ptr->ui.currentToolBarList, &QListWidget::itemDoubleClicked,
1710 this, [
this] { d_ptr->leftClicked(); });
1711 connect(d_ptr->ui.toolBarList, &QListWidget::itemChanged,
1712 this, [
this](QListWidgetItem *current) { d_ptr->toolBarRenamed(current); });
1716
1717
1724
1725
1726
1727
1730 if (d_ptr->toolBarManager == toolBarManager->d_ptr->manager)
1734 d_ptr->toolBarManager = toolBarManager->d_ptr->manager;
1740
1741
1744 if (!event->spontaneous())
1749
1750
1753 if (!event->spontaneous())
1759#include "moc_qttoolbardialog_p.cpp"
1760#include "qttoolbardialog.moc"