KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/MainWindow.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2019 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
19#include "MainWindow.h"
20#include "Config.h"
22#include "ViewWrapper_p.h"
23#include "core/DockRegistry_p.h"
24#include "core/DropArea.h"
25#include "core/MainWindow.h"
26#include "core/Group.h"
27#include "core/SideBar.h"
28#include "core/Window_p.h"
29#include "core/DockRegistry.h"
30
31#include "core/Logging_p.h"
32
33#include <QPainter>
34#include <QScreen>
35#include <QVBoxLayout>
36#include <QWindow>
37
38// clazy:excludeall=ctor-missing-parent-argument,missing-qobject-macro
39
40using namespace KDDockWidgets;
41using namespace KDDockWidgets::QtWidgets;
42
43namespace KDDockWidgets {
44class MyCentralWidget : public QWidget
45{
46public:
47 explicit MyCentralWidget(QWidget *parent = nullptr)
49 {
50 setObjectName(QStringLiteral("MyCentralWidget"));
51 }
52
53 ~MyCentralWidget() override;
54};
55}
56
57class MainWindow::Private
58{
59public:
60 explicit Private(MainWindow *qq)
61 : q(qq)
62 , m_controller(qq->mainWindow())
63 , m_supportsAutoHide(Config::self().flags() & Config::Flag_AutoHideSupport)
64 , m_centralWidget(new MyCentralWidget(qq))
65 , m_layout(new QHBoxLayout(m_centralWidget)) // 1 level of indirection so we can add some
66 // margins
67 {
68 }
69
70 ~Private()
71 {
72 }
73
74 void updateMargins()
75 {
76 const qreal factor = QtWidgets::logicalDpiFactor(q);
77 m_layout->setContentsMargins(m_centerWidgetMargins * factor);
78 }
79
80 MainWindow *const q;
81 Core::MainWindow *const m_controller;
82 const bool m_supportsAutoHide;
83 MyCentralWidget *const m_centralWidget;
84 QHBoxLayout *const m_layout;
85 QMargins m_centerWidgetMargins = { 1, 5, 1, 1 };
86};
87
88MyCentralWidget::~MyCentralWidget() = default;
89
90MainWindow::MainWindow(const QString &uniqueName, MainWindowOptions options,
91 QWidget *parent, Qt::WindowFlags flags)
92 : View<QMainWindow>(new Core::MainWindow(this, uniqueName, options),
93 Core::ViewType::MainWindow, parent, flags)
94 , MainWindowViewInterface(static_cast<Core::MainWindow *>(controller()))
95 , d(new Private(this))
96{
97 m_mainWindow->init(uniqueName);
98
99 d->m_layout->setSpacing(0);
100 d->updateMargins();
101
102 if (d->m_supportsAutoHide) {
103 d->m_layout->addWidget(
104 View_qt::asQWidget(d->m_controller->sideBar(SideBarLocation::West)->view()));
105 auto innerVLayout = new QVBoxLayout();
106 innerVLayout->setSpacing(0);
107 innerVLayout->setContentsMargins(0, 0, 0, 0);
108 innerVLayout->addWidget(
109 View_qt::asQWidget(d->m_controller->sideBar(SideBarLocation::North)));
110 innerVLayout->addWidget(View_qt::asQWidget(d->m_controller->layout()));
111 innerVLayout->addWidget(
112 View_qt::asQWidget(d->m_controller->sideBar(SideBarLocation::South)));
113 d->m_layout->addLayout(innerVLayout);
114 d->m_layout->addWidget(View_qt::asQWidget(d->m_controller->sideBar(SideBarLocation::East)));
115 } else {
116 d->m_layout->addWidget(View_qt::asQWidget(d->m_controller->layout()->view()));
117 }
118
119 setCentralWidget(d->m_centralWidget);
120
121 const bool isWindow = !parentWidget() || (flags & Qt::Window);
122 if (isWindow) {
123 // Update our margins when logical dpi changes.
124 // QWidget doesn't have any screenChanged signal, so we need to use QWindow::screenChanged.
125 // Note #1: Someone might be using this main window embedded into another main window, in
126 // which case it will never have a QWindow, so guard it with isWindow. Note #2: We don't use
127 // QWidget::isWindow() as that will always be true since QMainWindow sets it. Anyone wanting
128 // or not wanting this immediate create() needs to pass a parent/flag pair that makes sense.
129 // For example, some people might want to add this main window into a layout and avoid the
130 // create(), so they pass a parent, with null flag.
131
132 create(); // ensure QWindow exists
133 window()->onScreenChanged(this, [](QObject *context, auto window) {
134 if (auto mw = qobject_cast<MainWindow *>(context))
135 mw->updateMargins(); // logical dpi might have changed
136 DockRegistry::self()->dptr()->windowChangedScreen.emit(window);
137 });
138 }
139}
140
142{
143 delete d;
144}
145
146void MainWindow::setCentralWidget(QWidget *w)
147{
149}
150
152{
153 return d->m_centerWidgetMargins;
154}
155
157{
158 if (d->m_centerWidgetMargins == margins)
159 return;
160 d->m_centerWidgetMargins = margins;
161 d->updateMargins();
162}
163
165{
166 return centralWidget()->geometry();
167}
168
169void MainWindow::setContentsMargins(int left, int top, int right, int bottom)
170{
171 QMainWindow::setContentsMargins(left, top, right, bottom);
172}
173
175{
176 m_mainWindow->setPersistentCentralView(QtWidgets::ViewWrapper::create(widget));
177}
178
180{
181 auto view = m_mainWindow->persistentCentralView();
182 return View_qt::asQWidget(view.get());
183}
184
186{
187 return d->m_layout;
188}
189
191{
192 d->updateMargins();
193}
Application-wide config to tune certain behaviours of the framework.
Singleton to allow to choose certain behaviours of the framework.
Definition Config.h:64
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
std::shared_ptr< View > persistentCentralView() const
void setPersistentCentralView(std::shared_ptr< View > widget)
Sets a persistent central widget. It can't be detached.
static DockRegistry * self()
The QMainwindow sub-class that the application should use to be able to dock KDDockWidget DockWidget ...
void setPersistentCentralWidget(QWidget *widget)
QHBoxLayout * internalLayout() const
returns the internal layout this is rarely needed unless you want to layout other widgets next to the...
void setContentsMargins(int left, int top, int right, int bottom) override
Sets the main window's content margins.
~MainWindow() override
Destructor.
QWidget * persistentCentralWidget() const
void setCenterWidgetMargins(QMargins)
sets the margins for the contents widget
QMargins centerWidgetMargins() const override
returns the margins for the contents widget
void updateMargins()
for internal use only
QRect centralAreaGeometry() const override
A MultiSplitter with support for drop indicators when hovering over.
The MainWindow base-class that's shared between QtWidgets and QtQuick stack.
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
qreal logicalDpiFactor(const QWidget *w)
Class to abstract QAction, so code still works with QtQuick and Flutter.
QWidget * centralWidget() const const
void setCentralWidget(QWidget *widget)
void setObjectName(const QString &name)
QObject * parent() const const
typedef WindowFlags
A factory class for allowing the user to customize some internal widgets.
QMainWindow sub-class to enable KDDockWidgets support.
void setContentsMargins(int left, int top, int right, int bottom)

© 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