VirtualBox

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

Last change on this file since 77363 was 77363, checked in by vboxsync, 6 years ago

FE/Qt: bugref:9340. Re-enumerating medium registry during medium selector dialog construction.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.5 KB
Line 
1/* $Id: UIMediumSelector.cpp 77363 2019-02-19 15:25:18Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMediumSelector class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 "VBoxGlobal.h"
32#include "UIDesktopWidgetWatchdog.h"
33#include "UIExtraDataManager.h"
34#include "UIFDCreationDialog.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 "UIToolBar.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(UIMediumDeviceType enmMediumType, const QString &machineName /* = QString() */,
57 const QString &machineSettingsFilePath /* = QString() */,
58 const QString &strMachineGuestOSTypeId /*= QString() */, QWidget *pParent /* = 0 */)
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{
82 vboxGlobal().startMediumEnumeration();
83 configure();
84 finalize();
85}
86
87void UIMediumSelector::setEnableCreateAction(bool fEnable)
88{
89 if (!m_pActionCreate)
90 return;
91 m_pActionCreate->setEnabled(fEnable);
92 m_pActionCreate->setVisible(fEnable);
93}
94
95QList<QUuid> UIMediumSelector::selectedMediumIds() const
96{
97 QList<QUuid> selectedIds;
98 if (!m_pTreeWidget)
99 return selectedIds;
100 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
101 for (int i = 0; i < selectedItems.size(); ++i)
102 {
103 UIMediumItem *item = dynamic_cast<UIMediumItem*>(selectedItems.at(i));
104 if (item)
105 selectedIds.push_back(item->medium().id());
106 }
107 return selectedIds;
108}
109
110
111void UIMediumSelector::retranslateUi()
112{
113 if (m_pMainMenu)
114 m_pMainMenu->setTitle(tr("Medium"));
115
116 if (m_pActionAdd)
117 {
118 m_pActionAdd->setText(tr("&Add..."));
119 m_pActionAdd->setToolTip(tr("Add Disk Image"));
120 m_pActionAdd->setStatusTip(tr("Add existing disk image file"));
121 }
122
123 if (m_pActionCreate)
124 {
125 m_pActionCreate->setText(tr("&Create..."));
126 m_pActionCreate->setToolTip(tr("Create Disk Image"));
127 m_pActionCreate->setStatusTip(tr("Create new disk image file"));
128 }
129
130 if (m_pActionRefresh)
131 {
132 m_pActionRefresh->setText(tr("&Refresh"));
133 m_pActionRefresh->setToolTip(tr("Refresh Disk Image Files (%1)").arg(m_pActionRefresh->shortcut().toString()));
134 m_pActionRefresh->setStatusTip(tr("Refresh the list of disk image files"));
135 }
136
137 if (m_pCancelButton)
138 m_pCancelButton->setText(tr("Cancel"));
139 if (m_pLeaveEmptyButton)
140 m_pLeaveEmptyButton->setText(tr("Leave Empty"));
141 if (m_pChooseButton)
142 m_pChooseButton->setText(tr("Choose"));
143
144 if (m_pTreeWidget)
145 {
146 m_pTreeWidget->headerItem()->setText(0, tr("Name"));
147 m_pTreeWidget->headerItem()->setText(1, tr("Virtual Size"));
148 m_pTreeWidget->headerItem()->setText(2, tr("Actual Size"));
149 }
150}
151
152void UIMediumSelector::configure()
153{
154 /* Apply window icons: */
155 setWindowIcon(UIIconPool::iconSetFull(":/media_manager_32px.png", ":/media_manager_16px.png"));
156 setTitle();
157 prepareWidgets();
158 prepareActions();
159 prepareMenuAndToolBar();
160 prepareConnections();
161}
162
163void UIMediumSelector::prepareActions()
164{
165 QString strPrefix("hd");
166 switch (m_enmMediumType)
167 {
168 case UIMediumDeviceType_DVD:
169 strPrefix = "cd";
170 break;
171 case UIMediumDeviceType_Floppy:
172 strPrefix = "fd";
173 break;
174 case UIMediumDeviceType_HardDisk:
175 case UIMediumDeviceType_All:
176 case UIMediumDeviceType_Invalid:
177 default:
178 strPrefix = "hd";
179 break;
180 }
181
182 m_pActionAdd = new QAction(this);
183 if (m_pActionAdd)
184 {
185 /* Configure add-action: */
186 m_pActionAdd->setShortcut(QKeySequence(""));
187
188 m_pActionAdd->setIcon(UIIconPool::iconSetFull(QString(":/%1_add_32px.png").arg(strPrefix),
189 QString(":/%1_add_16px.png").arg(strPrefix),
190 QString(":/%1_add_disabled_32px.png").arg(strPrefix),
191 QString(":/%1_add_disabled_16px.png").arg(strPrefix)));
192
193 }
194
195 m_pActionCreate = new QAction(this);
196 if (m_pActionCreate)
197 {
198 m_pActionCreate->setShortcut(QKeySequence(""));
199 m_pActionCreate->setIcon(UIIconPool::iconSetFull(QString(":/%1_create_32px.png").arg(strPrefix),
200 QString(":/%1_create_16px.png").arg(strPrefix),
201 QString(":/%1_create_disabled_32px.png").arg(strPrefix),
202 QString(":/%1_create_disabled_16px.png").arg(strPrefix)));
203 }
204
205
206 m_pActionRefresh = new QAction(this);
207 if (m_pActionRefresh)
208 {
209 m_pActionRefresh->setShortcut(QKeySequence());
210 if (m_pActionRefresh && m_pActionRefresh->icon().isNull())
211 m_pActionRefresh->setIcon(UIIconPool::iconSetFull(":/refresh_32px.png", ":/refresh_16px.png",
212 ":/refresh_disabled_32px.png", ":/refresh_disabled_16px.png"));
213 }
214}
215
216void UIMediumSelector::prepareMenuAndToolBar()
217{
218 if (!m_pMainMenu || !m_pToolBar)
219 return;
220
221 m_pMainMenu->addAction(m_pActionAdd);
222 m_pMainMenu->addAction(m_pActionCreate);
223 m_pMainMenu->addSeparator();
224 m_pMainMenu->addAction(m_pActionRefresh);
225
226 m_pToolBar->addAction(m_pActionAdd);
227 m_pToolBar->addAction(m_pActionCreate);
228 m_pToolBar->addSeparator();
229 m_pToolBar->addAction(m_pActionRefresh);
230}
231
232void UIMediumSelector::prepareConnections()
233{
234 /* Configure medium-enumeration connections: */
235 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationStarted,
236 this, &UIMediumSelector::sltHandleMediumEnumerationStart);
237 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerated,
238 this, &UIMediumSelector::sltHandleMediumEnumerated);
239 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationFinished,
240 this, &UIMediumSelector::sltHandleMediumEnumerationFinish);
241 if (m_pActionAdd)
242 connect(m_pActionAdd, &QAction::triggered, this, &UIMediumSelector::sltAddMedium);
243 if (m_pActionCreate)
244 connect(m_pActionCreate, &QAction::triggered, this, &UIMediumSelector::sltCreateMedium);
245 if (m_pActionRefresh)
246 connect(m_pActionRefresh, &QAction::triggered, this, &UIMediumSelector::sltHandleRefresh);
247
248 if (m_pTreeWidget)
249 {
250 connect(m_pTreeWidget, &QITreeWidget::itemSelectionChanged, this, &UIMediumSelector::sltHandleItemSelectionChanged);
251 connect(m_pTreeWidget, &QITreeWidget::itemDoubleClicked, this, &UIMediumSelector::sltHandleTreeWidgetDoubleClick);
252 connect(m_pTreeWidget, &QITreeWidget::customContextMenuRequested, this, &UIMediumSelector::sltHandleTreeContextMenuRequest);
253 }
254
255 if (m_pCancelButton)
256 connect(m_pCancelButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonCancel);
257 if (m_pChooseButton)
258 connect(m_pChooseButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonChoose);
259 if (m_pLeaveEmptyButton)
260 connect(m_pLeaveEmptyButton, &QPushButton::clicked, this, &UIMediumSelector::sltButtonLeaveEmpty);
261
262
263 if (m_pSearchWidget)
264 {
265 connect(m_pSearchWidget, &UIMediumSearchWidget::sigPerformSearch,
266 this, &UIMediumSelector::sltHandlePerformSearch);
267 }
268}
269
270UIMediumItem* UIMediumSelector::addTreeItem(const UIMedium &medium, QITreeWidgetItem *pParent)
271{
272 if (!pParent)
273 return 0;
274 switch (m_enmMediumType)
275 {
276 case UIMediumDeviceType_DVD:
277 return new UIMediumItemCD(medium, pParent);
278 break;
279 case UIMediumDeviceType_Floppy:
280 return new UIMediumItemFD(medium, pParent);
281 break;
282 case UIMediumDeviceType_HardDisk:
283 case UIMediumDeviceType_All:
284 case UIMediumDeviceType_Invalid:
285 default:
286 return createHardDiskItem(medium, pParent);
287 break;
288 }
289}
290
291UIMediumItem* UIMediumSelector::createHardDiskItem(const UIMedium &medium, QITreeWidgetItem *pParent)
292{
293 if (medium.medium().isNull())
294 return 0;
295 if (!m_pTreeWidget)
296 return 0;
297 /* Search the tree to see if we already have the item: */
298 UIMediumItem *pMediumItem = searchItem(0, medium.id());
299 if (pMediumItem)
300 return pMediumItem;
301 /* Check if the corresponding medium has a parent */
302 if (medium.parentID() != UIMedium::nullID())
303 {
304 UIMediumItem *pParentMediumItem = searchItem(0, medium.parentID());
305 /* If parent medium-item was not found we create it: */
306 if (!pParentMediumItem)
307 {
308 /* Make sure corresponding parent medium is already cached! */
309 UIMedium parentMedium = vboxGlobal().medium(medium.parentID());
310 if (parentMedium.isNull())
311 AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toString().toUtf8().constData()));
312 /* Try to create parent medium-item: */
313 else
314 pParentMediumItem = createHardDiskItem(parentMedium, pParent);
315 }
316 if (pParentMediumItem)
317 {
318 pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
319 LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
320 }
321 else
322 AssertMsgFailed(("Parent medium with ID={%s} could not be created!\n", medium.parentID().toString().toUtf8().constData()));
323 }
324
325 /* No parents, thus just create item as top-level one: */
326 else
327 {
328 pMediumItem = new UIMediumItemHD(medium, pParent);
329 LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
330 }
331 return pMediumItem;
332}
333
334void UIMediumSelector::restoreSelection(const QList<QUuid> &selectedMediums, QVector<UIMediumItem*> &mediumList)
335{
336 if (!m_pTreeWidget)
337 return;
338 if (selectedMediums.isEmpty())
339 {
340 m_pTreeWidget->setCurrentItem(0);
341 return;
342 }
343 bool selected = false;
344 for (int i = 0; i < mediumList.size(); ++i)
345 {
346 if (!mediumList[i])
347 continue;
348 if (selectedMediums.contains(mediumList[i]->medium().id()))
349 {
350 mediumList[i]->setSelected(true);
351 selected = true;
352 }
353 }
354
355 if (!selected)
356 m_pTreeWidget->setCurrentItem(0);
357 return;
358}
359
360void UIMediumSelector::prepareWidgets()
361{
362 m_pCentralWidget = new QWidget;
363 if (!m_pCentralWidget)
364 return;
365 setCentralWidget(m_pCentralWidget);
366
367 m_pMainLayout = new QVBoxLayout;
368 m_pCentralWidget->setLayout(m_pMainLayout);
369
370 if (!m_pMainLayout || !menuBar())
371 return;
372
373 m_pMainMenu = menuBar()->addMenu(tr("Medium"));
374
375 m_pToolBar = new UIToolBar(parentWidget());
376 if (m_pToolBar)
377 {
378 /* Configure toolbar: */
379 const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize));
380 m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
381 m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
382 m_pMainLayout->addWidget(m_pToolBar);
383 }
384
385 m_pTreeWidget = new QITreeWidget;
386 if (m_pTreeWidget)
387 {
388 m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
389 m_pMainLayout->addWidget(m_pTreeWidget);
390 m_pTreeWidget->setAlternatingRowColors(true);
391 int iColumnCount = (m_enmMediumType == UIMediumDeviceType_HardDisk) ? 3 : 2;
392 m_pTreeWidget->setColumnCount(iColumnCount);
393 m_pTreeWidget->setSortingEnabled(true);
394 m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
395 m_pTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
396 }
397
398 m_pSearchWidget = new UIMediumSearchWidget;
399 if (m_pSearchWidget)
400 {
401 m_pMainLayout->addWidget(m_pSearchWidget);
402 }
403
404 m_pButtonBox = new QIDialogButtonBox;
405 if (m_pButtonBox)
406 {
407 /* Configure button-box: */
408 m_pCancelButton = m_pButtonBox->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
409
410 /* Only DVDs and Floppies can be left empty: */
411 if (m_enmMediumType == UIMediumDeviceType_DVD || m_enmMediumType == UIMediumDeviceType_Floppy)
412 m_pLeaveEmptyButton = m_pButtonBox->addButton(tr("Leave Empty"), QDialogButtonBox::ActionRole);
413
414 m_pChooseButton = m_pButtonBox->addButton(tr("Choose"), QDialogButtonBox::AcceptRole);
415 m_pCancelButton->setShortcut(Qt::Key_Escape);
416
417 /* Add button-box into main layout: */
418 m_pMainLayout->addWidget(m_pButtonBox);
419 }
420
421 repopulateTreeWidget();
422}
423
424void UIMediumSelector::sltButtonChoose()
425{
426 done(static_cast<int>(ReturnCode_Accepted));
427}
428
429void UIMediumSelector::sltButtonCancel()
430{
431 done(static_cast<int>(ReturnCode_Rejected));
432}
433
434void UIMediumSelector::sltButtonLeaveEmpty()
435{
436 done(static_cast<int>(ReturnCode_LeftEmpty));
437}
438
439void UIMediumSelector::sltAddMedium()
440{
441 QUuid uMediumID = vboxGlobal().openMediumWithFileOpenDialog(m_enmMediumType, this, m_strMachineFolder);
442 if (uMediumID.isNull())
443 return;
444 repopulateTreeWidget();
445 selectMedium(uMediumID);
446}
447
448void UIMediumSelector::sltCreateMedium()
449{
450 QUuid uMediumId = vboxGlobal().openMediumCreatorDialog(this, m_enmMediumType, m_strMachineFolder,
451 m_strMachineName, m_strMachineGuestOSTypeId);
452 if (uMediumId.isNull())
453 return;
454 /* Update the tree widget making sure we show the new item: */
455 repopulateTreeWidget();
456 /* Select the new item: */
457 selectMedium(uMediumId);
458 /* Update the search: */
459 m_pSearchWidget->search(m_pTreeWidget);
460}
461
462void UIMediumSelector::sltHandleItemSelectionChanged()
463{
464 updateChooseButton();
465}
466
467void UIMediumSelector::sltHandleTreeWidgetDoubleClick(QTreeWidgetItem * item, int column)
468{
469 Q_UNUSED(column);
470 if (!dynamic_cast<UIMediumItem*>(item))
471 return;
472 accept();
473}
474
475
476void UIMediumSelector::sltHandleMediumEnumerationStart()
477{
478 /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
479 if (m_pActionRefresh)
480 m_pActionRefresh->setEnabled(false);
481}
482
483void UIMediumSelector::sltHandleMediumEnumerated()
484{
485}
486
487void UIMediumSelector::sltHandleMediumEnumerationFinish()
488{
489 repopulateTreeWidget();
490 if (m_pActionRefresh)
491 m_pActionRefresh->setEnabled(true);
492}
493
494void UIMediumSelector::sltHandleRefresh()
495{
496 /* Initialize media enumation: */
497 vboxGlobal().startMediumEnumeration();
498 /* Update the search: */
499 m_pSearchWidget->search(m_pTreeWidget);
500}
501
502void UIMediumSelector::sltHandlePerformSearch()
503{
504 //performMediumSearch();
505 if (!m_pSearchWidget)
506 return;
507 m_pSearchWidget->search(m_pTreeWidget);
508}
509
510void UIMediumSelector::sltHandleTreeContextMenuRequest(const QPoint &point)
511{
512 QWidget *pSender = qobject_cast<QWidget*>(sender());
513 if (!pSender)
514 return;
515
516 QMenu menu;
517 QAction *pExpandAll = menu.addAction(tr("Expand All"));
518 QAction *pCollapseAll = menu.addAction(tr("Collapse All"));
519 if (!pExpandAll || !pCollapseAll)
520 return;
521
522 pExpandAll->setIcon(UIIconPool::iconSet(":/expand_all_16px.png"));
523 pCollapseAll->setIcon(UIIconPool::iconSet(":/collapse_all_16px.png"));
524
525 connect(pExpandAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeExpandAllSignal);
526 connect(pCollapseAll, &QAction::triggered, this, &UIMediumSelector::sltHandleTreeCollapseAllSignal);
527
528 menu.exec(pSender->mapToGlobal(point));
529}
530
531void UIMediumSelector::sltHandleTreeExpandAllSignal()
532{
533 if (m_pTreeWidget)
534 m_pTreeWidget->expandAll();
535}
536
537void UIMediumSelector::sltHandleTreeCollapseAllSignal()
538{
539 if (m_pTreeWidget)
540 m_pTreeWidget->collapseAll();
541
542 if (m_pAttachedSubTreeRoot)
543 m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pAttachedSubTreeRoot), true);
544 if (m_pNotAttachedSubTreeRoot)
545 m_pTreeWidget->setExpanded(m_pTreeWidget->itemIndex(m_pNotAttachedSubTreeRoot), true);
546}
547
548void UIMediumSelector::selectMedium(const QUuid &uMediumID)
549{
550 if (!m_pTreeWidget)
551 return;
552 UIMediumItem *pMediumItem = searchItem(0, uMediumID);
553 if (pMediumItem)
554 {
555 m_pTreeWidget->setCurrentItem(pMediumItem);
556
557 }
558}
559
560void UIMediumSelector::updateChooseButton()
561{
562
563 if (!m_pTreeWidget || !m_pChooseButton)
564 return;
565 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
566 if (selectedItems.isEmpty())
567 {
568 m_pChooseButton->setEnabled(false);
569 return;
570 }
571
572 /* check if at least one of the selected items is a UIMediumItem */
573 bool mediumItemSelected = false;
574 for (int i = 0; i < selectedItems.size() && !mediumItemSelected; ++i)
575 {
576 if (dynamic_cast<UIMediumItem*>(selectedItems.at(i)))
577 mediumItemSelected = true;
578 }
579 if (mediumItemSelected)
580 m_pChooseButton->setEnabled(true);
581 else
582 m_pChooseButton->setEnabled(false);
583}
584
585void UIMediumSelector::finalize()
586{
587 /* Apply language settings: */
588 retranslateUi();
589}
590
591void UIMediumSelector::showEvent(QShowEvent *pEvent)
592{
593 Q_UNUSED(pEvent);
594
595 /* Try to determine the initial size: */
596 QSize proposedSize;
597 int iHostScreen = 0;
598 if (m_pParent)
599 iHostScreen = gpDesktop->screenNumber(m_pParent);
600 else
601 iHostScreen = gpDesktop->screenNumber(this);
602 if (iHostScreen >= 0 && iHostScreen < gpDesktop->screenCount())
603 {
604 /* On the basis of current host-screen geometry if possible: */
605 const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
606 if (screenGeometry.isValid())
607 proposedSize = screenGeometry.size() * 5 / 15;
608 }
609 /* Fallback to default size if we failed: */
610 if (proposedSize.isNull())
611 proposedSize = QSize(800, 600);
612 /* Resize to initial size: */
613 resize(proposedSize);
614
615 if (m_pParent)
616 VBoxGlobal::centerWidget(this, m_pParent, false);
617
618}
619
620void UIMediumSelector::repopulateTreeWidget()
621{
622 if (!m_pTreeWidget)
623 return;
624 /* Cache the currently selected items: */
625 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
626 QList<QUuid> selectedMedia = selectedMediumIds();
627 /* uuid list of selected items: */
628 /* Reset the related data structure: */
629 m_mediumItemList.clear();
630 m_pTreeWidget->clear();
631 m_pAttachedSubTreeRoot = 0;
632 m_pNotAttachedSubTreeRoot = 0;
633 QVector<UIMediumItem*> menuItemVector;
634
635 foreach (const QUuid &uMediumID, vboxGlobal().mediumIDs())
636 {
637 UIMedium medium = vboxGlobal().medium(uMediumID);
638 if (medium.type() == m_enmMediumType)
639 {
640 bool isMediumAttached = !(medium.medium().GetMachineIds().isEmpty());
641 QITreeWidgetItem *pParent = 0;
642 if (isMediumAttached)
643 {
644 if (!m_pAttachedSubTreeRoot)
645 {
646 QStringList strList;
647 strList << "Attached";
648 m_pAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
649 }
650 pParent = m_pAttachedSubTreeRoot;
651
652 }
653 else
654 {
655 if (!m_pNotAttachedSubTreeRoot)
656 {
657 QStringList strList;
658 strList << "Not Attached";
659 m_pNotAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
660 }
661 pParent = m_pNotAttachedSubTreeRoot;
662 }
663 UIMediumItem *treeItem = addTreeItem(medium, pParent);
664 m_mediumItemList.append(treeItem);
665 menuItemVector.push_back(treeItem);
666 }
667 }
668 restoreSelection(selectedMedia, menuItemVector);
669 saveDefaultForeground();
670 updateChooseButton();
671 if (m_pAttachedSubTreeRoot)
672 m_pTreeWidget->expandItem(m_pAttachedSubTreeRoot);
673
674 if (m_pNotAttachedSubTreeRoot)
675 m_pTreeWidget->expandItem(m_pNotAttachedSubTreeRoot);
676
677 m_pTreeWidget->resizeColumnToContents(0);
678}
679
680void UIMediumSelector::saveDefaultForeground()
681{
682 if (!m_pTreeWidget)
683 return;
684 if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
685 {
686 QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
687 if (item)
688 {
689 QVariant data = item->data(0, Qt::ForegroundRole);
690 if (data.canConvert<QBrush>())
691 {
692 m_defaultItemForeground = data.value<QBrush>();
693 }
694 }
695 }
696}
697
698UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QUuid &mediumId)
699{
700 if (!m_pTreeWidget)
701 return 0;
702 if (!pParent)
703 {
704 pParent = m_pTreeWidget->invisibleRootItem();
705 }
706 if (!pParent)
707 return 0;
708
709 for (int i = 0; i < pParent->childCount(); ++i)
710 {
711 QTreeWidgetItem *pChild = pParent->child(i);
712 if (!pChild)
713 continue;
714 UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
715 if (mediumItem)
716 {
717 if (mediumItem->id() == mediumId)
718 {
719 return mediumItem;
720 }
721 }
722 UIMediumItem *pResult = searchItem(pChild, mediumId);
723 if (pResult)
724 return pResult;
725 }
726 return 0;
727}
728
729void UIMediumSelector::scrollToItem(UIMediumItem* pItem)
730{
731 if (!pItem)
732 return;
733
734 QModelIndex itemIndex = m_pTreeWidget->itemIndex(pItem);
735 for (int i = 0; i < m_mediumItemList.size(); ++i)
736 {
737 QFont font = m_mediumItemList[i]->font(0);
738 font.setBold(false);
739 m_mediumItemList[i]->setFont(0, font);
740 }
741 QFont font = pItem->font(0);
742 font.setBold(true);
743 pItem->setFont(0, font);
744
745 m_pTreeWidget->scrollTo(itemIndex);
746}
747
748void UIMediumSelector::setTitle()
749{
750 switch (m_enmMediumType)
751 {
752 case UIMediumDeviceType_DVD:
753 if (!m_strMachineName.isEmpty())
754 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Optical Disk Selector")));
755 else
756 setWindowTitle(QString("%1").arg(tr("Optical Disk Selector")));
757 break;
758 case UIMediumDeviceType_Floppy:
759 if (!m_strMachineName.isEmpty())
760 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Selector")));
761 else
762 setWindowTitle(QString("%1").arg(tr("Floppy Disk Selector")));
763 break;
764 case UIMediumDeviceType_HardDisk:
765 if (!m_strMachineName.isEmpty())
766 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Hard Disk Selector")));
767 else
768 setWindowTitle(QString("%1").arg(tr("Hard Disk Selector")));
769 break;
770 case UIMediumDeviceType_All:
771 case UIMediumDeviceType_Invalid:
772 default:
773 if (!m_strMachineName.isEmpty())
774 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Virtual Medium Selector")));
775 else
776 setWindowTitle(QString("%1").arg(tr("Virtual Medium Selector")));
777 break;
778 }
779}
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