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