5#include "ui_qttoolbardialog.h"
10#include <QtGui/QAction>
11#include <QtGui/QtEvents>
12#include <QtWidgets/QMainWindow>
13#include <QtWidgets/QPushButton>
14#include <QtWidgets/QToolBar>
20using namespace Qt::StringLiterals;
41 void addAction(QAction *action,
const QString &category);
49
50
51
64 void setToolBars(
const QHash<QToolBar *, QList<QAction *>> &actions);
65 void setToolBar(QToolBar *toolBar,
const QList<QAction *> &actions);
81
82
83
84
85
89 QScopedPointer<QtFullToolBarManagerPrivate> d_ptr;
97 Q_DECLARE_PUBLIC(QtFullToolBarManager)
135 if (widgetActions.contains(action))
136 return widgetActions.value(action);
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())
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())
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)
399 d_ptr->theMainWindow = mainWindow;
404 return d_ptr->theMainWindow;
409 d_ptr->categoryToActions[category] = QList<QAction *>();
414 return d_ptr->categoryToActions.contains(category);
419 return d_ptr->categoryToActions.keys();
424 const auto it = d_ptr->categoryToActions.constFind(category);
425 if (it != d_ptr->categoryToActions.constEnd())
432 return d_ptr->actionToCategory.value(action, {});
439 if (action->isSeparator())
441 if (d_ptr->allActions.contains(action))
443 if (qstrcmp(action->metaObject()->className(),
"QToolBarWidgetAction") == 0)
444 d_ptr->widgetActions.insert(action, 0);
446 d_ptr->regularActions.insert(action);
447 d_ptr->allActions.insert(action);
448 d_ptr->categoryToActions[category].append(action);
449 d_ptr->actionToCategory[action] = category;
454 if (!d_ptr->allActions.contains(action))
457 const auto toolBars = d_ptr->actionToToolBars[action];
458 for (QToolBar *toolBar : toolBars) {
459 d_ptr->toolBars[toolBar].removeAll(action);
460 d_ptr->toolBarsWithSeparators[toolBar].removeAll(action);
462 toolBar->removeAction(action);
465 auto itDefault = d_ptr->defaultToolBars.constBegin();
466 while (itDefault != d_ptr->defaultToolBars.constEnd()) {
467 if (itDefault.value().contains(action))
468 d_ptr->defaultToolBars[itDefault.key()].removeAll(action);
473 d_ptr->allActions.remove(action);
474 d_ptr->widgetActions.remove(action);
475 d_ptr->regularActions.remove(action);
476 d_ptr->actionToToolBars.remove(action);
478 QString category = d_ptr->actionToCategory.value(action);
479 d_ptr->actionToCategory.remove(action);
480 d_ptr->categoryToActions[category].removeAll(action);
482 if (d_ptr->categoryToActions[category].isEmpty())
483 d_ptr->categoryToActions.remove(category);
488 return d_ptr->allActions;
493 if (d_ptr->widgetActions.contains(action))
502 if (d_ptr->toolBars.contains(toolBar))
506 QList<QAction *> newActionsWithSeparators;
507 QList<QAction *> newActions;
508 const auto actions = toolBar->actions();
509 for (QAction *action : actions) {
510 addAction(action, category);
511 if (d_ptr->widgetActions.contains(action))
512 d_ptr->widgetActions.insert(action, toolBar);
513 newActionsWithSeparators.append(action);
514 if (action->isSeparator())
517 d_ptr->actionToToolBars[action].append(toolBar);
518 newActions.append(action);
520 d_ptr->defaultToolBars.insert(toolBar, newActions);
522 d_ptr->toolBars.insert(toolBar, newActions);
523 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
528 if (!d_ptr->defaultToolBars.contains(toolBar))
531 const auto defaultActions = d_ptr->defaultToolBars[toolBar];
532 setToolBar(toolBar, QList<QAction *>());
533 for (QAction *action : defaultActions)
534 removeAction(action);
536 d_ptr->toolBars.remove(toolBar);
537 d_ptr->toolBarsWithSeparators.remove(toolBar);
538 d_ptr->defaultToolBars.remove(toolBar);
540 for (QAction *action : defaultActions) {
542 toolBar->insertAction(0, action);
544 toolBar->insertSeparator(0);
550 return d_ptr->defaultToolBars;
555 if (d_ptr->defaultToolBars.contains(toolBar))
564 QToolBar *toolBar =
new QToolBar(toolBarName, mainWindow());
566 const QString prefix =
"_Custom_Toolbar_%1"_L1;
567 QString name = prefix.arg(i);
568 while (d_ptr->toolBarByName(name))
569 name = prefix.arg(++i);
570 toolBar->setObjectName(name);
572 d_ptr->customToolBars.append(toolBar);
573 d_ptr->toolBars.insert(toolBar, QList<QAction *>());
574 d_ptr->toolBarsWithSeparators.insert(toolBar, QList<QAction *>());
580 if (!d_ptr->toolBars.contains(toolBar))
582 if (d_ptr->defaultToolBars.contains(toolBar))
584 setToolBar(toolBar, QList<QAction *>());
585 d_ptr->customToolBars.removeAll(toolBar);
586 d_ptr->toolBars.remove(toolBar);
587 d_ptr->toolBarsWithSeparators.remove(toolBar);
593 if (d_ptr->toolBars.contains(toolBar))
594 return d_ptr->toolBars.value(toolBar);
595 return QList<QAction *>();
600 auto it = actions.constBegin();
601 while (it != actions.constEnd()) {
602 setToolBar(it.key(), it.value());
611 if (!d_ptr->toolBars.contains(toolBar))
614 if (actions == d_ptr->toolBars[toolBar])
617 QHash<QToolBar *, QList<QAction *>> toRemove;
619 QList<QAction *> newActions;
620 for (QAction *action : actions) {
621 if (!action || (!newActions.contains(action) && d_ptr->allActions.contains(action)))
622 newActions.append(action);
624 QToolBar *oldToolBar = d_ptr->toolBarWidgetAction(action);
625 if (oldToolBar && oldToolBar != toolBar)
626 toRemove[oldToolBar].append(action);
629 d_ptr->removeWidgetActions(toRemove);
631 const auto oldActions = d_ptr->toolBarsWithSeparators.value(toolBar);
632 for (QAction *action : oldActions) {
634
635
636
637 if (d_ptr->toolBarWidgetAction(action) == toolBar)
638 d_ptr->widgetActions.insert(action, 0);
639 toolBar->removeAction(action);
640 if (action->isSeparator())
643 d_ptr->actionToToolBars[action].removeAll(toolBar);
646 QList<QAction *> newActionsWithSeparators;
647 for (QAction *action : std::as_const(newActions)) {
648 QAction *newAction =
nullptr;
650 newAction = toolBar->insertSeparator(0);
651 if (d_ptr->allActions.contains(action)) {
652 toolBar->insertAction(0, action);
654 d_ptr->actionToToolBars[action].append(toolBar);
656 newActionsWithSeparators.append(newAction);
658 d_ptr->toolBars.insert(toolBar, newActions);
659 d_ptr->toolBarsWithSeparators.insert(toolBar, newActionsWithSeparators);
664 return d_ptr->toolBars;
671 setToolBar(toolBar, defaultToolBars().value(toolBar));
676 setToolBars(defaultToolBars());
677 const auto oldCustomToolBars = d_ptr->customToolBars;
678 for (QToolBar *tb : oldCustomToolBars)
685 QDataStream stream(&data, QIODevice::WriteOnly);
688 d_ptr->saveState(stream);
694 QByteArray sd = state;
695 QDataStream stream(&sd, QIODevice::ReadOnly);
701 return d_ptr->restoreState(stream);
708 Q_DECLARE_PUBLIC(QtToolBarManager)
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
744
745
746
749
750
752 : QObject(parent), d_ptr(
new QtToolBarManagerPrivate)
756 d_ptr->manager =
new QtFullToolBarManager(
this);
760
761
767
768
769
772 d_ptr->manager->setMainWindow(mainWindow);
776
777
780 return d_ptr->manager->mainWindow();
784
785
786
787
788
789
790
793 d_ptr->manager->addAction(action, category);
797
798
799
800
801
802
803
806 d_ptr->manager->removeAction(action);
810
811
812
813
814
815
816
817
818
819
820
821
822
825 d_ptr->manager->addDefaultToolBar(toolBar, category);
829
830
831
832
833
834
837 d_ptr->manager->removeDefaultToolBar(toolBar);
841
842
845 return d_ptr->manager->toolBarsActions().keys();
849
850
851
852
853
854
855
856
857
858
861
862
863
864
865
866
867
868
869
870
871
872
873
876 return d_ptr->manager->saveState(version);
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
899 return d_ptr->manager->restoreState(state, version);
918 { tbName = toolBarName; }
926 Q_DECLARE_PUBLIC(QtToolBarDialog)
987 allToolBarItems.insert(item);
994 allToolBarItems.insert(item);
1000 if (!allToolBarItems.contains(item))
1002 allToolBarItems.remove(item);
1008 ui.actionTree->clear();
1009 ui.toolBarList->clear();
1010 ui.currentToolBarList->clear();
1011 ui.removeButton->setEnabled(
false);
1012 ui.newButton->setEnabled(
false);
1013 ui.upButton->setEnabled(
false);
1014 ui.downButton->setEnabled(
false);
1015 ui.leftButton->setEnabled(
false);
1016 ui.rightButton->setEnabled(
false);
1018 actionToItem.clear();
1019 itemToAction.clear();
1020 toolBarToItem.clear();
1021 itemToToolBar.clear();
1022 actionToCurrentItem.clear();
1023 currentItemToAction.clear();
1024 widgetActionToToolBar.clear();
1025 toolBarToWidgetActions.clear();
1027 toolBarItems.clear();
1028 currentState.clear();
1029 createdItems.clear();
1030 removedItems.clear();
1031 qDeleteAll(allToolBarItems);
1032 allToolBarItems.clear();
1035 currentAction =
nullptr;
1043 QTreeWidgetItem *item =
new QTreeWidgetItem(ui.actionTree);
1044 item->setText(0, separatorText);
1045 ui.actionTree->setCurrentItem(item);
1046 currentAction = item;
1047 actionToItem.insert(0, item);
1048 itemToAction.insert(item, 0);
1049 const QStringList categories = toolBarManager->categories();
1050 for (
const QString &category : categories) {
1051 QTreeWidgetItem *categoryItem =
new QTreeWidgetItem(ui.actionTree);
1052 categoryItem->setText(0, category);
1053 const auto actions = toolBarManager->categoryActions(category);
1054 for (QAction *action : actions) {
1055 item =
new QTreeWidgetItem(categoryItem);
1056 item->setText(0, action->text());
1057 item->setIcon(0, action->icon());
1058 item->setTextAlignment(0, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1059 actionToItem.insert(action, item);
1060 itemToAction.insert(item, action);
1061 if (toolBarManager->isWidgetAction(action)) {
1062 item->setData(0, Qt::ForegroundRole, QColor(Qt::blue));
1063 widgetActionToToolBar.insert(action, 0);
1065 item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
1067 categoryItem->setExpanded(
true);
1072 auto it = toolBars.constBegin();
1073 while (it != toolBars.constEnd()) {
1074 QToolBar *toolBar = it.key();
1076 toolBarItems.insert(toolBar, tbItem);
1077 QListWidgetItem *item =
new QListWidgetItem(toolBar->windowTitle(),
1079 toolBarToItem.insert(tbItem, item);
1080 itemToToolBar.insert(item, tbItem);
1081 const auto actions = it.value();
1082 for (QAction *action : actions) {
1083 if (toolBarManager->isWidgetAction(action)) {
1084 widgetActionToToolBar.insert(action, tbItem);
1085 toolBarToWidgetActions[tbItem].insert(action);
1088 currentState.insert(tbItem, actions);
1089 if (it == toolBars.constBegin())
1090 ui.toolBarList->setCurrentItem(item);
1091 if (isDefaultToolBar(tbItem))
1092 item->setData(Qt::ForegroundRole, QColor(Qt::darkGreen));
1094 item->setFlags(item->flags() | Qt::ItemIsEditable);
1098 ui.toolBarList->sortItems();
1113 bool newEnabled =
false;
1114 bool removeEnabled =
false;
1115 bool renameEnabled =
false;
1116 bool upEnabled =
false;
1117 bool downEnabled =
false;
1118 bool leftEnabled =
false;
1119 bool rightEnabled =
false;
1124 renameEnabled = removeEnabled;
1125 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1126 if (currentToolBarAction) {
1127 int row = ui.currentToolBarList->row(currentToolBarAction);
1128 upEnabled = row > 0;
1129 downEnabled = row < ui.currentToolBarList->count() - 1;
1132 if (currentAction && currentToolBar)
1133 rightEnabled =
true;
1135 ui.newButton->setEnabled(newEnabled);
1136 ui.removeButton->setEnabled(removeEnabled);
1137 ui.renameButton->setEnabled(renameEnabled);
1138 ui.upButton->setEnabled(upEnabled);
1139 ui.downButton->setEnabled(downEnabled);
1140 ui.leftButton->setEnabled(leftEnabled);
1141 ui.rightButton->setEnabled(rightEnabled);
1146 QString toolBarName = QtToolBarDialog::tr(
"Custom Toolbar");
1149 currentState.insert(item, QList<QAction *>());
1150 createdItems.insert(item);
1151 QListWidgetItem *i =
new QListWidgetItem(toolBarName, ui.toolBarList);
1152 i->setFlags(i->flags() | Qt::ItemIsEditable);
1153 ui.toolBarList->setCurrentItem(i);
1154 itemToToolBar.insert(i, item);
1155 toolBarToItem.insert(item, i);
1156 ui.toolBarList->sortItems();
1157 ui.toolBarList->setCurrentItem(i);
1158 currentToolBarChanged(i);
1168 if (!toolBarToItem.contains(item))
1170 QListWidgetItem *i = toolBarToItem.value(item);
1171 bool wasCurrent =
false;
1172 if (i == ui.toolBarList->currentItem())
1174 int row = ui.toolBarList->row(i);
1175 const auto itToolBar = toolBarToWidgetActions.find(item);
1176 if (itToolBar != toolBarToWidgetActions.end()) {
1177 for (QAction *action : std::as_const(itToolBar.value()))
1178 widgetActionToToolBar.insert(action, 0);
1179 toolBarToWidgetActions.erase(itToolBar);
1182 currentState.remove(item);
1183 createdItems.remove(item);
1184 toolBarToItem.remove(item);
1185 itemToToolBar.remove(i);
1188 removedItems.insert(item);
1192 if (row == ui.toolBarList->count())
1197 ui.toolBarList->setCurrentRow(row);
1204 QListWidgetItem *i = ui.toolBarList->currentItem();
1214 auto itToolBar = defaultToolBars.constBegin();
1215 while (itToolBar != defaultToolBars.constEnd()) {
1216 QToolBar *toolBar = itToolBar.key();
1217 ToolBarItem *toolBarItem = toolBarItems.value(toolBar);
1219 const auto tbwit = toolBarToWidgetActions.find(toolBarItem);
1220 if (tbwit != toolBarToWidgetActions.end()) {
1221 for (QAction *action : std::as_const(tbwit.value()))
1222 widgetActionToToolBar.insert(action, 0);
1223 toolBarToWidgetActions.erase(tbwit);
1226 currentState.remove(toolBarItem);
1228 for (QAction *action : itToolBar.value()) {
1229 if (toolBarManager->isWidgetAction(action)) {
1230 ToolBarItem *otherToolBar = widgetActionToToolBar.value(action);
1232 toolBarToWidgetActions[otherToolBar].remove(action);
1233 currentState[otherToolBar].removeAll(action);
1235 widgetActionToToolBar.insert(action, toolBarItem);
1236 toolBarToWidgetActions[toolBarItem].insert(action);
1239 currentState.insert(toolBarItem, itToolBar.value());
1243 currentToolBarChanged(toolBarToItem.value(currentToolBar));
1245 const auto toolBars = currentState.keys();
1246 for (ToolBarItem *tb : toolBars)
1258 const auto toolBars = currentState;
1259 auto itToolBar = toolBars.constBegin();
1260 while (itToolBar != toolBars.constEnd()) {
1265 toolBar->setWindowTitle(item->toolBarName());
1271 const QSet<ToolBarItem *> toRemove = removedItems;
1272 for (ToolBarItem *item : toRemove) {
1273 QToolBar *toolBar = item->toolBar();
1274 removedItems.remove(item);
1275 currentState.remove(item);
1278 toolBarManager->deleteToolBar(toolBar);
1281 const QSet<ToolBarItem *> toCreate = createdItems;
1282 for (ToolBarItem *item : toCreate) {
1283 QString toolBarName = item->toolBarName();
1284 createdItems.remove(item);
1285 const auto actions = currentState.value(item);
1286 QToolBar *toolBar = toolBarManager->createToolBar(toolBarName);
1287 item->setToolBar(toolBar);
1288 toolBarManager->setToolBar(toolBar, actions);
1294 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1295 if (!currentToolBarAction)
1297 int row = ui.currentToolBarList->row(currentToolBarAction);
1300 ui.currentToolBarList->takeItem(row);
1301 int newRow = row - 1;
1302 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1303 auto actions = currentState.value(currentToolBar);
1304 QAction *action = actions.at(row);
1305 actions.removeAt(row);
1306 actions.insert(newRow, action);
1307 currentState.insert(currentToolBar, actions);
1308 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1314 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1315 if (!currentToolBarAction)
1317 int row = ui.currentToolBarList->row(currentToolBarAction);
1318 if (row == ui.currentToolBarList->count() - 1)
1320 ui.currentToolBarList->takeItem(row);
1321 int newRow = row + 1;
1322 ui.currentToolBarList->insertItem(newRow, currentToolBarAction);
1323 auto actions = currentState.value(currentToolBar);
1324 QAction *action = actions.at(row);
1325 actions.removeAt(row);
1326 actions.insert(newRow, action);
1327 currentState.insert(currentToolBar, actions);
1328 ui.currentToolBarList->setCurrentItem(currentToolBarAction);
1334 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1335 if (!currentToolBarAction)
1337 int row = ui.currentToolBarList->row(currentToolBarAction);
1338 currentState[currentToolBar].removeAt(row);
1339 QAction *action = currentItemToAction.value(currentToolBarAction);
1340 if (widgetActionToToolBar.contains(action)) {
1341 ToolBarItem *item = widgetActionToToolBar.value(action);
1343 toolBarToWidgetActions[item].remove(action);
1344 if (toolBarToWidgetActions[item].isEmpty())
1345 toolBarToWidgetActions.remove(item);
1347 widgetActionToToolBar.insert(action, 0);
1350 actionToCurrentItem.remove(action);
1351 currentItemToAction.remove(currentToolBarAction);
1352 delete currentToolBarAction;
1353 if (row == ui.currentToolBarList->count())
1356 QListWidgetItem *item = ui.currentToolBarList->item(row);
1357 ui.currentToolBarList->setCurrentItem(item);
1368 QListWidgetItem *currentToolBarAction = ui.currentToolBarList->currentItem();
1370 QAction *action = itemToAction.value(currentAction);
1371 QListWidgetItem *item =
nullptr;
1373 if (currentState[currentToolBar].contains(action)) {
1374 item = actionToCurrentItem.value(action);
1375 if (item == currentToolBarAction)
1377 int row = ui.currentToolBarList->row(item);
1378 ui.currentToolBarList->takeItem(row);
1379 currentState[currentToolBar].removeAt(row);
1382 item =
new QListWidgetItem(action->text());
1383 item->setIcon(action->icon());
1384 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1385 currentItemToAction.insert(item, action);
1386 actionToCurrentItem.insert(action, item);
1387 if (widgetActionToToolBar.contains(action)) {
1388 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1389 ToolBarItem *toolBar = widgetActionToToolBar.value(action);
1391 currentState[toolBar].removeAll(action);
1392 toolBarToWidgetActions[toolBar].remove(action);
1393 if (toolBarToWidgetActions[toolBar].isEmpty())
1394 toolBarToWidgetActions.remove(toolBar);
1396 widgetActionToToolBar.insert(action, currentToolBar);
1397 toolBarToWidgetActions[currentToolBar].insert(action);
1401 item =
new QListWidgetItem(separatorText);
1402 currentItemToAction.insert(item, 0);
1405 int row = ui.currentToolBarList->count();
1406 if (currentToolBarAction) {
1407 row = ui.currentToolBarList->row(currentToolBarAction) + 1;
1409 ui.currentToolBarList->insertItem(row, item);
1410 currentState[currentToolBar].insert(row, action);
1411 ui.currentToolBarList->setCurrentItem(item);
1421 QListWidgetItem *item = toolBarToItem.value(currentToolBar);
1422 ui.toolBarList->editItem(item);
1433 tbItem->setToolBarName(item->text());
1439 if (itemToAction.contains(current))
1440 currentAction = current;
1442 currentAction = NULL;
1448 currentToolBar = itemToToolBar.value(current);
1449 ui.currentToolBarList->clear();
1450 actionToCurrentItem.clear();
1451 currentItemToAction.clear();
1456 const auto actions = currentState.value(currentToolBar);
1457 QListWidgetItem *first =
nullptr;
1458 for (QAction *action : actions) {
1459 QString actionName = separatorText;
1461 actionName = action->text();
1462 QListWidgetItem *item =
new QListWidgetItem(actionName, ui.currentToolBarList);
1464 item->setIcon(action->icon());
1465 item->setTextAlignment(Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic));
1466 actionToCurrentItem.insert(action, item);
1467 if (widgetActionToToolBar.contains(action))
1468 item->setData(Qt::ForegroundRole, QColor(Qt::blue));
1470 currentItemToAction.insert(item, action);
1475 ui.currentToolBarList->setCurrentItem(first);
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
1619
1620
1621
1622
1623
1624
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
1660
1661
1662
1663
1664
1665
1668
1669
1670
1672 : QDialog(parent, flags), d_ptr(
new QtToolBarDialogPrivate)
1674 d_ptr->q_ptr =
this;
1675 d_ptr->ui.setupUi(
this);
1676 d_ptr->separatorText = tr(
"< S E P A R A T O R >");
1678 d_ptr->ui.actionTree->setColumnCount(1);
1679 d_ptr->ui.actionTree->setRootIsDecorated(
false);
1680 d_ptr->ui.actionTree->header()->hide();
1682 d_ptr->ui.upButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/up.png"_L1));
1683 d_ptr->ui.downButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/down.png"_L1));
1684 d_ptr->ui.leftButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/back.png"_L1));
1685 d_ptr->ui.rightButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/forward.png"_L1));
1686 d_ptr->ui.newButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/plus.png"_L1));
1687 d_ptr->ui.removeButton->setIcon(QIcon(
":/qt-project.org/qttoolbardialog/images/minus.png"_L1));
1689 connect(d_ptr->ui.newButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->newClicked(); });
1690 connect(d_ptr->ui.removeButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->removeClicked(); });
1691 connect(d_ptr->ui.renameButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->renameClicked(); });
1692 connect(d_ptr->ui.upButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->upClicked(); });
1693 connect(d_ptr->ui.downButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->downClicked(); });
1694 connect(d_ptr->ui.leftButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->leftClicked(); });
1695 connect(d_ptr->ui.rightButton, &QAbstractButton::clicked,
this, [
this] { d_ptr->rightClicked(); });
1697 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::RestoreDefaults),
1698 &QAbstractButton::clicked,
this, [
this] { d_ptr->defaultClicked(); });
1699 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok),
1700 &QAbstractButton::clicked,
this, [
this] { d_ptr->okClicked(); });
1701 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Apply),
1702 &QAbstractButton::clicked,
this, [
this] { d_ptr->applyClicked(); });
1703 connect(d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel),
1704 &QAbstractButton::clicked,
this, [
this] { d_ptr->cancelClicked(); });
1706 connect(d_ptr->ui.actionTree, &QTreeWidget::currentItemChanged,
1707 this, [
this](QTreeWidgetItem *current) { d_ptr->currentActionChanged(current); });
1708 connect(d_ptr->ui.currentToolBarList, &QListWidget::currentItemChanged,
1709 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarActionChanged(current); });
1710 connect(d_ptr->ui.toolBarList, &QListWidget::currentItemChanged,
1711 this, [
this](QListWidgetItem *current) { d_ptr->currentToolBarChanged(current); });
1713 connect(d_ptr->ui.actionTree, &QTreeWidget::itemDoubleClicked,
1714 this, [
this] { d_ptr->rightClicked(); });
1715 connect(d_ptr->ui.currentToolBarList, &QListWidget::itemDoubleClicked,
1716 this, [
this] { d_ptr->leftClicked(); });
1717 connect(d_ptr->ui.toolBarList, &QListWidget::itemChanged,
1718 this, [
this](QListWidgetItem *current) { d_ptr->toolBarRenamed(current); });
1722
1723
1730
1731
1732
1733
1736 if (d_ptr->toolBarManager == toolBarManager->d_ptr->manager)
1740 d_ptr->toolBarManager = toolBarManager->d_ptr->manager;
1746
1747
1750 if (!event->spontaneous())
1755
1756
1759 if (!event->spontaneous())
1765#include "moc_qttoolbardialog_p.cpp"
1766#include "qttoolbardialog.moc"