VirtualBox

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

Last change on this file since 103988 was 103771, checked in by vboxsync, 9 months ago

FE/Qt: UICommon: Switching dependency from UICommon to UIGlobalSession whenever is possible.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette