VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/medium/UIMediumSelector.cpp@ 92430

Last change on this file since 92430 was 92430, checked in by vboxsync, 3 years ago

FE/Qt: bugref:10141. Migrating medium selector's menu to the action pool.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.4 KB
Line 
1/* $Id: UIMediumSelector.cpp 92430 2021-11-15 14:49:05Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMediumSelector class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* Qt includes: */
19#include <QAction>
20#include <QHeaderView>
21#include <QMenuBar>
22#include <QVBoxLayout>
23#include <QPushButton>
24
25/* GUI includes: */
26#include "QIDialogButtonBox.h"
27#include "QIFileDialog.h"
28#include "QIMessageBox.h"
29#include "QITabWidget.h"
30#include "QIToolButton.h"
31#include "UIActionPool.h"
32#include "UICommon.h"
33#include "UIDesktopWidgetWatchdog.h"
34#include "UIExtraDataManager.h"
35#include "UIMediumSearchWidget.h"
36#include "UIMediumSelector.h"
37#include "UIMessageCenter.h"
38#include "UIIconPool.h"
39#include "UIMedium.h"
40#include "UIMediumItem.h"
41#include "QIToolBar.h"
42
43/* COM includes: */
44#include "COMEnums.h"
45#include "CMachine.h"
46#include "CMediumAttachment.h"
47#include "CMediumFormat.h"
48#include "CStorageController.h"
49#include "CSystemProperties.h"
50
51#ifdef VBOX_WS_MAC
52# include "UIWindowMenuManager.h"
53#endif /* VBOX_WS_MAC */
54
55
56UIMediumSelector::UIMediumSelector(const QUuid &uCurrentMediumId, UIMediumDeviceType enmMediumType, const QString &machineName,
57 const QString &machineSettingsFilePath, const QString &strMachineGuestOSTypeId,
58 const QUuid &uMachineID, QWidget *pParent, UIActionPool *pActionPool)
59 :QIWithRetranslateUI<QIMainDialog>(pParent)
60 , m_pCentralWidget(0)
61 , m_pMainLayout(0)
62 , m_pTreeWidget(0)
63 , m_enmMediumType(enmMediumType)
64 , m_pButtonBox(0)
65 , m_pCancelButton(0)
66 , m_pChooseButton(0)
67 , m_pLeaveEmptyButton(0)
68 , m_pMainMenu(0)
69 , m_pToolBar(0)
70 , m_pActionAdd(0)
71 , m_pActionCreate(0)
72 , m_pActionRefresh(0)
73 , m_pAttachedSubTreeRoot(0)
74 , m_pNotAttachedSubTreeRoot(0)
75 , m_pParent(pParent)
76 , m_pSearchWidget(0)
77 , m_iCurrentShownIndex(0)
78 , m_strMachineFolder(machineSettingsFilePath)
79 , m_strMachineName(machineName)
80 , m_strMachineGuestOSTypeId(strMachineGuestOSTypeId)
81 , m_uMachineID(uMachineID)
82 , m_pActionPool(pActionPool)
83{
84 /* Start full medium-enumeration (if necessary): */
85 if (!uiCommon().isFullMediumEnumerationRequested())
86 uiCommon().enumerateMedia();
87 configure();
88 finalize();
89 selectMedium(uCurrentMediumId);
90}
91
92void UIMediumSelector::setEnableCreateAction(bool fEnable)
93{
94 if (!m_pActionCreate)
95 return;
96 m_pActionCreate->setEnabled(fEnable);
97 m_pActionCreate->setVisible(fEnable);
98}
99
100QList<QUuid> UIMediumSelector::selectedMediumIds() const
101{
102 QList<QUuid> selectedIds;
103 if (!m_pTreeWidget)
104 return selectedIds;
105 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
106 for (int i = 0; i < selectedItems.size(); ++i)
107 {
108 UIMediumItem *item = dynamic_cast<UIMediumItem*>(selectedItems.at(i));
109 if (item)
110 selectedIds.push_back(item->medium().id());
111 }
112 return selectedIds;
113}
114
115void UIMediumSelector::retranslateUi()
116{
117 if (m_pCancelButton)
118 m_pCancelButton->setText(tr("&Cancel"));
119 if (m_pLeaveEmptyButton)
120 m_pLeaveEmptyButton->setText(tr("Leave &Empty"));
121 if (m_pChooseButton)
122 m_pChooseButton->setText(tr("C&hoose"));
123
124 if (m_pTreeWidget)
125 {
126 m_pTreeWidget->headerItem()->setText(0, tr("Name"));
127 m_pTreeWidget->headerItem()->setText(1, tr("Virtual Size"));
128 m_pTreeWidget->headerItem()->setText(2, tr("Actual Size"));
129 }
130}
131
132void UIMediumSelector::configure()
133{
134#ifndef VBOX_WS_MAC
135 /* Apply window icon, not for macOS, since in Qt 5.6.x applying
136 * icon for non-top-level modal window (macOS Sheet) may cause crash: */
137 setWindowIcon(UIIconPool::iconSetFull(":/media_manager_32px.png", ":/media_manager_16px.png"));
138#endif /* !VBOX_WS_MAC */
139 setTitle();
140 prepareWidgets();
141 prepareActions();
142 prepareMenuAndToolBar();
143 prepareConnections();
144}
145
146void UIMediumSelector::prepareActions()
147{
148 if (!m_pActionPool)
149 return;
150
151 switch (m_enmMediumType)
152 {
153 case UIMediumDeviceType_DVD:
154 m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddCD);
155 m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateCD);
156 break;
157 case UIMediumDeviceType_Floppy:
158 m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddFD);
159 m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateFD);
160 break;
161 case UIMediumDeviceType_HardDisk:
162 case UIMediumDeviceType_All:
163 case UIMediumDeviceType_Invalid:
164 default:
165 m_pActionAdd = m_pActionPool->action(UIActionIndex_M_MediumSelector_AddHD);
166 m_pActionCreate = m_pActionPool->action(UIActionIndex_M_MediumSelector_CreateHD);
167 break;
168 }
169
170 m_pActionRefresh = m_pActionPool->action(UIActionIndex_M_MediumSelector_Refresh);
171}
172
173void UIMediumSelector::prepareMenuAndToolBar()
174{
175 if (!m_pMainMenu || !m_pToolBar)
176 return;
177
178 m_pMainMenu->addAction(m_pActionAdd);
179 m_pMainMenu->addAction(m_pActionCreate);
180 m_pMainMenu->addSeparator();
181 m_pMainMenu->addAction(m_pActionRefresh);
182
183 m_pToolBar->addAction(m_pActionAdd);
184 if (!(gEDataManager->restrictedDialogTypes(m_uMachineID) & UIExtraDataMetaDefs::DialogType_VISOCreator))
185 m_pToolBar->addAction(m_pActionCreate);
186 m_pToolBar->addSeparator();
187 m_pToolBar->addAction(m_pActionRefresh);
188}
189
190void UIMediumSelector::prepareConnections()
191{
192 /* Configure medium-enumeration connections: */
193 connect(&uiCommon(), &UICommon::sigMediumCreated,
194 this, &UIMediumSelector::sltHandleMediumCreated);
195 connect(&uiCommon(), &UICommon::sigMediumEnumerationStarted,
196 this, &UIMediumSelector::sltHandleMediumEnumerationStart);
197 connect(&uiCommon(), &UICommon::sigMediumEnumerated,
198 this, &UIMediumSelector::sltHandleMediumEnumerated);
199 connect(&uiCommon(), &UICommon::sigMediumEnumerationFinished,
200 this, &UIMediumSelector::sltHandleMediumEnumerationFinish);
201 if (m_pActionAdd)
202 connect(m_pActionAdd, &QAction::triggered, this, &UIMediumSelector::sltAddMedium);
203 if (m_pActionCreate)
204 connect(m_pActionCreate, &QAction::triggered, this, &UIMediumSelector::sltCreateMedium);
205 if (m_pActionRefresh)
206 connect(m_pActionRefresh, &QAction::triggered, this, &UIMediumSelector::sltHandleRefresh);
207
208 if (m_pTreeWidget)
209 {
210 connect(m_pTreeWidget, &QITreeWidget::itemSelectionChanged, this, &UIMediumSelector::sltHandleItemSelectionChanged);
211 connect(m_pTreeWidget, &QITreeWidget::itemDoubleClicked, this, &UIMediumSelector::sltHandleTreeWidgetDoubleClick);
212 connect(m_pTreeWidget, &QITreeWidget::customContextMenuRequested, this, &UIMediumSelector::sltHandleTreeContextMenuRequest);
213 }
214
215 if (m_pCancelButton)
216 connect(m_pCancelButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonCancel);
217 if (m_pChooseButton)
218 connect(m_pChooseButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonChoose);
219 if (m_pLeaveEmptyButton)
220 connect(m_pLeaveEmptyButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonLeaveEmpty);
221
222 if (m_pSearchWidget)
223 {
224 connect(m_pSearchWidget, &UIMediumSearchWidget::sigPerformSearch,
225 this, &UIMediumSelector::sltHandlePerformSearch);
226 }
227}
228
229UIMediumItem* UIMediumSelector::addTreeItem(const UIMedium &medium, QITreeWidgetItem *pParent)
230{
231 if (!pParent)
232 return 0;
233 switch (m_enmMediumType)
234 {
235 case UIMediumDeviceType_DVD:
236 return new UIMediumItemCD(medium, pParent);
237 break;
238 case UIMediumDeviceType_Floppy:
239 return new UIMediumItemFD(medium, pParent);
240 break;
241 case UIMediumDeviceType_HardDisk:
242 case UIMediumDeviceType_All:
243 case UIMediumDeviceType_Invalid:
244 default:
245 return createHardDiskItem(medium, pParent);
246 break;
247 }
248}
249
250UIMediumItem* UIMediumSelector::createHardDiskItem(const UIMedium &medium, QITreeWidgetItem *pParent)
251{
252 if (medium.medium().isNull())
253 return 0;
254 if (!m_pTreeWidget)
255 return 0;
256 /* Search the tree to see if we already have the item: */
257 UIMediumItem *pMediumItem = searchItem(0, medium.id());
258 if (pMediumItem)
259 return pMediumItem;
260 /* Check if the corresponding medium has a parent */
261 if (medium.parentID() != UIMedium::nullID())
262 {
263 UIMediumItem *pParentMediumItem = searchItem(0, medium.parentID());
264 /* If parent medium-item was not found we create it: */
265 if (!pParentMediumItem)
266 {
267 /* Make sure corresponding parent medium is already cached! */
268 UIMedium parentMedium = uiCommon().medium(medium.parentID());
269 if (parentMedium.isNull())
270 AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toString().toUtf8().constData()));
271 /* Try to create parent medium-item: */
272 else
273 pParentMediumItem = createHardDiskItem(parentMedium, pParent);
274 }
275 if (pParentMediumItem)
276 {
277 pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
278 LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
279 }
280 else
281 AssertMsgFailed(("Parent medium with ID={%s} could not be created!\n", medium.parentID().toString().toUtf8().constData()));
282 }
283
284 /* No parents, thus just create item as top-level one: */
285 else
286 {
287 pMediumItem = new UIMediumItemHD(medium, pParent);
288 LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
289 }
290 return pMediumItem;
291}
292
293void UIMediumSelector::restoreSelection(const QList<QUuid> &selectedMediums, QVector<UIMediumItem*> &mediumList)
294{
295 if (!m_pTreeWidget)
296 return;
297 if (selectedMediums.isEmpty())
298 {
299 m_pTreeWidget->setCurrentItem(0);
300 return;
301 }
302 bool selected = false;
303 for (int i = 0; i < mediumList.size(); ++i)
304 {
305 if (!mediumList[i])
306 continue;
307 if (selectedMediums.contains(mediumList[i]->medium().id()))
308 {
309 mediumList[i]->setSelected(true);
310 selected = true;
311 }
312 }
313
314 if (!selected)
315 m_pTreeWidget->setCurrentItem(0);
316}
317
318void UIMediumSelector::prepareWidgets()
319{
320 m_pCentralWidget = new QWidget;
321 if (!m_pCentralWidget)
322 return;
323 setCentralWidget(m_pCentralWidget);
324
325 m_pMainLayout = new QVBoxLayout;
326 m_pCentralWidget->setLayout(m_pMainLayout);
327
328 if (!m_pMainLayout || !menuBar())
329 return;
330
331 if (m_pActionPool && m_pActionPool->action(UIActionIndex_M_MediumSelector))
332 {
333 m_pMainMenu = m_pActionPool->action(UIActionIndex_M_MediumSelector)->menu();
334 if (m_pMainMenu)
335 menuBar()->addMenu(m_pMainMenu);
336 }
337
338 m_pToolBar = new QIToolBar;
339 if (m_pToolBar)
340 {
341 /* Configure toolbar: */
342 const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize));
343 m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
344 m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
345 m_pMainLayout->addWidget(m_pToolBar);
346 }
347
348 m_pTreeWidget = new QITreeWidget;
349 if (m_pTreeWidget)
350 {
351 m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
352 m_pMainLayout->addWidget(m_pTreeWidget);
353 m_pTreeWidget->setAlternatingRowColors(true);
354 int iColumnCount = (m_enmMediumType == UIMediumDeviceType_HardDisk) ? 3 : 2;
355 m_pTreeWidget->setColumnCount(iColumnCount);
356 m_pTreeWidget->setSortingEnabled(true);
357 m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
358 m_pTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
359 }
360
361 m_pSearchWidget = new UIMediumSearchWidget;
362 if (m_pSearchWidget)
363 {
364 m_pMainLayout->addWidget(m_pSearchWidget);
365 }
366
367 m_pButtonBox = new QIDialogButtonBox;
368 if (m_pButtonBox)
369 {
370 /* Configure button-box: */
371 m_pCancelButton = m_pButtonBox->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
372
373 /* Only DVDs and Floppies can be left empty: */
374 if (m_enmMediumType == UIMediumDeviceType_DVD || m_enmMediumType == UIMediumDeviceType_Floppy)
375 m_pLeaveEmptyButton = m_pButtonBox->addButton(tr("Leave Empty"), QDialogButtonBox::ActionRole);
376
377 m_pChooseButton = m_pButtonBox->addButton(tr("Choose"), QDialogButtonBox::AcceptRole);
378 m_pCancelButton->setShortcut(Qt::Key_Escape);
379
380 /* Add button-box into main layout: */
381 m_pMainLayout->addWidget(m_pButtonBox);
382 }
383
384 repopulateTreeWidget();
385}
386
387void UIMediumSelector::sltButtonChoose()
388{
389 done(static_cast<int>(ReturnCode_Accepted));
390}
391
392void UIMediumSelector::sltButtonCancel()
393{
394 done(static_cast<int>(ReturnCode_Rejected));
395}
396
397void UIMediumSelector::sltButtonLeaveEmpty()
398{
399 done(static_cast<int>(ReturnCode_LeftEmpty));
400}
401
402void UIMediumSelector::sltAddMedium()
403{
404 QUuid uMediumID = uiCommon().openMediumWithFileOpenDialog(m_enmMediumType, this, m_strMachineFolder, true /* fUseLastFolder */);
405 if (uMediumID.isNull())
406 return;
407 repopulateTreeWidget();
408 selectMedium(uMediumID);
409}
410
411void UIMediumSelector::sltCreateMedium()
412{
413 QUuid uMediumId = uiCommon().openMediumCreatorDialog(m_pActionPool, this, m_enmMediumType, m_strMachineFolder,
414 m_strMachineName, m_strMachineGuestOSTypeId);
415 /* Make sure that the data structure is updated and newly created medium is selected and visible: */
416 sltHandleMediumCreated(uMediumId);
417}
418
419void UIMediumSelector::sltHandleItemSelectionChanged()
420{
421 updateChooseButton();
422}
423
424void UIMediumSelector::sltHandleTreeWidgetDoubleClick(QTreeWidgetItem * item, int column)
425{
426 Q_UNUSED(column);
427 if (!dynamic_cast<UIMediumItem*>(item))
428 return;
429 accept();
430}
431
432void UIMediumSelector::sltHandleMediumCreated(const QUuid &uMediumId)
433{
434 if (uMediumId.isNull())
435 return;
436 /* Update the tree widget making sure we show the new item: */
437 repopulateTreeWidget();
438 /* Select the new item: */
439 selectMedium(uMediumId);
440 /* Update the search: */
441 m_pSearchWidget->search(m_pTreeWidget);
442}
443
444void UIMediumSelector::sltHandleMediumEnumerationStart()
445{
446 /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
447 if (m_pActionRefresh)
448 m_pActionRefresh->setEnabled(false);
449}
450
451void UIMediumSelector::sltHandleMediumEnumerated()
452{
453}
454
455void UIMediumSelector::sltHandleMediumEnumerationFinish()
456{
457 repopulateTreeWidget();
458 if (m_pActionRefresh)
459 m_pActionRefresh->setEnabled(true);
460}
461
462void UIMediumSelector::sltHandleRefresh()
463{
464 /* Restart full medium-enumeration: */
465 uiCommon().enumerateMedia();
466 /* Update the search: */
467 m_pSearchWidget->search(m_pTreeWidget);
468}
469
470void UIMediumSelector::sltHandlePerformSearch()
471{
472 if (!m_pSearchWidget)
473 return;
474 m_pSearchWidget->search(m_pTreeWidget);
475}
476
477void UIMediumSelector::sltHandleTreeContextMenuRequest(const QPoint &point)
478{
479 QWidget *pSender = qobject_cast<QWidget*>(sender());
480 if (!pSender)
481 return;
482
483 QMenu menu;
484 QAction *pExpandAll = menu.addAction(tr("Expand All"));
485 QAction *pCollapseAll = menu.addAction(tr("Collapse All"));
486 if (!pExpandAll || !pCollapseAll)
487 return;
488
489 pExpandAll->setIcon(UIIconPool::iconSet(":/expand_all_16px.png"));
490 pCollapseAll->setIcon(UIIconPool::iconSet(":/collapse_all_16px.png"));
491
492 connect(pExpandAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeExpandAllSignal);
493 connect(pCollapseAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeCollapseAllSignal);
494
495 menu.exec(pSender->mapToGlobal(point));
496}
497
498void UIMediumSelector::sltHandleTreeExpandAllSignal()
499{
500 if (m_pTreeWidget)
501 m_pTreeWidget->expandAll();
502}
503
504void UIMediumSelector::sltHandleTreeCollapseAllSignal()
505{
506 if (m_pTreeWidget)
507 m_pTreeWidget->collapseAll();
508
509 if (m_pAttachedSubTreeRoot)
510 m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pAttachedSubTreeRoot), true);
511 if (m_pNotAttachedSubTreeRoot)
512 m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pNotAttachedSubTreeRoot), true);
513}
514
515void UIMediumSelector::selectMedium(const QUuid &uMediumID)
516{
517 if (!m_pTreeWidget || uMediumID.isNull())
518 return;
519 UIMediumItem *pMediumItem = searchItem(0, uMediumID);
520 if (pMediumItem)
521 {
522 m_pTreeWidget->setCurrentItem(pMediumItem);
523 QModelIndex itemIndex = m_pTreeWidget->itemIndex(pMediumItem);
524 if (itemIndex.isValid())
525 m_pTreeWidget->scrollTo(itemIndex, QAbstractItemView::EnsureVisible);
526 }
527}
528
529void UIMediumSelector::updateChooseButton()
530{
531 if (!m_pTreeWidget || !m_pChooseButton)
532 return;
533 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
534 if (selectedItems.isEmpty())
535 {
536 m_pChooseButton->setEnabled(false);
537 return;
538 }
539
540 /* check if at least one of the selected items is a UIMediumItem */
541 bool mediumItemSelected = false;
542 for (int i = 0; i < selectedItems.size() && !mediumItemSelected; ++i)
543 {
544 if (dynamic_cast<UIMediumItem*>(selectedItems.at(i)))
545 mediumItemSelected = true;
546 }
547 if (mediumItemSelected)
548 m_pChooseButton->setEnabled(true);
549 else
550 m_pChooseButton->setEnabled(false);
551}
552
553void UIMediumSelector::finalize()
554{
555 /* Apply language settings: */
556 retranslateUi();
557}
558
559void UIMediumSelector::showEvent(QShowEvent *pEvent)
560{
561 Q_UNUSED(pEvent);
562
563 /* Try to determine the initial size: */
564 QSize proposedSize;
565 int iHostScreen = 0;
566 if (m_pParent)
567 iHostScreen = gpDesktop->screenNumber(m_pParent);
568 else
569 iHostScreen = gpDesktop->screenNumber(this);
570 if (iHostScreen >= 0 && iHostScreen < gpDesktop->screenCount())
571 {
572 /* On the basis of current host-screen geometry if possible: */
573 const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
574 if (screenGeometry.isValid())
575 proposedSize = screenGeometry.size() / 2.5;
576 }
577 /* Fallback to default size if we failed: */
578 if (proposedSize.isNull())
579 proposedSize = QSize(800, 600);
580 /* Resize to initial size: */
581 resize(proposedSize);
582
583 if (m_pParent)
584 UIDesktopWidgetWatchdog::centerWidget(this, m_pParent, false);
585
586 if (m_pTreeWidget)
587 m_pTreeWidget->setFocus();
588}
589
590void UIMediumSelector::repopulateTreeWidget()
591{
592 if (!m_pTreeWidget)
593 return;
594 /* Cache the currently selected items: */
595 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
596 QList<QUuid> selectedMedia = selectedMediumIds();
597 /* uuid list of selected items: */
598 /* Reset the related data structure: */
599 m_mediumItemList.clear();
600 m_pTreeWidget->clear();
601 m_pAttachedSubTreeRoot = 0;
602 m_pNotAttachedSubTreeRoot = 0;
603 QVector<UIMediumItem*> menuItemVector;
604 foreach (const QUuid &uMediumID, uiCommon().mediumIDs())
605 {
606 UIMedium medium = uiCommon().medium(uMediumID);
607 if (medium.type() == m_enmMediumType)
608 {
609 bool isMediumAttached = !(medium.medium().GetMachineIds().isEmpty());
610 QITreeWidgetItem *pParent = 0;
611 if (isMediumAttached)
612 {
613 if (!m_pAttachedSubTreeRoot)
614 {
615 QStringList strList;
616 strList << "Attached";
617 m_pAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
618 }
619 pParent = m_pAttachedSubTreeRoot;
620
621 }
622 else
623 {
624 if (!m_pNotAttachedSubTreeRoot)
625 {
626 QStringList strList;
627 strList << "Not Attached";
628 m_pNotAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
629 }
630 pParent = m_pNotAttachedSubTreeRoot;
631 }
632 UIMediumItem *treeItem = addTreeItem(medium, pParent);
633 m_mediumItemList.append(treeItem);
634 menuItemVector.push_back(treeItem);
635 }
636 }
637 restoreSelection(selectedMedia, menuItemVector);
638 saveDefaultForeground();
639 updateChooseButton();
640 if (m_pAttachedSubTreeRoot)
641 m_pTreeWidget->expandItem(m_pAttachedSubTreeRoot);
642 if (m_pNotAttachedSubTreeRoot)
643 m_pTreeWidget->expandItem(m_pNotAttachedSubTreeRoot);
644 m_pTreeWidget->resizeColumnToContents(0);
645}
646
647void UIMediumSelector::saveDefaultForeground()
648{
649 if (!m_pTreeWidget)
650 return;
651 if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
652 {
653 QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
654 if (item)
655 {
656 QVariant data = item->data(0, Qt::ForegroundRole);
657 if (data.canConvert<QBrush>())
658 {
659 m_defaultItemForeground = data.value<QBrush>();
660 }
661 }
662 }
663}
664
665UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QUuid &mediumId)
666{
667 if (!m_pTreeWidget)
668 return 0;
669 if (!pParent)
670 pParent = m_pTreeWidget->invisibleRootItem();
671 if (!pParent)
672 return 0;
673
674 for (int i = 0; i < pParent->childCount(); ++i)
675 {
676 QTreeWidgetItem *pChild = pParent->child(i);
677 if (!pChild)
678 continue;
679 UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
680 if (mediumItem)
681 {
682 if (mediumItem->id() == mediumId)
683 return mediumItem;
684 }
685 UIMediumItem *pResult = searchItem(pChild, mediumId);
686 if (pResult)
687 return pResult;
688 }
689 return 0;
690}
691
692void UIMediumSelector::setTitle()
693{
694 switch (m_enmMediumType)
695 {
696 case UIMediumDeviceType_DVD:
697 if (!m_strMachineName.isEmpty())
698 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Optical Disk Selector")));
699 else
700 setWindowTitle(QString("%1").arg(tr("Optical Disk Selector")));
701 break;
702 case UIMediumDeviceType_Floppy:
703 if (!m_strMachineName.isEmpty())
704 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Selector")));
705 else
706 setWindowTitle(QString("%1").arg(tr("Floppy Disk Selector")));
707 break;
708 case UIMediumDeviceType_HardDisk:
709 if (!m_strMachineName.isEmpty())
710 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Hard Disk Selector")));
711 else
712 setWindowTitle(QString("%1").arg(tr("Hard Disk Selector")));
713 break;
714 case UIMediumDeviceType_All:
715 case UIMediumDeviceType_Invalid:
716 default:
717 if (!m_strMachineName.isEmpty())
718 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Virtual Medium Selector")));
719 else
720 setWindowTitle(QString("%1").arg(tr("Virtual Medium Selector")));
721 break;
722 }
723}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette