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