VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIMediaComboBox.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: UIMediaComboBox.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIMediaComboBox class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 <QAbstractItemView>
24# include <QDir>
25# include <QFileInfo>
26
27/* GUI includes: */
28# include "UIMediaComboBox.h"
29# include "UIMedium.h"
30
31#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
32
33
34UIMediaComboBox::UIMediaComboBox(QWidget *pParent)
35 : QComboBox(pParent)
36 , m_enmMediaType(UIMediumDeviceType_Invalid)
37 , m_uMachineId(QUuid())
38 , m_uLastItemId(QUuid())
39{
40 /* Prepare: */
41 prepare();
42}
43
44void UIMediaComboBox::refresh()
45{
46 /* Clearing lists: */
47 clear(), m_media.clear();
48
49 /* Use the medium creation handler to add all the items: */
50 foreach (const QUuid &uMediumId, vboxGlobal().mediumIDs())
51 sltHandleMediumCreated(uMediumId);
52
53 /* If at least one real medium present, remove null medium: */
54 if (count() > 1 && m_enmMediaType == UIMediumDeviceType_HardDisk)
55 {
56 removeItem(0);
57 m_media.erase(m_media.begin());
58 }
59
60 /* Notify listeners about active item changed. */
61 emit activated(currentIndex());
62}
63
64void UIMediaComboBox::repopulate()
65{
66 if (!vboxGlobal().isMediumEnumerationInProgress())
67 vboxGlobal().startMediumEnumeration();
68 else
69 refresh();
70}
71
72void UIMediaComboBox::setCurrentItem(const QUuid &uItemId)
73{
74 m_uLastItemId = uItemId;
75
76 int iIndex;
77 // WORKAROUND:
78 // Note that the media combo-box may be not populated here yet,
79 // so we don't assert..
80 if (findMediaIndex(uItemId, iIndex))
81 {
82 QComboBox::setCurrentIndex(iIndex);
83 emit activated(iIndex);
84 }
85}
86
87QUuid UIMediaComboBox::id(int iIndex /* = -1 */) const
88{
89 AssertReturn(iIndex == -1 ||
90 (iIndex >= 0 && iIndex < m_media.size()),
91 QUuid());
92
93 if (iIndex == -1)
94 iIndex = currentIndex();
95 return iIndex == -1 ? QUuid() : m_media.at(iIndex).id;
96}
97
98QString UIMediaComboBox::location(int iIndex /* = -1 */) const
99{
100 AssertReturn(iIndex == -1 ||
101 (iIndex >= 0 && iIndex < m_media.size()),
102 QString());
103
104 if (iIndex == -1)
105 iIndex = currentIndex();
106 return iIndex == -1 ? QString() : m_media.at(iIndex).location;
107}
108
109void UIMediaComboBox::sltHandleMediumCreated(const QUuid &uMediumId)
110{
111 /* Search for corresponding medium: */
112 UIMedium guiMedium = vboxGlobal().medium(uMediumId);
113
114 /* Ignore media (and their children) which are
115 * marked as hidden or attached to hidden machines only: */
116 if (UIMedium::isMediumAttachedToHiddenMachinesOnly(guiMedium))
117 return;
118
119 /* Add only 1. NULL medium and 2. media of required type: */
120 if (!guiMedium.isNull() && guiMedium.type() != m_enmMediaType)
121 return;
122
123 /* Ignore all diffs: */
124 if (guiMedium.type() == UIMediumDeviceType_HardDisk && guiMedium.parentID() != UIMedium::nullID())
125 return;
126
127 /* Append medium into combo-box: */
128 appendItem(guiMedium);
129
130 /* Activate the required item if any: */
131 if (guiMedium.id() == m_uLastItemId)
132 setCurrentItem(guiMedium.id());
133 /* Select last added item if there is no item selected: */
134 else if (currentText().isEmpty())
135 QComboBox::setCurrentIndex(count() - 1);
136}
137
138void UIMediaComboBox::sltHandleMediumEnumerated(const QUuid &uMediumId)
139{
140 /* Search for corresponding medium: */
141 UIMedium guiMedium = vboxGlobal().medium(uMediumId);
142
143 /* Add only 1. NULL medium and 2. media of required type: */
144 if (!guiMedium.isNull() && guiMedium.type() != m_enmMediaType)
145 return;
146
147 /* Search for corresponding item index: */
148 int iIndex;
149 if (!findMediaIndex(guiMedium.id(), iIndex))
150 return;
151
152 /* Replace medium in combo-box: */
153 replaceItem(iIndex, guiMedium);
154
155 /* Ensure the parent dialog handles the change of the selected item's data: */
156 emit activated(currentIndex());
157}
158
159void UIMediaComboBox::sltHandleMediumDeleted(const QUuid &uMediumId)
160{
161 /* Search for corresponding item index: */
162 int iIndex;
163 if (!findMediaIndex(uMediumId, iIndex))
164 return;
165
166 /* Replace medium from combo-box: */
167 removeItem(iIndex);
168 m_media.erase(m_media.begin() + iIndex);
169
170 /* If no real medium left, add the NULL medium: */
171 if (count() == 0)
172 sltHandleMediumCreated(UIMedium::nullID());
173
174 /* Ensure the parent dialog handles the change of the selected item: */
175 emit activated(currentIndex());
176}
177
178void UIMediaComboBox::sltHandleMediumEnumerationStart()
179{
180 refresh();
181}
182
183void UIMediaComboBox::sltHandleComboActivated(int iIndex)
184{
185 AssertReturnVoid(iIndex >= 0 && iIndex < m_media.size());
186
187 m_uLastItemId = m_media.at(iIndex).id;
188
189 updateToolTip(iIndex);
190}
191
192void UIMediaComboBox::sltHandleComboHovered(const QModelIndex &index)
193{
194 /* Set the combo-box item's tooltip: */
195 const int iIndex = index.row();
196 view()->viewport()->setToolTip(QString());
197 view()->viewport()->setToolTip(m_media.at(iIndex).toolTip);
198}
199
200void UIMediaComboBox::prepare()
201{
202 /* Setup the elide mode: */
203 view()->setTextElideMode(Qt::ElideRight);
204 QSizePolicy sp1(QSizePolicy::Ignored, QSizePolicy::Fixed, QSizePolicy::ComboBox);
205 sp1.setHorizontalStretch(2);
206 setSizePolicy(sp1);
207
208 /* Setup medium-processing handlers: */
209 connect(&vboxGlobal(), &VBoxGlobal::sigMediumCreated,
210 this, &UIMediaComboBox::sltHandleMediumCreated);
211 connect(&vboxGlobal(), &VBoxGlobal::sigMediumDeleted,
212 this, &UIMediaComboBox::sltHandleMediumDeleted);
213
214 /* Setup medium-enumeration handlers: */
215 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationStarted,
216 this, &UIMediaComboBox::sltHandleMediumEnumerationStart);
217 connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerated,
218 this, &UIMediaComboBox::sltHandleMediumEnumerated);
219
220 /* Setup other connections: */
221 connect(this, static_cast<void(UIMediaComboBox::*)(int)>(&UIMediaComboBox::activated),
222 this, &UIMediaComboBox::sltHandleComboActivated);
223 connect(view(), &QAbstractItemView::entered,
224 this, &UIMediaComboBox::sltHandleComboHovered);
225}
226
227void UIMediaComboBox::updateToolTip(int iIndex)
228{
229 /* Set the combo-box tooltip: */
230 setToolTip(QString());
231 if (iIndex >= 0 && iIndex < m_media.size())
232 setToolTip(m_media.at(iIndex).toolTip);
233}
234
235void UIMediaComboBox::appendItem(const UIMedium &guiMedium)
236{
237 m_media.append(Medium(guiMedium.id(), guiMedium.location(),
238 guiMedium.toolTipCheckRO(true, false)));
239
240 insertItem(count(), guiMedium.iconCheckRO(true), guiMedium.details(true));
241}
242
243void UIMediaComboBox::replaceItem(int iIndex, const UIMedium &guiMedium)
244{
245 AssertReturnVoid(iIndex >= 0 && iIndex < m_media.size());
246
247 m_media[iIndex].id = guiMedium.id();
248 m_media[iIndex].location = guiMedium.location();
249 m_media[iIndex].toolTip = guiMedium.toolTipCheckRO(true, false);
250
251 setItemText(iIndex, guiMedium.details(true));
252 setItemIcon(iIndex, guiMedium.iconCheckRO(true));
253
254 if (iIndex == currentIndex())
255 updateToolTip(iIndex);
256}
257
258bool UIMediaComboBox::findMediaIndex(const QUuid &uId, int &iIndex)
259{
260 iIndex = 0;
261
262 for (; iIndex < m_media.size(); ++ iIndex)
263 if (m_media.at(iIndex).id == uId)
264 break;
265
266 return iIndex < m_media.size();
267}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use