VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/VBoxAboutDlg.cpp@ 105392

Last change on this file since 105392 was 105392, checked in by vboxsync, 3 months ago

FE/Qt: bugref:10724: VBoxAboutDlg: Make sure dialog is centered according to screen on macOS and according to parent-widget on other platforms.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: VBoxAboutDlg.cpp 105392 2024-07-17 21:28:45Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - VBoxAboutDlg 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 <QEvent>
30#include <QLabel>
31#include <QPainter>
32#include <QPushButton>
33#include <QVBoxLayout>
34#include <QWindow>
35
36/* GUI includes: */
37#include "QIDialogButtonBox.h"
38#include "UIIconPool.h"
39#include "UIDesktopWidgetWatchdog.h"
40#include "UITranslationEventListener.h"
41#include "UIVersion.h"
42#include "VBoxAboutDlg.h"
43
44/* Other VBox includes: */
45#include <iprt/path.h> /* RTPathExecDir */
46#include <VBox/version.h> /* VBOX_VENDOR */
47
48
49VBoxAboutDlg::VBoxAboutDlg(QWidget *pParent, const QString &strVersion)
50#ifdef VBOX_WS_MAC
51 // No need for About dialog parent on macOS.
52 // First of all, non of other native apps (Safari, App Store, iTunes) centers About dialog according the app itself, they do
53 // it according to screen instead, we should do it as well. Besides that since About dialog is not modal, it will be in
54 // conflict with modal dialogs if there will be a parent passed, because the dialog will not have own event-loop in that case.
55 : QDialog(0)
56 , m_pPseudoParent(pParent)
57#else
58 // On other hosts we will keep the current behavior for now.
59 // First of all it's quite difficult to find native (Metro UI) Windows app which have About dialog at all. But non-native
60 // cross-platform apps (Qt Creator, VLC) centers About dialog according the app exactly.
61 : QDialog(pParent)
62 , m_pPseudoParent(0)
63#endif
64 , m_fPolished(false)
65 , m_strVersion(strVersion)
66 , m_pMainLayout(0)
67 , m_pLabel(0)
68{
69 prepare();
70}
71
72void VBoxAboutDlg::showEvent(QShowEvent *pEvent)
73{
74 /* Call to base-class: */
75 QDialog::showEvent(pEvent);
76
77 /* Polish once: */
78 if (!m_fPolished)
79 {
80 m_fPolished = true;
81 setFixedSize(m_size);
82
83 QRect geo = geometry();
84#ifdef VBOX_WS_MAC
85 /* Center according to parent screen: */
86 geo.moveCenter(gpDesktop->screenGeometry(m_pPseudoParent).center());
87#else
88 /* Center according to parent widget: */
89 geo.moveCenter(parentWidget()->geometry().center());
90#endif
91 setGeometry(geo);
92 }
93}
94
95void VBoxAboutDlg::paintEvent(QPaintEvent *)
96{
97 /* Draw About-VirtualBox background image: */
98 QPainter painter(this);
99 painter.drawPixmap(0, 0, m_pixmap);
100}
101
102void VBoxAboutDlg::sltRetranslateUI()
103{
104 setWindowTitle(tr("VirtualBox - About"));
105
106 if (m_pLabel)
107 {
108 const QString strAboutText = tr("VirtualBox Graphical User Interface");
109#ifdef VBOX_BLEEDING_EDGE
110 const QString strVersionText = "EXPERIMENTAL build %1 - " + QString(VBOX_BLEEDING_EDGE);
111#else
112 const QString strVersionText = tr("Version %1");
113#endif
114#ifdef VBOX_OSE
115 m_strAboutText = strAboutText + " " + strVersionText.arg(m_strVersion) + "\n"
116 + QString("%1 2004-" VBOX_C_YEAR " " VBOX_VENDOR).arg(QChar(0xa9));
117#else
118 m_strAboutText = strAboutText + "\n" + strVersionText.arg(m_strVersion);
119#endif
120 m_strAboutText = m_strAboutText + QString(" (Qt%1)").arg(qVersion());
121 m_strAboutText = m_strAboutText + "\n" + QString("Copyright %1 %2 %3.")
122 .arg(QChar(0xa9)).arg(VBOX_C_YEAR).arg(VBOX_VENDOR);
123 m_pLabel->setText(m_strAboutText);
124 }
125}
126
127void VBoxAboutDlg::prepare()
128{
129 /* Delete dialog on close: */
130 setAttribute(Qt::WA_DeleteOnClose);
131 /* Do not count that window as important for application,
132 * it will NOT be taken into account when other top-level windows will be closed: */
133 setAttribute(Qt::WA_QuitOnClose, false);
134
135 /* Make sure the dialog is deleted on pseudo-parent destruction: */
136 if (m_pPseudoParent)
137 connect(m_pPseudoParent, &QObject::destroyed, this, &VBoxAboutDlg::close);
138
139 /* Choose default image: */
140 QString strPath(":/about.png");
141
142 /* Branding: Use a custom about splash picture if set: */
143 const QString strSplash = UIVersionInfo::brandingGetKey("UI/AboutSplash");
144 if (UIVersionInfo::brandingIsActive() && !strSplash.isEmpty())
145 {
146 char szExecPath[1024];
147 RTPathExecDir(szExecPath, 1024);
148 QString strTmpPath = QString("%1/%2").arg(szExecPath).arg(strSplash);
149 if (QFile::exists(strTmpPath))
150 strPath = strTmpPath;
151 }
152
153 /* Load image: */
154 const QIcon icon = UIIconPool::iconSet(strPath);
155 m_size = QSize(640, 480);
156 QWidget *pParent = m_pPseudoParent ? m_pPseudoParent : parentWidget();
157 const qreal fDevicePixelRatio = pParent ? pParent->windowHandle()->devicePixelRatio() : 1;
158 m_pixmap = icon.pixmap(m_size, fDevicePixelRatio);
159
160 /* Prepare main-layout: */
161 prepareMainLayout();
162
163 /* Translate: */
164 sltRetranslateUI();
165 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
166 this, &VBoxAboutDlg::sltRetranslateUI);
167}
168
169void VBoxAboutDlg::prepareMainLayout()
170{
171 /* Create main-layout: */
172 m_pMainLayout = new QVBoxLayout(this);
173 if (m_pMainLayout)
174 {
175 /* Prepare stuff: */
176 prepareLabel();
177 prepareCloseButton();
178 }
179}
180
181void VBoxAboutDlg::prepareLabel()
182{
183 /* Create label for version text: */
184 m_pLabel = new QLabel(this);
185 if (m_pLabel)
186 {
187 /* Prepare label for version text: */
188 QPalette palette;
189 /* Branding: Set a different text color (because splash also could be white),
190 * otherwise use white as default color: */
191 const QString strColor = UIVersionInfo::brandingGetKey("UI/AboutTextColor");
192 if (!strColor.isEmpty())
193 palette.setColor(QPalette::WindowText, QColor(strColor).name());
194 else
195 palette.setColor(QPalette::WindowText, Qt::white);
196 m_pLabel->setPalette(palette);
197 m_pLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
198 m_pLabel->setFont(font());
199
200 /* Add label to the main-layout: */
201 if (m_pMainLayout)
202 {
203 m_pMainLayout->addWidget(m_pLabel);
204 m_pMainLayout->setAlignment(m_pLabel, Qt::AlignRight | Qt::AlignBottom);
205 }
206 }
207}
208
209void VBoxAboutDlg::prepareCloseButton()
210{
211 /* Create button-box: */
212 QIDialogButtonBox *pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, this);
213 if (pButtonBox)
214 {
215 /* Prepare close-button: */
216 connect(pButtonBox, &QDialogButtonBox::rejected, this, &VBoxAboutDlg::close);
217
218 /* Add button-box to the main-layout: */
219 if (m_pMainLayout)
220 m_pMainLayout->addWidget(pButtonBox);
221 }
222}
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