1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "toolbarmanager.h" |
6 | |
7 | class ToolBarManagerPrivate |
8 | { |
9 | friend class ToolBarManager; |
10 | struct Item { |
11 | QString id; |
12 | QAction *action; |
13 | QString group; |
14 | }; |
15 | |
16 | QMap<QString, QAction *> groupIndex; |
17 | QList<Item> itemVector; |
18 | QToolBar *toolbar = nullptr; |
19 | }; |
20 | |
21 | ToolBarManager::ToolBarManager(const QString &name, QObject *parent) |
22 | : QObject(parent) |
23 | , d(new ToolBarManagerPrivate()) |
24 | { |
25 | d->toolbar = new QToolBar(name); |
26 | d->toolbar->setIconSize(QSize(25, 25)); |
27 | } |
28 | |
29 | ToolBarManager::~ToolBarManager() |
30 | { |
31 | if (d) { |
32 | delete d; |
33 | } |
34 | } |
35 | |
36 | QToolBar *ToolBarManager::getToolBar() const |
37 | { |
38 | return d->toolbar; |
39 | } |
40 | |
41 | bool ToolBarManager::addActionItem(const QString &id, QAction *action, const QString &group) |
42 | { |
43 | if (!action || id.isEmpty() || !d->toolbar || hasOverrideActionItem(id, action, group)) |
44 | return false; |
45 | QStringList groupIndex = group.split("." ); |
46 | QString groupName = groupIndex.at(0); |
47 | |
48 | if(!d->groupIndex.contains(groupName)) { |
49 | d->groupIndex.insert(groupName, nullptr); |
50 | } |
51 | |
52 | auto currentIterator = d->groupIndex.find(groupName); |
53 | auto nextIterator = currentIterator + 1; |
54 | QAction *before = nullptr; |
55 | for (; nextIterator != d->groupIndex.end(); ++nextIterator) { |
56 | if (nextIterator.value() != nullptr) { |
57 | QString temp = nextIterator.key(); |
58 | before = nextIterator.value(); |
59 | break; |
60 | } |
61 | } |
62 | |
63 | d->toolbar->insertAction(before, action); |
64 | |
65 | if (currentIterator.value() == nullptr) |
66 | d->groupIndex[groupName] = action; |
67 | if (groupIndex.size() > 1) { |
68 | if (groupIndex.at(1) == "Start" ) { |
69 | if (currentIterator.value() != nullptr && currentIterator.value() != action) |
70 | before = currentIterator.value(); |
71 | d->toolbar->insertAction(before, action); |
72 | currentIterator.value() = action; |
73 | } else if (groupIndex.at(1) == "End" ) { |
74 | d->toolbar->insertSeparator(before); |
75 | } |
76 | } |
77 | |
78 | return true; |
79 | } |
80 | |
81 | bool ToolBarManager::addWidgetItem(const QString &id, QWidget *widget, const QString &group) |
82 | { |
83 | if (!widget || id.isEmpty() || !d->toolbar) |
84 | return false; |
85 | |
86 | bool exist = false; |
87 | QAction *a = nullptr; |
88 | |
89 | for (int i = 0; i < d->itemVector.size(); i++) { |
90 | if (d->itemVector.at(i).id == id) { |
91 | d->toolbar->removeAction(d->itemVector.at(i).action); |
92 | if (i - 1 >= 0) { |
93 | QAction *before = d->itemVector.at(i - 1).action; |
94 | a = d->toolbar->insertWidget(before, widget); |
95 | } else { |
96 | a = d->toolbar->addWidget(widget); |
97 | } |
98 | d->itemVector.replace(i, {id, a, group}); |
99 | exist = true; |
100 | break; |
101 | } |
102 | } |
103 | |
104 | if (!exist) { |
105 | a = d->toolbar->addWidget(widget); |
106 | d->itemVector.push_back({id, a, group}); |
107 | } |
108 | |
109 | return true; |
110 | } |
111 | |
112 | bool ToolBarManager::hasOverrideActionItem(const QString &id, QAction *action, const QString &group) |
113 | { |
114 | for (int i = 0; i < d->itemVector.size(); i++) { |
115 | if (d->itemVector.at(i).id == id) { |
116 | d->toolbar->removeAction(d->itemVector.at(i).action); |
117 | if (i - 1 >= 0) { |
118 | QAction *before = d->itemVector.at(i - 1).action; |
119 | d->toolbar->insertAction(before, action); |
120 | } else { |
121 | d->toolbar->addAction(action); |
122 | } |
123 | d->itemVector.replace(i, {id, action, group}); |
124 | return true; |
125 | } |
126 | } |
127 | |
128 | return false; |
129 | } |
130 | |
131 | void ToolBarManager::removeItem(const QString &id) |
132 | { |
133 | for (auto iter = d->itemVector.begin(); iter != d->itemVector.end(); ++iter) { |
134 | if (iter->id == id) { |
135 | d->toolbar->removeAction(iter->action); |
136 | d->itemVector.erase(iter); |
137 | return; |
138 | } |
139 | } |
140 | } |
141 | |
142 | void ToolBarManager::disableItem(const QString &id, bool disable) |
143 | { |
144 | for (auto iter = d->itemVector.begin(); iter != d->itemVector.end(); ++iter) { |
145 | if (iter->id == id) { |
146 | iter->action->setDisabled(disable); |
147 | return; |
148 | } |
149 | } |
150 | } |
151 | |