KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
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 QString debugName() const override
148 {
149 return objectName();
150 }
151
152 void setHost(Core::LayoutingHost *parent) override
153 {
154 if (auto p = dynamic_cast<QWidget *>(parent)) {
156 } else if (!parent) {
157 QWidget::setParent(nullptr);
158 } else {
159 qFatal("Expected a QWidget");
160 }
161 }
162
163 Core::LayoutingHost *host() const override
164 {
165 return dynamic_cast<Core::LayoutingHost *>(parentWidget());
166 }
167
168 QString id() const override
169 {
170 return m_uniqueName;
171 }
172
173 virtual bool freed() const override
174 {
175 return false;
176 }
177
178 const QString m_uniqueName;
179 const QColor m_color;
180};
181
182}
183
184int main(int argc, char **argv)
185{
186 QApplication app(argc, argv);
187
189 Core::Item::setCreateSeparatorFunc([](Core::LayoutingHost *host, Qt::Orientation orientation, Core::ItemBoxContainer *container) -> Core::LayoutingSeparator * {
190 return new Separator(host, orientation, container);
191 });
192
193 Host host;
194 host.resize(1000, 1000);
195
196 auto guest1 = new Guest(&host, "1", QColor("#41729F"));
197 auto guest2 = new Guest(&host, "2", QColor("#5885AF"));
198 auto guest3 = new Guest(&host, "3", QColor("#274472"));
199 auto guest4 = new Guest(&host, "4", QColor("#C3E0E5"));
200
201 // Guest1 will occupy the whole left
202 host.insertItem(guest1, KDDockWidgets::Location_OnLeft);
203
204 // Guest2 will occupy the whole right
205 host.insertItem(guest2, KDDockWidgets::Location_OnRight);
206
207
208 // Actually guest2 will need to split the right side with guest 3 as well.
209 host.insertItemRelativeTo(guest3, /*relativeTo=*/guest2, KDDockWidgets::Location_OnBottom);
210
211 // Guest4 goes on top of everything, but with 200px height (width is ignored)
212 host.insertItem(guest4, KDDockWidgets::Location_OnTop, Size(0, 200));
213
214 // For debugging only:
215 // host.root.dumpLayout();
216
217 host.show();
218
219 return app.exec();
220}
int main(int argc, char **argv)
Definition main.cpp:184
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
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