VirtualBox

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

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

FE/Qt: bugref:9241: VirtualBox Manager: Forgot something in r125754.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.3 KB
Line 
1/* $Id: UIMediumSelector.cpp 74811 2018-10-12 16:34:26Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMediumSelector class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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#ifdef VBOX_WITH_PRECOMPILED_HEADERS
19# include <precomp.h>
20#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
21
22/* Qt includes: */
23# include <QAction>
24# include <QHeaderView>
25# include <QVBoxLayout>
26# include <QPushButton>
27
28/* GUI includes: */
29# include "QIComboBox.h"
30# include "QIDialogButtonBox.h"
31# include "QIFileDialog.h"
32# include "QILineEdit.h"
33# include "QIMessageBox.h"
34# include "QITabWidget.h"
35# include "QIToolButton.h"
36# include "VBoxGlobal.h"
37# include "UIDesktopWidgetWatchdog.h"
38# include "UIExtraDataManager.h"
39# include "UIFDCreationDialog.h"
40# include "UIMediumSelector.h"
41# include "UIMessageCenter.h"
42# include "UIIconPool.h"
43# include "UIMedium.h"
44# include "UIMediumItem.h"
45# include "UIToolBar.h"
46
47/* COM includes: */
48# include "COMEnums.h"
49# include "CMachine.h"
50# include "CMediumAttachment.h"
51# include "CMediumFormat.h"
52# include "CStorageController.h"
53# include "CSystemProperties.h"
54
55# ifdef VBOX_WS_MAC
56# include "UIWindowMenuManager.h"
57# endif /* VBOX_WS_MAC */
58
59#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
60
61
62class UIMediumSearchWidget : public QWidget
63{
64 Q_OBJECT;
65
66public:
67
68 enum SearchType
69 {
70 SearchByName,
71 SearchByUUID,
72 SearchByMax
73 };
74
75signals:
76
77 void sigSearchTypeChanged(int newType);
78 void sigSearchTermChanged(QString searchTerm);
79
80public:
81
82 UIMediumSearchWidget(QWidget *pParent = 0);
83 SearchType searchType() const;
84 QString searchTerm() const;
85
86private:
87
88 void prepareWidgets();
89 QIComboBox *m_pSearchComboxBox;
90 QLineEdit *m_pSearchTermLineEdit;
91};
92
93
94/*********************************************************************************************************************************
95* UIMediumSearchWidget implementation. *
96*********************************************************************************************************************************/
97
98UIMediumSearchWidget::UIMediumSearchWidget(QWidget *pParent)
99 :QWidget(pParent)
100 , m_pSearchComboxBox(0)
101 , m_pSearchTermLineEdit(0)
102{
103 prepareWidgets();
104}
105
106void UIMediumSearchWidget::prepareWidgets()
107{
108 QHBoxLayout *pLayout = new QHBoxLayout;
109 setLayout(pLayout);
110 pLayout->setContentsMargins(0, 0, 0, 0);
111 pLayout->setSpacing(0);
112
113 m_pSearchComboxBox = new QIComboBox;
114 if (m_pSearchComboxBox)
115 {
116 m_pSearchComboxBox->setEditable(false);
117 m_pSearchComboxBox->insertItem(SearchByName, "Search By Name");
118 m_pSearchComboxBox->insertItem(SearchByUUID, "Search By UUID");
119 pLayout->addWidget(m_pSearchComboxBox);
120
121 connect(m_pSearchComboxBox, static_cast<void(QIComboBox::*)(int)>(&QIComboBox::currentIndexChanged),
122 this, &UIMediumSearchWidget::sigSearchTypeChanged);
123
124 }
125
126 m_pSearchTermLineEdit = new QLineEdit;
127 if (m_pSearchTermLineEdit)
128 {
129 m_pSearchTermLineEdit->setClearButtonEnabled(true);
130 pLayout->addWidget(m_pSearchTermLineEdit);
131 connect(m_pSearchTermLineEdit, &QILineEdit::textChanged,
132 this, &UIMediumSearchWidget::sigSearchTermChanged);
133 }
134}
135
136UIMediumSearchWidget::SearchType UIMediumSearchWidget::searchType() const
137{
138 if (!m_pSearchComboxBox || m_pSearchComboxBox->currentIndex() >= static_cast<int>(SearchByMax))
139 return SearchByMax;
140 return static_cast<SearchType>(m_pSearchComboxBox->currentIndex());
141}
142
143QString UIMediumSearchWidget::searchTerm() const
144{
145 if (!m_pSearchTermLineEdit)
146 return QString();
147 return m_pSearchTermLineEdit->text();
148}
149
150
151UIMediumSelector::UIMediumSelector(UIMediumDeviceType enmMediumType, const QString &machineName /* = QString() */,
152 const QString &machineSettigFilePath /* = QString() */, QWidget *pParent /* = 0 */)
153 :QIWithRetranslateUI<QIDialog>(pParent)
154 , m_pMainLayout(0)
155 , m_pTreeWidget(0)
156 , m_enmMediumType(enmMediumType)
157 , m_pButtonBox(0)
158 , m_pToolBar(0)
159 , m_pActionAdd(0)
160 , m_pActionCreate(0)
161 , m_pActionRefresh(0)
162 , m_pAttachedSubTreeRoot(0)
163 , m_pNotAttachedSubTreeRoot(0)
164 , m_pParent(pParent)
165 , m_pSearchWidget(0)
166 , m_strMachineSettingsFilePath(machineSettigFilePath)
167 , m_strMachineName(machineName)
168{
169 configure();
170 finalize();
171}
172
173QStringList UIMediumSelector::selectedMediumIds() const
174{
175 QStringList selectedIds;
176 if (!m_pTreeWidget)
177 return selectedIds;
178 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
179 for (int i = 0; i < selectedItems.size(); ++i)
180 {
181 UIMediumItem *item = dynamic_cast<UIMediumItem*>(selectedItems.at(i));
182 if (item)
183 selectedIds.push_back(item->medium().id());
184 }
185 return selectedIds;
186}
187
188
189void UIMediumSelector::retranslateUi()
190{
191 if (m_pActionAdd)
192 {
193 m_pActionAdd->setText(QApplication::translate("UIMediumManager", "&Add"));
194 m_pActionAdd->setToolTip(QApplication::translate("UIMediumManager", "Add Disk Image File"));
195 m_pActionAdd->setStatusTip(QApplication::translate("UIMediumManager", "Add disk image file"));
196 }
197
198 if (m_pActionCreate)
199 {
200 m_pActionCreate->setText(QApplication::translate("UIMediumManager", "&Create"));
201 m_pActionCreate->setToolTip(QApplication::translate("UIMediumManager", "Create an Empty Disk Image"));
202 m_pActionCreate->setStatusTip(QApplication::translate("UIMediumManager", "Create an Empty Disk Image"));
203 }
204
205 if (m_pActionRefresh)
206 {
207 m_pActionRefresh->setText(QApplication::translate("UIMediumManager","Re&fresh"));
208 m_pActionRefresh->setToolTip(QApplication::translate("UIMediumManager","Refresh Disk Image Files (%1)").arg(m_pActionRefresh->shortcut().toString()));
209 m_pActionRefresh->setStatusTip(QApplication::translate("UIMediumManager","Refresh the list of disk image files"));
210 }
211
212 if (m_pButtonBox)
213 m_pButtonBox->button(QDialogButtonBox::Ok)->setText("Choose");
214
215 if (m_pTreeWidget)
216 {
217 m_pTreeWidget->headerItem()->setText(0, QApplication::translate("UIMediumManager","Name"));
218 m_pTreeWidget->headerItem()->setText(1, QApplication::translate("UIMediumManager","Virtual Size"));
219 m_pTreeWidget->headerItem()->setText(2, QApplication::translate("UIMediumManager","Actual Size"));
220 }
221}
222
223void UIMediumSelector::configure()
224{
225 /* Apply window icons: */
226 setWindowIcon(UIIconPool::iconSetFull(":/media_manager_32px.png", ":/media_manager_16px.png"));
227 prepareActions();
228 prepareWidgets();
229 prepareConnections();
230}
231
232void UIMediumSelector::prepareActions()
233{
234 QString strPrefix("hd");
235 switch (m_enmMediumType)
236 {
237 case UIMediumDeviceType_DVD:
238 strPrefix = "cd";
239 break;
240 case UIMediumDeviceType_Floppy:
241 strPrefix = "fd";
242 break;
243 case UIMediumDeviceType_HardDisk:
244 case UIMediumDeviceType_All:
245 case UIMediumDeviceType_Invalid:
246 default:
247 strPrefix = "hd";
248 break;
249 }
250
251 m_pActionAdd = new QAction(this);
252 if (m_pActionAdd)
253 {
254 /* Configure add-action: */
255 m_pActionAdd->setShortcut(QKeySequence("Ctrl+A"));
256
257 m_pActionAdd->setIcon(UIIconPool::iconSetFull(QString(":/%1_add_32px.png").arg(strPrefix),
258 QString(":/%1_add_16px.png").arg(strPrefix),
259 QString(":/%1_add_disabled_32px.png").arg(strPrefix),
260 QString(":/%1_add_disabled_16px.png").arg(strPrefix)));
261 }
262
263 /* Currently create is supported only for Floppy: */
264 if (m_enmMediumType == UIMediumDeviceType_Floppy)
265 {
266 m_pActionCreate = new QAction(this);
267 }
268 if (m_pActionCreate)
269 {
270
271 m_pActionCreate->setShortcut(QKeySequence("Ctrl+C"));
272 m_pActionCreate->setIcon(UIIconPool::iconSetFull(QString(":/%1_add_32px.png").arg(strPrefix),
273 QString(":/%1_add_16px.png").arg(strPrefix),
274 QString(":/%1_add_disabled_32px.png").arg(strPrefix),
275 QString(":/%1_add_disabled_16px.png").arg(strPrefix)));
276 }
277
278
279 m_pActionRefresh = new QAction(this);
280 if (m_pActionRefresh)
281 {
282 m_pActionRefresh->setShortcut(QKeySequence(QKeySequence::Refresh));
283 if (m_pActionRefresh && m_pActionRefresh->icon().isNull())
284 m_pActionRefresh->setIcon(UIIconPool::iconSetFull(":/refresh_32px.png", ":/refresh_16px.png",
285 ":/refresh_disabled_32px.png", ":/refresh_disabled_16px.png"));
286 }
287}
288
289void UIMediumSelector::prepareConnections()
290{
291 /* Configure medium-enumeration connections: */
292 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationStarted,
293 this, &UIMediumSelector::sltHandleMediumEnumerationStart);
294 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerated,
295 this, &UIMediumSelector::sltHandleMediumEnumerated);
296 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationFinished,
297 this, &UIMediumSelector::sltHandleMediumEnumerationFinish);
298 if (m_pActionAdd)
299 connect(m_pActionAdd, &QAction::triggered, this, &UIMediumSelector::sltAddMedium);
300 if (m_pActionCreate)
301 connect(m_pActionCreate, &QAction::triggered, this, &UIMediumSelector::sltCreateMedium);
302 if (m_pActionRefresh)
303 connect(m_pActionRefresh, &QAction::triggered, this, &UIMediumSelector::sltHandleRefresh);
304
305 if (m_pTreeWidget)
306 {
307 connect(m_pTreeWidget, &QITreeWidget::itemSelectionChanged, this, &UIMediumSelector::sltHandleItemSelectionChanged);
308 connect(m_pTreeWidget, &QITreeWidget::itemDoubleClicked, this, &UIMediumSelector::sltHandleTreeWidgetDoubleClick);
309 }
310
311 if (m_pButtonBox)
312 {
313 connect(m_pButtonBox, &QIDialogButtonBox::rejected, this, &UIMediumSelector::close);
314 connect(m_pButtonBox, &QIDialogButtonBox::accepted, this, &UIMediumSelector::accept);
315 }
316
317 if (m_pSearchWidget)
318 {
319 connect(m_pSearchWidget, &UIMediumSearchWidget::sigSearchTypeChanged,
320 this, &UIMediumSelector::sltHandleSearchTypeChange);
321 connect(m_pSearchWidget, &UIMediumSearchWidget::sigSearchTermChanged,
322 this, &UIMediumSelector::sltHandleSearchTermChange);
323 }
324}
325
326UIMediumItem* UIMediumSelector::addTreeItem(const UIMedium &medium, QITreeWidgetItem *pParent)
327{
328 if (!pParent)
329 return 0;
330 switch (m_enmMediumType)
331 {
332 case UIMediumDeviceType_DVD:
333 return new UIMediumItemCD(medium, pParent);
334 break;
335 case UIMediumDeviceType_Floppy:
336 return new UIMediumItemFD(medium, pParent);
337 break;
338 case UIMediumDeviceType_HardDisk:
339 case UIMediumDeviceType_All:
340 case UIMediumDeviceType_Invalid:
341 default:
342 return createHardDiskItem(medium, pParent);
343 break;
344 }
345}
346
347UIMediumItem* UIMediumSelector::createHardDiskItem(const UIMedium &medium, QITreeWidgetItem *pParent)
348{
349 if (medium.medium().isNull())
350 return 0;
351 if (!m_pTreeWidget)
352 return 0;
353 /* Search the tree to see if we already have the item: */
354 UIMediumItem *pMediumItem = searchItem(0, medium.id());
355 if (pMediumItem)
356 return pMediumItem;
357 /* Check if the corresponding medium has a parent */
358 if (medium.parentID() != UIMedium::nullID())
359 {
360 UIMediumItem *pParentMediumItem = searchItem(0, medium.parentID());
361 /* If parent medium-item was not found we create it: */
362 if (!pParentMediumItem)
363 {
364 /* Make sure corresponding parent medium is already cached! */
365 UIMedium parentMedium = vboxGlobal().medium(medium.parentID());
366 if (parentMedium.isNull())
367 AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toUtf8().constData()));
368 /* Try to create parent medium-item: */
369 else
370 pParentMediumItem = createHardDiskItem(parentMedium, pParent);
371 }
372 if (pParentMediumItem)
373 {
374 pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
375 LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toUtf8().constData()));
376 }
377 else
378 AssertMsgFailed(("Parent medium with ID={%s} could not be created!\n", medium.parentID().toUtf8().constData()));
379 }
380
381 /* No parents, thus just create item as top-level one: */
382 else
383 {
384 pMediumItem = new UIMediumItemHD(medium, pParent);
385 LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toUtf8().constData()));
386 }
387 return pMediumItem;
388}
389
390void UIMediumSelector::restoreSelection(const QStringList &selectedMediums, QVector<UIMediumItem*> &mediumList)
391{
392 if (!m_pTreeWidget)
393 return;
394 if (selectedMediums.isEmpty())
395 {
396 m_pTreeWidget->setCurrentItem(0);
397 return;
398 }
399 bool selected = false;
400 for (int i = 0; i < mediumList.size(); ++i)
401 {
402 if (!mediumList[i])
403 continue;
404 if (selectedMediums.contains(mediumList[i]->medium().id()))
405 {
406 mediumList[i]->setSelected(true);
407 selected = true;
408 }
409 }
410
411 if (!selected)
412 m_pTreeWidget->setCurrentItem(0);
413 return;
414}
415
416void UIMediumSelector::prepareWidgets()
417{
418 m_pMainLayout = new QVBoxLayout;
419 if (!m_pMainLayout)
420 return;
421
422 setLayout(m_pMainLayout);
423
424 m_pToolBar = new UIToolBar(parentWidget());
425 if (m_pToolBar)
426 {
427 /* Configure toolbar: */
428 const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize) * 1.375);
429 m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
430 m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
431 /* Add toolbar actions: */
432 if (m_pActionAdd)
433 m_pToolBar->addAction(m_pActionAdd);
434 if (m_pActionCreate)
435 m_pToolBar->addAction(m_pActionCreate);
436 if (m_pActionRefresh)
437 m_pToolBar->addAction(m_pActionRefresh);
438
439 m_pMainLayout->addWidget(m_pToolBar);
440 }
441
442 m_pTreeWidget = new QITreeWidget;
443 if (m_pTreeWidget)
444 {
445 m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
446 m_pMainLayout->addWidget(m_pTreeWidget);
447 m_pTreeWidget->setAlternatingRowColors(true);
448 int iColumnCount = (m_enmMediumType == UIMediumDeviceType_HardDisk) ? 3 : 2;
449 m_pTreeWidget->setColumnCount(iColumnCount);
450 m_pTreeWidget->setSortingEnabled(true);
451 m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
452 }
453
454 m_pSearchWidget = new UIMediumSearchWidget;
455 if (m_pSearchWidget)
456 {
457 m_pMainLayout->addWidget(m_pSearchWidget);
458 }
459
460 m_pButtonBox = new QIDialogButtonBox;
461 if (m_pButtonBox)
462 {
463 /* Configure button-box: */
464 m_pButtonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
465 m_pButtonBox->button(QDialogButtonBox::Cancel)->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::sltAddMedium()
475{
476 QString strDefaultMachineFolder = vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
477 QString strMediumID = vboxGlobal().openMediumWithFileOpenDialog(m_enmMediumType, this, strDefaultMachineFolder);
478 if (strMediumID.isEmpty())
479 return;
480 repopulateTreeWidget();
481 selectMedium(strMediumID);
482
483}
484
485void UIMediumSelector::sltCreateMedium()
486{
487 QString strMachineFolder = QFileInfo(m_strMachineSettingsFilePath).absolutePath();
488 UIFDCreationDialog *pDialog = new UIFDCreationDialog(this, m_strMachineName, strMachineFolder);
489 if (pDialog->exec())
490 {
491 repopulateTreeWidget();
492 selectMedium(pDialog->mediumID());
493 }
494 delete pDialog;
495}
496
497void UIMediumSelector::sltHandleItemSelectionChanged()
498{
499 updateOkButton();
500}
501
502void UIMediumSelector::sltHandleTreeWidgetDoubleClick(QTreeWidgetItem * item, int column)
503{
504 Q_UNUSED(column);
505 if (!dynamic_cast<UIMediumItem*>(item))
506 return;
507 accept();
508}
509
510
511void UIMediumSelector::sltHandleMediumEnumerationStart()
512{
513 /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
514 if (m_pActionRefresh)
515 m_pActionRefresh->setEnabled(false);
516}
517
518void UIMediumSelector::sltHandleMediumEnumerated()
519{
520}
521
522void UIMediumSelector::sltHandleMediumEnumerationFinish()
523{
524 repopulateTreeWidget();
525 if (m_pActionRefresh)
526 m_pActionRefresh->setEnabled(true);
527}
528
529void UIMediumSelector::sltHandleRefresh()
530{
531 /* Initialize media enumation: */
532 vboxGlobal().startMediumEnumeration();
533}
534
535void UIMediumSelector::sltHandleSearchTypeChange(int type)
536{
537 Q_UNUSED(type);
538 performMediumSearch();
539}
540
541void UIMediumSelector::sltHandleSearchTermChange(QString searchTerm)
542{
543 Q_UNUSED(searchTerm);
544 performMediumSearch();
545}
546
547void UIMediumSelector::selectMedium(const QString &strMediumID)
548{
549 if (!m_pTreeWidget)
550 return;
551 UIMediumItem *pMediumItem = searchItem(0, strMediumID);
552 if (pMediumItem)
553 {
554 m_pTreeWidget->setCurrentItem(pMediumItem);
555
556 }
557}
558
559void UIMediumSelector::updateOkButton()
560{
561
562 if (!m_pTreeWidget || !m_pButtonBox || !m_pButtonBox->button(QDialogButtonBox::Ok))
563 return;
564 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
565 if (selectedItems.isEmpty())
566 {
567 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
568 return;
569 }
570
571 /* check if at least one of the selected items is a UIMediumItem */
572 bool mediumItemSelected = false;
573 for (int i = 0; i < selectedItems.size() && !mediumItemSelected; ++i)
574 {
575 if (dynamic_cast<UIMediumItem*>(selectedItems.at(i)))
576 mediumItemSelected = true;
577 }
578 if (mediumItemSelected)
579 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
580 else
581 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
582}
583
584void UIMediumSelector::finalize()
585{
586 /* Apply language settings: */
587 retranslateUi();
588}
589
590void UIMediumSelector::showEvent(QShowEvent *pEvent)
591{
592 Q_UNUSED(pEvent);
593
594 /* Try to determine the initial size: */
595 QSize proposedSize;
596 int iHostScreen = 0;
597 if (m_pParent)
598 iHostScreen = gpDesktop->screenNumber(m_pParent);
599 else
600 iHostScreen = gpDesktop->screenNumber(this);
601 if (iHostScreen >= 0 && iHostScreen < gpDesktop->screenCount())
602 {
603 /* On the basis of current host-screen geometry if possible: */
604 const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
605 if (screenGeometry.isValid())
606 proposedSize = screenGeometry.size() * 5 / 15;
607 }
608 /* Fallback to default size if we failed: */
609 if (proposedSize.isNull())
610 proposedSize = QSize(800, 600);
611 /* Resize to initial size: */
612 resize(proposedSize);
613
614 if (m_pParent)
615 VBoxGlobal::centerWidget(this, m_pParent, false);
616
617}
618
619void UIMediumSelector::repopulateTreeWidget()
620{
621 if (!m_pTreeWidget)
622 return;
623 /* Cache the currently selected items: */
624 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
625 QStringList selectedMedia = selectedMediumIds();
626 /* uuid list of selected items: */
627 /* Reset the related data structure: */
628 m_mediumItemList.clear();
629 m_pTreeWidget->clear();
630 m_pAttachedSubTreeRoot = 0;
631 m_pNotAttachedSubTreeRoot = 0;
632 QVector<UIMediumItem*> menuItemVector;
633
634 foreach (const QString &strMediumID, vboxGlobal().mediumIDs())
635 {
636 UIMedium medium = vboxGlobal().medium(strMediumID);
637 //printf("name %s\n", qPrintable(medium.name()));
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 updateOkButton();
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 performMediumSearch();
679}
680
681void UIMediumSelector::saveDefaultForeground()
682{
683 if (!m_pTreeWidget)
684 return;
685 if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
686 {
687 QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
688 if (item)
689 {
690 QVariant data = item->data(0, Qt::ForegroundRole);
691 if (data.canConvert<QBrush>())
692 {
693 m_defaultItemForeground = data.value<QBrush>();
694 }
695 }
696 }
697}
698
699UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QString &mediumId)
700{
701 if (!m_pTreeWidget)
702 return 0;
703 if (!pParent)
704 {
705 pParent = m_pTreeWidget->invisibleRootItem();
706 }
707 if (!pParent)
708 return 0;
709
710 for (int i = 0; i < pParent->childCount(); ++i)
711 {
712 QTreeWidgetItem *pChild = pParent->child(i);
713 if (!pChild)
714 continue;
715 UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
716 if (mediumItem)
717 {
718 if (mediumItem->id() == mediumId)
719 {
720 return mediumItem;
721 }
722 }
723 UIMediumItem *pResult = searchItem(pChild, mediumId);
724 if (pResult)
725 return pResult;
726 }
727 return 0;
728}
729
730void UIMediumSelector::performMediumSearch()
731{
732 if (!m_pSearchWidget || !m_pTreeWidget)
733 return;
734 /* Unmark all tree items to remove the highltights: */
735 for (int i = 0; i < m_mediumItemList.size(); ++i)
736 {
737 for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
738 {
739 if (m_mediumItemList[i])
740 m_mediumItemList[i]->setData(j, Qt::ForegroundRole, m_defaultItemForeground);
741 }
742 }
743
744
745 UIMediumSearchWidget::SearchType searchType =
746 m_pSearchWidget->searchType();
747 if (searchType >= UIMediumSearchWidget::SearchByMax)
748 return;
749 QString strTerm = m_pSearchWidget->searchTerm();
750 if (strTerm.isEmpty())
751 return;
752
753 for (int i = 0; i < m_mediumItemList.size(); ++i)
754 {
755 if (!m_mediumItemList[i])
756 continue;
757 QString strMedium;
758 if (searchType == UIMediumSearchWidget::SearchByName)
759 strMedium = m_mediumItemList[i]->medium().name();
760 else if(searchType == UIMediumSearchWidget::SearchByUUID)
761 strMedium = m_mediumItemList[i]->medium().id();
762 if (strMedium.isEmpty())
763 continue;
764 if (strMedium.contains(strTerm, Qt::CaseInsensitive))
765 {
766 // mark the item
767 for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
768 m_mediumItemList[i]->setData(j, Qt::ForegroundRole, QBrush(QColor(255, 0, 0)));
769 }
770 }
771}
772
773#include "UIMediumSelector.moc"
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