VirtualBox

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

Last change on this file since 74942 was 71905, checked in by vboxsync, 6 years ago

FE/Qt: bugref:9049: Full and heavy cleanup for UIFilmContainer and move it to VBoxGlobal library.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use