1 | /* $Id: UIDetailsGroup.cpp 103710 2024-03-06 16:53:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIDetailsGroup class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 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 | /* Qt include: */
|
---|
29 | #include <QGraphicsLinearLayout>
|
---|
30 | #include <QGraphicsScene>
|
---|
31 | #include <QPainter>
|
---|
32 | #include <QStyle>
|
---|
33 | #include <QStyleOptionGraphicsItem>
|
---|
34 |
|
---|
35 | /* GUI includes: */
|
---|
36 | #include "UIGraphicsScrollArea.h"
|
---|
37 | #include "UIDetailsGroup.h"
|
---|
38 | #include "UIDetailsModel.h"
|
---|
39 | #include "UIDetailsSet.h"
|
---|
40 | #include "UIDetailsView.h"
|
---|
41 | #include "UIExtraDataManager.h"
|
---|
42 | #include "UIVirtualMachineItem.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | UIDetailsGroup::UIDetailsGroup(QGraphicsScene *pParent)
|
---|
46 | : UIDetailsItem(0)
|
---|
47 | , m_pBuildStep(0)
|
---|
48 | , m_pScrollArea(0)
|
---|
49 | , m_pContainer(0)
|
---|
50 | , m_pLayout(0)
|
---|
51 | , m_iPreviousMinimumWidthHint(0)
|
---|
52 | {
|
---|
53 | /* Prepare scroll-area: */
|
---|
54 | m_pScrollArea = new UIGraphicsScrollArea(Qt::Vertical, this);
|
---|
55 | if (m_pScrollArea)
|
---|
56 | {
|
---|
57 | /* Prepare container: */
|
---|
58 | m_pContainer = new QIGraphicsWidget;
|
---|
59 | if (m_pContainer)
|
---|
60 | {
|
---|
61 | /* Prepare layout: */
|
---|
62 | m_pLayout = new QGraphicsLinearLayout(Qt::Vertical, m_pContainer);
|
---|
63 | if (m_pLayout)
|
---|
64 | {
|
---|
65 | m_pLayout->setContentsMargins(0, 0, 0, 0);
|
---|
66 | m_pLayout->setSpacing(0);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* Assign to scroll-area: */
|
---|
70 | m_pScrollArea->setViewport(m_pContainer);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* Add group to the parent scene: */
|
---|
75 | pParent->addItem(this);
|
---|
76 |
|
---|
77 | /* Prepare connections: */
|
---|
78 | prepareConnections();
|
---|
79 | }
|
---|
80 |
|
---|
81 | UIDetailsGroup::~UIDetailsGroup()
|
---|
82 | {
|
---|
83 | /* Cleanup items: */
|
---|
84 | clearItems();
|
---|
85 | }
|
---|
86 |
|
---|
87 | void UIDetailsGroup::buildGroup(const QList<UIVirtualMachineItem*> &machineItems)
|
---|
88 | {
|
---|
89 | /* Filter out cloud VM items for now: */
|
---|
90 | QList<UIVirtualMachineItem*> filteredItems;
|
---|
91 | foreach (UIVirtualMachineItem *pItem, machineItems)
|
---|
92 | if ( pItem->itemType() == UIVirtualMachineItemType_Local
|
---|
93 | || pItem->itemType() == UIVirtualMachineItemType_CloudReal)
|
---|
94 | filteredItems << pItem;
|
---|
95 |
|
---|
96 | /* Remember passed machine-items: */
|
---|
97 | m_machineItems = filteredItems;
|
---|
98 |
|
---|
99 | /* Cleanup superflous items: */
|
---|
100 | const bool fCleanupPerformed = m_items.size() > m_machineItems.size();
|
---|
101 | while (m_items.size() > m_machineItems.size())
|
---|
102 | delete m_items.last();
|
---|
103 | foreach (UIDetailsItem *pItem, m_items)
|
---|
104 | pItem->toSet()->clearSet();
|
---|
105 | if (fCleanupPerformed)
|
---|
106 | updateGeometry();
|
---|
107 |
|
---|
108 | /* Start building group: */
|
---|
109 | rebuildGroup();
|
---|
110 | }
|
---|
111 |
|
---|
112 | void UIDetailsGroup::rebuildGroup()
|
---|
113 | {
|
---|
114 | /* Cleanup build-step: */
|
---|
115 | delete m_pBuildStep;
|
---|
116 | m_pBuildStep = 0;
|
---|
117 |
|
---|
118 | /* Generate new group-id: */
|
---|
119 | m_uGroupId = QUuid::createUuid();
|
---|
120 |
|
---|
121 | /* Request to build first step: */
|
---|
122 | emit sigBuildStep(m_uGroupId, 0);
|
---|
123 | }
|
---|
124 |
|
---|
125 | void UIDetailsGroup::stopBuildingGroup()
|
---|
126 | {
|
---|
127 | /* Generate new group-id: */
|
---|
128 | m_uGroupId = QUuid::createUuid();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIDetailsGroup::installEventFilterHelper(QObject *pSource)
|
---|
132 | {
|
---|
133 | /* The only object which need's that filter for now is scroll-area: */
|
---|
134 | pSource->installEventFilter(m_pScrollArea);
|
---|
135 | }
|
---|
136 |
|
---|
137 | QList<UIDetailsItem*> UIDetailsGroup::items(UIDetailsItemType enmType /* = UIDetailsItemType_Set */) const
|
---|
138 | {
|
---|
139 | switch (enmType)
|
---|
140 | {
|
---|
141 | case UIDetailsItemType_Set: return m_items;
|
---|
142 | case UIDetailsItemType_Any: return items(UIDetailsItemType_Set);
|
---|
143 | default: AssertMsgFailed(("Invalid item type!")); break;
|
---|
144 | }
|
---|
145 | return QList<UIDetailsItem*>();
|
---|
146 | }
|
---|
147 |
|
---|
148 | void UIDetailsGroup::updateLayout()
|
---|
149 | {
|
---|
150 | /* Acquire view: */
|
---|
151 | UIDetailsView *pView = model()->view();
|
---|
152 |
|
---|
153 | /* Adjust children scroll-area: */
|
---|
154 | m_pScrollArea->resize(pView->size());
|
---|
155 | m_pScrollArea->setPos(0, 0);
|
---|
156 |
|
---|
157 | /* Layout all the sets: */
|
---|
158 | foreach (UIDetailsItem *pItem, items())
|
---|
159 | pItem->updateLayout();
|
---|
160 | }
|
---|
161 |
|
---|
162 | int UIDetailsGroup::minimumWidthHint() const
|
---|
163 | {
|
---|
164 | return m_pContainer->minimumSizeHint().width();
|
---|
165 | }
|
---|
166 |
|
---|
167 | int UIDetailsGroup::minimumHeightHint() const
|
---|
168 | {
|
---|
169 | return m_pContainer->minimumSizeHint().height();
|
---|
170 | }
|
---|
171 |
|
---|
172 | void UIDetailsGroup::sltBuildStep(const QUuid &uStepId, int iStepNumber)
|
---|
173 | {
|
---|
174 | /* Cleanup build-step: */
|
---|
175 | delete m_pBuildStep;
|
---|
176 | m_pBuildStep = 0;
|
---|
177 |
|
---|
178 | /* Is step id valid? */
|
---|
179 | if (uStepId != m_uGroupId)
|
---|
180 | return;
|
---|
181 |
|
---|
182 | /* Step number feats the bounds: */
|
---|
183 | if (iStepNumber >= 0 && iStepNumber < m_machineItems.size())
|
---|
184 | {
|
---|
185 | /* Should we create a new set for this step? */
|
---|
186 | UIDetailsSet *pSet = 0;
|
---|
187 | if (iStepNumber > m_items.size() - 1)
|
---|
188 | pSet = new UIDetailsSet(this);
|
---|
189 | /* Or use existing? */
|
---|
190 | else
|
---|
191 | pSet = m_items.at(iStepNumber)->toSet();
|
---|
192 |
|
---|
193 | /* Create next build-step: */
|
---|
194 | m_pBuildStep = new UIPrepareStep(this, pSet, uStepId, iStepNumber + 1);
|
---|
195 |
|
---|
196 | /* Build set: */
|
---|
197 | pSet->buildSet(m_machineItems[iStepNumber], m_machineItems.size() == 1, model()->categories());
|
---|
198 | }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | /* Notify listener about build done: */
|
---|
202 | emit sigBuildDone();
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | void UIDetailsGroup::addItem(UIDetailsItem *pItem)
|
---|
207 | {
|
---|
208 | switch (pItem->type())
|
---|
209 | {
|
---|
210 | case UIDetailsItemType_Set:
|
---|
211 | {
|
---|
212 | m_pLayout->addItem(pItem);
|
---|
213 | m_items.append(pItem);
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | default:
|
---|
217 | {
|
---|
218 | AssertMsgFailed(("Invalid item type!"));
|
---|
219 | break;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | void UIDetailsGroup::removeItem(UIDetailsItem *pItem)
|
---|
225 | {
|
---|
226 | switch (pItem->type())
|
---|
227 | {
|
---|
228 | case UIDetailsItemType_Set: m_items.removeAt(m_items.indexOf(pItem)); break;
|
---|
229 | default: AssertMsgFailed(("Invalid item type!")); break;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | bool UIDetailsGroup::hasItems(UIDetailsItemType enmType /* = UIDetailsItemType_Set */) const
|
---|
234 | {
|
---|
235 | switch (enmType)
|
---|
236 | {
|
---|
237 | case UIDetailsItemType_Set: return !m_items.isEmpty();
|
---|
238 | case UIDetailsItemType_Any: return hasItems(UIDetailsItemType_Set);
|
---|
239 | default: AssertMsgFailed(("Invalid item type!")); break;
|
---|
240 | }
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | void UIDetailsGroup::clearItems(UIDetailsItemType enmType /* = UIDetailsItemType_Set */)
|
---|
245 | {
|
---|
246 | switch (enmType)
|
---|
247 | {
|
---|
248 | case UIDetailsItemType_Set: while (!m_items.isEmpty()) { delete m_items.last(); } break;
|
---|
249 | case UIDetailsItemType_Any: clearItems(UIDetailsItemType_Set); break;
|
---|
250 | default: AssertMsgFailed(("Invalid item type!")); break;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void UIDetailsGroup::updateGeometry()
|
---|
255 | {
|
---|
256 | /* Update/activate children layout: */
|
---|
257 | m_pLayout->updateGeometry();
|
---|
258 | m_pLayout->activate();
|
---|
259 |
|
---|
260 | /* Call to base class: */
|
---|
261 | UIDetailsItem::updateGeometry();
|
---|
262 |
|
---|
263 | /* Group-item should notify details-view if minimum-width-hint was changed: */
|
---|
264 | int iMinimumWidthHint = minimumWidthHint();
|
---|
265 | if (m_iPreviousMinimumWidthHint != iMinimumWidthHint)
|
---|
266 | {
|
---|
267 | /* Save new minimum-width-hint, notify listener: */
|
---|
268 | m_iPreviousMinimumWidthHint = iMinimumWidthHint;
|
---|
269 | emit sigMinimumWidthHintChanged(m_iPreviousMinimumWidthHint);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | void UIDetailsGroup::prepareConnections()
|
---|
274 | {
|
---|
275 | /* Prepare group-item connections: */
|
---|
276 | connect(this, &UIDetailsGroup::sigMinimumWidthHintChanged,
|
---|
277 | model(), &UIDetailsModel::sigRootItemMinimumWidthHintChanged);
|
---|
278 | }
|
---|