1 | /* $Id: UIMediumSelector.cpp 104226 2024-04-08 12:07:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMediumSelector class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 includes: */
|
---|
29 | #include <QAction>
|
---|
30 | #include <QHeaderView>
|
---|
31 | #include <QMenuBar>
|
---|
32 | #include <QVBoxLayout>
|
---|
33 | #include <QPushButton>
|
---|
34 |
|
---|
35 | /* GUI includes: */
|
---|
36 | #include "QIDialogButtonBox.h"
|
---|
37 | #include "QIFileDialog.h"
|
---|
38 | #include "QIMessageBox.h"
|
---|
39 | #include "QITabWidget.h"
|
---|
40 | #include "QIToolBar.h"
|
---|
41 | #include "QIToolButton.h"
|
---|
42 | #include "UIActionPool.h"
|
---|
43 | #include "UICommon.h"
|
---|
44 | #include "UIDesktopWidgetWatchdog.h"
|
---|
45 | #include "UIExtraDataManager.h"
|
---|
46 | #include "UILoggingDefs.h"
|
---|
47 | #include "UIMediumSearchWidget.h"
|
---|
48 | #include "UIMediumSelector.h"
|
---|
49 | #include "UIMessageCenter.h"
|
---|
50 | #include "UIModalWindowManager.h"
|
---|
51 | #include "UIIconPool.h"
|
---|
52 | #include "UIMedium.h"
|
---|
53 | #include "UIMediumItem.h"
|
---|
54 | #include "UITranslationEventListener.h"
|
---|
55 |
|
---|
56 | /* COM includes: */
|
---|
57 | #include "CMachine.h"
|
---|
58 | #include "CMediumAttachment.h"
|
---|
59 | #include "CMediumFormat.h"
|
---|
60 | #include "CStorageController.h"
|
---|
61 | #include "CSystemProperties.h"
|
---|
62 |
|
---|
63 | #ifdef VBOX_WS_MAC
|
---|
64 | # include "UIWindowMenuManager.h"
|
---|
65 | #endif /* VBOX_WS_MAC */
|
---|
66 |
|
---|
67 |
|
---|
68 | UIMediumSelector::UIMediumSelector(const QUuid &uCurrentMediumId, UIMediumDeviceType enmMediumType, const QString &machineName,
|
---|
69 | const QString &machineSettingsFilePath, const QString &strMachineGuestOSTypeId,
|
---|
70 | const QUuid &uMachineID, QWidget *pParent, UIActionPool *pActionPool)
|
---|
71 | : QIWithRestorableGeometry<QIMainDialog>(pParent)
|
---|
72 | , m_pCentralWidget(0)
|
---|
73 | , m_pMainLayout(0)
|
---|
74 | , m_pTreeWidget(0)
|
---|
75 | , m_enmMediumType(enmMediumType)
|
---|
76 | , m_pButtonBox(0)
|
---|
77 | , m_pCancelButton(0)
|
---|
78 | , m_pChooseButton(0)
|
---|
79 | , m_pLeaveEmptyButton(0)
|
---|
80 | , m_pMainMenu(0)
|
---|
81 | , m_pToolBar(0)
|
---|
82 | , m_pActionAdd(0)
|
---|
83 | , m_pActionCreate(0)
|
---|
84 | , m_pActionRefresh(0)
|
---|
85 | , m_pAttachedSubTreeRoot(0)
|
---|
86 | , m_pNotAttachedSubTreeRoot(0)
|
---|
87 | , m_pParent(pParent)
|
---|
88 | , m_pSearchWidget(0)
|
---|
89 | , m_iCurrentShownIndex(0)
|
---|
90 | , m_strMachineFolder(machineSettingsFilePath)
|
---|
91 | , m_strMachineName(machineName)
|
---|
92 | , m_strMachineGuestOSTypeId(strMachineGuestOSTypeId)
|
---|
93 | , m_uMachineID(uMachineID)
|
---|
94 | , m_pActionPool(pActionPool)
|
---|
95 | , m_iGeometrySaveTimerId(-1)
|
---|
96 | {
|
---|
97 | /* Start full medium-enumeration (if necessary): */
|
---|
98 | if (!uiCommon().isFullMediumEnumerationRequested())
|
---|
99 | uiCommon().enumerateMedia();
|
---|
100 | configure();
|
---|
101 | finalize();
|
---|
102 | selectMedium(uCurrentMediumId);
|
---|
103 | loadSettings();
|
---|
104 | }
|
---|
105 |
|
---|
106 | void UIMediumSelector::setEnableCreateAction(bool fEnable)
|
---|
107 | {
|
---|
108 | if (!m_pActionCreate)
|
---|
109 | return;
|
---|
110 | m_pActionCreate->setEnabled(fEnable);
|
---|
111 | m_pActionCreate->setVisible(fEnable);
|
---|
112 | }
|
---|
113 |
|
---|
114 | QList<QUuid> UIMediumSelector::selectedMediumIds() const
|
---|
115 | {
|
---|
116 | QList<QUuid> selectedIds;
|
---|
117 | if (!m_pTreeWidget)
|
---|
118 | return selectedIds;
|
---|
119 | QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
|
---|
120 | for (int i = 0; i < selectedItems.size(); ++i)
|
---|
121 | {
|
---|
122 | UIMediumItem *item = dynamic_cast<UIMediumItem*>(selectedItems.at(i));
|
---|
123 | if (item)
|
---|
124 | selectedIds.push_back(item->medium().id());
|
---|
125 | }
|
---|
126 | return selectedIds;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /* static */
|
---|
130 | int UIMediumSelector::openMediumSelectorDialog(QWidget *pParent, UIMediumDeviceType enmMediumType, const QUuid &uCurrentMediumId,
|
---|
131 | QUuid &uSelectedMediumUuid, const QString &strMachineFolder, const QString &strMachineName,
|
---|
132 | const QString &strMachineGuestOSTypeId, bool fEnableCreate, const QUuid &uMachineID,
|
---|
133 | UIActionPool *pActionPool)
|
---|
134 | {
|
---|
135 | QUuid uMachineOrGlobalId = uMachineID == QUuid() ? gEDataManager->GlobalID : uMachineID;
|
---|
136 |
|
---|
137 | QWidget *pDialogParent = windowManager().realParentWindow(pParent);
|
---|
138 | QPointer<UIMediumSelector> pSelector = new UIMediumSelector(uCurrentMediumId, enmMediumType, strMachineName,
|
---|
139 | strMachineFolder, strMachineGuestOSTypeId,
|
---|
140 | uMachineOrGlobalId, pDialogParent, pActionPool);
|
---|
141 |
|
---|
142 | if (!pSelector)
|
---|
143 | return static_cast<int>(UIMediumSelector::ReturnCode_Rejected);
|
---|
144 | pSelector->setEnableCreateAction(fEnableCreate);
|
---|
145 | windowManager().registerNewParent(pSelector, pDialogParent);
|
---|
146 |
|
---|
147 | int iResult = pSelector->exec(false);
|
---|
148 | UIMediumSelector::ReturnCode returnCode;
|
---|
149 |
|
---|
150 | if (iResult >= static_cast<int>(UIMediumSelector::ReturnCode_Max) || iResult < 0)
|
---|
151 | returnCode = UIMediumSelector::ReturnCode_Rejected;
|
---|
152 | else
|
---|
153 | returnCode = static_cast<UIMediumSelector::ReturnCode>(iResult);
|
---|
154 |
|
---|
155 | if (returnCode == UIMediumSelector::ReturnCode_Accepted)
|
---|
156 | {
|
---|
157 | QList<QUuid> selectedMediumIds = pSelector->selectedMediumIds();
|
---|
158 |
|
---|
159 | /* Currently we only care about the 0th since we support single selection by intention: */
|
---|
160 | if (selectedMediumIds.isEmpty())
|
---|
161 | returnCode = UIMediumSelector::ReturnCode_Rejected;
|
---|
162 | else
|
---|
163 | {
|
---|
164 | uSelectedMediumUuid = selectedMediumIds[0];
|
---|
165 | uiCommon().updateRecentlyUsedMediumListAndFolder(enmMediumType, uiCommon().medium(uSelectedMediumUuid).location());
|
---|
166 | }
|
---|
167 | }
|
---|
168 | delete pSelector;
|
---|
169 | return static_cast<int>(returnCode);
|
---|
170 | }
|
---|
171 |
|
---|
172 | void UIMediumSelector::sltRetranslateUI()
|
---|
173 | {
|
---|
174 | if (m_pCancelButton)
|
---|
175 | {
|
---|
176 | m_pCancelButton->setText(tr("&Cancel"));
|
---|
177 | m_pCancelButton->setToolTip(tr("Cancel"));
|
---|
178 | }
|
---|
179 | if (m_pLeaveEmptyButton)
|
---|
180 | {
|
---|
181 | m_pLeaveEmptyButton->setText(tr("Leave &Empty"));
|
---|
182 | m_pLeaveEmptyButton->setToolTip(tr("Leave the drive empty"));
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (m_pChooseButton)
|
---|
186 | {
|
---|
187 | m_pChooseButton->setText(tr("C&hoose"));
|
---|
188 | m_pChooseButton->setToolTip(tr("Attach the selected medium to the drive"));
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (m_pTreeWidget)
|
---|
192 | {
|
---|
193 | m_pTreeWidget->headerItem()->setText(0, tr("Name"));
|
---|
194 | m_pTreeWidget->headerItem()->setText(1, tr("Virtual Size"));
|
---|
195 | m_pTreeWidget->headerItem()->setText(2, tr("Actual Size"));
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | bool UIMediumSelector::event(QEvent *pEvent)
|
---|
200 | {
|
---|
201 | if (pEvent->type() == QEvent::Resize || pEvent->type() == QEvent::Move)
|
---|
202 | {
|
---|
203 | if (m_iGeometrySaveTimerId != -1)
|
---|
204 | killTimer(m_iGeometrySaveTimerId);
|
---|
205 | m_iGeometrySaveTimerId = startTimer(300);
|
---|
206 | }
|
---|
207 | else if (pEvent->type() == QEvent::Timer)
|
---|
208 | {
|
---|
209 | QTimerEvent *pTimerEvent = static_cast<QTimerEvent*>(pEvent);
|
---|
210 | if (pTimerEvent->timerId() == m_iGeometrySaveTimerId)
|
---|
211 | {
|
---|
212 | killTimer(m_iGeometrySaveTimerId);
|
---|
213 | m_iGeometrySaveTimerId = -1;
|
---|
214 | saveDialogGeometry();
|
---|
215 | }
|
---|
216 | }
|
---|
217 | return QIWithRestorableGeometry<QIMainDialog>::event(pEvent);
|
---|
218 | }
|
---|
219 |
|
---|
220 | void UIMediumSelector::configure()
|
---|
221 | {
|
---|
222 | #ifndef VBOX_WS_MAC
|
---|
223 | /* Assign window icon: */
|
---|
224 | setWindowIcon(UIIconPool::iconSetFull(":/media_manager_32px.png", ":/media_manager_16px.png"));
|
---|
225 | #endif
|
---|
226 |
|
---|
227 | setTitle();
|
---|
228 | prepareWidgets();
|
---|
229 | prepareActions();
|
---|
230 | prepareMenuAndToolBar();
|
---|
231 | prepareConnections();
|
---|
232 | }
|
---|
233 |
|
---|
234 | void UIMediumSelector::prepareActions()
|
---|
235 | {
|
---|
236 | if (!m_pActionPool)
|
---|
237 | return;
|
---|
238 |
|
---|
239 | switch (m_enmMediumType)
|
---|
240 | {
|
---|
241 | case UIMediumDeviceType_DVD:
|
---|
242 | m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddCD);
|
---|
243 | m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateCD);
|
---|
244 | break;
|
---|
245 | case UIMediumDeviceType_Floppy:
|
---|
246 | m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddFD);
|
---|
247 | m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateFD);
|
---|
248 | break;
|
---|
249 | case UIMediumDeviceType_HardDisk:
|
---|
250 | case UIMediumDeviceType_All:
|
---|
251 | case UIMediumDeviceType_Invalid:
|
---|
252 | default:
|
---|
253 | m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddHD);
|
---|
254 | m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateHD);
|
---|
255 | break;
|
---|
256 | }
|
---|
257 |
|
---|
258 | m_pActionRefresh = m_pActionPool->action(UIActionIndex_M_MediumSelector_Refresh);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void UIMediumSelector::prepareMenuAndToolBar()
|
---|
262 | {
|
---|
263 | if (!m_pMainMenu || !m_pToolBar)
|
---|
264 | return;
|
---|
265 |
|
---|
266 | m_pMainMenu->addAction(m_pActionAdd);
|
---|
267 | m_pMainMenu->addAction(m_pActionCreate);
|
---|
268 | m_pMainMenu->addSeparator();
|
---|
269 | m_pMainMenu->addAction(m_pActionRefresh);
|
---|
270 |
|
---|
271 | m_pToolBar->addAction(m_pActionAdd);
|
---|
272 | if (!(gEDataManager->restrictedDialogTypes(m_uMachineID) & UIExtraDataMetaDefs::DialogType_VISOCreator))
|
---|
273 | m_pToolBar->addAction(m_pActionCreate);
|
---|
274 | m_pToolBar->addSeparator();
|
---|
275 | m_pToolBar->addAction(m_pActionRefresh);
|
---|
276 | }
|
---|
277 |
|
---|
278 | void UIMediumSelector::prepareConnections()
|
---|
279 | {
|
---|
280 | /* Configure medium-enumeration connections: */
|
---|
281 | connect(&uiCommon(), &UICommon::sigMediumCreated,
|
---|
282 | this, &UIMediumSelector::sltHandleMediumCreated);
|
---|
283 | connect(&uiCommon(), &UICommon::sigMediumEnumerationStarted,
|
---|
284 | this, &UIMediumSelector::sltHandleMediumEnumerationStart);
|
---|
285 | connect(&uiCommon(), &UICommon::sigMediumEnumerated,
|
---|
286 | this, &UIMediumSelector::sltHandleMediumEnumerated);
|
---|
287 | connect(&uiCommon(), &UICommon::sigMediumEnumerationFinished,
|
---|
288 | this, &UIMediumSelector::sltHandleMediumEnumerationFinish);
|
---|
289 | if (m_pActionAdd)
|
---|
290 | connect(m_pActionAdd, &QAction::triggered, this, &UIMediumSelector::sltAddMedium);
|
---|
291 | if (m_pActionCreate)
|
---|
292 | connect(m_pActionCreate, &QAction::triggered, this, &UIMediumSelector::sltCreateMedium);
|
---|
293 | if (m_pActionRefresh)
|
---|
294 | connect(m_pActionRefresh, &QAction::triggered, this, &UIMediumSelector::sltHandleRefresh);
|
---|
295 |
|
---|
296 | if (m_pTreeWidget)
|
---|
297 | {
|
---|
298 | connect(m_pTreeWidget, &QITreeWidget::itemSelectionChanged, this, &UIMediumSelector::sltHandleItemSelectionChanged);
|
---|
299 | connect(m_pTreeWidget, &QITreeWidget::itemDoubleClicked, this, &UIMediumSelector::sltHandleTreeWidgetDoubleClick);
|
---|
300 | connect(m_pTreeWidget, &QITreeWidget::customContextMenuRequested, this, &UIMediumSelector::sltHandleTreeContextMenuRequest);
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (m_pCancelButton)
|
---|
304 | connect(m_pCancelButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonCancel);
|
---|
305 | if (m_pChooseButton)
|
---|
306 | connect(m_pChooseButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonChoose);
|
---|
307 | if (m_pLeaveEmptyButton)
|
---|
308 | connect(m_pLeaveEmptyButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonLeaveEmpty);
|
---|
309 |
|
---|
310 | if (m_pSearchWidget)
|
---|
311 | {
|
---|
312 | connect(m_pSearchWidget, &UIMediumSearchWidget::sigPerformSearch,
|
---|
313 | this, &UIMediumSelector::sltHandlePerformSearch);
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | UIMediumItem* UIMediumSelector::addTreeItem(const UIMedium &medium, QITreeWidgetItem *pParent)
|
---|
318 | {
|
---|
319 | if (!pParent)
|
---|
320 | return 0;
|
---|
321 | switch (m_enmMediumType)
|
---|
322 | {
|
---|
323 | case UIMediumDeviceType_DVD:
|
---|
324 | return new UIMediumItemCD(medium, pParent);
|
---|
325 | break;
|
---|
326 | case UIMediumDeviceType_Floppy:
|
---|
327 | return new UIMediumItemFD(medium, pParent);
|
---|
328 | break;
|
---|
329 | case UIMediumDeviceType_HardDisk:
|
---|
330 | case UIMediumDeviceType_All:
|
---|
331 | case UIMediumDeviceType_Invalid:
|
---|
332 | default:
|
---|
333 | return createHardDiskItem(medium, pParent);
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | UIMediumItem* UIMediumSelector::createHardDiskItem(const UIMedium &medium, QITreeWidgetItem *pParent)
|
---|
339 | {
|
---|
340 | if (medium.medium().isNull())
|
---|
341 | return 0;
|
---|
342 | if (!m_pTreeWidget)
|
---|
343 | return 0;
|
---|
344 | /* Search the tree to see if we already have the item: */
|
---|
345 | UIMediumItem *pMediumItem = searchItem(0, medium.id());
|
---|
346 | if (pMediumItem)
|
---|
347 | return pMediumItem;
|
---|
348 | /* Check if the corresponding medium has a parent */
|
---|
349 | if (medium.parentID() != UIMedium::nullID())
|
---|
350 | {
|
---|
351 | UIMediumItem *pParentMediumItem = searchItem(0, medium.parentID());
|
---|
352 | /* If parent medium-item was not found we create it: */
|
---|
353 | if (!pParentMediumItem)
|
---|
354 | {
|
---|
355 | /* Make sure corresponding parent medium is already cached! */
|
---|
356 | UIMedium parentMedium = uiCommon().medium(medium.parentID());
|
---|
357 | if (parentMedium.isNull())
|
---|
358 | AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toString().toUtf8().constData()));
|
---|
359 | /* Try to create parent medium-item: */
|
---|
360 | else
|
---|
361 | pParentMediumItem = createHardDiskItem(parentMedium, pParent);
|
---|
362 | }
|
---|
363 | if (pParentMediumItem)
|
---|
364 | {
|
---|
365 | pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
|
---|
366 | LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
|
---|
367 | }
|
---|
368 | else
|
---|
369 | AssertMsgFailed(("Parent medium with ID={%s} could not be created!\n", medium.parentID().toString().toUtf8().constData()));
|
---|
370 | }
|
---|
371 |
|
---|
372 | /* No parents, thus just create item as top-level one: */
|
---|
373 | else
|
---|
374 | {
|
---|
375 | pMediumItem = new UIMediumItemHD(medium, pParent);
|
---|
376 | LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
|
---|
377 | }
|
---|
378 | return pMediumItem;
|
---|
379 | }
|
---|
380 |
|
---|
381 | void UIMediumSelector::restoreSelection(const QList<QUuid> &selectedMediums, QVector<UIMediumItem*> &mediumList)
|
---|
382 | {
|
---|
383 | if (!m_pTreeWidget)
|
---|
384 | return;
|
---|
385 | if (selectedMediums.isEmpty())
|
---|
386 | {
|
---|
387 | m_pTreeWidget->setCurrentItem(0);
|
---|
388 | return;
|
---|
389 | }
|
---|
390 | bool selected = false;
|
---|
391 | for (int i = 0; i < mediumList.size(); ++i)
|
---|
392 | {
|
---|
393 | if (!mediumList[i])
|
---|
394 | continue;
|
---|
395 | if (selectedMediums.contains(mediumList[i]->medium().id()))
|
---|
396 | {
|
---|
397 | mediumList[i]->setSelected(true);
|
---|
398 | selected = true;
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (!selected)
|
---|
403 | m_pTreeWidget->setCurrentItem(0);
|
---|
404 | }
|
---|
405 |
|
---|
406 | void UIMediumSelector::prepareWidgets()
|
---|
407 | {
|
---|
408 | m_pCentralWidget = new QWidget;
|
---|
409 | if (!m_pCentralWidget)
|
---|
410 | return;
|
---|
411 | setCentralWidget(m_pCentralWidget);
|
---|
412 |
|
---|
413 | m_pMainLayout = new QVBoxLayout;
|
---|
414 | m_pCentralWidget->setLayout(m_pMainLayout);
|
---|
415 |
|
---|
416 | if (!m_pMainLayout || !menuBar())
|
---|
417 | return;
|
---|
418 |
|
---|
419 | if (m_pActionPool && m_pActionPool->action(UIActionIndex_M_MediumSelector))
|
---|
420 | {
|
---|
421 | m_pMainMenu = m_pActionPool->action(UIActionIndex_M_MediumSelector)->menu();
|
---|
422 | if (m_pMainMenu)
|
---|
423 | menuBar()->addMenu(m_pMainMenu);
|
---|
424 | }
|
---|
425 |
|
---|
426 | m_pToolBar = new QIToolBar;
|
---|
427 | if (m_pToolBar)
|
---|
428 | {
|
---|
429 | /* Configure toolbar: */
|
---|
430 | const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize));
|
---|
431 | m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
|
---|
432 | m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
---|
433 | m_pMainLayout->addWidget(m_pToolBar);
|
---|
434 | }
|
---|
435 |
|
---|
436 | m_pTreeWidget = new QITreeWidget;
|
---|
437 | if (m_pTreeWidget)
|
---|
438 | {
|
---|
439 | m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
440 | m_pMainLayout->addWidget(m_pTreeWidget);
|
---|
441 | m_pTreeWidget->setAlternatingRowColors(true);
|
---|
442 | int iColumnCount = (m_enmMediumType == UIMediumDeviceType_HardDisk) ? 3 : 2;
|
---|
443 | m_pTreeWidget->setColumnCount(iColumnCount);
|
---|
444 | m_pTreeWidget->setSortingEnabled(true);
|
---|
445 | m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
|
---|
446 | m_pTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
---|
447 | }
|
---|
448 |
|
---|
449 | m_pSearchWidget = new UIMediumSearchWidget;
|
---|
450 | if (m_pSearchWidget)
|
---|
451 | {
|
---|
452 | m_pMainLayout->addWidget(m_pSearchWidget);
|
---|
453 | }
|
---|
454 |
|
---|
455 | m_pButtonBox = new QIDialogButtonBox;
|
---|
456 | if (m_pButtonBox)
|
---|
457 | {
|
---|
458 | /* Configure button-box: */
|
---|
459 | m_pCancelButton = m_pButtonBox->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
|
---|
460 |
|
---|
461 | /* Only DVDs and Floppies can be left empty: */
|
---|
462 | if (m_enmMediumType == UIMediumDeviceType_DVD || m_enmMediumType == UIMediumDeviceType_Floppy)
|
---|
463 | m_pLeaveEmptyButton = m_pButtonBox->addButton(tr("Leave Empty"), QDialogButtonBox::ActionRole);
|
---|
464 |
|
---|
465 | m_pChooseButton = m_pButtonBox->addButton(tr("Choose"), QDialogButtonBox::AcceptRole);
|
---|
466 | m_pCancelButton->setShortcut(Qt::Key_Escape);
|
---|
467 |
|
---|
468 | /* Add button-box into main layout: */
|
---|
469 | m_pMainLayout->addWidget(m_pButtonBox);
|
---|
470 | }
|
---|
471 |
|
---|
472 | repopulateTreeWidget();
|
---|
473 | }
|
---|
474 |
|
---|
475 | void UIMediumSelector::sltButtonChoose()
|
---|
476 | {
|
---|
477 | done(static_cast<int>(ReturnCode_Accepted));
|
---|
478 | }
|
---|
479 |
|
---|
480 | void UIMediumSelector::sltButtonCancel()
|
---|
481 | {
|
---|
482 | done(static_cast<int>(ReturnCode_Rejected));
|
---|
483 | }
|
---|
484 |
|
---|
485 | void UIMediumSelector::sltButtonLeaveEmpty()
|
---|
486 | {
|
---|
487 | done(static_cast<int>(ReturnCode_LeftEmpty));
|
---|
488 | }
|
---|
489 |
|
---|
490 | void UIMediumSelector::sltAddMedium()
|
---|
491 | {
|
---|
492 | QUuid uMediumID = uiCommon().openMediumWithFileOpenDialog(m_enmMediumType, this, m_strMachineFolder, true /* fUseLastFolder */);
|
---|
493 | if (uMediumID.isNull())
|
---|
494 | return;
|
---|
495 | repopulateTreeWidget();
|
---|
496 | selectMedium(uMediumID);
|
---|
497 | }
|
---|
498 |
|
---|
499 | void UIMediumSelector::sltCreateMedium()
|
---|
500 | {
|
---|
501 | QUuid uMediumId = uiCommon().openMediumCreatorDialog(m_pActionPool, this, m_enmMediumType, m_strMachineFolder,
|
---|
502 | m_strMachineName, m_strMachineGuestOSTypeId);
|
---|
503 | /* Make sure that the data structure is updated and newly created medium is selected and visible: */
|
---|
504 | sltHandleMediumCreated(uMediumId);
|
---|
505 | }
|
---|
506 |
|
---|
507 | void UIMediumSelector::sltHandleItemSelectionChanged()
|
---|
508 | {
|
---|
509 | updateChooseButton();
|
---|
510 | }
|
---|
511 |
|
---|
512 | void UIMediumSelector::sltHandleTreeWidgetDoubleClick(QTreeWidgetItem * item, int column)
|
---|
513 | {
|
---|
514 | Q_UNUSED(column);
|
---|
515 | if (!dynamic_cast<UIMediumItem*>(item))
|
---|
516 | return;
|
---|
517 | accept();
|
---|
518 | }
|
---|
519 |
|
---|
520 | void UIMediumSelector::sltHandleMediumCreated(const QUuid &uMediumId)
|
---|
521 | {
|
---|
522 | if (uMediumId.isNull())
|
---|
523 | return;
|
---|
524 | /* Update the tree widget making sure we show the new item: */
|
---|
525 | repopulateTreeWidget();
|
---|
526 | /* Select the new item: */
|
---|
527 | selectMedium(uMediumId);
|
---|
528 | /* Update the search: */
|
---|
529 | m_pSearchWidget->search(m_pTreeWidget);
|
---|
530 | }
|
---|
531 |
|
---|
532 | void UIMediumSelector::sltHandleMediumEnumerationStart()
|
---|
533 | {
|
---|
534 | /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
|
---|
535 | if (m_pActionRefresh)
|
---|
536 | m_pActionRefresh->setEnabled(false);
|
---|
537 | }
|
---|
538 |
|
---|
539 | void UIMediumSelector::sltHandleMediumEnumerated()
|
---|
540 | {
|
---|
541 | }
|
---|
542 |
|
---|
543 | void UIMediumSelector::sltHandleMediumEnumerationFinish()
|
---|
544 | {
|
---|
545 | repopulateTreeWidget();
|
---|
546 | if (m_pActionRefresh)
|
---|
547 | m_pActionRefresh->setEnabled(true);
|
---|
548 | }
|
---|
549 |
|
---|
550 | void UIMediumSelector::sltHandleRefresh()
|
---|
551 | {
|
---|
552 | /* Restart full medium-enumeration: */
|
---|
553 | uiCommon().enumerateMedia();
|
---|
554 | /* Update the search: */
|
---|
555 | m_pSearchWidget->search(m_pTreeWidget);
|
---|
556 | }
|
---|
557 |
|
---|
558 | void UIMediumSelector::sltHandlePerformSearch()
|
---|
559 | {
|
---|
560 | if (!m_pSearchWidget)
|
---|
561 | return;
|
---|
562 | m_pSearchWidget->search(m_pTreeWidget);
|
---|
563 | }
|
---|
564 |
|
---|
565 | void UIMediumSelector::sltHandleTreeContextMenuRequest(const QPoint &point)
|
---|
566 | {
|
---|
567 | QWidget *pSender = qobject_cast<QWidget*>(sender());
|
---|
568 | if (!pSender)
|
---|
569 | return;
|
---|
570 |
|
---|
571 | QMenu menu;
|
---|
572 | QAction *pExpandAll = menu.addAction(tr("Expand All"));
|
---|
573 | QAction *pCollapseAll = menu.addAction(tr("Collapse All"));
|
---|
574 | if (!pExpandAll || !pCollapseAll)
|
---|
575 | return;
|
---|
576 |
|
---|
577 | pExpandAll->setIcon(UIIconPool::iconSet(":/expand_all_16px.png"));
|
---|
578 | pCollapseAll->setIcon(UIIconPool::iconSet(":/collapse_all_16px.png"));
|
---|
579 |
|
---|
580 | connect(pExpandAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeExpandAllSignal);
|
---|
581 | connect(pCollapseAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeCollapseAllSignal);
|
---|
582 |
|
---|
583 | menu.exec(pSender->mapToGlobal(point));
|
---|
584 | }
|
---|
585 |
|
---|
586 | void UIMediumSelector::sltHandleTreeExpandAllSignal()
|
---|
587 | {
|
---|
588 | if (m_pTreeWidget)
|
---|
589 | m_pTreeWidget->expandAll();
|
---|
590 | }
|
---|
591 |
|
---|
592 | void UIMediumSelector::sltHandleTreeCollapseAllSignal()
|
---|
593 | {
|
---|
594 | if (m_pTreeWidget)
|
---|
595 | m_pTreeWidget->collapseAll();
|
---|
596 |
|
---|
597 | if (m_pAttachedSubTreeRoot)
|
---|
598 | m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pAttachedSubTreeRoot), true);
|
---|
599 | if (m_pNotAttachedSubTreeRoot)
|
---|
600 | m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pNotAttachedSubTreeRoot), true);
|
---|
601 | }
|
---|
602 |
|
---|
603 | void UIMediumSelector::selectMedium(const QUuid &uMediumID)
|
---|
604 | {
|
---|
605 | if (!m_pTreeWidget || uMediumID.isNull())
|
---|
606 | return;
|
---|
607 | UIMediumItem *pMediumItem = searchItem(0, uMediumID);
|
---|
608 | if (pMediumItem)
|
---|
609 | {
|
---|
610 | m_pTreeWidget->setCurrentItem(pMediumItem);
|
---|
611 | QModelIndex itemIndex = m_pTreeWidget->itemIndex(pMediumItem);
|
---|
612 | if (itemIndex.isValid())
|
---|
613 | m_pTreeWidget->scrollTo(itemIndex, QAbstractItemView::PositionAtCenter);
|
---|
614 | }
|
---|
615 | }
|
---|
616 |
|
---|
617 | void UIMediumSelector::updateChooseButton()
|
---|
618 | {
|
---|
619 | if (!m_pTreeWidget || !m_pChooseButton)
|
---|
620 | return;
|
---|
621 | QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
|
---|
622 | if (selectedItems.isEmpty())
|
---|
623 | {
|
---|
624 | m_pChooseButton->setEnabled(false);
|
---|
625 | return;
|
---|
626 | }
|
---|
627 |
|
---|
628 | /* check if at least one of the selected items is a UIMediumItem */
|
---|
629 | bool mediumItemSelected = false;
|
---|
630 | for (int i = 0; i < selectedItems.size() && !mediumItemSelected; ++i)
|
---|
631 | {
|
---|
632 | if (dynamic_cast<UIMediumItem*>(selectedItems.at(i)))
|
---|
633 | mediumItemSelected = true;
|
---|
634 | }
|
---|
635 | if (mediumItemSelected)
|
---|
636 | m_pChooseButton->setEnabled(true);
|
---|
637 | else
|
---|
638 | m_pChooseButton->setEnabled(false);
|
---|
639 | }
|
---|
640 |
|
---|
641 | void UIMediumSelector::finalize()
|
---|
642 | {
|
---|
643 | /* Apply language settings: */
|
---|
644 | sltRetranslateUI();
|
---|
645 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
646 | this, &UIMediumSelector::sltRetranslateUI);
|
---|
647 | }
|
---|
648 |
|
---|
649 | void UIMediumSelector::showEvent(QShowEvent *pEvent)
|
---|
650 | {
|
---|
651 | Q_UNUSED(pEvent);
|
---|
652 |
|
---|
653 | if (m_pTreeWidget)
|
---|
654 | m_pTreeWidget->setFocus();
|
---|
655 | }
|
---|
656 |
|
---|
657 | void UIMediumSelector::repopulateTreeWidget()
|
---|
658 | {
|
---|
659 | if (!m_pTreeWidget)
|
---|
660 | return;
|
---|
661 | /* Cache the currently selected items: */
|
---|
662 | QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
|
---|
663 | QList<QUuid> selectedMedia = selectedMediumIds();
|
---|
664 | /* uuid list of selected items: */
|
---|
665 | /* Reset the related data structure: */
|
---|
666 | m_mediumItemList.clear();
|
---|
667 | m_pTreeWidget->clear();
|
---|
668 | m_pAttachedSubTreeRoot = 0;
|
---|
669 | m_pNotAttachedSubTreeRoot = 0;
|
---|
670 | QVector<UIMediumItem*> menuItemVector;
|
---|
671 | foreach (const QUuid &uMediumID, uiCommon().mediumIDs())
|
---|
672 | {
|
---|
673 | UIMedium medium = uiCommon().medium(uMediumID);
|
---|
674 | if (medium.type() == m_enmMediumType)
|
---|
675 | {
|
---|
676 | bool isMediumAttached = !(medium.medium().GetMachineIds().isEmpty());
|
---|
677 | QITreeWidgetItem *pParent = 0;
|
---|
678 | if (isMediumAttached)
|
---|
679 | {
|
---|
680 | if (!m_pAttachedSubTreeRoot)
|
---|
681 | {
|
---|
682 | QStringList strList;
|
---|
683 | strList << "Attached";
|
---|
684 | m_pAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
|
---|
685 | }
|
---|
686 | pParent = m_pAttachedSubTreeRoot;
|
---|
687 |
|
---|
688 | }
|
---|
689 | else
|
---|
690 | {
|
---|
691 | if (!m_pNotAttachedSubTreeRoot)
|
---|
692 | {
|
---|
693 | QStringList strList;
|
---|
694 | strList << "Not Attached";
|
---|
695 | m_pNotAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
|
---|
696 | }
|
---|
697 | pParent = m_pNotAttachedSubTreeRoot;
|
---|
698 | }
|
---|
699 | UIMediumItem *treeItem = addTreeItem(medium, pParent);
|
---|
700 | m_mediumItemList.append(treeItem);
|
---|
701 | menuItemVector.push_back(treeItem);
|
---|
702 | }
|
---|
703 | }
|
---|
704 | restoreSelection(selectedMedia, menuItemVector);
|
---|
705 | saveDefaultForeground();
|
---|
706 | updateChooseButton();
|
---|
707 | if (m_pAttachedSubTreeRoot)
|
---|
708 | m_pTreeWidget->expandItem(m_pAttachedSubTreeRoot);
|
---|
709 | if (m_pNotAttachedSubTreeRoot)
|
---|
710 | m_pTreeWidget->expandItem(m_pNotAttachedSubTreeRoot);
|
---|
711 | m_pTreeWidget->resizeColumnToContents(0);
|
---|
712 | }
|
---|
713 |
|
---|
714 | void UIMediumSelector::saveDefaultForeground()
|
---|
715 | {
|
---|
716 | if (!m_pTreeWidget)
|
---|
717 | return;
|
---|
718 | if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
|
---|
719 | {
|
---|
720 | QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
|
---|
721 | if (item)
|
---|
722 | {
|
---|
723 | QVariant data = item->data(0, Qt::ForegroundRole);
|
---|
724 | if (data.canConvert<QBrush>())
|
---|
725 | {
|
---|
726 | m_defaultItemForeground = data.value<QBrush>();
|
---|
727 | }
|
---|
728 | }
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QUuid &mediumId)
|
---|
733 | {
|
---|
734 | if (!m_pTreeWidget)
|
---|
735 | return 0;
|
---|
736 | if (!pParent)
|
---|
737 | pParent = m_pTreeWidget->invisibleRootItem();
|
---|
738 | if (!pParent)
|
---|
739 | return 0;
|
---|
740 |
|
---|
741 | for (int i = 0; i < pParent->childCount(); ++i)
|
---|
742 | {
|
---|
743 | QTreeWidgetItem *pChild = pParent->child(i);
|
---|
744 | if (!pChild)
|
---|
745 | continue;
|
---|
746 | UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
|
---|
747 | if (mediumItem)
|
---|
748 | {
|
---|
749 | if (mediumItem->id() == mediumId)
|
---|
750 | return mediumItem;
|
---|
751 | }
|
---|
752 | UIMediumItem *pResult = searchItem(pChild, mediumId);
|
---|
753 | if (pResult)
|
---|
754 | return pResult;
|
---|
755 | }
|
---|
756 | return 0;
|
---|
757 | }
|
---|
758 |
|
---|
759 | void UIMediumSelector::setTitle()
|
---|
760 | {
|
---|
761 | switch (m_enmMediumType)
|
---|
762 | {
|
---|
763 | case UIMediumDeviceType_DVD:
|
---|
764 | if (!m_strMachineName.isEmpty())
|
---|
765 | setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Optical Disk Selector")));
|
---|
766 | else
|
---|
767 | setWindowTitle(QString("%1").arg(tr("Optical Disk Selector")));
|
---|
768 | break;
|
---|
769 | case UIMediumDeviceType_Floppy:
|
---|
770 | if (!m_strMachineName.isEmpty())
|
---|
771 | setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Selector")));
|
---|
772 | else
|
---|
773 | setWindowTitle(QString("%1").arg(tr("Floppy Disk Selector")));
|
---|
774 | break;
|
---|
775 | case UIMediumDeviceType_HardDisk:
|
---|
776 | if (!m_strMachineName.isEmpty())
|
---|
777 | setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Hard Disk Selector")));
|
---|
778 | else
|
---|
779 | setWindowTitle(QString("%1").arg(tr("Hard Disk Selector")));
|
---|
780 | break;
|
---|
781 | case UIMediumDeviceType_All:
|
---|
782 | case UIMediumDeviceType_Invalid:
|
---|
783 | default:
|
---|
784 | if (!m_strMachineName.isEmpty())
|
---|
785 | setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Virtual Medium Selector")));
|
---|
786 | else
|
---|
787 | setWindowTitle(QString("%1").arg(tr("Virtual Medium Selector")));
|
---|
788 | break;
|
---|
789 | }
|
---|
790 | }
|
---|
791 |
|
---|
792 | void UIMediumSelector::saveDialogGeometry()
|
---|
793 | {
|
---|
794 | const QRect geo = currentGeometry();
|
---|
795 | LogRel2(("GUI: UIMediumSelector: Saving geometry as: Origin=%dx%d, Size=%dx%d\n",
|
---|
796 | geo.x(), geo.y(), geo.width(), geo.height()));
|
---|
797 | gEDataManager->setMediumSelectorDialogGeometry(geo, isCurrentlyMaximized());
|
---|
798 | }
|
---|
799 |
|
---|
800 | void UIMediumSelector::loadSettings()
|
---|
801 | {
|
---|
802 | const QRect availableGeo = gpDesktop->availableGeometry(this);
|
---|
803 | int iDefaultWidth = availableGeo.width() / 2;
|
---|
804 | int iDefaultHeight = availableGeo.height() * 3 / 4;
|
---|
805 | QRect defaultGeo(0, 0, iDefaultWidth, iDefaultHeight);
|
---|
806 |
|
---|
807 | QWidget *pParent = windowManager().realParentWindow(m_pParent ? m_pParent : windowManager().mainWindowShown());
|
---|
808 | /* Load geometry from extradata: */
|
---|
809 | const QRect geo = gEDataManager->mediumSelectorDialogGeometry(this, pParent, defaultGeo);
|
---|
810 | LogRel2(("GUI: UISoftKeyboard: Restoring geometry to: Origin=%dx%d, Size=%dx%d\n",
|
---|
811 | geo.x(), geo.y(), geo.width(), geo.height()));
|
---|
812 |
|
---|
813 | restoreGeometry(geo);
|
---|
814 | }
|
---|