1 | /* $Id: UIChooserNodeGroup.cpp 108158 2025-02-11 13:45:15Z 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 "UIChooserNodeMachine.h"
|
---|
32 | #include "UITranslationEventListener.h"
|
---|
33 |
|
---|
34 | /* Other VBox includes: */
|
---|
35 | #include "iprt/assert.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
39 | int iPosition,
|
---|
40 | const QUuid &uId,
|
---|
41 | const QString &strName,
|
---|
42 | UIChooserNodeGroupType enmGroupType,
|
---|
43 | bool fOpened)
|
---|
44 | : UIChooserNode(pParent)
|
---|
45 | , m_uId(uId)
|
---|
46 | , m_strName(strName)
|
---|
47 | , m_enmGroupType(enmGroupType)
|
---|
48 | , m_fOpened(fOpened)
|
---|
49 | {
|
---|
50 | /* Add to parent: */
|
---|
51 | if (parentNode())
|
---|
52 | parentNode()->addNode(this, iPosition);
|
---|
53 |
|
---|
54 | /* Apply language settings: */
|
---|
55 | sltRetranslateUI();
|
---|
56 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
57 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
58 | }
|
---|
59 |
|
---|
60 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
61 | int iPosition,
|
---|
62 | UIChooserNodeGroup *pCopyFrom)
|
---|
63 | : UIChooserNode(pParent)
|
---|
64 | , m_uId(pCopyFrom->id())
|
---|
65 | , m_strName(pCopyFrom->name())
|
---|
66 | , m_enmGroupType(pCopyFrom->groupType())
|
---|
67 | , m_fOpened(pCopyFrom->isOpened())
|
---|
68 | {
|
---|
69 | /* Add to parent: */
|
---|
70 | if (parentNode())
|
---|
71 | parentNode()->addNode(this, iPosition);
|
---|
72 |
|
---|
73 | /* Copy internal stuff: */
|
---|
74 | copyContents(pCopyFrom);
|
---|
75 |
|
---|
76 | /* Apply language settings: */
|
---|
77 | sltRetranslateUI();
|
---|
78 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
79 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
80 | }
|
---|
81 |
|
---|
82 | UIChooserNodeGroup::~UIChooserNodeGroup()
|
---|
83 | {
|
---|
84 | /* Cleanup groups first, that
|
---|
85 | * gives us proper recursion: */
|
---|
86 | while (!m_nodesGroup.isEmpty())
|
---|
87 | delete m_nodesGroup.last();
|
---|
88 | while (!m_nodesMachine.isEmpty())
|
---|
89 | delete m_nodesMachine.last();
|
---|
90 |
|
---|
91 | /* Delete item: */
|
---|
92 | delete item();
|
---|
93 |
|
---|
94 | /* Remove from parent: */
|
---|
95 | if (parentNode())
|
---|
96 | parentNode()->removeNode(this);
|
---|
97 | }
|
---|
98 |
|
---|
99 | QString UIChooserNodeGroup::name() const
|
---|
100 | {
|
---|
101 | return m_strName;
|
---|
102 | }
|
---|
103 |
|
---|
104 | QString UIChooserNodeGroup::fullName() const
|
---|
105 | {
|
---|
106 | /* Return "/" for root item: */
|
---|
107 | if (isRoot())
|
---|
108 | return "/";
|
---|
109 | /* Get full parent name, append with '/' if not yet appended: */
|
---|
110 | QString strFullParentName = parentNode()->fullName();
|
---|
111 | if (!strFullParentName.endsWith('/'))
|
---|
112 | strFullParentName.append('/');
|
---|
113 | /* Return full item name based on parent prefix: */
|
---|
114 | return strFullParentName + name();
|
---|
115 | }
|
---|
116 |
|
---|
117 | QString UIChooserNodeGroup::description() const
|
---|
118 | {
|
---|
119 | return name();
|
---|
120 | }
|
---|
121 |
|
---|
122 | QString UIChooserNodeGroup::definition(bool fFull /* = false */) const
|
---|
123 | {
|
---|
124 | QString strNodePrefix;
|
---|
125 | switch (groupType())
|
---|
126 | {
|
---|
127 | case UIChooserNodeGroupType_Local:
|
---|
128 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Local);
|
---|
129 | break;
|
---|
130 | case UIChooserNodeGroupType_Provider:
|
---|
131 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Provider);
|
---|
132 | break;
|
---|
133 | case UIChooserNodeGroupType_Profile:
|
---|
134 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Profile);
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | AssertFailedReturn(QString());
|
---|
138 | }
|
---|
139 | const QString strNodeOptionOpened = UIChooserAbstractModel::optionToString(UIChooserNodeDataOptionType_GroupOpened);
|
---|
140 | return fFull
|
---|
141 | ? QString("%1%2=%3").arg(strNodePrefix).arg(isOpened() ? strNodeOptionOpened : "").arg(name())
|
---|
142 | : QString("%1=%2").arg(strNodePrefix).arg(fullName());
|
---|
143 | }
|
---|
144 |
|
---|
145 | bool UIChooserNodeGroup::hasNodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
146 | {
|
---|
147 | switch (enmType)
|
---|
148 | {
|
---|
149 | case UIChooserNodeType_Any:
|
---|
150 | return hasNodes(UIChooserNodeType_Group) || hasNodes(UIChooserNodeType_Machine);
|
---|
151 | case UIChooserNodeType_Group:
|
---|
152 | return !m_nodesGroup.isEmpty();
|
---|
153 | case UIChooserNodeType_Machine:
|
---|
154 | return !m_nodesMachine.isEmpty();
|
---|
155 | }
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | QList<UIChooserNode*> UIChooserNodeGroup::nodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
160 | {
|
---|
161 | switch (enmType)
|
---|
162 | {
|
---|
163 | case UIChooserNodeType_Any: return m_nodesGroup + m_nodesMachine;
|
---|
164 | case UIChooserNodeType_Group: return m_nodesGroup;
|
---|
165 | case UIChooserNodeType_Machine: return m_nodesMachine;
|
---|
166 | }
|
---|
167 | AssertFailedReturn(QList<UIChooserNode*>());
|
---|
168 | }
|
---|
169 |
|
---|
170 | void UIChooserNodeGroup::addNode(UIChooserNode *pNode, int iPosition)
|
---|
171 | {
|
---|
172 | switch (pNode->type())
|
---|
173 | {
|
---|
174 | case UIChooserNodeType_Group: m_nodesGroup.insert(iPosition < 0 || iPosition > m_nodesGroup.size() ? m_nodesGroup.size() : iPosition, pNode); return;
|
---|
175 | case UIChooserNodeType_Machine: m_nodesMachine.insert(iPosition < 0 || iPosition > m_nodesMachine.size() ? m_nodesMachine.size() : iPosition, pNode); return;
|
---|
176 | default: break;
|
---|
177 | }
|
---|
178 | AssertFailedReturnVoid();
|
---|
179 | }
|
---|
180 |
|
---|
181 | void UIChooserNodeGroup::removeNode(UIChooserNode *pNode)
|
---|
182 | {
|
---|
183 | switch (pNode->type())
|
---|
184 | {
|
---|
185 | case UIChooserNodeType_Group: m_nodesGroup.removeAll(pNode); return;
|
---|
186 | case UIChooserNodeType_Machine: m_nodesMachine.removeAll(pNode); return;
|
---|
187 | default: break;
|
---|
188 | }
|
---|
189 | AssertFailedReturnVoid();
|
---|
190 | }
|
---|
191 |
|
---|
192 | void UIChooserNodeGroup::removeAllNodes(const QUuid &uId)
|
---|
193 | {
|
---|
194 | foreach (UIChooserNode *pNode, nodes())
|
---|
195 | pNode->removeAllNodes(uId);
|
---|
196 | }
|
---|
197 |
|
---|
198 | void UIChooserNodeGroup::updateAllNodes(const QUuid &uId)
|
---|
199 | {
|
---|
200 | // Nothing to update for group-node..
|
---|
201 |
|
---|
202 | /* Update group-item: */
|
---|
203 | item()->updateItem();
|
---|
204 |
|
---|
205 | /* Update all the children recursively: */
|
---|
206 | foreach (UIChooserNode *pNode, nodes())
|
---|
207 | pNode->updateAllNodes(uId);
|
---|
208 | }
|
---|
209 |
|
---|
210 | int UIChooserNodeGroup::positionOf(UIChooserNode *pNode)
|
---|
211 | {
|
---|
212 | switch (pNode->type())
|
---|
213 | {
|
---|
214 | case UIChooserNodeType_Group: return m_nodesGroup.indexOf(pNode->toGroupNode());
|
---|
215 | case UIChooserNodeType_Machine: return m_nodesMachine.indexOf(pNode->toMachineNode());
|
---|
216 | default: break;
|
---|
217 | }
|
---|
218 | AssertFailedReturn(0);
|
---|
219 | }
|
---|
220 |
|
---|
221 | void UIChooserNodeGroup::setName(const QString &strName)
|
---|
222 | {
|
---|
223 | /* Make sure something changed: */
|
---|
224 | if (m_strName == strName)
|
---|
225 | return;
|
---|
226 |
|
---|
227 | /* Save name: */
|
---|
228 | m_strName = strName;
|
---|
229 |
|
---|
230 | /* Update group-item: */
|
---|
231 | if (item())
|
---|
232 | item()->updateItem();
|
---|
233 | }
|
---|
234 |
|
---|
235 | void UIChooserNodeGroup::searchForNodes(const QString &strSearchTerm, int iSearchFlags, QList<UIChooserNode*> &matchedItems)
|
---|
236 | {
|
---|
237 | /* If we are searching for the group-node: */
|
---|
238 | if ( ( iSearchFlags & UIChooserItemSearchFlag_LocalGroup
|
---|
239 | && groupType() == UIChooserNodeGroupType_Local)
|
---|
240 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProvider
|
---|
241 | && groupType() == UIChooserNodeGroupType_Provider)
|
---|
242 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProfile
|
---|
243 | && groupType() == UIChooserNodeGroupType_Profile))
|
---|
244 | {
|
---|
245 | /* If the search term is empty we just add the node to the matched list: */
|
---|
246 | if (strSearchTerm.isEmpty())
|
---|
247 | matchedItems << this;
|
---|
248 | else
|
---|
249 | {
|
---|
250 | /* If exact ID flag specified => check node ID: */
|
---|
251 | if (iSearchFlags & UIChooserItemSearchFlag_ExactId)
|
---|
252 | {
|
---|
253 | if (id().toString() == strSearchTerm)
|
---|
254 | matchedItems << this;
|
---|
255 | }
|
---|
256 | /* If exact name flag specified => check node name: */
|
---|
257 | else if (iSearchFlags & UIChooserItemSearchFlag_ExactName)
|
---|
258 | {
|
---|
259 | if (name() == strSearchTerm)
|
---|
260 | matchedItems << this;
|
---|
261 | }
|
---|
262 | /* If full name flag specified => check full node name: */
|
---|
263 | else if (iSearchFlags & UIChooserItemSearchFlag_FullName)
|
---|
264 | {
|
---|
265 | if (fullName() == strSearchTerm)
|
---|
266 | matchedItems << this;
|
---|
267 | }
|
---|
268 | /* Otherwise check if name contains search term: */
|
---|
269 | else
|
---|
270 | {
|
---|
271 | if (name().contains(strSearchTerm, Qt::CaseInsensitive))
|
---|
272 | matchedItems << this;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* Search among all the children: */
|
---|
278 | foreach (UIChooserNode *pNode, m_nodesGroup)
|
---|
279 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
280 | foreach (UIChooserNode *pNode, m_nodesMachine)
|
---|
281 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
282 | }
|
---|
283 |
|
---|
284 | void UIChooserNodeGroup::sortNodes()
|
---|
285 | {
|
---|
286 | QMap<QString, UIChooserNode*> mapGroup;
|
---|
287 | foreach (UIChooserNode *pNode, m_nodesGroup)
|
---|
288 | mapGroup[pNode->name()] = pNode;
|
---|
289 | m_nodesGroup = mapGroup.values();
|
---|
290 |
|
---|
291 | QMap<QString, UIChooserNode*> mapMachine;
|
---|
292 | foreach (UIChooserNode *pNode, m_nodesMachine)
|
---|
293 | mapMachine[pNode->name()] = pNode;
|
---|
294 | m_nodesMachine = mapMachine.values();
|
---|
295 | }
|
---|
296 |
|
---|
297 | QUuid UIChooserNodeGroup::id() const
|
---|
298 | {
|
---|
299 | return m_uId;
|
---|
300 | }
|
---|
301 |
|
---|
302 | void UIChooserNodeGroup::sltRetranslateUI()
|
---|
303 | {
|
---|
304 | /* Update group-item: */
|
---|
305 | if (item())
|
---|
306 | item()->updateItem();
|
---|
307 | }
|
---|
308 |
|
---|
309 | void UIChooserNodeGroup::copyContents(UIChooserNodeGroup *pCopyFrom)
|
---|
310 | {
|
---|
311 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Group))
|
---|
312 | new UIChooserNodeGroup(this, m_nodesGroup.size(), pNode->toGroupNode());
|
---|
313 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Machine))
|
---|
314 | new UIChooserNodeMachine(this, m_nodesMachine.size(), pNode->toMachineNode());
|
---|
315 | }
|
---|