VirtualBox

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

Last change on this file was 104358, checked in by vboxsync, 5 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: 10.0 KB
Line 
1/* $Id: UIFilmContainer.cpp 104358 2024-04-18 05:33:40Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIFilmContainer class implementation.
4 */
5
6/*
7 * Copyright (C) 2013-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 <QCheckBox>
31#include <QHBoxLayout>
32#include <QPainter>
33#include <QPainterPath>
34#include <QScrollArea>
35#include <QVBoxLayout>
36#ifndef VBOX_WS_MAC
37# include <QStyle>
38#endif
39
40/* GUI includes: */
41#include "UIFilmContainer.h"
42#include "UITranslationEventListener.h"
43
44/** QWidget subclass providing GUI with UIFilmContainer item prototype.
45 * @todo Rename to something more suitable like UIScreenThumbnail. */
46class UIFilm : public QWidget
47{
48 Q_OBJECT;
49
50public:
51
52 /** Constructs film widget passing @a pParent to the base-class.
53 * @param iScreenIndex Brings the guest-screen index this film referencing.
54 * @param fEnabled Brings whether the guest-screen mentioned above is enabled. */
55 UIFilm(int iScreenIndex, bool fEnabled, QWidget *pParent = 0);
56
57 /** Returns whether guest-screen is enabled. */
58 bool checked() const;
59
60protected:
61
62 /** Handles paint @a pEvent. */
63 virtual void paintEvent(QPaintEvent *pEvent) RT_OVERRIDE;
64
65 /** Returns minimum size-hint. */
66 virtual QSize minimumSizeHint() const RT_OVERRIDE;
67
68private slots:
69
70 /** Handles translation event. */
71 void sltRetranslateUI();
72
73private:
74
75 /** Prepares all. */
76 void prepare();
77 /** Prepares layout. */
78 void prepareLayout();
79 /** Prepares check-box. */
80 void prepareCheckBox();
81
82 /** Holds the guest-screen index. */
83 int m_iScreenIndex;
84 /** Holds whether guest-screen was enabled. */
85 bool m_fWasEnabled;
86
87 /** Holds the main-layout instance. */
88 QVBoxLayout *m_pMainLayout;
89 /** Holds the check-box instance. */
90 QCheckBox *m_pCheckBox;
91};
92
93
94/*********************************************************************************************************************************
95* Class UIFilm implementation. *
96*********************************************************************************************************************************/
97
98UIFilm::UIFilm(int iScreenIndex, bool fEnabled, QWidget *pParent /* = 0*/)
99 : QWidget(pParent)
100 , m_iScreenIndex(iScreenIndex)
101 , m_fWasEnabled(fEnabled)
102 , m_pCheckBox(0)
103{
104 /* Prepare: */
105 prepare();
106}
107
108bool UIFilm::checked() const
109{
110 /* Is the check-box currently checked? */
111 return m_pCheckBox->isChecked();
112}
113
114void UIFilm::sltRetranslateUI()
115{
116 /* Translate check-box: */
117 m_pCheckBox->setText(QApplication::translate("UIMachineSettingsDisplay", "Screen %1").arg(m_iScreenIndex + 1));
118 m_pCheckBox->setWhatsThis(QApplication::translate("UIMachineSettingsDisplay", "When checked, enables video recording for screen %1.").arg(m_iScreenIndex + 1));
119}
120
121void UIFilm::paintEvent(QPaintEvent *)
122{
123 /* Compose painting rectangle: */
124 const QRect rect(1, 1, width() - 2, height() - 2);
125
126 /* Create painter: */
127 QPainter painter(this);
128 painter.setRenderHint(QPainter::Antialiasing);
129
130 /* Configure painter clipping: */
131 QPainterPath path;
132 int iDiameter = 6;
133 QSizeF arcSize(2 * iDiameter, 2 * iDiameter);
134 path.moveTo(rect.x() + iDiameter, rect.y());
135 path.arcTo(QRectF(path.currentPosition(), arcSize).translated(-iDiameter, 0), 90, 90);
136 path.lineTo(path.currentPosition().x(), rect.height() - iDiameter);
137 path.arcTo(QRectF(path.currentPosition(), arcSize).translated(0, -iDiameter), 180, 90);
138 path.lineTo(rect.width() - iDiameter, path.currentPosition().y());
139 path.arcTo(QRectF(path.currentPosition(), arcSize).translated(-iDiameter, -2 * iDiameter), 270, 90);
140 path.lineTo(path.currentPosition().x(), rect.y() + iDiameter);
141 path.arcTo(QRectF(path.currentPosition(), arcSize).translated(-2 * iDiameter, -iDiameter), 0, 90);
142 path.closeSubpath();
143
144 /* Get current background color: */
145 QColor currentColor(palette().color(backgroundRole()));
146
147 /* Fill with background: */
148 painter.setClipPath(path);
149 QColor newColor1 = currentColor;
150 QColor newColor2 = currentColor.darker(125);
151 QLinearGradient headerGradient(rect.topLeft(), rect.bottomRight());
152 headerGradient.setColorAt(0, newColor1);
153 headerGradient.setColorAt(1, newColor2);
154 painter.fillRect(rect, headerGradient);
155
156 /* Stroke with border: */
157 QColor strokeColor = currentColor.darker(150);
158 painter.setClipping(false);
159 painter.strokePath(path, strokeColor);
160}
161
162QSize UIFilm::minimumSizeHint() const
163{
164 /* Return 16:9 aspect-ratio msh: */
165 QSize msh = QWidget::minimumSizeHint();
166 return QSize(msh.width(), (msh.width() * 9) / 16);
167}
168
169void UIFilm::prepare()
170{
171 /* Prepare layout: */
172 prepareLayout();
173 /* Prepare check-box: */
174 prepareCheckBox();
175
176 /* Apply language settings: */
177 sltRetranslateUI();
178 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
179 this, &UIFilm::sltRetranslateUI);
180}
181
182void UIFilm::prepareLayout()
183{
184 /* Create layout: */
185 m_pMainLayout = new QVBoxLayout(this);
186 if (m_pMainLayout)
187 {
188 /* Configure layout: */
189#ifdef VBOX_WS_MAC
190 m_pMainLayout->setContentsMargins(10, 10, 15, 10);
191#endif
192
193 /* Add strech: */
194 m_pMainLayout->addStretch();
195 }
196}
197
198void UIFilm::prepareCheckBox()
199{
200 /* Create check-box: */
201 m_pCheckBox = new QCheckBox;
202 if (m_pCheckBox)
203 {
204 /* Configure check-box: */
205 m_pCheckBox->setChecked(static_cast<bool>(m_fWasEnabled));
206 /* Configure font: */
207 QFont currentFont = m_pCheckBox->font();
208#ifdef VBOX_WS_MAC
209 currentFont.setPointSize(currentFont.pointSize() - 2);
210#else
211 currentFont.setPointSize(currentFont.pointSize() - 1);
212#endif
213 m_pCheckBox->setFont(currentFont);
214
215 /* Insert into layout: */
216 m_pMainLayout->insertWidget(0, m_pCheckBox);
217 }
218}
219
220
221/*********************************************************************************************************************************
222* Class UIFilmContainer implementation. *
223*********************************************************************************************************************************/
224
225UIFilmContainer::UIFilmContainer(QWidget *pParent /* = 0*/)
226 : QWidget(pParent)
227 , m_pMainLayout(0)
228 , m_pScroller(0)
229{
230 /* Prepare: */
231 prepare();
232}
233
234QVector<bool> UIFilmContainer::value() const
235{
236 /* Enumerate all the existing widgets: */
237 QVector<bool> value;
238 foreach (UIFilm *pWidget, m_widgets)
239 value << static_cast<bool>(pWidget->checked());
240
241 /* Return value: */
242 return value;
243}
244
245void UIFilmContainer::setValue(const QVector<bool> &value)
246{
247 /* Cleanup viewport/widget list: */
248 delete m_pScroller->takeWidget();
249 m_widgets.clear();
250
251 /* Create widget: */
252 QWidget *pWidget = new QWidget;
253 if (pWidget)
254 {
255 /* Create widget-layout: */
256 QHBoxLayout *pWidgetLayout = new QHBoxLayout(pWidget);
257 if (pWidgetLayout)
258 {
259 /* Configure widget-layout: */
260 pWidgetLayout->setContentsMargins(0, 0, 0, 0);
261#ifdef VBOX_WS_MAC
262 pWidgetLayout->setContentsMargins(5, 5, 5, 5);
263#else
264 pWidgetLayout->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) / 2);
265#endif
266
267 /* Create new films according passed vector: */
268 for (int iScreenIndex = 0; iScreenIndex < value.size(); ++iScreenIndex)
269 {
270 /* Create new film: */
271 UIFilm *pFilm = new UIFilm(iScreenIndex, value[iScreenIndex]);
272 if (pFilm)
273 {
274 /* Add film into the widget list: */
275 m_widgets << pFilm;
276
277 /* Add into layout: */
278 pWidgetLayout->addWidget(pFilm);
279 }
280 }
281 }
282
283 /* Assign scroller with widget: */
284 m_pScroller->setWidget(pWidget);
285 /* Reconfigure scroller widget: */
286 m_pScroller->widget()->setAutoFillBackground(false);
287 /* And adjust that widget geometry: */
288 QSize msh = m_pScroller->widget()->minimumSizeHint();
289 int iMinimumHeight = msh.height();
290 m_pScroller->viewport()->setFixedHeight(iMinimumHeight);
291 }
292}
293
294void UIFilmContainer::prepare()
295{
296 /* Prepare layout: */
297 prepareLayout();
298 /* Prepare scroller: */
299 prepareScroller();
300
301 /* Append with 'default' value: */
302 setValue(QVector<bool>() << true);
303}
304
305void UIFilmContainer::prepareLayout()
306{
307 /* Create layout: */
308 m_pMainLayout = new QVBoxLayout(this);
309 if (m_pMainLayout)
310 {
311 /* Configure layout: */
312 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
313 m_pMainLayout->setSpacing(0);
314 }
315}
316
317void UIFilmContainer::prepareScroller()
318{
319 /* Create scroller: */
320 m_pScroller = new QScrollArea;
321 if (m_pScroller)
322 {
323 /* Configure scroller: */
324 m_pScroller->setFrameShape(QFrame::NoFrame);
325 m_pScroller->viewport()->setAutoFillBackground(false);
326 m_pScroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
327 m_pScroller->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
328
329 /* Add into layout: */
330 m_pMainLayout->addWidget(m_pScroller);
331 }
332}
333
334
335#include "UIFilmContainer.moc"
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use