[42529] | 1 | /* $Id: UIChooserNodeGroup.cpp 104251 2024-04-09 12:36:47Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[77636] | 3 | * VBox Qt GUI - UIChooserNodeGroup class implementation.
|
---|
[42529] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
[42529] | 8 | *
|
---|
[96407] | 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
|
---|
[42529] | 26 | */
|
---|
| 27 |
|
---|
| 28 | /* GUI includes: */
|
---|
[84376] | 29 | #include "UIChooserAbstractModel.h"
|
---|
[77636] | 30 | #include "UIChooserNodeGroup.h"
|
---|
| 31 | #include "UIChooserNodeGlobal.h"
|
---|
| 32 | #include "UIChooserNodeMachine.h"
|
---|
[104251] | 33 | #include "UITranslationEventListener.h"
|
---|
[42529] | 34 |
|
---|
[82944] | 35 | /* Other VBox includes: */
|
---|
| 36 | #include "iprt/assert.h"
|
---|
[52730] | 37 |
|
---|
[82944] | 38 |
|
---|
[77636] | 39 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
[77638] | 40 | int iPosition,
|
---|
[86742] | 41 | const QUuid &uId,
|
---|
[73948] | 42 | const QString &strName,
|
---|
[86742] | 43 | UIChooserNodeGroupType enmGroupType,
|
---|
| 44 | bool fOpened)
|
---|
[84610] | 45 | : UIChooserNode(pParent, false /* favorite */)
|
---|
[86742] | 46 | , m_uId(uId)
|
---|
[43954] | 47 | , m_strName(strName)
|
---|
[83673] | 48 | , m_enmGroupType(enmGroupType)
|
---|
[86742] | 49 | , m_fOpened(fOpened)
|
---|
[42529] | 50 | {
|
---|
[83315] | 51 | /* Add to parent: */
|
---|
[77638] | 52 | if (parentNode())
|
---|
| 53 | parentNode()->addNode(this, iPosition);
|
---|
[83315] | 54 |
|
---|
| 55 | /* Apply language settings: */
|
---|
[104251] | 56 | sltRetranslateUI();
|
---|
| 57 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
| 58 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
[42529] | 59 | }
|
---|
| 60 |
|
---|
[77638] | 61 | UIChooserNodeGroup::UIChooserNodeGroup(UIChooserNode *pParent,
|
---|
[84610] | 62 | int iPosition,
|
---|
| 63 | UIChooserNodeGroup *pCopyFrom)
|
---|
| 64 | : UIChooserNode(pParent, false /* favorite */)
|
---|
[86742] | 65 | , m_uId(pCopyFrom->id())
|
---|
[77638] | 66 | , m_strName(pCopyFrom->name())
|
---|
[83673] | 67 | , m_enmGroupType(pCopyFrom->groupType())
|
---|
[86742] | 68 | , m_fOpened(pCopyFrom->isOpened())
|
---|
[77638] | 69 | {
|
---|
[83315] | 70 | /* Add to parent: */
|
---|
[77638] | 71 | if (parentNode())
|
---|
| 72 | parentNode()->addNode(this, iPosition);
|
---|
[83315] | 73 |
|
---|
| 74 | /* Copy internal stuff: */
|
---|
[77638] | 75 | copyContents(pCopyFrom);
|
---|
[83315] | 76 |
|
---|
| 77 | /* Apply language settings: */
|
---|
[104251] | 78 | sltRetranslateUI();
|
---|
| 79 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
| 80 | this, &UIChooserNodeGroup::sltRetranslateUI);
|
---|
[77638] | 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();
|
---|
[83315] | 93 |
|
---|
| 94 | /* Delete item: */
|
---|
[77638] | 95 | delete item();
|
---|
[83315] | 96 |
|
---|
| 97 | /* Remove from parent: */
|
---|
[77638] | 98 | if (parentNode())
|
---|
| 99 | parentNode()->removeNode(this);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[77636] | 102 | QString UIChooserNodeGroup::name() const
|
---|
[42529] | 103 | {
|
---|
[73948] | 104 | return m_strName;
|
---|
[43823] | 105 | }
|
---|
| 106 |
|
---|
[77636] | 107 | QString UIChooserNodeGroup::fullName() const
|
---|
[43821] | 108 | {
|
---|
[77434] | 109 | /* Return "/" for root item: */
|
---|
| 110 | if (isRoot())
|
---|
[73948] | 111 | return "/";
|
---|
| 112 | /* Get full parent name, append with '/' if not yet appended: */
|
---|
[77636] | 113 | QString strFullParentName = parentNode()->fullName();
|
---|
[73948] | 114 | if (!strFullParentName.endsWith('/'))
|
---|
| 115 | strFullParentName.append('/');
|
---|
| 116 | /* Return full item name based on parent prefix: */
|
---|
| 117 | return strFullParentName + name();
|
---|
[43821] | 118 | }
|
---|
| 119 |
|
---|
[77636] | 120 | QString UIChooserNodeGroup::description() const
|
---|
[43821] | 121 | {
|
---|
[93867] | 122 | return name();
|
---|
[43821] | 123 | }
|
---|
| 124 |
|
---|
[84373] | 125 | QString UIChooserNodeGroup::definition(bool fFull /* = false */) const
|
---|
[42529] | 126 | {
|
---|
[84378] | 127 | QString strNodePrefix;
|
---|
| 128 | switch (groupType())
|
---|
| 129 | {
|
---|
| 130 | case UIChooserNodeGroupType_Local:
|
---|
[84412] | 131 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Local);
|
---|
[84378] | 132 | break;
|
---|
| 133 | case UIChooserNodeGroupType_Provider:
|
---|
[84412] | 134 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Provider);
|
---|
[84378] | 135 | break;
|
---|
| 136 | case UIChooserNodeGroupType_Profile:
|
---|
[84412] | 137 | strNodePrefix = UIChooserAbstractModel::prefixToString(UIChooserNodeDataPrefixType_Profile);
|
---|
[84378] | 138 | break;
|
---|
[84400] | 139 | default:
|
---|
| 140 | AssertFailedReturn(QString());
|
---|
[84378] | 141 | }
|
---|
[84412] | 142 | const QString strNodeOptionOpened = UIChooserAbstractModel::optionToString(UIChooserNodeDataOptionType_GroupOpened);
|
---|
[84373] | 143 | return fFull
|
---|
[84376] | 144 | ? QString("%1%2=%3").arg(strNodePrefix).arg(isOpened() ? strNodeOptionOpened : "").arg(name())
|
---|
| 145 | : QString("%1=%2").arg(strNodePrefix).arg(fullName());
|
---|
[42529] | 146 | }
|
---|
| 147 |
|
---|
[83672] | 148 | bool UIChooserNodeGroup::hasNodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
[77638] | 149 | {
|
---|
| 150 | switch (enmType)
|
---|
| 151 | {
|
---|
[83672] | 152 | case UIChooserNodeType_Any:
|
---|
| 153 | return hasNodes(UIChooserNodeType_Group) || hasNodes(UIChooserNodeType_Global) || hasNodes(UIChooserNodeType_Machine);
|
---|
| 154 | case UIChooserNodeType_Group:
|
---|
[77638] | 155 | return !m_nodesGroup.isEmpty();
|
---|
[83672] | 156 | case UIChooserNodeType_Global:
|
---|
[77638] | 157 | return !m_nodesGlobal.isEmpty();
|
---|
[83672] | 158 | case UIChooserNodeType_Machine:
|
---|
[77638] | 159 | return !m_nodesMachine.isEmpty();
|
---|
| 160 | }
|
---|
| 161 | return false;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[83672] | 164 | QList<UIChooserNode*> UIChooserNodeGroup::nodes(UIChooserNodeType enmType /* = UIChooserNodeType_Any */) const
|
---|
[77638] | 165 | {
|
---|
| 166 | switch (enmType)
|
---|
| 167 | {
|
---|
[83672] | 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;
|
---|
[77638] | 172 | }
|
---|
| 173 | AssertFailedReturn(QList<UIChooserNode*>());
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | void UIChooserNodeGroup::addNode(UIChooserNode *pNode, int iPosition)
|
---|
| 177 | {
|
---|
| 178 | switch (pNode->type())
|
---|
| 179 | {
|
---|
[100554] | 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;
|
---|
[77638] | 183 | default: break;
|
---|
| 184 | }
|
---|
| 185 | AssertFailedReturnVoid();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | void UIChooserNodeGroup::removeNode(UIChooserNode *pNode)
|
---|
| 189 | {
|
---|
| 190 | switch (pNode->type())
|
---|
| 191 | {
|
---|
[83672] | 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;
|
---|
[77638] | 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 | {
|
---|
[83672] | 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());
|
---|
[77638] | 225 | default: break;
|
---|
| 226 | }
|
---|
| 227 | AssertFailedReturn(0);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[77636] | 230 | void UIChooserNodeGroup::setName(const QString &strName)
|
---|
[42529] | 231 | {
|
---|
[77636] | 232 | /* Make sure something changed: */
|
---|
| 233 | if (m_strName == strName)
|
---|
[73948] | 234 | return;
|
---|
[43853] | 235 |
|
---|
[77636] | 236 | /* Save name: */
|
---|
| 237 | m_strName = strName;
|
---|
[43853] | 238 |
|
---|
[77636] | 239 | /* Update group-item: */
|
---|
| 240 | if (item())
|
---|
| 241 | item()->updateItem();
|
---|
[43853] | 242 | }
|
---|
| 243 |
|
---|
[84625] | 244 | void UIChooserNodeGroup::searchForNodes(const QString &strSearchTerm, int iSearchFlags, QList<UIChooserNode*> &matchedItems)
|
---|
[77683] | 245 | {
|
---|
[83316] | 246 | /* If we are searching for the group-node: */
|
---|
[84625] | 247 | if ( ( iSearchFlags & UIChooserItemSearchFlag_LocalGroup
|
---|
[84371] | 248 | && groupType() == UIChooserNodeGroupType_Local)
|
---|
[84625] | 249 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProvider
|
---|
[84371] | 250 | && groupType() == UIChooserNodeGroupType_Provider)
|
---|
[84625] | 251 | || ( iSearchFlags & UIChooserItemSearchFlag_CloudProfile
|
---|
[84371] | 252 | && groupType() == UIChooserNodeGroupType_Profile))
|
---|
[77683] | 253 | {
|
---|
[83316] | 254 | /* If the search term is empty we just add the node to the matched list: */
|
---|
[77701] | 255 | if (strSearchTerm.isEmpty())
|
---|
| 256 | matchedItems << this;
|
---|
[77683] | 257 | else
|
---|
| 258 | {
|
---|
[86744] | 259 | /* If exact ID flag specified => check node ID: */
|
---|
[84625] | 260 | if (iSearchFlags & UIChooserItemSearchFlag_ExactId)
|
---|
[77701] | 261 | {
|
---|
[86744] | 262 | if (id().toString() == strSearchTerm)
|
---|
[83919] | 263 | matchedItems << this;
|
---|
| 264 | }
|
---|
| 265 | /* If exact name flag specified => check node name: */
|
---|
[84625] | 266 | else if (iSearchFlags & UIChooserItemSearchFlag_ExactName)
|
---|
[83919] | 267 | {
|
---|
[77701] | 268 | if (name() == strSearchTerm)
|
---|
| 269 | matchedItems << this;
|
---|
| 270 | }
|
---|
[86744] | 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 | }
|
---|
[83316] | 277 | /* Otherwise check if name contains search term: */
|
---|
[77701] | 278 | else
|
---|
| 279 | {
|
---|
| 280 | if (name().contains(strSearchTerm, Qt::CaseInsensitive))
|
---|
| 281 | matchedItems << this;
|
---|
| 282 | }
|
---|
[77683] | 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[83316] | 286 | /* Search among all the children: */
|
---|
[77846] | 287 | foreach (UIChooserNode *pNode, m_nodesGroup)
|
---|
[84625] | 288 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
[77846] | 289 | foreach (UIChooserNode *pNode, m_nodesGlobal)
|
---|
[84625] | 290 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
[77846] | 291 | foreach (UIChooserNode *pNode, m_nodesMachine)
|
---|
[84625] | 292 | pNode->searchForNodes(strSearchTerm, iSearchFlags, matchedItems);
|
---|
[77683] | 293 | }
|
---|
| 294 |
|
---|
[77847] | 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 |
|
---|
[86742] | 313 | QUuid UIChooserNodeGroup::id() const
|
---|
| 314 | {
|
---|
| 315 | return m_uId;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[104251] | 318 | void UIChooserNodeGroup::sltRetranslateUI()
|
---|
[42593] | 319 | {
|
---|
[77636] | 320 | /* Update group-item: */
|
---|
| 321 | if (item())
|
---|
| 322 | item()->updateItem();
|
---|
[73948] | 323 | }
|
---|
[77638] | 324 |
|
---|
| 325 | void UIChooserNodeGroup::copyContents(UIChooserNodeGroup *pCopyFrom)
|
---|
| 326 | {
|
---|
[83672] | 327 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Group))
|
---|
[84610] | 328 | new UIChooserNodeGroup(this, m_nodesGroup.size(), pNode->toGroupNode());
|
---|
[83672] | 329 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Global))
|
---|
[84610] | 330 | new UIChooserNodeGlobal(this, m_nodesGlobal.size(), pNode->toGlobalNode());
|
---|
[83672] | 331 | foreach (UIChooserNode *pNode, pCopyFrom->nodes(UIChooserNodeType_Machine))
|
---|
[84610] | 332 | new UIChooserNodeMachine(this, m_nodesMachine.size(), pNode->toMachineNode());
|
---|
[77638] | 333 | }
|
---|