KDDockWidgets API Documentation 2.1
Loading...
Searching...
No Matches
qtwidgets/main.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2023 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
15
16#include "../../Item_p.h"
17#include "../../LayoutingHost_p.h"
18#include "../../LayoutingGuest_p.h"
19#include "../../LayoutingSeparator_p.h"
20
21#include <QApplication>
22#include <QDebug>
23#include <QPainter>
24
25using namespace KDDockWidgets;
26
27namespace {
28
30class Separator : public QWidget, public Core::LayoutingSeparator
31{
32public:
33 explicit Separator(Core::LayoutingHost *host, Qt::Orientation orientation, Core::ItemBoxContainer *container)
34 : QWidget(dynamic_cast<QWidget *>(host))
35 , Core::LayoutingSeparator(host, orientation, container)
36 {
37 }
38
39 Rect geometry() const override
40 {
41 return QWidget::geometry();
42 }
43
44 void setGeometry(Rect r) override
45 {
47 }
48
49 void mousePressEvent(QMouseEvent *) override
50 {
51 onMousePress();
52 }
53
54 void mouseReleaseEvent(QMouseEvent *) override
55 {
56 onMouseRelease();
57 }
58
59 void mouseMoveEvent(QMouseEvent *ev) override
60 {
61 onMouseMove(mapToParent(ev->pos()));
62 }
63
64 void enterEvent(KDDockWidgets::Qt5Qt6Compat::QEnterEvent *) override
65 {
66 if (isVertical())
68 else
70 }
71
72 void leaveEvent(QEvent *) override
73 {
75 }
76};
77
79class Host : public QWidget, public Core::LayoutingHost
80{
81public:
82 Host()
83 {
84 m_rootItem = new Core::ItemBoxContainer(this);
85
86 setObjectName("LayoutingHost QWidget");
87 m_rootItem->setSize_recursive(size());
88 }
89
90 bool supportsHonouringLayoutMinSize() const override
91 {
92 // Yes, it's supported by QtWidgets
93 return true;
94 }
95
97 void resizeEvent(QResizeEvent *) override
98 {
99 m_rootItem->setSize_recursive(size());
100 }
101};
102
104class Guest : public QWidget, public Core::LayoutingGuest
105{
106public:
107 explicit Guest(Host *host, const QString &uniqueName, QColor color)
108 : m_uniqueName(uniqueName)
109 , m_color(color)
110 {
111 setObjectName(QString("%1 %2").arg(uniqueName).arg(color.name()));
112 auto item = new Core::Item(host);
113 item->setGuest(this);
114 }
115
116 void paintEvent(QPaintEvent *) override
117 {
118 QPainter p(this);
119 p.fillRect(rect(), m_color);
120 }
121
122 Size minSize() const override
123 {
124 return QWidget::minimumSize();
125 }
126
127 Size maxSizeHint() const override
128 {
129 return QWidget::maximumSize();
130 }
131
132 void setGeometry(Rect r) override
133 {
135 }
136
137 void setVisible(bool is) override
138 {
140 }
141
142 Rect geometry() const override
143 {
144 return QWidget::geometry();
145 }
146
147 void setHost(Core::LayoutingHost *parent) override
148 {
149 if (auto p = dynamic_cast<QWidget *>(parent)) {
151 } else if (!parent) {
152 QWidget::setParent(nullptr);
153 } else {
154 qFatal("Expected a QWidget");
155 }
156 }
157
158 Core::LayoutingHost *host() const override
159 {
160 return dynamic_cast<Core::LayoutingHost *>(parentWidget());
161 }
162
163 QString id() const override
164 {
165 return m_uniqueName;
166 }
167
168 virtual bool freed() const override
169 {
170 return false;
171 }
172
173 const QString m_uniqueName;
174 const QColor m_color;
175};
176
177}
178
179int main(int argc, char **argv)
180{
181 QApplication app(argc, argv);
182
184 Core::Item::setCreateSeparatorFunc([](Core::LayoutingHost *host, Qt::Orientation orientation, Core::ItemBoxContainer *container) -> Core::LayoutingSeparator * {
185 return new Separator(host, orientation, container);
186 });
187
188 Host host;
189 host.resize(1000, 1000);
190
191 auto guest1 = new Guest(&host, "1", QColor("#41729F"));
192 auto guest2 = new Guest(&host, "2", QColor("#5885AF"));
193 auto guest3 = new Guest(&host, "3", QColor("#274472"));
194 auto guest4 = new Guest(&host, "4", QColor("#C3E0E5"));
195
196 // Guest1 will occupy the whole left
197 host.insertItem(guest1, KDDockWidgets::Location_OnLeft);
198
199 // Guest2 will occupy the whole right
200 host.insertItem(guest2, KDDockWidgets::Location_OnRight);
201
202
203 // Actually guest2 will need to split the right side with guest 3 as well.
204 host.insertItemRelativeTo(guest3, /*relativeTo=*/guest2, KDDockWidgets::Location_OnBottom);
205
206 // Guest4 goes on top of everything, but with 200px height (width is ignored)
207 host.insertItem(guest4, KDDockWidgets::Location_OnTop, Size(0, 200));
208
209 // For debugging only:
210 // host.root.dumpLayout();
211
212 host.show();
213
214 return app.exec();
215}
Class to abstract QAction, so code still works with QtQuick and Flutter.
@ Location_OnTop
‍Left docking location
@ Location_OnRight
‍Top docking location
@ Location_OnBottom
‍Right docking location
QString name() const const
QPoint pos() const const
void setObjectName(const QString &name)
SizeVerCursor
Orientation
int main(int argc, char **argv)
void setCursor(const QCursor &)
virtual void enterEvent(QEvent *event)
virtual void leaveEvent(QEvent *event)
QPoint mapToParent(const QPoint &pos) const const
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void paintEvent(QPaintEvent *event)
QWidget * parentWidget() const const
virtual void resizeEvent(QResizeEvent *event)
void setGeometry(int x, int y, int w, int h)
void setParent(QWidget *parent)
virtual void setVisible(bool visible)

© 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