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 int zOrder() const override
180 {
181 if (auto p = QWidget::parentWidget()) {
182 // Some of them might be non-QtWidget QObject, but this is good enough
183 // to unit test raise() in MDI area
184 // TODO: Remove cast to int, and make method return qsizetype after we add /WX
185 return int(p->children().indexOf(const_cast<QtWidgets::View<Base> *>(this)));
186 }
187
188 return 0;
189 }
190
191 static void setParentFor(QWidget *widget, Core::View *parent)
192 {
193 if (!parent) {
194 widget->QWidget::setParent(nullptr);
195 return;
196 }
197
198 if (auto qwidget = View_qt::asQWidget(parent)) {
199 widget->QWidget::setParent(qwidget);
200 } else {
201 qWarning() << Q_FUNC_INFO << "parent is not a widget, you have a bug";
202 Q_ASSERT(false);
203 }
204 }
205
206 void setParent(Core::View *parent) override
207 {
208 setParentFor(this, parent);
209 }
210
211 static void raiseAndActivate(QWidget *w)
212 {
213 w->window()->raise();
214 const bool isWayland = qApp->platformName() == QLatin1String("wayland");
215 if (!isWayland)
216 w->window()->activateWindow();
217 }
218
219 void raiseAndActivate() override
220 {
221 raiseAndActivate(this);
222 }
223
224 void activateWindow() override
225 {
226 Base::activateWindow();
227 }
228
229 void raise() override
230 {
231 Base::raise();
232 }
233
234 bool isRootView() const override
235 {
236 return QWidget::isWindow();
237 }
238
239 QPoint mapToGlobal(QPoint localPt) const override
240 {
241 return Base::mapToGlobal(localPt);
242 }
243
244 QPoint mapFromGlobal(QPoint globalPt) const override
245 {
246 return Base::mapFromGlobal(globalPt);
247 }
248
249 QPoint mapTo(Core::View *someAncestor, QPoint pos) const override
250 {
251 return QWidget::mapTo(View_qt::asQWidget(someAncestor), pos);
252 }
253
254 void setWindowOpacity(double v) override
255 {
257 }
258
259 void render(QPainter *p) override
260 {
261 Base::render(p);
262 }
263
264 void setCursor(Qt::CursorShape shape) override
265 {
266 QWidget::setCursor(shape);
267 }
268
269 void setMouseTracking(bool enable) override
270 {
272 }
273
274 bool close() override
275 {
276 return QWidget::close();
277 }
278
279 void setFlag(Qt::WindowType flag, bool on = true) override
280 {
281 QWidget::setWindowFlag(flag, on);
282 }
283
284 void enableAttribute(Qt::WidgetAttribute attr, bool enable = true) override
285 {
286 QWidget::setAttribute(attr, enable);
287 }
288
289 bool hasAttribute(Qt::WidgetAttribute attr) const override
290 {
291 return QWidget::testAttribute(attr);
292 }
293
294 Qt::WindowFlags flags() const override
295 {
296 return QWidget::windowFlags();
297 }
298
299 void setWindowTitle(const QString &title) override
300 {
302 }
303
304 void setWindowIcon(const QIcon &icon) override
305 {
307 }
308
309 bool isActiveWindow() const override
310 {
312 }
313
314 void showNormal() override
315 {
316 return QWidget::showNormal();
317 }
318
319 void showMinimized() override
320 {
321 return QWidget::showMinimized();
322 }
323
324 void showMaximized() override
325 {
326 return QWidget::showMaximized();
327 }
328
329 bool isMinimized() const override
330 {
331 return QWidget::isMinimized();
332 }
333
334 bool isMaximized() const override
335 {
336 return QWidget::isMaximized();
337 }
338
340 {
341 return QWidget::focusPolicy();
342 }
343
344 bool hasFocus() const override
345 {
346 return QWidget::hasFocus();
347 }
348
349 std::shared_ptr<Core::View> childViewAt(QPoint localPos) const override;
350
351 std::shared_ptr<Core::Window> window() const override;
352
353 std::shared_ptr<Core::View> rootView() const override;
354
355 std::shared_ptr<Core::View> parentView() const override;
356
357 std::shared_ptr<Core::View> asWrapper() override;
358
359 void grabMouse() override
360 {
362 }
363
364 void releaseMouse() override
365 {
367 }
368
369 void releaseKeyboard() override
370 {
372 }
373
374 void setFocus(Qt::FocusReason reason) override
375 {
376 QWidget::setFocus(reason);
377 }
378
379 void setFocusPolicy(Qt::FocusPolicy policy) override
380 {
382 }
383
384 QString viewName() const override
385 {
386 return QWidget::objectName();
387 }
388
389 static QVector<std::shared_ptr<Core::View>> childViewsFor(const QWidget *parent);
390
392 {
393 return childViewsFor(this);
394 }
395
396protected:
397 bool event(QEvent *e) override;
398 void closeEvent(QCloseEvent *ev) override;
399
400 void resizeEvent(QResizeEvent *ev) override
401 {
402 if (!onResize(ev->size()))
403 Base::resizeEvent(ev);
404 }
405
406private:
407 Q_DISABLE_COPY(View)
408};
409
410inline qreal logicalDpiFactor(const QWidget *w)
411{
412#ifdef DOCKS_DEVELOPER_MODE
413 if (QtCommon::View_qt::s_logicalDpiFactorOverride > 0)
414 return QtCommon::View_qt::s_logicalDpiFactorOverride;
415#endif
416
417#ifdef Q_OS_MACOS
418 // It's always 72 on mac
419 Q_UNUSED(w);
420 return 1;
421#else
422 return w->logicalDpiX() / 96.0;
423#endif
424}
425
427{
428 return w ? w->window()->windowHandle() : nullptr;
429}
430
431
433inline void setTopLevelGeometry(QRect geometry, const QWidget *widget)
434{
435 if (!widget)
436 return;
437
438 if (QWidget *topLevel = widget->window())
439 topLevel->setGeometry(geometry);
440}
441
442}
443
444#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)
QWidget * parentWidget() const const
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