5#include "ui_qttoolbardialog.h"
7#include <QtWidgets/qmainwindow.h>
8#include <QtWidgets/qpushbutton.h>
9#include <QtWidgets/qtoolbar.h>
11#include <QtGui/qaction.h>
12#include <QtGui/qevent.h>
14#include <QtCore/qmap.h>
15#include <QtCore/qhash.h>
16#include <QtCore/qset.h>
22using namespace Qt::StringLiterals;
43 void addAction(QAction *action,
const QString &category);
51
52
53
66 void setToolBars(
const QHash<QToolBar *, QList<QAction *>> &actions);
67 void setToolBar(QToolBar *toolBar,
const QList<QAction *> &actions);
83
84
85
86
87
91 QScopedPointer<QtFullToolBarManagerPrivate> d_ptr;
99 Q_DECLARE_PUBLIC(QtFullToolBarManager)
137 return widgetActions.value(action,
nullptr);
143 auto itToolBar = actions.constBegin();
144 while (itToolBar != actions.constEnd()) {
145 QToolBar *toolBar = itToolBar.key();
146 auto newActions = toolBars.value(toolBar);
147 auto newActionsWithSeparators = toolBarsWithSeparators.value(toolBar);
149 QList<QAction *> removedActions;
150 const auto actionList = itToolBar.value();
151 for (QAction *action : actionList) {
152 if (newActions.contains(action) && toolBarWidgetAction(action) == toolBar) {
153 newActions.removeAll(action);
154 newActionsWithSeparators.removeAll(action);
155 removedActions.append(action);
161 toolBars.insert(toolBar, newActions);
162 toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
163 for (QAction *oldAction : std::as_const(removedActions)) {
164 widgetActions.insert(oldAction, 0);
165 actionToToolBars[oldAction].removeAll(toolBar);
174 stream << (uchar) ToolBarMarker;
175 stream << defaultToolBars.size();
176 auto itToolBar = defaultToolBars.constBegin();
177 while (itToolBar != defaultToolBars.constEnd()) {
178 QToolBar *tb = itToolBar.key();
179 if (tb->objectName().isEmpty()) {
180 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QToolBar "
181 "%p '%s', using 'windowTitle' instead",
182 tb, tb->windowTitle().toLocal8Bit().constData());
183 stream << tb->windowTitle();
185 stream << tb->objectName();
188 const auto actions = toolBars.value(tb);
189 stream << actions.size();
190 for (QAction *action : actions) {
192 if (action->objectName().isEmpty()) {
193 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QAction "
194 "%p '%s', using 'text' instead",
195 action, action->text().toLocal8Bit().constData());
196 stream << action->text();
198 stream << action->objectName();
208 stream << (uchar) CustomToolBarMarker;
209 stream << toolBars.size() - defaultToolBars.size();
210 itToolBar = toolBars.constBegin();
211 while (itToolBar != toolBars.constEnd()) {
212 QToolBar *tb = itToolBar.key();
213 if (!defaultToolBars.contains(tb)) {
214 stream << tb->objectName();
215 stream << tb->windowTitle();
217 stream << toolBars[tb].size();
219 const auto actions = toolBars.value(tb);
220 for (QAction *action : actions) {
222 if (action->objectName().isEmpty()) {
223 qWarning(
"QtToolBarManager::saveState(): 'objectName' not set for QAction "
224 "%p '%s', using 'text' instead",
225 action, action->text().toLocal8Bit().constData());
226 stream << action->text();
228 stream << action->objectName();
248 for (
int i = 0; i < toolBars; i++) {
250 stream >> objectName;
252 stream >> actionCount;
253 QList<QAction *> actions;
254 for (
int j = 0; j < actionCount; j++) {
256 stream >> actionName;
258 if (actionName.isEmpty())
259 actions.append(
nullptr);
261 QAction *action = findAction(actionName);
263 actions.append(action);
267 QToolBar *toolBar = findDefaultToolBar(objectName);
269 q_ptr->setToolBar(toolBar, actions);
279 auto oldCustomToolBars = customToolBars;
282 for (
int i = 0; i < toolBars; i++) {
286 stream >> objectName;
287 stream >> toolBarName;
288 stream >> actionCount;
289 QList<QAction *> actions;
290 for (
int j = 0; j < actionCount; j++) {
292 stream >> actionName;
294 if (actionName.isEmpty())
295 actions.append(
nullptr);
297 QAction *action = findAction(actionName);
299 actions.append(action);
303 QToolBar *toolBar = toolBarByName(objectName);
305 toolBar->setWindowTitle(toolBarName);
306 oldCustomToolBars.removeAll(toolBar);
309 toolBar = q_ptr->createToolBar(toolBarName);
311 toolBar->setObjectName(objectName);
312 q_ptr->setToolBar(toolBar, actions);
315 for (QToolBar *toolBar : std::as_const(oldCustomToolBars))
316 q_ptr->deleteToolBar(toolBar);
322 auto itToolBar = defaultToolBars.constBegin();
323 while (itToolBar != defaultToolBars.constEnd()) {
324 QToolBar *tb = itToolBar.key();
325 if (tb->objectName() == objectName)
331 qWarning(
"QtToolBarManager::restoreState(): cannot find a QToolBar named "
332 "'%s', trying to match using 'windowTitle' instead.",
333 objectName.toLocal8Bit().constData());
335 itToolBar = defaultToolBars.constBegin();
336 while (itToolBar != defaultToolBars.constEnd()) {
337 QToolBar *tb = itToolBar.key();
338 if (tb->windowTitle() == objectName)
343 qWarning(
"QtToolBarManager::restoreState(): cannot find a QToolBar with "
344 "matching 'windowTitle' (looking for '%s').",
345 objectName.toLocal8Bit().constData());
353 std::find_if(allActions.cbegin(), allActions.cend(),
354 [&actionName] (
const QAction *a) {
return a->objectName() == actionName; });
355 if (it != allActions.cend())
357 qWarning(
"QtToolBarManager::restoreState(): cannot find a QAction named "
358 "'%s', trying to match using 'text' instead.",
359 actionName.toLocal8Bit().constData());
361 it =
std::find_if(allActions.cbegin(), allActions.cend(),
362 [&actionName] (
const QAction *a) {
return a->text() == actionName; });
363 if (it != allActions.cend())
365 qWarning(
"QtToolBarManager::restoreState(): cannot find a QAction with "
366 "matching 'text' (looking for '%s').",
367 actionName.toLocal8Bit().constData());
374 auto itToolBar = toolBars.constBegin();
375 while (itToolBar != toolBars.constEnd()) {
376 QToolBar *toolBar = itToolBar.key();
377 if (toolBar->objectName() == toolBarName)
388 : QObject(parent), d_ptr(
new QtFullToolBarManagerPrivate)
397 d_ptr->theMainWindow = mainWindow;
402 return d_ptr->theMainWindow;
407 d_ptr->categoryToActions[category] = QList<QAction *>();
412 return d_ptr->categoryToActions.contains(category);
417 return d_ptr->categoryToActions.keys();
422 const auto it = d_ptr->categoryToActions.constFind(category);
423 if (it != d_ptr->categoryToActions.constEnd())
430 return d_ptr->actionToCategory.value(action, {});
437 if (action->isSeparator())
439 if (d_ptr->allActions.contains(action))
441 if (qstrcmp(action->metaObject()->className(),
"QToolBarWidgetAction") == 0)
442 d_ptr->widgetActions.insert(action, 0);
444 d_ptr->regularActions.insert(action);
445 d_ptr->allActions.insert(action);
446 d_ptr->categoryToActions[category].append(action);
447 d_ptr->actionToCategory[action] = category;
452 if (!d_ptr->allActions.contains(action))
455 const auto toolBars = d_ptr->actionToToolBars[action];
456 for (QToolBar *toolBar : toolBars) {
457 d_ptr->toolBars[toolBar].removeAll(action);
458 d_ptr->toolBarsWithSeparators[toolBar].removeAll(action);
460 toolBar->removeAction(action);
463 auto itDefault = d_ptr->defaultToolBars.constBegin();
464 while (itDefault != d_ptr->defaultToolBars.constEnd()) {
465 if (itDefault.value().contains(action))
466 d_ptr->defaultToolBars[itDefault.key()].removeAll(action);
471 d_ptr->allActions.remove(action);
472 d_ptr->widgetActions.remove(action);
473 d_ptr->regularActions.remove(action);
474 d_ptr->actionToToolBars.remove(action);
476 QString category = d_ptr->actionToCategory.value(action);
477 d_ptr->actionToCategory.remove(action);
478 d_ptr->categoryToActions[category].removeAll(action);
480 if (d_ptr->categoryToActions[category].isEmpty())
481 d_ptr->categoryToActions.remove(category);
486 return d_ptr->allActions;
491 return d_ptr->widgetActions.contains(action);
498 if (d_ptr->toolBars.contains(toolBar))
502 QList<QAction *> newActionsWithSeparators;
503 QList<QAction *> newActions;
504 const auto actions = toolBar->actions();
505 for (QAction *action : actions) {
506 addAction(action, category);
507 if (d_ptr->widgetActions.contains(action))
508 d_ptr->widgetActions.insert(action, toolBar);
509 newActionsWithSeparators.append(action);
510 if (action->isSeparator())
513 d_ptr->actionToToolBars[action].append(toolBar);
514 newActions.append(action);
516 d_ptr->defaultToolBars.insert(toolBar, newActions);
518 d_ptr->toolBars.insert(toolBar, newActions);
519 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
524 if (!d_ptr->defaultToolBars.contains(toolBar))
527 const auto defaultActions = d_ptr->defaultToolBars[toolBar];
528 setToolBar(toolBar, QList<QAction *>());
529 for (QAction *action : defaultActions)
530 removeAction(action);
532 d_ptr->toolBars.remove(toolBar);
533 d_ptr->toolBarsWithSeparators.remove(toolBar);
534 d_ptr->defaultToolBars.remove(toolBar);
536 for (QAction *action : defaultActions) {
538 toolBar->insertAction(
nullptr, action);
540 toolBar->insertSeparator(
nullptr);
546 return d_ptr->defaultToolBars;
551 if (d_ptr->defaultToolBars.contains(toolBar))
560 auto *toolBar =
new QToolBar(toolBarName, mainWindow());
562 const QString prefix =
"_Custom_Toolbar_%1"_L1;
563 QString name = prefix.arg(i);
564 while (d_ptr->toolBarByName(name))
565 name = prefix.arg(++i);
566 toolBar->setObjectName(name);
568 d_ptr->customToolBars.append(toolBar);
569 d_ptr->toolBars.insert(toolBar, QList<QAction *>());
570 d_ptr->toolBarsWithSeparators.insert(toolBar, QList<QAction *>());
576 if (!d_ptr->toolBars.contains(toolBar))
578 if (d_ptr->defaultToolBars.contains(toolBar))
580 setToolBar(toolBar, QList<QAction *>());
581 d_ptr->customToolBars.removeAll(toolBar);
582 d_ptr->toolBars.remove(toolBar);
583 d_ptr->toolBarsWithSeparators.remove(toolBar);
589 if (d_ptr->toolBars.contains(toolBar))
590 return d_ptr->toolBars.value(toolBar);
596 auto it = actions.constBegin();
597 while (it != actions.constEnd()) {
598 setToolBar(it.key(), it.value());
607 if (!d_ptr->toolBars.contains(toolBar))
610 if (actions == d_ptr->toolBars[toolBar])
613 QHash<QToolBar *, QList<QAction *>> toRemove;
615 QList<QAction *> newActions;
616 for (QAction *action : actions) {
617 if (!action || (!newActions.contains(action) && d_ptr->allActions.contains(action)))
618 newActions.append(action);
620 QToolBar *oldToolBar = d_ptr->toolBarWidgetAction(action);
621 if (oldToolBar && oldToolBar != toolBar)
622 toRemove[oldToolBar].append(action);
625 d_ptr->removeWidgetActions(toRemove);
627 const auto oldActions = d_ptr->toolBarsWithSeparators.value(toolBar);
628 for (QAction *action : oldActions) {
630
631
632
633 if (d_ptr->toolBarWidgetAction(action) == toolBar)
634 d_ptr->widgetActions.insert(action, 0);
635 toolBar->removeAction(action);
636 if (action->isSeparator())
639 d_ptr->actionToToolBars[action].removeAll(toolBar);
642 QList<QAction *> newActionsWithSeparators;
643 for (QAction *action : std::as_const(newActions)) {
644 QAction *newAction =
nullptr;
646 newAction = toolBar->insertSeparator(
nullptr);
647 if (d_ptr->allActions.contains(action)) {
648 toolBar->insertAction(
nullptr, action);
650 d_ptr->actionToToolBars[action].append(toolBar);
652 newActionsWithSeparators.append(newAction);
654 d_ptr->toolBars.insert(toolBar, newActions);
655 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
660 return d_ptr->toolBars;
667 setToolBar(toolBar, defaultToolBars().value(toolBar));
672 setToolBars(defaultToolBars());
673 const auto oldCustomToolBars = d_ptr->customToolBars;
674 for (QToolBar *tb : oldCustomToolBars)
681 QDataStream stream(&data, QIODevice::WriteOnly);
684 d_ptr->saveState(stream);
690 QByteArray sd = state;
691 QDataStream stream(&sd, QIODevice::ReadOnly);
692 int marker = 0, v = 0;
697 return d_ptr->restoreState(stream);
704 Q_DECLARE_PUBLIC(QtToolBarManager)
712
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
745
746
748 : QObject(parent), d_ptr(
new QtToolBarManagerPrivate)
752 d_ptr->manager =
new QtFullToolBarManager(
this);
756
757
761
762
763
766 d_ptr->manager->setMainWindow(mainWindow);
770
771
774 return d_ptr->manager->mainWindow();
778
779
780
781
782
783
784
787 d_ptr->manager->addAction(action, category);
791
792
793
794
795
796
797
800 d_ptr->manager->removeAction(action);
804
805
806
807
808
809
810
811
812
813
814
815
816
819 d_ptr->manager->addDefaultToolBar(toolBar, category);
823
824
825
826
827
828
831 d_ptr->manager->removeDefaultToolBar(toolBar);
835
836
839 return d_ptr->manager->toolBarsActions().keys();
843
844
845
846
847
848
849
850
851
852
855
856
857
858
859
860
861
862
863
864
865
866
867
870 return d_ptr->manager->saveState(version);
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
893 return d_ptr->manager->restoreState(state, version);
912 { tbName = toolBarName; }
920 Q_DECLARE_PUBLIC(QtToolBarDialog)
980 auto *item =
new ToolBarItem(toolBar, toolBar->windowTitle());
981 allToolBarItems.insert(item);
988 allToolBarItems.insert(item);
994 if (!allToolBarItems.contains(item))
996 allToolBarItems.remove(item);
1002 ui.actionTree->clear();
1003 ui.toolBarList->clear();
1004 ui.currentToolBarList->clear();
1005 ui.removeButton->setEnabled(
false);
1006 ui.newButton->setEnabled(
false);
1007 ui.upButton->setEnabled(
false);
1008 ui.downButton->setEnabled(
false);
1009 ui.leftButton->setEnabled(
false);
1010 ui.rightButton->setEnabled(
false);
1012 actionToItem.clear();
1013 itemToAction.clear();
1014 toolBarToItem.clear();
1015 itemToToolBar.clear();
1016 actionToCurrentItem.clear();
1017 currentItemToAction.clear();
1018 widgetActionToToolBar.clear();
1019 toolBarToWidgetActions.clear();
1021 toolBarItems.clear();
1022 currentState.clear();
1023 createdItems.clear();
1024 removedItems.clear();
1025 qDeleteAll(allToolBarItems);
1026 allToolBarItems.clear();
1029 currentAction =
nullptr;
1037 auto *item =
new QTreeWidgetItem(ui.actionTree);
1038 item->setText(0, separatorText);
1039 ui.actionTree->setCurrentItem(item);
1040 currentAction = item;
1041 actionToItem.insert(0, item);
1042 itemToAction.insert(item, 0);
1044 for (
const QString &category : categories) {
1045 auto *categoryItem =
new QTreeWidgetItem(ui.actionTree);
1046 categoryItem->setText(0, category);
1047 const auto actions = toolBarManager->categoryActions(category);
1048 for (QAction *action : actions) {
1049 item =
new QTreeWidgetItem(categoryItem);
1050 item->setText(0, action->text());
1051 item->setIcon(0, action->icon());
1052 item->setTextAlignment(0, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1053 actionToItem.insert(action, item);
1054 itemToAction.insert(item, action);
1055 if (toolBarManager->isWidgetAction(action)) {
1056 item->setData(0, Qt::ForegroundRole, QColor(Qt::blue));
1057 widgetActionToToolBar.insert(action, 0);
1059 item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
1061 categoryItem->setExpanded(
true);
1066 auto it = toolBars.constBegin();
1067 while (it != toolBars.constEnd()) {
1068 QToolBar *toolBar = it.key();
1070 toolBarItems.insert(toolBar, tbItem);
1071 auto *item =
new QListWidgetItem(toolBar->windowTitle(), ui.toolBarList);
1072 toolBarToItem.insert(tbItem, item);
1073 itemToToolBar.insert(item, tbItem);
1074 const auto actions = it.value();
1075 for (QAction *action : actions) {
1076 if (toolBarManager->isWidgetAction(action)) {
1077 widgetActionToToolBar.insert(action, tbItem);
1078 toolBarToWidgetActions[tbItem].insert(action);
1081 currentState.insert(tbItem, actions);
1082 if (it == toolBars.constBegin())
1083 ui.toolBarList->setCurrentItem(item);
1085 item->setData(Qt::ForegroundRole, QColor(Qt::darkGreen));
1087 item->setFlags(item->flags() | Qt::ItemIsEditable);
1091 ui.toolBarList->sortItems();
1106 bool newEnabled =
false;
1107 bool removeEnabled =
false;
1108 bool renameEnabled =
false;
1109 bool upEnabled =
false;
1110 bool downEnabled =
false;
1111 bool leftEnabled =
false;
1112 bool rightEnabled =
false;
1117 renameEnabled = removeEnabled;
1118 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1119 if (currentToolBarAction) {
1120 int row = ui.currentToolBarList->row(currentToolBarAction);
1121 upEnabled = row > 0;
1122 downEnabled = row < ui.currentToolBarList->count() - 1;
1125 if (currentAction && currentToolBar)
1126 rightEnabled =
true;
1128 ui.newButton->setEnabled(newEnabled);
1129 ui.removeButton->setEnabled(removeEnabled);
1130 ui.renameButton->setEnabled(renameEnabled);
1131 ui.upButton->setEnabled(upEnabled);
1132 ui.downButton->setEnabled(downEnabled);
1133 ui.leftButton->setEnabled(leftEnabled);
1134 ui.rightButton->setEnabled(rightEnabled);
1139 QString toolBarName = QtToolBarDialog::tr(
"Custom Toolbar");
1142 currentState.insert(item, QList<QAction *>());
1143 createdItems.insert(item);
1144 auto *i =
new QListWidgetItem(toolBarName, ui.toolBarList);
1145 i->setFlags(i->flags() | Qt::ItemIsEditable);
1146 ui.toolBarList->setCurrentItem(i);
1147 itemToToolBar.insert(i, item);
1148 toolBarToItem.insert(item, i);
1149 ui.toolBarList->sortItems();
1150 ui.toolBarList->setCurrentItem(i);
1151 currentToolBarChanged(i);
1161 if (!toolBarToItem.contains(item))
1163 QListWidgetItem *i = toolBarToItem.value(item);
1164 bool wasCurrent =
false;
1165 if (i == ui.toolBarList->currentItem())
1167 int row = ui.toolBarList->row(i);
1168 const auto itToolBar = toolBarToWidgetActions.find(item);
1169 if (itToolBar != toolBarToWidgetActions.end()) {
1170 for (QAction *action : std::as_const(itToolBar.value()))
1171 widgetActionToToolBar.insert(action, 0);
1172 toolBarToWidgetActions.erase(itToolBar);
1175 currentState.remove(item);
1176 createdItems.remove(item);
1177 toolBarToItem.remove(item);
1178 itemToToolBar.remove(i);
1181 removedItems.insert(item);
1185 if (row == ui.toolBarList->count())
1190 ui.toolBarList->setCurrentRow(row);
1197 QListWidgetItem *i = ui.toolBarList->currentItem();
1207 auto itToolBar = defaultToolBars.constBegin();
1208 while (itToolBar != defaultToolBars.constEnd()) {
1209 QToolBar *toolBar = itToolBar.key();
1210 ToolBarItem *toolBarItem = toolBarItems.value(toolBar);
1212 const auto tbwit = toolBarToWidgetActions.find(toolBarItem);
1213 if (tbwit != toolBarToWidgetActions.end()) {
1214 for (QAction *action : std::as_const(tbwit.value()))
1215 widgetActionToToolBar.insert(action, 0);
1216 toolBarToWidgetActions.erase(tbwit);
1219 currentState.remove(toolBarItem);
1221 for (QAction *action : itToolBar.value()) {
1222 if (toolBarManager->isWidgetAction(action)) {
1223 ToolBarItem *otherToolBar = widgetActionToToolBar.value(action);
1225 toolBarToWidgetActions[otherToolBar].remove(action);
1226 currentState[otherToolBar].removeAll(action);
1228 widgetActionToToolBar.insert(action, toolBarItem);
1229 toolBarToWidgetActions[toolBarItem].insert(action);
1232 currentState.insert(toolBarItem, itToolBar.value());
1236 currentToolBarChanged(toolBarToItem.value(currentToolBar));
1238 const auto toolBars = currentState.keys();
1239 for (ToolBarItem *tb : toolBars)
1251 const auto toolBars = currentState;
1252 auto itToolBar = toolBars.constBegin();
1253 while (itToolBar != toolBars.constEnd()) {
1258 toolBar->setWindowTitle(item->toolBarName());
1264 const QSet<ToolBarItem *> toRemove = removedItems;
1265 for (ToolBarItem *item : toRemove) {
1266 QToolBar *toolBar = item->toolBar();
1267 removedItems.remove(item);
1268 currentState.remove(item);
1271 toolBarManager->deleteToolBar(toolBar);
1274 const QSet<ToolBarItem *> toCreate = createdItems;
1275 for (ToolBarItem *item : toCreate) {
1276 QString toolBarName = item->toolBarName();
1277 createdItems.remove(item);
1278 const auto actions = currentState.value(item);
1279 QToolBar *toolBar = toolBarManager->createToolBar(toolBarName);
1280 item->setToolBar(toolBar);
1281 toolBarManager->setToolBar(toolBar, actions);
1287 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1288 if (!currentToolBarAction)
1290 int row = ui.currentToolBarList->row(currentToolBarAction);
1293 ui.currentToolBarList->takeItem(row);
1294 int newRow = row - 1;
1295 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1296 auto actions = currentState.value(currentToolBar);
1297 QAction *action = actions.at(row);
1298 actions.removeAt(row);
1299 actions.insert(newRow, action);
1300 currentState.insert(currentToolBar, actions);
1301 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1307 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1308 if (!currentToolBarAction)
1310 int row = ui.currentToolBarList->row(currentToolBarAction);
1311 if (row == ui.currentToolBarList->count() - 1)
1313 ui.currentToolBarList->takeItem(row);
1314 int newRow = row + 1;
1315 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1316 auto actions = currentState.value(currentToolBar);
1317 QAction *action = actions.at(row);
1318 actions.removeAt(row);
1319 actions.insert(newRow, action);
1320 currentState.insert(currentToolBar, actions);
1321 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1327 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1328 if (!currentToolBarAction)
1330 int row = ui.currentToolBarList->row(currentToolBarAction);
1331 currentState[currentToolBar].removeAt(row);
1332 QAction *action = currentItemToAction.value(currentToolBarAction);
1333 if (widgetActionToToolBar.contains(action)) {
1334 ToolBarItem *item = widgetActionToToolBar.value(action);
1336 toolBarToWidgetActions[item].remove(action);
1337 if (toolBarToWidgetActions[item].isEmpty())
1338 toolBarToWidgetActions.remove(item);
1340 widgetActionToToolBar.insert(action, 0);
1343 actionToCurrentItem.remove(action);
1344 currentItemToAction.remove(currentToolBarAction);
1345 delete currentToolBarAction;
1346 if (row == ui.currentToolBarList->count())
1349 QListWidgetItem *item = ui.currentToolBarList->item(row);
1350 ui.currentToolBarList->setCurrentItem(item);
1361 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1363 QAction *action = itemToAction.value(currentAction);
1364 QListWidgetItem *item =
nullptr;
1366 if (currentState[currentToolBar].contains(action)) {
1367 item = actionToCurrentItem.value(action);
1368 if (item == currentToolBarAction)
1370 int row = ui.currentToolBarList->row(item);
1371 ui.currentToolBarList->takeItem(row);
1372 currentState[currentToolBar].removeAt(row);
1375 item =
new QListWidgetItem(action->text());
1376 item->setIcon(action->icon());
1377 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1378 currentItemToAction.insert(item, action);
1379 actionToCurrentItem.insert(action, item);
1380 if (widgetActionToToolBar.contains(action)) {
1381 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1382 ToolBarItem *toolBar = widgetActionToToolBar.value(action);
1384 currentState[toolBar].removeAll(action);
1385 toolBarToWidgetActions[toolBar].remove(action);
1386 if (toolBarToWidgetActions[toolBar].isEmpty())
1387 toolBarToWidgetActions.remove(toolBar);
1389 widgetActionToToolBar.insert(action, currentToolBar);
1390 toolBarToWidgetActions[currentToolBar].insert(action);
1394 item =
new QListWidgetItem(separatorText);
1395 currentItemToAction.insert(item, 0);
1398 int row = ui.currentToolBarList->count();
1399 if (currentToolBarAction) {
1400 row = ui.currentToolBarList->row(currentToolBarAction) + 1;
1402 ui.currentToolBarList->insertItem(row, item);
1403 currentState[currentToolBar].insert(row, action);
1404 ui.currentToolBarList->setCurrentItem(item);
1414 QListWidgetItem *item = toolBarToItem.value(currentToolBar);
1415 ui.toolBarList->editItem(item);
1426 tbItem->setToolBarName(item->text());
1432 if (itemToAction.contains(current))
1433 currentAction = current;
1435 currentAction =
nullptr;
1441 currentToolBar = itemToToolBar.value(current);
1442 ui.currentToolBarList->clear();
1443 actionToCurrentItem.clear();
1444 currentItemToAction.clear();
1449 const auto actions = currentState.value(currentToolBar);
1450 QListWidgetItem *first =
nullptr;
1451 for (QAction *action : actions) {
1452 QString actionName = separatorText;
1454 actionName = action->text();
1455 auto *item =
new QListWidgetItem(actionName, ui.currentToolBarList);
1457 item->setIcon(action->icon());
1458 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1459 actionToCurrentItem.insert(action, item);
1460 if (widgetActionToToolBar.contains(action))
1461 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1463 currentItemToAction.insert(item, action);
1468 ui.currentToolBarList->setCurrentItem(first);
1484
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
1620
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
1661
1662
1663
1665 : QDialog(parent, flags), d_ptr(
new QtToolBarDialogPrivate)
1667 d_ptr->q_ptr =
this;
1668 d_ptr->ui.setupUi(
this);
1669 d_ptr->separatorText = tr(
"< S E P A R A T O R >");
1671 d_ptr->ui.actionTree->setColumnCount(1);
1672 d_ptr->ui.actionTree->setRootIsDecorated(
false);
1673 d_ptr->ui.actionTree->header()->hide();
1675 d_ptr->ui.upButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/up.png"_L1));
1676 d_ptr->ui.downButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/down.png"_L1));
1677 d_ptr->ui.leftButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/back.png"_L1));
1678 d_ptr->ui.rightButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/forward.png"_L1));
1679 d_ptr->ui.newButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/plus.png"_L1));
1680 d_ptr->ui.removeButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/minus.png"_L1));
1682 connect(d_ptr->ui.newButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->newClicked(); });
1683 connect(d_ptr->ui.removeButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->removeClicked(); });
1684 connect(d_ptr->ui.renameButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->renameClicked(); });
1685 connect(d_ptr->ui.upButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->upClicked(); });
1686 connect(d_ptr->ui.downButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->downClicked(); });
1687 connect(d_ptr->ui.leftButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->leftClicked(); });
1688 connect(d_ptr->ui.rightButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->rightClicked(); });
1690 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::RestoreDefaults),
1691 &QAbstractButton::clicked,
this, [
this] { d_ptr->defaultClicked(); });
1692 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok),
1693 &QAbstractButton::clicked,
this, [
this] { d_ptr->okClicked(); });
1694 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Apply),
1695 &QAbstractButton::clicked,
this, [
this] { d_ptr->applyClicked(); });
1696 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel),
1697 &QAbstractButton::clicked,
this, [
this] { d_ptr->cancelClicked(); });
1699 connect(d_ptr->ui.actionTree, &QTreeWidget::currentItemChanged,
1700 this, [
this](QTreeWidgetItem *current) { d_ptr->currentActionChanged(current); });
1701 connect(d_ptr->ui.currentToolBarList, &QListWidget::currentItemChanged,
1702 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarActionChanged(current); });
1703 connect(d_ptr->ui.toolBarList, &QListWidget::currentItemChanged,
1704 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarChanged(current); });
1706 connect(d_ptr->ui.actionTree, &QTreeWidget::itemDoubleClicked,
1707 this, [
this] { d_ptr->rightClicked(); });
1708 connect(d_ptr->ui.currentToolBarList, &QListWidget::itemDoubleClicked,
1709 this, [
this] { d_ptr->leftClicked(); });
1710 connect(d_ptr->ui.toolBarList, &QListWidget::itemChanged,
1711 this, [
this](QListWidgetItem *current) { d_ptr->toolBarRenamed(current); });
1715
1716
1723
1724
1725
1726
1729 if (d_ptr->toolBarManager == toolBarManager->d_ptr->manager)
1733 d_ptr->toolBarManager = toolBarManager->d_ptr->manager;
1739
1740
1743 if (!event->spontaneous())
1748
1749
1752 if (!event->spontaneous())
1758#include "moc_qttoolbardialog_p.cpp"
1759#include "qttoolbardialog.moc"