KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/View.h
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: SĂ©rgio Martins <sergio.martins@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
8
9 Contact KDAB at <info@kdab.com> for commercial licensing options.
10*/
11
12#ifndef KD_VIEW_QTWIDGETS_H
13#define KD_VIEW_QTWIDGETS_H
14
15#pragma once
16
17#include "kddockwidgets/core/Controller.h"
18#include "kddockwidgets/qtcommon/View.h"
19
20#include <QDebug>
21#include <QEvent>
22#include <QResizeEvent>
23#include <QSizePolicy>
24#include <QWidget>
25#include <QApplication>
26#include <QWindow>
27
28#include <memory>
29
31
32DOCKS_EXPORT QSize boundedMaxSize(QSize min, QSize max);
33
34template<typename Base>
35class DOCKS_EXPORT View : public Base, public QtCommon::View_qt
36{
37public:
38 using Core::View::close;
39 using Core::View::height;
40 using Core::View::minimumHeight;
41 using Core::View::minimumWidth;
42 using Core::View::move;
43 using Core::View::pos;
44 using Core::View::rect;
45 using Core::View::resize;
46 using Core::View::setSize;
47 using Core::View::size;
48 using Core::View::width;
49 using Core::View::x;
50 using Core::View::y;
51
52 explicit View(Core::Controller *controller, Core::ViewType type,
53 QWidget *parent = nullptr, Qt::WindowFlags windowFlags = {});
54
55 ~View() override = default;
56
57 QSize minSize() const override
58 {
59 const int minW =
60 Base::minimumWidth() > 0 ? Base::minimumWidth() : minimumSizeHint().width();
61
62 const int minH =
63 Base::minimumHeight() > 0 ? Base::minimumHeight() : minimumSizeHint().height();
64
65 return QSize(minW, minH).expandedTo(Core::View::hardcodedMinimumSize());
66 }
67
68 QSize minimumSizeHint() const override
69 {
70 return Base::minimumSizeHint();
71 }
72
73 void setMinimumSize(QSize sz) override;
74
75 QSize maxSizeHint() const override
76 {
77 // The max size is usually QWidget::maximumSize(), but we also honour the
78 // QSizePolicy::Fixed+sizeHint() case as widgets don't need to have QWidget::maximumSize()
79 // to have a max size honoured
80
81 const QSize min = minSize();
82 QSize max = Base::maximumSize();
83 max = QtWidgets::boundedMaxSize(min, max); // for safety against weird values
84
85 const auto vPolicy = QWidget::sizePolicy().verticalPolicy();
86 const auto hPolicy = QWidget::sizePolicy().horizontalPolicy();
87
88 if (vPolicy == QSizePolicy::Fixed || vPolicy == QSizePolicy::Maximum)
89 max.setHeight(qMin(max.height(), Base::sizeHint().height()));
90 if (hPolicy == QSizePolicy::Fixed || hPolicy == QSizePolicy::Maximum)
91 max.setWidth(qMin(max.width(), Base::sizeHint().width()));
92
93 max = QtWidgets::boundedMaxSize(min, max); // for safety against weird values
94 return max;
95 }
96
97 QRect geometry() const override
98 {
99 return Base::geometry();
100 }
101
102 QRect normalGeometry() const override
103 {
104 return Base::normalGeometry();
105 }
106
107 void setGeometry(QRect geo) override
108 {
109 Base::setGeometry(geo);
110 }
111
112 void setMaximumSize(QSize sz) override;
113
114 bool isVisible() const override
115 {
116 return Base::isVisible();
117 }
118
119 bool isExplicitlyHidden() const override
120 {
121 return Base::isHidden();
122 }
123
124 void setVisible(bool is) override
125 {
126 Base::setVisible(is);
127 }
128
129 void move(int x, int y) override
130 {
131 Base::move(x, y);
132 }
133
134 void setSize(int width, int height) override
135 {
136 Base::resize(width, height);
137 }
138
139 void setWidth(int width) override
140 {
141 setSize(width, QWidget::height());
142 }
143
144 void setHeight(int height) override
145 {
146 setSize(QWidget::width(), height);
147 }
148
149 void setFixedWidth(int w) override
150 {
152 }
153
154 void setFixedHeight(int h) override
155 {
157 }
158
159 void show() override
160 {
161 Base::show();
162 }
163
164 void createPlatformWindow() override
165 {
167 }
168
169 void hide() override
170 {
171 Base::hide();
172 }
173
174 void update() override
175 {
176 Base::update();
177 }
178
179 static void setParentFor(QWidget *widget, Core::View *parent)
180 {
181 if (!parent) {
182 widget->QWidget::setParent(nullptr);
183 return;
184 }
185
186 if (auto qwidget = View_qt::asQWidget(parent)) {
187 widget->QWidget::setParent(qwidget);
188 } else {
189 qWarning() << Q_FUNC_INFO << "parent is not a widget, you have a bug";
190 Q_ASSERT(false);
191 }
192 }
193
194 void setParent(Core::View *parent) override
195 {
196 setParentFor(this, parent);
197 }
198
199 static void raiseAndActivate(QWidget *w)
200 {
201 w->window()->raise();
202 const bool isWayland = qApp->platformName() == QLatin1String("wayland");
203 if (!isWayland)
204 w->window()->activateWindow();
205 }
206
207 void raiseAndActivate() override
208 {
209 raiseAndActivate(this);
210 }
211
212 void activateWindow() override
213 {
214 Base::activateWindow();
215 }
216
217 void raise() override
218 {
219 Base::raise();
220 }
221
222 bool isRootView() const override
223 {
224 return QWidget::isWindow();
225 }
226
227 QPoint mapToGlobal(QPoint localPt) const override
228 {
229 return Base::mapToGlobal(localPt);
230 }
231
232 QPoint mapFromGlobal(QPoint globalPt) const override
233 {
234 return Base::mapFromGlobal(globalPt);
235 }
236
237 QPoint mapTo(Core::View *someAncestor, QPoint pos) const override
238 {
239 return QWidget::mapTo(View_qt::asQWidget(someAncestor), pos);
240 }
241
242 void setWindowOpacity(double v) override
243 {
245 }
246
247 void render(QPainter *p) override
248 {
249 Base::render(p);
250 }
251
252 void setCursor(Qt::CursorShape shape) override
253 {
254 QWidget::setCursor(shape);
255 }
256
257 void setMouseTracking(bool enable) override
258 {
260 }
261
262 bool close() override
263 {
264 return QWidget::close();
265 }
266
267 void setFlag(Qt::WindowType flag, bool on = true) override
268 {
269 QWidget::setWindowFlag(flag, on);
270 }
271
272 void enableAttribute(Qt::WidgetAttribute attr, bool enable = true) override
273 {
274 QWidget::setAttribute(attr, enable);
275 }
276
277 bool hasAttribute(Qt::WidgetAttribute attr) const override
278 {
279 return QWidget::testAttribute(attr);
280 }
281
282 Qt::WindowFlags flags() const override
283 {
284 return QWidget::windowFlags();
285 }
286
287 void setWindowTitle(const QString &title) override
288 {
290 }
291
292 void setWindowIcon(const QIcon &icon) override
293 {
295 }
296
297 bool isActiveWindow() const override
298 {
300 }
301
302 void showNormal() override
303 {
304 return QWidget::showNormal();
305 }
306
307 void showMinimized() override
308 {
309 return QWidget::showMinimized();
310 }
311
312 void showMaximized() override
313 {
314 return QWidget::showMaximized();
315 }
316
317 bool isMinimized() const override
318 {
319 return QWidget::isMinimized();
320 }
321
322 bool isMaximized() const override
323 {
324 return QWidget::isMaximized();
325 }
326
328 {
329 return QWidget::focusPolicy();
330 }
331
332 bool hasFocus() const override
333 {
334 return QWidget::hasFocus();
335 }
336
337 std::shared_ptr<Core::View> childViewAt(QPoint localPos) const override;
338
339 std::shared_ptr<Core::Window> window() const override;
340
341 std::shared_ptr<Core::View> rootView() const override;
342
343 std::shared_ptr<Core::View> parentView() const override;
344
345 std::shared_ptr<Core::View> asWrapper() override;
346
347 void grabMouse() override
348 {
350 }
351
352 void releaseMouse() override
353 {
355 }
356
357 void releaseKeyboard() override
358 {
360 }
361
362 void setFocus(Qt::FocusReason reason) override
363 {
364 QWidget::setFocus(reason);
365 }
366
367 void setFocusPolicy(Qt::FocusPolicy policy) override
368 {
370 }
371
372 QString viewName() const override
373 {
374 return QWidget::objectName();
375 }
376
377 static QVector<std::shared_ptr<Core::View>> childViewsFor(const QWidget *parent);
378
380 {
381 return childViewsFor(this);
382 }
383
384protected:
385 bool event(QEvent *e) override;
386 void closeEvent(QCloseEvent *ev) override;
387
388 void resizeEvent(QResizeEvent *ev) override
389 {
390 if (!onResize(ev->size()))
391 Base::resizeEvent(ev);
392 }
393
394private:
395 Q_DISABLE_COPY(View)
396};
397
398inline qreal logicalDpiFactor(const QWidget *w)
399{
400#ifdef DOCKS_DEVELOPER_MODE
401 if (QtCommon::View_qt::s_logicalDpiFactorOverride > 0)
402 return QtCommon::View_qt::s_logicalDpiFactorOverride;
403#endif
404
405#ifdef Q_OS_MACOS
406 // It's always 72 on mac
407 Q_UNUSED(w);
408 return 1;
409#else
410 return w->logicalDpiX() / 96.0;
411#endif
412}
413
415{
416 return w ? w->window()->windowHandle() : nullptr;
417}
418
419
421inline void setTopLevelGeometry(QRect geometry, const QWidget *widget)
422{
423 if (!widget)
424 return;
425
426 if (QWidget *topLevel = widget->window())
427 topLevel->setGeometry(geometry);
428}
429
430}
431
432#endif
void setFlag(Qt::WindowType flag, bool on=true) override
void render(QPainter *p) override
void setSize(int width, int height) override
QString viewName() const override
void resizeEvent(QResizeEvent *ev) override
void setFocus(Qt::FocusReason reason) override
QVector< std::shared_ptr< Core::View > > childViews() const override
@Returns a list of child views
QPoint mapToGlobal(QPoint localPt) const override
void setWindowIcon(const QIcon &icon) override
View(Core::Controller *controller, Core::ViewType type, QWidget *parent=nullptr, Qt::WindowFlags windowFlags={})
QRect normalGeometry() const override
bool isExplicitlyHidden() const override
static void setParentFor(QWidget *widget, Core::View *parent)
QPoint mapTo(Core::View *someAncestor, QPoint pos) const override
QSize minimumSizeHint() const override
void setMouseTracking(bool enable) override
bool hasAttribute(Qt::WidgetAttribute attr) const override
void setWindowTitle(const QString &title) override
void setParent(Core::View *parent) override
void setCursor(Qt::CursorShape shape) override
Qt::FocusPolicy focusPolicy() const override
void enableAttribute(Qt::WidgetAttribute attr, bool enable=true) override
void setWindowOpacity(double v) override
void setFocusPolicy(Qt::FocusPolicy policy) override
void move(int x, int y) override
void setHeight(int height) override
Qt::WindowFlags flags() const override
static void raiseAndActivate(QWidget *w)
QPoint mapFromGlobal(QPoint globalPt) const override
void setGeometry(QRect geo) override
void setWidth(int width) override
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
qreal logicalDpiFactor(const QWidget *w)
QSize boundedMaxSize(QSize min, QSize max)
void setTopLevelGeometry(QRect geometry, const QWidget *widget)
sets the geometry on the QWindow containing the specified item
QWindow * windowForWidget(const QWidget *w)
int logicalDpiX() const const
const QSize & size() const const
QSize expandedTo(const QSize &otherSize) const const
int height() const const
void setHeight(int height)
void setWidth(int width)
int width() const const
CursorShape
FocusPolicy
FocusReason
WidgetAttribute
typedef WindowFlags
void activateWindow()
bool close()
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setCursor(const QCursor &)
bool hasFocus() const const
void grabMouse()
bool isWindow() const const
QPoint mapTo(const QWidget *parent, const QPoint &pos) const const
bool isMaximized() const const
bool isMinimized() const const
void setMouseTracking(bool enable)
void raise()
void releaseKeyboard()
void releaseMouse()
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void setFixedHeight(int h)
void setFixedWidth(int w)
void setFocus()
void setWindowFlag(Qt::WindowType flag, bool on)
void showMaximized()
void showMinimized()
void showNormal()
bool testAttribute(Qt::WidgetAttribute attribute) const const
QWidget * window() const const
QWindow * windowHandle() const const
void setWindowIcon(const QIcon &icon)
void setWindowOpacity(qreal level)
void setWindowTitle(const QString &)
void setGeometry(int posx, int posy, int w, int h)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8