VirtualBox

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

Last change on this file since 104158 was 103803, checked in by vboxsync, 9 months ago

FE/Qt. bugref:10618. Splitting COMEnums.h file into individual enum header files.

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