VirtualBox

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

Last change on this file was 104358, checked in by vboxsync, 4 weeks ago

FE/Qt. bugref:10622. More refactoring around the retranslation functionality.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use