QtBase  v6.3.1
window.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef WINDOW_H
30 #define WINDOW_H
31 
32 
33 #include <QGraphicsScene>
34 #include <QGraphicsWidget>
35 #include <QGraphicsLinearLayout>
36 #include <QGraphicsView>
37 #include <QSpinBox>
38 #include <QCheckBox>
39 #include <QPushButton>
40 #include <QLabel>
41 #include <QHBoxLayout>
42 #include <QPainter>
43 #include <QApplication>
44 #include <QThread>
45 #include <QMap>
46 #include <QElapsedTimer>
47 #include <QDebug>
48 
49 struct Statistics {
52  {
53  }
59  void sleep()
60  {
62  }
65 
66 };
67 
68 
69 class Window;
70 
71 class SlowWidget : public QGraphicsWidget {
72 public:
73  SlowWidget(QGraphicsWidget *w = nullptr, Qt::WindowFlags wFlags = {}) : QGraphicsWidget(w, wFlags)
74  {
75  m_window = 0;
76  }
77 
78  void setStats(Statistics *stats)
79  {
80  m_stats = stats;
81  }
82 
84  {
85  m_window = window;
86  }
87 
88  void setGeometry(const QRectF &rect);
89 
90  bool event(QEvent *e)
91  {
92  if (e->type() == QEvent::LayoutRequest) {
93  if (m_stats->sleepMsecs > 0) {
94  m_stats->sleep();
95  qDebug("sleep %d ms\n", m_stats->sleepMsecs);
96  }
97  }
98  return QGraphicsWidget::event(e);
99  }
100 
102  {
103  Q_UNUSED(option);
104  Q_UNUSED(widget);
105  painter->setBrush(m_brush);
107  painter->drawLine(rect().topLeft(), rect().bottomRight());
108  painter->drawLine(rect().bottomLeft(), rect().topRight());
109  }
110 
111  void setBrush(const QBrush &brush)
112  {
113  m_brush = brush;
114  }
115 private:
116  QBrush m_brush;
117  Statistics *m_stats;
118  Window *m_window;
119 };
120 
121 class Window : public QWidget {
122  Q_OBJECT
123 public:
125  {
127 
128  m_window = 0;
129  m_leaf = 0;
130 
131  m_button = new QPushButton(tr("Relayout"));
132  m_button->setObjectName("button");
133 
134  m_sleepLabel = new QLabel(tr("Sleep:"));
135  m_sleepSpinBox = new QSpinBox;
136  m_sleepSpinBox->setRange(0, 1000);
138 
139  m_depthLabel = new QLabel(tr("Depth:"));
140  m_depthSpinBox = new QSpinBox;
141  m_depthSpinBox->setObjectName("depthSpinBox");
142  m_depthSpinBox->setRange(1, 200);
144 
145  m_benchmarkIterationsLabel = new QLabel(tr("Benchmark iterations"));
147  m_benchmarkIterationsSpinBox->setObjectName("benchmarkIterationsSpinBox");
151 
152  m_instantCheckBox = new QCheckBox(tr("Instant propagation"));
153  m_instantCheckBox->setObjectName("instantPropagationCheckbox");
156 
157  m_resultLabel = new QLabel(tr("Press relayout to start test"));
158 
159  QHBoxLayout *hbox = new QHBoxLayout;
160  hbox->addWidget(m_sleepLabel);
161  hbox->addWidget(m_sleepSpinBox);
162  hbox->addWidget(m_depthLabel);
163  hbox->addWidget(m_depthSpinBox);
167  hbox->addWidget(m_resultLabel);
168  hbox->addStretch();
169  hbox->addWidget(m_button);
170 
171  QVBoxLayout *vbox = new QVBoxLayout;
172  vbox->addWidget(m_view);
173  vbox->addLayout(hbox);
174  setLayout(vbox);
175 
176  metaObject()->connectSlotsByName(this);
177 
178  m_depthSpinBox->setValue(20); // triggers purposedly on_depthSpinBox_valueChanged
179  }
180 
181 private slots:
182  void on_depthSpinBox_valueChanged(int value)
183  {
184  m_stats.relayoutClicked = false;
185  if (m_window) {
187  delete m_window;
188  }
189  m_window = new SlowWidget(0, Qt::Window);
191  m_window->setWindow(this);
192  QColor col(Qt::black);
193  m_window->setBrush(col);
195  m_leaf = 0;
196  const int depth = value;
198  for (int i = 1; i < depth; ++i) {
200  l->setContentsMargins(2,2,2,2);
201  SlowWidget *child = new SlowWidget;
202  QColor col;
203  col.setHsl(0, 0, 255*i/(depth - 1));
204  child->setBrush(col);
205  child->setStats(&m_stats);
206  child->setWindow(this);
207  l->addItem(child);
208  parent = child;
209  }
210  m_leaf = parent;
211  }
212 
213  void on_button_clicked(bool /*check = false*/)
214  {
215  m_stats.relayoutClicked = true;
216  if (m_leaf) {
217  QSizeF sz = m_leaf->size();
218  int w = int(sz.width());
219  w^=16;
220  sz = QSizeF(w,w);
222  m_stats.output->setText(QString("wait..."));
226  m_stats.timer.start();
228  m_leaf->setMinimumSize(sz);
229  m_leaf->setMaximumSize(sz);
230  }
231  }
232 
233  void on_instantPropagationCheckbox_toggled(bool checked)
234  {
236  }
237 
238 public slots:
239  void doAgain()
240  {
241  if (m_leaf) {
242  QSizeF sz = m_leaf->size();
243  int w = int(sz.width());
244  w^=16;
245  sz = QSizeF(w,w);
246  m_leaf->setMinimumSize(sz);
247  m_leaf->setMaximumSize(sz);
248  }
249  }
250 
251 private:
252 public:
267 
268 
269 };
270 
271 
272 #endif //WINDOW_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
void addWidget(QWidget *, int stretch=0, Qt::Alignment alignment=Qt::Alignment())
void addStretch(int stretch=0)
void addLayout(QLayout *layout, int stretch=0)
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:66
The QCheckBox widget provides a checkbox with a text label.
Definition: qcheckbox.h:55
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
void setHsl(int h, int s, int l, int a=255)
Definition: qcolor.cpp:1209
static void processEvents(QEventLoop::ProcessEventsFlags flags=QEventLoop::AllEvents)
friend class QPushButton
[1]
Definition: qdialog.h:57
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:49
void start() noexcept
The QEvent class is the base class of all event classes. Event objects contain event parameters.
Definition: qcoreevent.h:58
@ LayoutRequest
Definition: qcoreevent.h:125
friend class QGraphicsWidget
QGraphicsWidget * window() const
static void setInstantInvalidatePropagation(bool enable)
static bool instantInvalidatePropagation()
void setMaximumSize(const QSizeF &size)
void setMinimumSize(const QSizeF &size)
The QGraphicsLinearLayout class provides a horizontal or vertical layout for managing widgets in Grap...
QGraphicsObject * parent
the parent of the item
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
void setLayout(QGraphicsLayout *layout)
friend class QGraphicsView
QRectF rect() const
bool event(QEvent *event) override
QSizeF size
the size of the widget
The QHBoxLayout class lines up widgets horizontally.
Definition: qboxlayout.h:114
The QLabel widget provides a text or image display.
Definition: qlabel.h:56
void setText(const QString &)
Definition: qlabel.cpp:297
void clear()
Definition: qmap.h:324
void setObjectName(const QString &name)
Definition: qobject.cpp:1261
int depth() const
Definition: qpaintdevice.h:88
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void drawLine(const QLineF &line)
Definition: qpainter.h:477
void setBrush(const QBrush &brush)
Definition: qpainter.cpp:3755
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
Definition: qpainter.cpp:3924
The QPushButton widget provides a command button.
Definition: qpushbutton.h:56
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
constexpr qreal width() const noexcept
Definition: qsize.h:349
The QSpinBox class provides a spin box widget.
Definition: qspinbox.h:52
void setValue(int val)
Definition: qspinbox.cpp:229
void setSingleStep(int val)
Definition: qspinbox.cpp:343
int value
the value of the spin box
Definition: qspinbox.h:62
void setRange(int min, int max)
Definition: qspinbox.cpp:418
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
static void msleep(unsigned long)
The QVBoxLayout class lines up widgets vertically.
Definition: qboxlayout.h:127
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
bool event(QEvent *e)
Definition: window.h:90
void setWindow(Window *window)
Definition: window.h:83
SlowWidget(QGraphicsWidget *w=nullptr, Qt::WindowFlags wFlags={})
Definition: window.h:73
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: window.h:101
void setStats(Statistics *stats)
Definition: window.h:78
void setGeometry(const QRectF &rect)
Definition: window.cpp:31
void setBrush(const QBrush &brush)
Definition: window.h:111
[Window class definition]
Definition: window.h:64
QPushButton * m_button
Definition: window.h:255
QGraphicsWidget * m_leaf
Definition: window.h:264
QSpinBox * m_sleepSpinBox
Definition: window.h:257
void doAgain()
Definition: window.h:239
QSpinBox * m_depthSpinBox
Definition: window.h:259
Statistics m_stats
Definition: window.h:266
QLabel * m_sleepLabel
Definition: window.h:256
QLabel * m_resultLabel
Definition: window.h:263
SlowWidget * m_window
Definition: window.h:265
Window()
Definition: window.h:124
QGraphicsScene scene
Definition: window.h:253
QGraphicsView * m_view
Definition: window.h:254
QLabel * m_benchmarkIterationsLabel
Definition: window.h:260
QSpinBox * m_benchmarkIterationsSpinBox
Definition: window.h:261
QCheckBox * m_instantCheckBox
Definition: window.h:262
QLabel * m_depthLabel
Definition: window.h:258
QOpenGLWidget * widget
[1]
double e
@ RelativeSize
Definition: qnamespace.h:1163
@ black
Definition: qnamespace.h:61
@ Window
Definition: qnamespace.h:232
Definition: brush.cpp:52
#define QString()
Definition: parse-defines.h:51
EGLOutputLayerEXT EGLint EGLAttrib value
#define qDebug
[1]
Definition: qlogging.h:177
GLint GLenum GLsizei GLsizei GLsizei depth
GLfloat GLfloat GLfloat w
[0]
GLuint GLenum option
Definition: qopenglext.h:5929
#define tr(X)
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
Q_UNUSED(salary)
[21]
obj metaObject() -> className()
QLayoutItem * child
[0]
QPainter painter(this)
[7]
void sleep()
Definition: window.h:59
int currentBenchmarkIteration
Definition: window.h:63
Statistics()
Definition: window.h:50
bool relayoutClicked
Definition: window.h:64
QMap< QGraphicsWidget *, int > setGeometryTracker
Definition: window.h:54
QElapsedTimer timer
Definition: window.h:55
int setGeometryCount
Definition: window.h:56
int sleepMsecs
Definition: window.h:57
QLabel * output
Definition: window.h:58