1 | /* $Id: UIMediaComboBox.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIMediaComboBox class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QAbstractItemView>
|
---|
30 | #include <QDir>
|
---|
31 | #include <QFileInfo>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "UIMediaComboBox.h"
|
---|
35 | #include "UIMedium.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | UIMediaComboBox::UIMediaComboBox(QWidget *pParent /* = 0 */)
|
---|
39 | : QComboBox(pParent)
|
---|
40 | , m_enmMediaType(UIMediumDeviceType_Invalid)
|
---|
41 | , m_uMachineId(QUuid())
|
---|
42 | , m_uLastItemId(QUuid())
|
---|
43 | {
|
---|
44 | /* Prepare: */
|
---|
45 | prepare();
|
---|
46 | }
|
---|
47 |
|
---|
48 | void UIMediaComboBox::refresh()
|
---|
49 | {
|
---|
50 | /* Clearing lists: */
|
---|
51 | clear(), m_media.clear();
|
---|
52 |
|
---|
53 | /* Use the medium creation handler to add all the items: */
|
---|
54 | foreach (const QUuid &uMediumId, uiCommon().mediumIDs())
|
---|
55 | sltHandleMediumCreated(uMediumId);
|
---|
56 |
|
---|
57 | /* If at least one real medium present,
|
---|
58 | * remove null medium: */
|
---|
59 | if (count() > 1)
|
---|
60 | {
|
---|
61 | removeItem(0);
|
---|
62 | m_media.erase(m_media.begin());
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Notify listeners about active item changed. */
|
---|
66 | emit activated(currentIndex());
|
---|
67 | }
|
---|
68 |
|
---|
69 | void UIMediaComboBox::repopulate()
|
---|
70 | {
|
---|
71 | /* Start medium-enumeration for optical drives/images (if necessary): */
|
---|
72 | if ( m_enmMediaType == UIMediumDeviceType_DVD
|
---|
73 | && !uiCommon().isFullMediumEnumerationRequested())
|
---|
74 | {
|
---|
75 | CMediumVector comMedia;
|
---|
76 | comMedia << uiCommon().host().GetDVDDrives();
|
---|
77 | comMedia << uiCommon().virtualBox().GetDVDImages();
|
---|
78 | uiCommon().enumerateMedia(comMedia);
|
---|
79 | }
|
---|
80 | refresh();
|
---|
81 | }
|
---|
82 |
|
---|
83 | void UIMediaComboBox::setCurrentItem(const QUuid &uItemId)
|
---|
84 | {
|
---|
85 | m_uLastItemId = uItemId;
|
---|
86 |
|
---|
87 | int iIndex;
|
---|
88 | // WORKAROUND:
|
---|
89 | // Note that the media combo-box may be not populated here yet,
|
---|
90 | // so we don't assert..
|
---|
91 | if (findMediaIndex(uItemId, iIndex))
|
---|
92 | {
|
---|
93 | QComboBox::setCurrentIndex(iIndex);
|
---|
94 | emit activated(iIndex);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | QUuid UIMediaComboBox::id(int iIndex /* = -1 */) const
|
---|
99 | {
|
---|
100 | AssertReturn(iIndex == -1 ||
|
---|
101 | (iIndex >= 0 && iIndex < m_media.size()),
|
---|
102 | QUuid());
|
---|
103 |
|
---|
104 | if (iIndex == -1)
|
---|
105 | iIndex = currentIndex();
|
---|
106 | return iIndex == -1 ? QUuid() : m_media.at(iIndex).id;
|
---|
107 | }
|
---|
108 |
|
---|
109 | QString UIMediaComboBox::location(int iIndex /* = -1 */) const
|
---|
110 | {
|
---|
111 | AssertReturn(iIndex == -1 ||
|
---|
112 | (iIndex >= 0 && iIndex < m_media.size()),
|
---|
113 | QString());
|
---|
114 |
|
---|
115 | if (iIndex == -1)
|
---|
116 | iIndex = currentIndex();
|
---|
117 | return iIndex == -1 ? QString() : m_media.at(iIndex).location;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void UIMediaComboBox::sltHandleMediumCreated(const QUuid &uMediumId)
|
---|
121 | {
|
---|
122 | /* Search for corresponding medium: */
|
---|
123 | UIMedium guiMedium = uiCommon().medium(uMediumId);
|
---|
124 |
|
---|
125 | /* Ignore media (and their children) which are
|
---|
126 | * marked as hidden or attached to hidden machines only: */
|
---|
127 | if (UIMedium::isMediumAttachedToHiddenMachinesOnly(guiMedium))
|
---|
128 | return;
|
---|
129 |
|
---|
130 | /* Add only 1. NULL medium and 2. media of required type: */
|
---|
131 | if (!guiMedium.isNull() && guiMedium.type() != m_enmMediaType)
|
---|
132 | return;
|
---|
133 |
|
---|
134 | /* Ignore all diffs: */
|
---|
135 | if (guiMedium.type() == UIMediumDeviceType_HardDisk && guiMedium.parentID() != UIMedium::nullID())
|
---|
136 | return;
|
---|
137 |
|
---|
138 | /* Append medium into combo-box: */
|
---|
139 | appendItem(guiMedium);
|
---|
140 |
|
---|
141 | /* Activate the required item if any: */
|
---|
142 | if (guiMedium.id() == m_uLastItemId)
|
---|
143 | setCurrentItem(guiMedium.id());
|
---|
144 | /* Select last added item if there is no item selected: */
|
---|
145 | else if (currentText().isEmpty())
|
---|
146 | QComboBox::setCurrentIndex(count() - 1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void UIMediaComboBox::sltHandleMediumEnumerated(const QUuid &uMediumId)
|
---|
150 | {
|
---|
151 | /* Search for corresponding medium: */
|
---|
152 | UIMedium guiMedium = uiCommon().medium(uMediumId);
|
---|
153 |
|
---|
154 | /* Add only 1. NULL medium and 2. media of required type: */
|
---|
155 | if (!guiMedium.isNull() && guiMedium.type() != m_enmMediaType)
|
---|
156 | return;
|
---|
157 |
|
---|
158 | /* Search for corresponding item index: */
|
---|
159 | int iIndex;
|
---|
160 | if (!findMediaIndex(guiMedium.id(), iIndex))
|
---|
161 | return;
|
---|
162 |
|
---|
163 | /* Replace medium in combo-box: */
|
---|
164 | replaceItem(iIndex, guiMedium);
|
---|
165 |
|
---|
166 | /* Ensure the parent dialog handles the change of the selected item's data: */
|
---|
167 | emit activated(currentIndex());
|
---|
168 | }
|
---|
169 |
|
---|
170 | void UIMediaComboBox::sltHandleMediumDeleted(const QUuid &uMediumId)
|
---|
171 | {
|
---|
172 | /* Search for corresponding item index: */
|
---|
173 | int iIndex;
|
---|
174 | if (!findMediaIndex(uMediumId, iIndex))
|
---|
175 | return;
|
---|
176 |
|
---|
177 | /* Replace medium from combo-box: */
|
---|
178 | removeItem(iIndex);
|
---|
179 | m_media.erase(m_media.begin() + iIndex);
|
---|
180 |
|
---|
181 | /* If no real medium left, add the NULL medium: */
|
---|
182 | if (count() == 0)
|
---|
183 | sltHandleMediumCreated(UIMedium::nullID());
|
---|
184 |
|
---|
185 | /* Ensure the parent dialog handles the change of the selected item: */
|
---|
186 | emit activated(currentIndex());
|
---|
187 | }
|
---|
188 |
|
---|
189 | void UIMediaComboBox::sltHandleMediumEnumerationStart()
|
---|
190 | {
|
---|
191 | refresh();
|
---|
192 | }
|
---|
193 |
|
---|
194 | void UIMediaComboBox::sltHandleComboActivated(int iIndex)
|
---|
195 | {
|
---|
196 | AssertReturnVoid(iIndex >= 0 && iIndex < m_media.size());
|
---|
197 |
|
---|
198 | m_uLastItemId = m_media.at(iIndex).id;
|
---|
199 |
|
---|
200 | updateToolTip(iIndex);
|
---|
201 | }
|
---|
202 |
|
---|
203 | void UIMediaComboBox::sltHandleComboHovered(const QModelIndex &index)
|
---|
204 | {
|
---|
205 | /* Set the combo-box item's tooltip: */
|
---|
206 | const int iIndex = index.row();
|
---|
207 | view()->viewport()->setToolTip(QString());
|
---|
208 | view()->viewport()->setToolTip(m_media.at(iIndex).toolTip);
|
---|
209 | }
|
---|
210 |
|
---|
211 | void UIMediaComboBox::prepare()
|
---|
212 | {
|
---|
213 | /* Setup the elide mode: */
|
---|
214 | view()->setTextElideMode(Qt::ElideRight);
|
---|
215 | QSizePolicy sp1(QSizePolicy::Ignored, QSizePolicy::Fixed, QSizePolicy::ComboBox);
|
---|
216 | sp1.setHorizontalStretch(2);
|
---|
217 | setSizePolicy(sp1);
|
---|
218 |
|
---|
219 | /* Setup medium-processing handlers: */
|
---|
220 | connect(&uiCommon(), &UICommon::sigMediumCreated,
|
---|
221 | this, &UIMediaComboBox::sltHandleMediumCreated);
|
---|
222 | connect(&uiCommon(), &UICommon::sigMediumDeleted,
|
---|
223 | this, &UIMediaComboBox::sltHandleMediumDeleted);
|
---|
224 |
|
---|
225 | /* Setup medium-enumeration handlers: */
|
---|
226 | connect(&uiCommon(), &UICommon::sigMediumEnumerationStarted,
|
---|
227 | this, &UIMediaComboBox::sltHandleMediumEnumerationStart);
|
---|
228 | connect(&uiCommon(), &UICommon::sigMediumEnumerated,
|
---|
229 | this, &UIMediaComboBox::sltHandleMediumEnumerated);
|
---|
230 |
|
---|
231 | /* Setup other connections: */
|
---|
232 | connect(this, static_cast<void(UIMediaComboBox::*)(int)>(&UIMediaComboBox::activated),
|
---|
233 | this, &UIMediaComboBox::sltHandleComboActivated);
|
---|
234 | connect(view(), &QAbstractItemView::entered,
|
---|
235 | this, &UIMediaComboBox::sltHandleComboHovered);
|
---|
236 | }
|
---|
237 |
|
---|
238 | void UIMediaComboBox::updateToolTip(int iIndex)
|
---|
239 | {
|
---|
240 | /* Set the combo-box tooltip: */
|
---|
241 | setToolTip(QString());
|
---|
242 | if (iIndex >= 0 && iIndex < m_media.size())
|
---|
243 | setToolTip(m_media.at(iIndex).toolTip);
|
---|
244 | }
|
---|
245 |
|
---|
246 | void UIMediaComboBox::appendItem(const UIMedium &guiMedium)
|
---|
247 | {
|
---|
248 | m_media.append(Medium(guiMedium.id(), guiMedium.location(),
|
---|
249 | guiMedium.toolTipCheckRO(true, false)));
|
---|
250 |
|
---|
251 | insertItem(count(), guiMedium.iconCheckRO(true), guiMedium.details(true));
|
---|
252 | }
|
---|
253 |
|
---|
254 | void UIMediaComboBox::replaceItem(int iIndex, const UIMedium &guiMedium)
|
---|
255 | {
|
---|
256 | AssertReturnVoid(iIndex >= 0 && iIndex < m_media.size());
|
---|
257 |
|
---|
258 | m_media[iIndex].id = guiMedium.id();
|
---|
259 | m_media[iIndex].location = guiMedium.location();
|
---|
260 | m_media[iIndex].toolTip = guiMedium.toolTipCheckRO(true, false);
|
---|
261 |
|
---|
262 | setItemText(iIndex, guiMedium.details(true));
|
---|
263 | setItemIcon(iIndex, guiMedium.iconCheckRO(true));
|
---|
264 |
|
---|
265 | if (iIndex == currentIndex())
|
---|
266 | updateToolTip(iIndex);
|
---|
267 | }
|
---|
268 |
|
---|
269 | bool UIMediaComboBox::findMediaIndex(const QUuid &uId, int &iIndex)
|
---|
270 | {
|
---|
271 | iIndex = 0;
|
---|
272 |
|
---|
273 | for (; iIndex < m_media.size(); ++ iIndex)
|
---|
274 | if (m_media.at(iIndex).id == uId)
|
---|
275 | break;
|
---|
276 |
|
---|
277 | return iIndex < m_media.size();
|
---|
278 | }
|
---|