VirtualBox

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

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

FE/Qt: bugref:8472. Refactoring the term 'mediums' to media

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.2 KB
Line 
1/* $Id: UIMediumSelector.cpp 73926 2018-08-28 10:02:14Z 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(UIMediumType 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 UIMediumType_DVD:
238 strPrefix = "cd";
239 break;
240 case UIMediumType_Floppy:
241 strPrefix = "fd";
242 break;
243 case UIMediumType_HardDisk:
244 case UIMediumType_All:
245 case UIMediumType_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_22px.png").arg(strPrefix),
258 QString(":/%1_add_16px.png").arg(strPrefix),
259 QString(":/%1_add_disabled_22px.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 == UIMediumType_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_22px.png").arg(strPrefix),
273 QString(":/%1_add_16px.png").arg(strPrefix),
274 QString(":/%1_add_disabled_22px.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_22px.png", ":/refresh_16px.png",
285 ":/refresh_disabled_22px.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 UIMediumType_DVD:
333 return new UIMediumItemCD(medium, pParent);
334 break;
335 case UIMediumType_Floppy:
336 return new UIMediumItemFD(medium, pParent);
337 break;
338 case UIMediumType_HardDisk:
339 case UIMediumType_All:
340 case UIMediumType_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 /* If parent medium-item was found: */
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 }
382 /* Else just create item as top-level one: */
383 else
384 {
385 pMediumItem = new UIMediumItemHD(medium, pParent);
386 LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toUtf8().constData()));
387 }
388 return pMediumItem;
389}
390
391void UIMediumSelector::restoreSelection(const QStringList &selectedMediums, QVector<UIMediumItem*> &mediumList)
392{
393 if (!m_pTreeWidget)
394 return;
395 if (selectedMediums.isEmpty())
396 {
397 m_pTreeWidget->setCurrentItem(0);
398 return;
399 }
400 bool selected = false;
401 for (int i = 0; i < mediumList.size(); ++i)
402 {
403 if (!mediumList[i])
404 continue;
405 if (selectedMediums.contains(mediumList[i]->medium().id()))
406 {
407 mediumList[i]->setSelected(true);
408 selected = true;
409 }
410 }
411
412 if (!selected)
413 m_pTreeWidget->setCurrentItem(0);
414 return;
415}
416
417void UIMediumSelector::prepareWidgets()
418{
419 m_pMainLayout = new QVBoxLayout;
420 if (!m_pMainLayout)
421 return;
422
423 setLayout(m_pMainLayout);
424
425 m_pToolBar = new UIToolBar(parentWidget());
426 if (m_pToolBar)
427 {
428 /* Configure toolbar: */
429 const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize) * 1.375);
430 m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
431 m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
432 /* Add toolbar actions: */
433 if (m_pActionAdd)
434 m_pToolBar->addAction(m_pActionAdd);
435 if (m_pActionCreate)
436 m_pToolBar->addAction(m_pActionCreate);
437 if (m_pActionRefresh)
438 m_pToolBar->addAction(m_pActionRefresh);
439
440 m_pMainLayout->addWidget(m_pToolBar);
441 }
442
443 m_pTreeWidget = new QITreeWidget;
444 if (m_pTreeWidget)
445 {
446 m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
447 m_pMainLayout->addWidget(m_pTreeWidget);
448 m_pTreeWidget->setAlternatingRowColors(true);
449 int iColumnCount = (m_enmMediumType == UIMediumType_HardDisk) ? 3 : 2;
450 m_pTreeWidget->setColumnCount(iColumnCount);
451 m_pTreeWidget->setSortingEnabled(true);
452 m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
453 }
454
455 m_pSearchWidget = new UIMediumSearchWidget;
456 if (m_pSearchWidget)
457 {
458 m_pMainLayout->addWidget(m_pSearchWidget);
459 }
460
461 m_pButtonBox = new QIDialogButtonBox;
462 if (m_pButtonBox)
463 {
464 /* Configure button-box: */
465 m_pButtonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
466 m_pButtonBox->button(QDialogButtonBox::Cancel)->setShortcut(Qt::Key_Escape);
467
468 /* Add button-box into main layout: */
469 m_pMainLayout->addWidget(m_pButtonBox);
470 }
471
472 repopulateTreeWidget();
473}
474
475void UIMediumSelector::sltAddMedium()
476{
477 QString strDefaultMachineFolder = vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
478 QString strMediumID = vboxGlobal().openMediumWithFileOpenDialog(m_enmMediumType, this, strDefaultMachineFolder);
479 if (strMediumID.isEmpty())
480 return;
481 repopulateTreeWidget();
482 selectMedium(strMediumID);
483
484}
485
486void UIMediumSelector::sltCreateMedium()
487{
488 QString strMachineFolder = QFileInfo(m_strMachineSettingsFilePath).absolutePath();
489 UIFDCreationDialog *pDialog = new UIFDCreationDialog(this, m_strMachineName, strMachineFolder);
490 if (pDialog->exec())
491 {
492 repopulateTreeWidget();
493 selectMedium(pDialog->mediumID());
494 }
495 delete pDialog;
496}
497
498void UIMediumSelector::sltHandleItemSelectionChanged()
499{
500 updateOkButton();
501}
502
503void UIMediumSelector::sltHandleTreeWidgetDoubleClick(QTreeWidgetItem * item, int column)
504{
505 Q_UNUSED(column);
506 if (!dynamic_cast<UIMediumItem*>(item))
507 return;
508 accept();
509}
510
511
512void UIMediumSelector::sltHandleMediumEnumerationStart()
513{
514 /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
515 if (m_pActionRefresh)
516 m_pActionRefresh->setEnabled(false);
517}
518
519void UIMediumSelector::sltHandleMediumEnumerated()
520{
521}
522
523void UIMediumSelector::sltHandleMediumEnumerationFinish()
524{
525 repopulateTreeWidget();
526 if (m_pActionRefresh)
527 m_pActionRefresh->setEnabled(true);
528}
529
530void UIMediumSelector::sltHandleRefresh()
531{
532 /* Initialize media enumation: */
533 vboxGlobal().startMediumEnumeration();
534}
535
536void UIMediumSelector::sltHandleSearchTypeChange(int type)
537{
538 Q_UNUSED(type);
539 performMediumSearch();
540}
541
542void UIMediumSelector::sltHandleSearchTermChange(QString searchTerm)
543{
544 Q_UNUSED(searchTerm);
545 performMediumSearch();
546}
547
548void UIMediumSelector::selectMedium(const QString &strMediumID)
549{
550 if (!m_pTreeWidget)
551 return;
552 UIMediumItem *pMediumItem = searchItem(0, strMediumID);
553 if (pMediumItem)
554 {
555 m_pTreeWidget->setCurrentItem(pMediumItem);
556
557 }
558}
559
560void UIMediumSelector::updateOkButton()
561{
562
563 if (!m_pTreeWidget || !m_pButtonBox || !m_pButtonBox->button(QDialogButtonBox::Ok))
564 return;
565 QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
566 if (selectedItems.isEmpty())
567 {
568 m_pButtonBox->button(QDialogButtonBox::Ok)->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_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
581 else
582 m_pButtonBox->button(QDialogButtonBox::Ok)->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 QStringList 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 QString &strMediumID, vboxGlobal().mediumIDs())
636 {
637 UIMedium medium = vboxGlobal().medium(strMediumID);
638
639 if (medium.type() == m_enmMediumType)
640 {
641 bool isMediumAttached = !(medium.medium().GetMachineIds().isEmpty());
642 QITreeWidgetItem *pParent = 0;
643 if (isMediumAttached)
644 {
645 if (!m_pAttachedSubTreeRoot)
646 {
647 QStringList strList;
648 strList << "Attached";
649 m_pAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
650 }
651 pParent = m_pAttachedSubTreeRoot;
652
653 }
654 else
655 {
656 if (!m_pNotAttachedSubTreeRoot)
657 {
658 QStringList strList;
659 strList << "Not Attached";
660 m_pNotAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
661 }
662 pParent = m_pNotAttachedSubTreeRoot;
663 }
664 UIMediumItem *treeItem = addTreeItem(medium, pParent);
665 m_mediumItemList.append(treeItem);
666 menuItemVector.push_back(treeItem);
667 }
668 }
669 restoreSelection(selectedMedia, menuItemVector);
670 saveDefaultForeground();
671 updateOkButton();
672 if (m_pAttachedSubTreeRoot)
673 m_pTreeWidget->expandItem(m_pAttachedSubTreeRoot);
674
675 if (m_pNotAttachedSubTreeRoot)
676 m_pTreeWidget->expandItem(m_pNotAttachedSubTreeRoot);
677
678 m_pTreeWidget->resizeColumnToContents(0);
679 performMediumSearch();
680}
681
682void UIMediumSelector::saveDefaultForeground()
683{
684 if (!m_pTreeWidget)
685 return;
686 if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
687 {
688 QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
689 if (item)
690 {
691 QVariant data = item->data(0, Qt::ForegroundRole);
692 if (data.canConvert<QBrush>())
693 {
694 m_defaultItemForeground = data.value<QBrush>();
695 }
696 }
697 }
698}
699
700UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QString &mediumId)
701{
702 if (!m_pTreeWidget)
703 return 0;
704 if (!pParent)
705 {
706 pParent = m_pTreeWidget->invisibleRootItem();
707 }
708 if (!pParent)
709 return 0;
710
711 for (int i = 0; i < pParent->childCount(); ++i)
712 {
713 QTreeWidgetItem *pChild = pParent->child(i);
714 if (!pChild)
715 continue;
716 UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
717 if (mediumItem)
718 {
719 if (mediumItem->id() == mediumId)
720 {
721 return mediumItem;
722 }
723 }
724 UIMediumItem *pResult = searchItem(pChild, mediumId);
725 if (pResult)
726 return pResult;
727 }
728 return 0;
729}
730
731void UIMediumSelector::performMediumSearch()
732{
733 if (!m_pSearchWidget || !m_pTreeWidget)
734 return;
735 /* Unmark all tree items to remove the highltights: */
736 for (int i = 0; i < m_mediumItemList.size(); ++i)
737 {
738 for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
739 {
740 if (m_mediumItemList[i])
741 m_mediumItemList[i]->setData(j, Qt::ForegroundRole, m_defaultItemForeground);
742 }
743 }
744
745
746 UIMediumSearchWidget::SearchType searchType =
747 m_pSearchWidget->searchType();
748 if (searchType >= UIMediumSearchWidget::SearchByMax)
749 return;
750 QString strTerm = m_pSearchWidget->searchTerm();
751 if (strTerm.isEmpty())
752 return;
753
754 for (int i = 0; i < m_mediumItemList.size(); ++i)
755 {
756 if (!m_mediumItemList[i])
757 continue;
758 QString strMedium;
759 if (searchType == UIMediumSearchWidget::SearchByName)
760 strMedium = m_mediumItemList[i]->medium().name();
761 else if(searchType == UIMediumSearchWidget::SearchByUUID)
762 strMedium = m_mediumItemList[i]->medium().id();
763 if (strMedium.isEmpty())
764 continue;
765 if (strMedium.contains(strTerm, Qt::CaseInsensitive))
766 {
767 // mark the item
768 for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
769 m_mediumItemList[i]->setData(j, Qt::ForegroundRole, QBrush(QColor(255, 0, 0)));
770 }
771 }
772}
773
774#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