KDDockWidgets API Documentation 1.7
Loading...
Searching...
No Matches
MainWindow.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2019-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
19#include "MainWindow.h"
20#include "Config.h"
22
23#include "private/DockRegistry_p.h"
24#include "private/DropAreaWithCentralFrame_p.h"
25#include "private/DropArea_p.h"
26#include "private/Frame_p.h"
27#include "private/Logging_p.h"
28#include "private/SideBar_p.h"
29
30#include <QPainter>
31#include <QScreen>
32#include <QVBoxLayout>
33#include <QWindow>
34
35// clazy:excludeall=ctor-missing-parent-argument,missing-qobject-macro
36
37using namespace KDDockWidgets;
38
39namespace KDDockWidgets {
40class MyCentralWidget : public QWidget
41{
42public:
43 explicit MyCentralWidget(QWidget *parent = nullptr)
45 {
46 setObjectName(QStringLiteral("MyCentralWidget"));
47 }
48
49 ~MyCentralWidget() override;
50};
51}
52
53class MainWindow::Private
54{
55public:
56 explicit Private(MainWindowOptions, MainWindow *mainWindow)
57 : q(mainWindow)
58 , m_supportsAutoHide(Config::self().flags() & Config::Flag_AutoHideSupport)
59 , m_centralWidget(new MyCentralWidget(mainWindow))
60 , m_layout(new QHBoxLayout(m_centralWidget)) // 1 level of indirection so we can add some margins
61 {
62 if (m_supportsAutoHide) {
63 for (auto location : { SideBarLocation::North, SideBarLocation::East,
65 m_sideBars.insert(location, Config::self().frameworkWidgetFactory()->createSideBar(location, mainWindow));
66 }
67 }
68
69 m_layout->setSpacing(0);
70 updateMargins();
71 }
72
73 void updateMargins()
74 {
75 const qreal factor = logicalDpiFactor(q);
76 m_layout->setContentsMargins(m_centerWidgetMargins * factor);
77 }
78
79 MainWindow *const q;
80 const bool m_supportsAutoHide;
82 MyCentralWidget *const m_centralWidget;
83 QHBoxLayout *const m_layout;
84 QMargins m_centerWidgetMargins = { 1, 5, 1, 1 };
85};
86
87MyCentralWidget::~MyCentralWidget() = default;
88
89
90MainWindow::MainWindow(const QString &name, MainWindowOptions options,
91 QWidget *parent, Qt::WindowFlags flags)
92 : MainWindowBase(name, options, parent, flags)
93 , d(new Private(options, this))
94{
95 if (d->m_supportsAutoHide) {
96 d->m_layout->addWidget(sideBar(SideBarLocation::West));
97 auto innerVLayout = new QVBoxLayout();
98 innerVLayout->setSpacing(0);
99 innerVLayout->setContentsMargins(0, 0, 0, 0);
101 innerVLayout->addWidget(layoutWidget());
103 d->m_layout->addLayout(innerVLayout);
104 d->m_layout->addWidget(sideBar(SideBarLocation::East));
105 } else {
106 d->m_layout->addWidget(layoutWidget());
107 }
108
109 setCentralWidget(d->m_centralWidget);
110
111 const bool isWindow = !parent || (flags & Qt::Window);
112 if (isWindow) {
113 // Update our margins when logical dpi changes.
114 // QWidget doesn't have any screenChanged signal, so we need to use QWindow::screenChanged.
115 // Note #1: Someone might be using this main window embedded into another main window, in which case it will
116 // never have a QWindow, so guard it with isWindow.
117 // Note #2: We don't use QWidget::isWindow() as that will always be true since QMainWindow sets it. Anyone wanting
118 // or not wanting this immediate create() needs to pass a parent/flag pair that makes sense. For example, some people
119 // might want to add this main window into a layout and avoid the create(), so they pass a parent, with null flag.
120
121 create(); // ensure QWindow exists
122 connect(windowHandle(), &QWindow::screenChanged, DockRegistry::self(),
123 [this] {
124 d->updateMargins();
125 Q_EMIT DockRegistry::self()->windowChangedScreen(windowHandle());
126 });
127 }
128}
129
131{
132 delete d;
133}
134
135void MainWindow::setCentralWidget(QWidget *w)
136{
138}
139
140SideBar *MainWindow::sideBar(SideBarLocation location) const
141{
142 return d->m_sideBars.value(location);
143}
144
146{
148 onResized(ev); // Also call our own handler, since QtQuick doesn't have resizeEvent()
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
168
173
175{
176 return d->m_layout;
177}
Application-wide config to tune certain behaviours of the framework.
A factory class for allowing the user to customize some internal widgets.
QMainWindow sub-class to enable KDDockWidgets support.
Singleton to allow to choose certain behaviours of the framework.
Definition Config.h:75
FrameworkWidgetFactory * frameworkWidgetFactory() const
getter for the framework widget factory
Definition Config.cpp:145
static Config & self()
returns the singleton Config instance
Definition Config.cpp:84
virtual SideBar * createSideBar(SideBarLocation loc, MainWindowBase *parent) const =0
Called internally by the framework to create a SideBar.
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
LayoutWidget * layoutWidget() const
The QMainwindow sub-class that the application should use to be able to dock KDDockWidget::DockWidget...
Definition MainWindow.h:37
~MainWindow() override
Destructor.
void resizeEvent(QResizeEvent *) override
void closeEvent(QCloseEvent *) override
QRect centralAreaGeometry() const override
QMargins centerWidgetMargins() const override
void setCenterWidgetMargins(QMargins)
QHBoxLayout * internalLayout() const
returns the internal layout this is rarely needed unless you want to layout other widgets next to the...
MainWindow(const QString &uniqueName, MainWindowOptions options=MainWindowOption_None, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor. Use it as you would use QMainWindow.
SideBar * sideBar(SideBarLocation) const override
returns the sidebar for the specified location
SideBarLocation
Each main window supports 4 sidebars.
QWidget * centralWidget() const const
void setCentralWidget(QWidget *widget)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setObjectName(const QString &name)
QObject * parent() const const
T qobject_cast(QObject *object)
typedef WindowFlags
void create(WId window, bool initializeWindow, bool destroyOldWindow)
bool isWindow() const const
virtual void resizeEvent(QResizeEvent *event)
QWindow * windowHandle() const const
void screenChanged(QScreen *screen)

© 2019-2023 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 on Wed Nov 1 2023 00:02:31 for KDDockWidgets API Documentation by doxygen 1.9.8