1 | /* $Id: UIChooserNodeGroup.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIChooserNodeGroup class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* GUI includes: */
|
---|
29 | #include "UIChooserAbstractModel.h"
|
---|
30 | #include "UIChooserNodeGroup.h"
|
---|
31 | #include "UIChooserNodeGlobal.h"
|
---|
32 | #include "UIChooserNodeMachine.h"
|
---|
33 | #include "UITranslationEventListener.h"
|
---|
34 |
|
---|
35 | /* Other VBox includes: */
|
---|
36 | #include "iprt/assert.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
40 | int iPosition,
|
---|
41 | const QUuid &uId,
|
---|
42 | const QString &strName,
|
---|
43 | UIChooserNodeGroupType enmGroupType,
|
---|
44 | bool fOpened)
|
---|
45 | : UIChooserNode(pParent, false /* favorite */)
|
---|
46 | , m_uId(uId)
|
---|
47 | , m_strName(strName)
|
---|
48 | , m_enmGroupType(enmGroupType)
|
---|
49 | , m_fOpened(fOpened)
|
---|
50 | {
|
---|
51 | /* Add to parent: */
|
---|
52 | if (parentNode())
|
---|
53 | parentNode()->addNode(this, iPosition);
|
---|
54 |
|
---|
55 | /* Apply language settings: */
|
---|
56 | sltRetranslateUI();
|
---|
57 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
58 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
59 | }
|
---|
60 |
|
---|
61 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
62 | int iPosition,
|
---|
63 | UIChooserNodeGroup *pCopyFrom)
|
---|
64 | : UIChooserNode(pParent, false /* favorite */)
|
---|
65 | , m_uId(pCopyFrom->id())
|
---|
66 | , m_strName(pCopyFrom->name())
|
---|
67 | , m_enmGroupType(pCopyFrom->groupType())
|
---|
68 | , m_fOpened(pCopyFrom->isOpened())
|
---|
69 | {
|
---|
70 | /* Add to parent: */
|
---|
71 | if (parentNode())
|
---|
72 | parentNode()->addNode(this, iPosition);
|
---|
73 |
|
---|
74 | /* Copy internal stuff: */
|
---|
75 | copyContents(pCopyFrom);
|
---|
76 |
|
---|
77 | /* Apply language settings: */
|
---|
78 | sltRetranslateUI();
|
---|
79 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
80 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
81 | }
|
---|
82 |
|
---|
83 | UIChooserNodeGroup::~UIChooserNodeGroup()
|
---|
84 | {
|
---|
85 | /* Cleanup groups first, that
|
---|
86 | * gives us proper recursion: */
|
---|
87 | while (!m_nodesGroup.isEmpty())
|
---|
88 | delete m_nodesGroup.last();
|
---|
89 | while (!m_nodesGlobal.isEmpty())
|
---|
90 | delete m_nodesGlobal.last();
|
---|
91 | while (!m_nodesMachine.isEmpty())
|
---|
92 | delete m_nodesMachine.last();
|
---|
93 |
|
---|
94 | /* Delete item: */
|
---|
95 | delete item();
|
---|
96 |
|
---|
97 | /* Remove from parent: */
|
---|
98 | if (parentNode())
|
---|
99 | parentNode()->removeNode(this);
|
---|
100 | }
|
---|
101 |
|
---|
102 | QString UIChooserNodeGroup::name() const
|
---|
103 | {
|
---|
104 | return m_strName;
|
---|
105 | }
|
---|
106 |
|
---|
107 | QString UIChooserNodeGroup::fullName() const
|
---|
108 | {
|
---|
109 | /* Return "/" for root item: */
|
---|
110 | if (isRoot())
|
---|
111 | return "/";
|
---|
112 | /* Get full parent name, append with '/' if not yet appended: */
|
---|
113 | QString strFullParentName = parentNode()->fullName();
|
---|
114 | if (!strFullParentName.endsWith('/'))
|
---|
115 | strFullParentName.append('/');
|
---|
116 | /* Return full item name based on parent prefix: */
|
---|
117 | return strFullParentName + name();
|
---|
118 | }
|
---|
119 |
|
---|
120 | QString UIChooserNodeGroup::description() const
|
---|
121 | {
|
---|
122 | return name();
|
---|
123 | }
|
---|
124 |
|
---|
125 | QString UIChooserNodeGroup::definition(bool fFull /* = false */) const
|
---|
126 | {
|
---|
127 | QString strNodePrefix;
|
---|
128 | switch (groupType())
|
---|
129 | {
|
---|
130 | case UIChooserNodeGroupType_Local:
|
---|
131 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Local);
|
---|
132 | break;
|
---|
133 | case UIChooserNodeGroupType_Provider:
|
---|
134 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Provider);
|
---|
135 | break;
|
---|
136 | case UIChooserNodeGroupType_Profile:
|
---|
137 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Profile);
|
---|
138 | break;
|
---|
139 | default:
|
---|
140 | AssertFailedReturn(QString());
|
---|
141 | }
|
---|
142 | const QString strNodeOptionOpened = UIChooserAbstractModel::optionToString(UIChooserNodeDataOptionType_GroupOpened);
|
---|
143 | return fFull
|
---|
144 | ? QString("%1%2=%3").arg(strNodePrefix).arg(isOpened() ? strNodeOptionOpened : "").arg(name())
|
---|
145 | : QString("%1=%2").arg(strNodePrefix).arg(fullName());
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool UIChooserNodeGroup::hasNodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
149 | {
|
---|
150 | switch (enmType)
|
---|
151 | {
|
---|
152 | case UIChooserNodeType_Any:
|
---|
153 | return hasNodes(UIChooserNodeType_Group) || hasNodes(UIChooserNodeType_Global) || hasNodes(UIChooserNodeType_Machine);
|
---|
154 | case UIChooserNodeType_Group:
|
---|
155 | return !m_nodesGroup.isEmpty();
|
---|
156 | case UIChooserNodeType_Global:
|
---|
157 | return !m_nodesGlobal.isEmpty();
|
---|
158 | case UIChooserNodeType_Machine:
|
---|
159 | return !m_nodesMachine.isEmpty();
|
---|
160 | }
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | QList<UIChooserNode*> UIChooserNodeGroup::nodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
165 | {
|
---|
166 | switch (enmType)
|
---|
167 | {
|
---|
168 | case UIChooserNodeType_Any: return m_nodesGlobal + m_nodesGroup + m_nodesMachine;
|
---|
169 | case UIChooserNodeType_Group: return m_nodesGroup;
|
---|
170 | case UIChooserNodeType_Global: return m_nodesGlobal;
|
---|
171 | case UIChooserNodeType_Machine: return m_nodesMachine;
|
---|
172 | }
|
---|
173 | AssertFailedReturn(QList<UIChooserNode*>());
|
---|
174 | }
|
---|
175 |
|
---|
176 | void UIChooserNodeGroup::addNode(UIChooserNode *pNode, int iPosition)
|
---|
177 | {
|
---|
178 | switch (pNode->type())
|
---|
179 | {
|
---|
180 | case UIChooserNodeType_Group: m_nodesGroup.insert(iPosition < 0 || iPosition > m_nodesGroup.size() ? m_nodesGroup.size() : iPosition, pNode); return;
|
---|
181 | case UIChooserNodeType_Global: m_nodesGlobal.insert(iPosition < 0 || iPosition > m_nodesGlobal.size() ? m_nodesGlobal.size() : iPosition, pNode); return;
|
---|
182 | case UIChooserNodeType_Machine: m_nodesMachine.insert(iPosition < 0 || iPosition > m_nodesMachine.size() ? m_nodesMachine.size() : iPosition, pNode); return;
|
---|
183 | default: break;
|
---|
184 | }
|
---|
185 | AssertFailedReturnVoid();
|
---|
186 | }
|
---|
187 |
|
---|
188 | void UIChooserNodeGroup::removeNode(UIChooserNode *pNode)
|
---|
189 | {
|
---|
190 | switch (pNode->type())
|
---|
191 | {
|
---|
192 | case UIChooserNodeType_Group: m_nodesGroup.removeAll(pNode); return;
|
---|
193 | case UIChooserNodeType_Global: m_nodesGlobal.removeAll(pNode); return;
|
---|
194 | case UIChooserNodeType_Machine: m_nodesMachine.removeAll(pNode); return;
|
---|
195 | default: break;
|
---|
196 | }
|
---|
197 | AssertFailedReturnVoid();
|
---|
198 | }
|
---|
199 |
|
---|
200 | void UIChooserNodeGroup::removeAllNodes(const QUuid &uId)
|
---|
201 | {
|
---|
202 | foreach (UIChooserNode *pNode, nodes())
|
---|
203 | pNode->removeAllNodes(uId);
|
---|
204 | }
|
---|
205 |
|
---|
206 | void UIChooserNodeGroup::updateAllNodes(const QUuid &uId)
|
---|
207 | {
|
---|
208 | // Nothing to update for group-node..
|
---|
209 |
|
---|
210 | /* Update group-item: */
|
---|
211 | item()->updateItem();
|
---|
212 |
|
---|
213 | /* Update all the children recursively: */
|
---|
214 | foreach (UIChooserNode *pNode, nodes())
|
---|
215 | pNode->updateAllNodes(uId);
|
---|
216 | }
|
---|
217 |
|
---|
218 | int UIChooserNodeGroup::positionOf(UIChooserNode *pNode)
|
---|
219 | {
|
---|
220 | switch (pNode->type())
|
---|
221 | {
|
---|
222 | case UIChooserNodeType_Group: return m_nodesGroup.indexOf(pNode->toGroupNode());
|
---|
223 | case UIChooserNodeType_Global: return m_nodesGlobal.indexOf(pNode->toGlobalNode());
|
---|
224 | case UIChooserNodeType_Machine: return m_nodesMachine.indexOf(pNode->toMachineNode());
|
---|
225 | default: break;
|
---|
226 | }
|
---|
227 | AssertFailedReturn(0);
|
---|
228 | }
|
---|
229 |
|
---|
230 | void UIChooserNodeGroup::setName(const QString &strName)
|
---|
231 | {
|
---|
232 | /* Make sure something changed: */
|
---|
233 | if (m_strName == strName)
|
---|
234 | return;
|
---|
235 |
|
---|
236 | /* Save name: */
|
---|
237 | m_strName = strName;
|
---|
238 |
|
---|
239 | /* Update group-item: */
|
---|
240 | if (item())
|
---|
241 | item()->updateItem();
|
---|
242 | }
|
---|
243 |
|
---|
244 | void UIChooserNodeGroup::searchForNodes(const QString &strSearchTerm, int iSearchFlags, QList<UIChooserNode*> &matchedItems)
|
---|
245 | {
|
---|
246 | /* If we are searching for the group-node: */
|
---|
247 | if ( ( iSearchFlags & UIChooserItemSearchFlag_LocalGroup
|
---|
248 | && groupType() == UIChooserNodeGroupType_Local)
|
---|
249 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProvider
|
---|
250 | && groupType() == UIChooserNodeGroupType_Provider)
|
---|
251 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProfile
|
---|
252 | && groupType() == UIChooserNodeGroupType_Profile))
|
---|
253 | {
|
---|
254 | /* If the search term is empty we just add the node to the matched list: */
|
---|
255 | if (strSearchTerm.isEmpty())
|
---|
256 | matchedItems << this;
|
---|
257 | else
|
---|
258 | {
|
---|
259 | /* If exact ID flag specified => check node ID: */
|
---|
260 | if (iSearchFlags & UIChooserItemSearchFlag_ExactId)
|
---|
261 | {
|
---|
262 | if (id().toString() == strSearchTerm)
|
---|
263 | matchedItems << this;
|
---|
264 | }
|
---|
265 | /* If exact name flag specified => check node name: */
|
---|
266 | else if (iSearchFlags & UIChooserItemSearchFlag_ExactName)
|
---|
267 | {
|
---|
268 | if (name() == strSearchTerm)
|
---|
269 | matchedItems << this;
|
---|
270 | }
|
---|
271 | /* If full name flag specified => check full node name: */
|
---|
272 | else if (iSearchFlags & UIChooserItemSearchFlag_FullName)
|
---|
273 | {
|
---|
274 | if (fullName() == strSearchTerm)
|
---|
275 | matchedItems << this;
|
---|
276 | }
|
---|
277 | /* Otherwise check if name contains search term: */
|
---|
278 | else
|
---|
279 | {
|
---|
280 | if (name().contains(strSearchTerm, Qt::CaseInsensitive))
|
---|
281 | matchedItems << this;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Search among all the children: */
|
---|
287 | foreach (UIChooserNode *pNode, m_nodesGroup)
|
---|
288 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
289 | foreach (UIChooserNode *pNode, m_nodesGlobal)
|
---|
290 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
291 | foreach (UIChooserNode *pNode, m_nodesMachine)
|
---|
292 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
293 | }
|
---|
294 |
|
---|
295 | void UIChooserNodeGroup::sortNodes()
|
---|
296 | {
|
---|
297 | QMap<QString, UIChooserNode*> mapGroup;
|
---|
298 | foreach (UIChooserNode *pNode, m_nodesGroup)
|
---|
299 | mapGroup[pNode->name()] = pNode;
|
---|
300 | m_nodesGroup = mapGroup.values();
|
---|
301 |
|
---|
302 | QMap<QString, UIChooserNode*> mapGlobal;
|
---|
303 | foreach (UIChooserNode *pNode, m_nodesGlobal)
|
---|
304 | mapGlobal[pNode->name()] = pNode;
|
---|
305 | m_nodesGlobal = mapGlobal.values();
|
---|
306 |
|
---|
307 | QMap<QString, UIChooserNode*> mapMachine;
|
---|
308 | foreach (UIChooserNode *pNode, m_nodesMachine)
|
---|
309 | mapMachine[pNode->name()] = pNode;
|
---|
310 | m_nodesMachine = mapMachine.values();
|
---|
311 | }
|
---|
312 |
|
---|
313 | QUuid UIChooserNodeGroup::id() const
|
---|
314 | {
|
---|
315 | return m_uId;
|
---|
316 | }
|
---|
317 |
|
---|
318 | void UIChooserNodeGroup::sltRetranslateUI()
|
---|
319 | {
|
---|
320 | /* Update group-item: */
|
---|
321 | if (item())
|
---|
322 | item()->updateItem();
|
---|
323 | }
|
---|
324 |
|
---|
325 | void UIChooserNodeGroup::copyContents(UIChooserNodeGroup *pCopyFrom)
|
---|
326 | {
|
---|
327 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Group))
|
---|
328 | new UIChooserNodeGroup(this, m_nodesGroup.size(), pNode->toGroupNode());
|
---|
329 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Global))
|
---|
330 | new UIChooserNodeGlobal(this, m_nodesGlobal.size(), pNode->toGlobalNode());
|
---|
331 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Machine))
|
---|
332 | new UIChooserNodeMachine(this, m_nodesMachine.size(), pNode->toMachineNode());
|
---|
333 | }
|
---|