VirtualBox

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

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

FE/Qt: bugref:10384: UIGuestOSType: A bit of cleanup for family/subtype/type stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/* $Id: UIGuestOSTypeSelectionButton.cpp 103893 2024-03-18 12:48:07Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIGuestOSTypeSelectionButton class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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 <QMenu>
30#include <QSignalMapper>
31#include <QStyle>
32
33/* GUI includes */
34#include "UIGlobalSession.h"
35#include "UIGuestOSType.h"
36#include "UIGuestOSTypeSelectionButton.h"
37#include "UIIconPool.h"
38
39
40UIGuestOSTypeSelectionButton::UIGuestOSTypeSelectionButton(QWidget *pParent)
41 : QIWithRetranslateUI<QPushButton>(pParent)
42{
43 /* Determine icon metric: */
44 const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
45 setIconSize(QSize(iIconMetric, iIconMetric));
46
47 /* We have to make sure that the button has strong focus, otherwise
48 * the editing is ended when the menu is shown: */
49 setFocusPolicy(Qt::StrongFocus);
50
51 /* Create a signal mapper so that we not have to react to
52 * every single menu activation ourself: */
53 m_pSignalMapper = new QSignalMapper(this);
54 if (m_pSignalMapper)
55 connect(m_pSignalMapper, &QSignalMapper::mappedString,
56 this, &UIGuestOSTypeSelectionButton::setOSTypeId);
57
58 /* Create main menu: */
59 m_pMainMenu = new QMenu(pParent);
60 if (m_pMainMenu)
61 setMenu(m_pMainMenu);
62
63 /* Apply language settings: */
64 retranslateUi();
65}
66
67bool UIGuestOSTypeSelectionButton::isMenuShown() const
68{
69 return m_pMainMenu->isVisible();
70}
71
72void UIGuestOSTypeSelectionButton::setOSTypeId(const QString &strOSTypeId)
73{
74 if (m_strOSTypeId == strOSTypeId)
75 return;
76 m_strOSTypeId = strOSTypeId;
77
78#ifndef VBOX_WS_MAC
79 /* Looks ugly on the Mac: */
80 setIcon(generalIconPool().guestOSTypePixmapDefault(m_strOSTypeId));
81#endif
82
83 setText(gpGlobalSession->guestOSTypeManager().getDescription(m_strOSTypeId));
84}
85
86void UIGuestOSTypeSelectionButton::retranslateUi()
87{
88 populateMenu();
89}
90
91void UIGuestOSTypeSelectionButton::createOSTypeMenu(const UIGuestOSTypeManager::UIGuestOSTypeInfo &typeList, QMenu *pMenu)
92{
93 for (int j = 0; j < typeList.size(); ++j)
94 {
95 const QPair<QString, QString> &typeInfo = typeList[j];
96 QAction *pAction = pMenu->addAction(generalIconPool().guestOSTypePixmapDefault(typeInfo.first), typeInfo.second);
97 connect(pAction, &QAction::triggered,
98 m_pSignalMapper, static_cast<void(QSignalMapper::*)(void)>(&QSignalMapper::map)); // swallow bool argument ..
99 m_pSignalMapper->setMapping(pAction, typeInfo.first);
100 }
101}
102
103void UIGuestOSTypeSelectionButton::populateMenu()
104{
105 /* Clear initially: */
106 m_pMainMenu->clear();
107
108 const UIGuestOSTypeManager::UIGuestOSFamilyInfo families
109 = gpGlobalSession->guestOSTypeManager().getFamilies();
110
111 for (int i = 0; i < families.size(); ++i)
112 {
113 const UIFamilyInfo &fi = families.at(i);
114 QMenu *pSubMenu = m_pMainMenu->addMenu(fi.m_strDescription);
115 const UIGuestOSTypeManager::UIGuestOSSubtypeInfo distributions
116 = gpGlobalSession->guestOSTypeManager().getSubtypesForFamilyId(fi.m_strId);
117
118 if (distributions.isEmpty())
119 createOSTypeMenu(gpGlobalSession->guestOSTypeManager().getTypesForFamilyId(fi.m_strId), pSubMenu);
120 else
121 {
122 foreach (const UISubtypeInfo &distribution, distributions)
123 createOSTypeMenu(gpGlobalSession->guestOSTypeManager().getTypesForSubtype(distribution.m_strName),
124 pSubMenu->addMenu(distribution.m_strName));
125 }
126 }
127}
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