1 | /* $Id: UIMediumSelector.cpp 72903 2018-07-04 19:59:39Z 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 |
|
---|
62 | class UIMediumSearchWidget : public QWidget
|
---|
63 | {
|
---|
64 | Q_OBJECT;
|
---|
65 |
|
---|
66 | public:
|
---|
67 |
|
---|
68 | enum SearchType
|
---|
69 | {
|
---|
70 | SearchByName,
|
---|
71 | SearchByUUID,
|
---|
72 | SearchByMax
|
---|
73 | };
|
---|
74 |
|
---|
75 | signals:
|
---|
76 |
|
---|
77 | void sigSearchTypeChanged(int newType);
|
---|
78 | void sigSearchTermChanged(QString searchTerm);
|
---|
79 |
|
---|
80 | public:
|
---|
81 |
|
---|
82 | UIMediumSearchWidget(QWidget *pParent = 0);
|
---|
83 | SearchType searchType() const;
|
---|
84 | QString searchTerm() const;
|
---|
85 |
|
---|
86 | private:
|
---|
87 |
|
---|
88 | void prepareWidgets();
|
---|
89 | QIComboBox *m_pSearchComboxBox;
|
---|
90 | QLineEdit *m_pSearchTermLineEdit;
|
---|
91 | };
|
---|
92 |
|
---|
93 |
|
---|
94 | /*********************************************************************************************************************************
|
---|
95 | * UIMediumSearchWidget implementation. *
|
---|
96 | *********************************************************************************************************************************/
|
---|
97 |
|
---|
98 | UIMediumSearchWidget::UIMediumSearchWidget(QWidget *pParent)
|
---|
99 | :QWidget(pParent)
|
---|
100 | , m_pSearchComboxBox(0)
|
---|
101 | , m_pSearchTermLineEdit(0)
|
---|
102 | {
|
---|
103 | prepareWidgets();
|
---|
104 | }
|
---|
105 |
|
---|
106 | void 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 |
|
---|
136 | UIMediumSearchWidget::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 |
|
---|
143 | QString UIMediumSearchWidget::searchTerm() const
|
---|
144 | {
|
---|
145 | if (!m_pSearchTermLineEdit)
|
---|
146 | return QString();
|
---|
147 | return m_pSearchTermLineEdit->text();
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | UIMediumSelector::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 |
|
---|
173 | QStringList 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 |
|
---|
189 | void 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 |
|
---|
223 | void 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 | }
|
---|
232 |
|
---|
233 | void UIMediumSelector::prepareActions()
|
---|
234 | {
|
---|
235 | QString strPrefix("hd");
|
---|
236 | switch (m_enmMediumType)
|
---|
237 | {
|
---|
238 | case UIMediumType_DVD:
|
---|
239 | strPrefix = "cd";
|
---|
240 | break;
|
---|
241 | case UIMediumType_Floppy:
|
---|
242 | strPrefix = "fd";
|
---|
243 | break;
|
---|
244 | case UIMediumType_HardDisk:
|
---|
245 | case UIMediumType_All:
|
---|
246 | case UIMediumType_Invalid:
|
---|
247 | default:
|
---|
248 | strPrefix = "hd";
|
---|
249 | break;
|
---|
250 | }
|
---|
251 |
|
---|
252 | m_pActionAdd = new QAction(this);
|
---|
253 | if (m_pActionAdd)
|
---|
254 | {
|
---|
255 | /* Configure add-action: */
|
---|
256 | m_pActionAdd->setShortcut(QKeySequence("Ctrl+A"));
|
---|
257 |
|
---|
258 | m_pActionAdd->setIcon(UIIconPool::iconSetFull(QString(":/%1_add_22px.png").arg(strPrefix),
|
---|
259 | QString(":/%1_add_16px.png").arg(strPrefix),
|
---|
260 | QString(":/%1_add_disabled_22px.png").arg(strPrefix),
|
---|
261 | QString(":/%1_add_disabled_16px.png").arg(strPrefix)));
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Currently create is supported only for Floppy: */
|
---|
265 | if (m_enmMediumType == UIMediumType_Floppy)
|
---|
266 | {
|
---|
267 | m_pActionCreate = new QAction(this);
|
---|
268 | }
|
---|
269 | if (m_pActionCreate)
|
---|
270 | {
|
---|
271 |
|
---|
272 | m_pActionCreate->setShortcut(QKeySequence("Ctrl+C"));
|
---|
273 | m_pActionCreate->setIcon(UIIconPool::iconSetFull(QString(":/%1_add_22px.png").arg(strPrefix),
|
---|
274 | QString(":/%1_add_16px.png").arg(strPrefix),
|
---|
275 | QString(":/%1_add_disabled_22px.png").arg(strPrefix),
|
---|
276 | QString(":/%1_add_disabled_16px.png").arg(strPrefix)));
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | m_pActionRefresh = new QAction(this);
|
---|
281 | if (m_pActionRefresh)
|
---|
282 | {
|
---|
283 | m_pActionRefresh->setShortcut(QKeySequence(QKeySequence::Refresh));
|
---|
284 | if (m_pActionRefresh && m_pActionRefresh->icon().isNull())
|
---|
285 | m_pActionRefresh->setIcon(UIIconPool::iconSetFull(":/refresh_22px.png", ":/refresh_16px.png",
|
---|
286 | ":/refresh_disabled_22px.png", ":/refresh_disabled_16px.png"));
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | void UIMediumSelector::prepareConnections()
|
---|
291 | {
|
---|
292 | /* Configure medium-enumeration connections: */
|
---|
293 | connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationStarted,
|
---|
294 | this, &UIMediumSelector::sltHandleMediumEnumerationStart);
|
---|
295 | connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerated,
|
---|
296 | this, &UIMediumSelector::sltHandleMediumEnumerated);
|
---|
297 | connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationFinished,
|
---|
298 | this, &UIMediumSelector::sltHandleMediumEnumerationFinish);
|
---|
299 | if (m_pActionAdd)
|
---|
300 | connect(m_pActionAdd, &QAction::triggered, this, &UIMediumSelector::sltAddMedium);
|
---|
301 | if (m_pActionCreate)
|
---|
302 | connect(m_pActionCreate, &QAction::triggered, this, &UIMediumSelector::sltCreateMedium);
|
---|
303 | if (m_pActionRefresh)
|
---|
304 | connect(m_pActionRefresh, &QAction::triggered, this, &UIMediumSelector::sltHandleRefresh);
|
---|
305 |
|
---|
306 | if (m_pTreeWidget)
|
---|
307 | connect(m_pTreeWidget, &QITreeWidget::itemSelectionChanged, this, &UIMediumSelector::sltHandleItemSelectionChanged);
|
---|
308 |
|
---|
309 | if (m_pButtonBox)
|
---|
310 | {
|
---|
311 | connect(m_pButtonBox, &QIDialogButtonBox::rejected, this, &UIMediumSelector::close);
|
---|
312 | connect(m_pButtonBox, &QIDialogButtonBox::accepted, this, &UIMediumSelector::accept);
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (m_pSearchWidget)
|
---|
316 | {
|
---|
317 | connect(m_pSearchWidget, &UIMediumSearchWidget::sigSearchTypeChanged,
|
---|
318 | this, &UIMediumSelector::sltHandleSearchTypeChange);
|
---|
319 | connect(m_pSearchWidget, &UIMediumSearchWidget::sigSearchTermChanged,
|
---|
320 | this, &UIMediumSelector::sltHandleSearchTermChange);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | UIMediumItem* UIMediumSelector::addTreeItem(const UIMedium &medium, QITreeWidgetItem *pParent)
|
---|
325 | {
|
---|
326 | if (!pParent)
|
---|
327 | return 0;
|
---|
328 | switch (m_enmMediumType)
|
---|
329 | {
|
---|
330 | case UIMediumType_DVD:
|
---|
331 | return new UIMediumItemCD(medium, pParent);
|
---|
332 | break;
|
---|
333 | case UIMediumType_Floppy:
|
---|
334 | return new UIMediumItemFD(medium, pParent);
|
---|
335 | break;
|
---|
336 | case UIMediumType_HardDisk:
|
---|
337 | case UIMediumType_All:
|
---|
338 | case UIMediumType_Invalid:
|
---|
339 | default:
|
---|
340 | return createHardDiskItem(medium, pParent);
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | UIMediumItem* UIMediumSelector::createHardDiskItem(const UIMedium &medium, QITreeWidgetItem *pParent)
|
---|
346 | {
|
---|
347 | if (medium.medium().isNull())
|
---|
348 | return 0;
|
---|
349 | if (!m_pTreeWidget)
|
---|
350 | return 0;
|
---|
351 | /* Search the tree to see if we already have the item: */
|
---|
352 | UIMediumItem *pMediumItem = searchItem(0, medium.id());
|
---|
353 | if (pMediumItem)
|
---|
354 | return pMediumItem;
|
---|
355 | /* Check if the corresponding medium has a parent */
|
---|
356 | if (medium.parentID() != UIMedium::nullID())
|
---|
357 | {
|
---|
358 | UIMediumItem *pParentMediumItem = searchItem(0, medium.parentID());
|
---|
359 | /* If parent medium-item was not found we create it: */
|
---|
360 | if (!pParentMediumItem)
|
---|
361 | {
|
---|
362 | /* Make sure corresponding parent medium is already cached! */
|
---|
363 | UIMedium parentMedium = vboxGlobal().medium(medium.parentID());
|
---|
364 | if (parentMedium.isNull())
|
---|
365 | AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toUtf8().constData()));
|
---|
366 | /* Try to create parent medium-item: */
|
---|
367 | else
|
---|
368 | pParentMediumItem = createHardDiskItem(parentMedium, pParent);
|
---|
369 | /* If parent medium-item was found: */
|
---|
370 | if (pParentMediumItem)
|
---|
371 | {
|
---|
372 | pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
|
---|
373 | LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toUtf8().constData()));
|
---|
374 | }
|
---|
375 | else
|
---|
376 | AssertMsgFailed(("Parent medium with ID={%s} could not be created!\n", medium.parentID().toUtf8().constData()));
|
---|
377 |
|
---|
378 | }
|
---|
379 | }
|
---|
380 | /* Else just create item as top-level one: */
|
---|
381 | else
|
---|
382 | {
|
---|
383 | pMediumItem = new UIMediumItemHD(medium, pParent);
|
---|
384 | LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toUtf8().constData()));
|
---|
385 | }
|
---|
386 | return pMediumItem;
|
---|
387 | }
|
---|
388 |
|
---|
389 | void UIMediumSelector::restoreSelection(const QStringList &selectedMediums, QVector<UIMediumItem*> &mediumList)
|
---|
390 | {
|
---|
391 | if (!m_pTreeWidget)
|
---|
392 | return;
|
---|
393 | if (selectedMediums.isEmpty())
|
---|
394 | {
|
---|
395 | m_pTreeWidget->setCurrentItem(0);
|
---|
396 | return;
|
---|
397 | }
|
---|
398 | bool selected = false;
|
---|
399 | for (int i = 0; i < mediumList.size(); ++i)
|
---|
400 | {
|
---|
401 | if (!mediumList[i])
|
---|
402 | continue;
|
---|
403 | if (selectedMediums.contains(mediumList[i]->medium().id()))
|
---|
404 | {
|
---|
405 | mediumList[i]->setSelected(true);
|
---|
406 | selected = true;
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (!selected)
|
---|
411 | m_pTreeWidget->setCurrentItem(0);
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | void UIMediumSelector::prepareWidgets()
|
---|
416 | {
|
---|
417 | m_pMainLayout = new QVBoxLayout;
|
---|
418 | if (!m_pMainLayout)
|
---|
419 | return;
|
---|
420 |
|
---|
421 | setLayout(m_pMainLayout);
|
---|
422 |
|
---|
423 | m_pToolBar = new UIToolBar(parentWidget());
|
---|
424 | if (m_pToolBar)
|
---|
425 | {
|
---|
426 | /* Configure toolbar: */
|
---|
427 | const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize) * 1.375);
|
---|
428 | m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
|
---|
429 | m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
---|
430 | /* Add toolbar actions: */
|
---|
431 | if (m_pActionAdd)
|
---|
432 | m_pToolBar->addAction(m_pActionAdd);
|
---|
433 | if (m_pActionCreate)
|
---|
434 | m_pToolBar->addAction(m_pActionCreate);
|
---|
435 | if (m_pActionRefresh)
|
---|
436 | m_pToolBar->addAction(m_pActionRefresh);
|
---|
437 |
|
---|
438 | m_pMainLayout->addWidget(m_pToolBar);
|
---|
439 | }
|
---|
440 |
|
---|
441 | m_pTreeWidget = new QITreeWidget;
|
---|
442 | if (m_pTreeWidget)
|
---|
443 | {
|
---|
444 | m_pTreeWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
445 | m_pMainLayout->addWidget(m_pTreeWidget);
|
---|
446 | m_pTreeWidget->setAlternatingRowColors(true);
|
---|
447 | int iColumnCount = (m_enmMediumType == UIMediumType_HardDisk) ? 3 : 2;
|
---|
448 | m_pTreeWidget->setColumnCount(iColumnCount);
|
---|
449 | m_pTreeWidget->setSortingEnabled(true);
|
---|
450 | m_pTreeWidget->sortItems(0, Qt::AscendingOrder);
|
---|
451 | }
|
---|
452 |
|
---|
453 | m_pSearchWidget = new UIMediumSearchWidget;
|
---|
454 | if (m_pSearchWidget)
|
---|
455 | {
|
---|
456 | m_pMainLayout->addWidget(m_pSearchWidget);
|
---|
457 | }
|
---|
458 |
|
---|
459 | m_pButtonBox = new QIDialogButtonBox;
|
---|
460 | if (m_pButtonBox)
|
---|
461 | {
|
---|
462 | /* Configure button-box: */
|
---|
463 | m_pButtonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
|
---|
464 | m_pButtonBox->button(QDialogButtonBox::Cancel)->setShortcut(Qt::Key_Escape);
|
---|
465 |
|
---|
466 | /* Add button-box into main layout: */
|
---|
467 | m_pMainLayout->addWidget(m_pButtonBox);
|
---|
468 | }
|
---|
469 |
|
---|
470 | repopulateTreeWidget();
|
---|
471 | }
|
---|
472 |
|
---|
473 | void UIMediumSelector::sltAddMedium()
|
---|
474 | {
|
---|
475 | QString strDefaultMachineFolder = vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
|
---|
476 | vboxGlobal().openMediumWithFileOpenDialog(m_enmMediumType, this, strDefaultMachineFolder);
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | void UIMediumSelector::sltCreateMedium()
|
---|
481 | {
|
---|
482 | QString strMachineFolder = QFileInfo(m_strMachineSettingsFilePath).absolutePath();
|
---|
483 | UIFDCreationDialog *pDialog = new UIFDCreationDialog(this, m_strMachineName, strMachineFolder);
|
---|
484 | if (pDialog->exec())
|
---|
485 | {
|
---|
486 | sltHandleRefresh();
|
---|
487 | }
|
---|
488 | delete pDialog;
|
---|
489 | }
|
---|
490 |
|
---|
491 | void UIMediumSelector::sltHandleItemSelectionChanged()
|
---|
492 | {
|
---|
493 | updateOkButton();
|
---|
494 | }
|
---|
495 |
|
---|
496 | void UIMediumSelector::sltHandleMediumEnumerationStart()
|
---|
497 | {
|
---|
498 | /* Disable controls. Left Alone button box 'Ok' button. it is handle by tree population: */
|
---|
499 | if (m_pActionRefresh)
|
---|
500 | m_pActionRefresh->setEnabled(false);
|
---|
501 | }
|
---|
502 |
|
---|
503 | void UIMediumSelector::sltHandleMediumEnumerated()
|
---|
504 | {
|
---|
505 | }
|
---|
506 |
|
---|
507 | void UIMediumSelector::sltHandleMediumEnumerationFinish()
|
---|
508 | {
|
---|
509 | repopulateTreeWidget();
|
---|
510 | if (m_pActionRefresh)
|
---|
511 | m_pActionRefresh->setEnabled(true);
|
---|
512 | }
|
---|
513 |
|
---|
514 | void UIMediumSelector::sltHandleRefresh()
|
---|
515 | {
|
---|
516 | /* Initialize media enumation: */
|
---|
517 | vboxGlobal().startMediumEnumeration();
|
---|
518 | }
|
---|
519 |
|
---|
520 | void UIMediumSelector::sltHandleSearchTypeChange(int type)
|
---|
521 | {
|
---|
522 | Q_UNUSED(type);
|
---|
523 | performMediumSearch();
|
---|
524 | }
|
---|
525 |
|
---|
526 | void UIMediumSelector::sltHandleSearchTermChange(QString searchTerm)
|
---|
527 | {
|
---|
528 | Q_UNUSED(searchTerm);
|
---|
529 | performMediumSearch();
|
---|
530 | }
|
---|
531 |
|
---|
532 | void UIMediumSelector::updateOkButton()
|
---|
533 | {
|
---|
534 |
|
---|
535 | if (!m_pTreeWidget || !m_pButtonBox || !m_pButtonBox->button(QDialogButtonBox::Ok))
|
---|
536 | return;
|
---|
537 | QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
|
---|
538 | if (selectedItems.isEmpty())
|
---|
539 | {
|
---|
540 | m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
541 | return;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* check if at least one of the selected items is a UIMediumItem */
|
---|
545 | bool mediumItemSelected = false;
|
---|
546 | for (int i = 0; i < selectedItems.size() && !mediumItemSelected; ++i)
|
---|
547 | {
|
---|
548 | if (dynamic_cast<UIMediumItem*>(selectedItems.at(i)))
|
---|
549 | mediumItemSelected = true;
|
---|
550 | }
|
---|
551 | if (mediumItemSelected)
|
---|
552 | m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
|
---|
553 | else
|
---|
554 | m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
|
---|
555 | }
|
---|
556 |
|
---|
557 | void UIMediumSelector::finalize()
|
---|
558 | {
|
---|
559 | /* Apply language settings: */
|
---|
560 | retranslateUi();
|
---|
561 | }
|
---|
562 |
|
---|
563 | void UIMediumSelector::showEvent(QShowEvent *pEvent)
|
---|
564 | {
|
---|
565 | Q_UNUSED(pEvent);
|
---|
566 |
|
---|
567 | /* Try to determine the initial size: */
|
---|
568 | QSize proposedSize;
|
---|
569 | int iHostScreen = 0;
|
---|
570 | if (m_pParent)
|
---|
571 | iHostScreen = gpDesktop->screenNumber(m_pParent);
|
---|
572 | else
|
---|
573 | iHostScreen = gpDesktop->screenNumber(this);
|
---|
574 | if (iHostScreen >= 0 && iHostScreen < gpDesktop->screenCount())
|
---|
575 | {
|
---|
576 | /* On the basis of current host-screen geometry if possible: */
|
---|
577 | const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
|
---|
578 | if (screenGeometry.isValid())
|
---|
579 | proposedSize = screenGeometry.size() * 5 / 15;
|
---|
580 | }
|
---|
581 | /* Fallback to default size if we failed: */
|
---|
582 | if (proposedSize.isNull())
|
---|
583 | proposedSize = QSize(800, 600);
|
---|
584 | /* Resize to initial size: */
|
---|
585 | resize(proposedSize);
|
---|
586 |
|
---|
587 | if (m_pParent)
|
---|
588 | VBoxGlobal::centerWidget(this, m_pParent, false);
|
---|
589 |
|
---|
590 | }
|
---|
591 |
|
---|
592 | void UIMediumSelector::repopulateTreeWidget()
|
---|
593 | {
|
---|
594 | if (!m_pTreeWidget)
|
---|
595 | return;
|
---|
596 | /* Cache the currently selected items: */
|
---|
597 | QList<QTreeWidgetItem*> selectedItems = m_pTreeWidget->selectedItems();
|
---|
598 | QStringList selectedMediums = selectedMediumIds();
|
---|
599 | /* uuid list of selected items: */
|
---|
600 | /* Reset the related data structure: */
|
---|
601 | m_mediumItemList.clear();
|
---|
602 | m_pTreeWidget->clear();
|
---|
603 | m_pAttachedSubTreeRoot = 0;
|
---|
604 | m_pNotAttachedSubTreeRoot = 0;
|
---|
605 | QVector<UIMediumItem*> menuItemVector;
|
---|
606 |
|
---|
607 | foreach (const QString &strMediumID, vboxGlobal().mediumIDs())
|
---|
608 | {
|
---|
609 | UIMedium medium = vboxGlobal().medium(strMediumID);
|
---|
610 |
|
---|
611 | if (medium.type() == m_enmMediumType)
|
---|
612 | {
|
---|
613 | bool isMediumAttached = !(medium.medium().GetMachineIds().isEmpty());
|
---|
614 | QITreeWidgetItem *pParent = 0;
|
---|
615 | if (isMediumAttached)
|
---|
616 | {
|
---|
617 | if (!m_pAttachedSubTreeRoot)
|
---|
618 | {
|
---|
619 | QStringList strList;
|
---|
620 | strList << "Attached";
|
---|
621 | m_pAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
|
---|
622 | }
|
---|
623 | pParent = m_pAttachedSubTreeRoot;
|
---|
624 |
|
---|
625 | }
|
---|
626 | else
|
---|
627 | {
|
---|
628 | if (!m_pNotAttachedSubTreeRoot)
|
---|
629 | {
|
---|
630 | QStringList strList;
|
---|
631 | strList << "Not Attached";
|
---|
632 | m_pNotAttachedSubTreeRoot = new QITreeWidgetItem(m_pTreeWidget, strList);
|
---|
633 | }
|
---|
634 | pParent = m_pNotAttachedSubTreeRoot;
|
---|
635 | }
|
---|
636 | UIMediumItem *treeItem = addTreeItem(medium, pParent);
|
---|
637 | m_mediumItemList.append(treeItem);
|
---|
638 | menuItemVector.push_back(treeItem);
|
---|
639 | }
|
---|
640 | }
|
---|
641 | restoreSelection(selectedMediums, menuItemVector);
|
---|
642 | saveDefaultForeground();
|
---|
643 | updateOkButton();
|
---|
644 | if (m_pAttachedSubTreeRoot)
|
---|
645 | m_pTreeWidget->expandItem(m_pAttachedSubTreeRoot);
|
---|
646 |
|
---|
647 | if (m_pNotAttachedSubTreeRoot)
|
---|
648 | m_pTreeWidget->expandItem(m_pNotAttachedSubTreeRoot);
|
---|
649 |
|
---|
650 | m_pTreeWidget->resizeColumnToContents(0);
|
---|
651 | performMediumSearch();
|
---|
652 | }
|
---|
653 |
|
---|
654 | void UIMediumSelector::saveDefaultForeground()
|
---|
655 | {
|
---|
656 | if (!m_pTreeWidget)
|
---|
657 | return;
|
---|
658 | if (m_defaultItemForeground == QBrush() && m_pTreeWidget->topLevelItemCount() >= 1)
|
---|
659 | {
|
---|
660 | QTreeWidgetItem *item = m_pTreeWidget->topLevelItem(0);
|
---|
661 | if (item)
|
---|
662 | {
|
---|
663 | QVariant data = item->data(0, Qt::ForegroundRole);
|
---|
664 | if (data.canConvert<QBrush>())
|
---|
665 | {
|
---|
666 | m_defaultItemForeground = data.value<QBrush>();
|
---|
667 | }
|
---|
668 | }
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | UIMediumItem* UIMediumSelector::searchItem(const QTreeWidgetItem *pParent, const QString &mediumId)
|
---|
673 | {
|
---|
674 | if (!m_pTreeWidget)
|
---|
675 | return 0;
|
---|
676 | if (!pParent)
|
---|
677 | {
|
---|
678 | pParent = m_pTreeWidget->invisibleRootItem();
|
---|
679 | }
|
---|
680 | if (!pParent)
|
---|
681 | return 0;
|
---|
682 |
|
---|
683 | for (int i = 0; i < pParent->childCount(); ++i)
|
---|
684 | {
|
---|
685 | QTreeWidgetItem *pChild = pParent->child(i);
|
---|
686 | if (!pChild)
|
---|
687 | continue;
|
---|
688 | UIMediumItem *mediumItem = dynamic_cast<UIMediumItem*>(pChild);
|
---|
689 | if (mediumItem)
|
---|
690 | {
|
---|
691 | if (mediumItem->id() == mediumId)
|
---|
692 | return mediumItem;
|
---|
693 | }
|
---|
694 | searchItem(pChild, mediumId);
|
---|
695 | }
|
---|
696 | return 0;
|
---|
697 | }
|
---|
698 |
|
---|
699 | void UIMediumSelector::performMediumSearch()
|
---|
700 | {
|
---|
701 | if (!m_pSearchWidget)
|
---|
702 | return;
|
---|
703 | /* Unmark all tree items to remove the highltights: */
|
---|
704 | for (int i = 0; i < m_mediumItemList.size(); ++i)
|
---|
705 | {
|
---|
706 | for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
|
---|
707 | m_mediumItemList[i]->setData(j, Qt::ForegroundRole, m_defaultItemForeground);
|
---|
708 | }
|
---|
709 |
|
---|
710 |
|
---|
711 | UIMediumSearchWidget::SearchType searchType =
|
---|
712 | m_pSearchWidget->searchType();
|
---|
713 | if (searchType >= UIMediumSearchWidget::SearchByMax)
|
---|
714 | return;
|
---|
715 | QString strTerm = m_pSearchWidget->searchTerm();
|
---|
716 | if (strTerm.isEmpty())
|
---|
717 | return;
|
---|
718 |
|
---|
719 | for (int i = 0; i < m_mediumItemList.size(); ++i)
|
---|
720 | {
|
---|
721 | if (!m_mediumItemList[i])
|
---|
722 | continue;
|
---|
723 | QString strMedium;
|
---|
724 | if (searchType == UIMediumSearchWidget::SearchByName)
|
---|
725 | strMedium = m_mediumItemList[i]->medium().name();
|
---|
726 | else if(searchType == UIMediumSearchWidget::SearchByUUID)
|
---|
727 | strMedium = m_mediumItemList[i]->medium().id();
|
---|
728 | if (strMedium.isEmpty())
|
---|
729 | continue;
|
---|
730 | if (strMedium.contains(strTerm, Qt::CaseInsensitive))
|
---|
731 | {
|
---|
732 | // mark the item
|
---|
733 | for (int j = 0; j < m_pTreeWidget->columnCount(); ++j)
|
---|
734 | m_mediumItemList[i]->setData(j, Qt::ForegroundRole, QBrush(QColor(255, 0, 0)));
|
---|
735 | }
|
---|
736 | }
|
---|
737 | }
|
---|
738 |
|
---|
739 | #include "UIMediumSelector.moc"
|
---|