KDDockWidgets API Documentation  1.6
MainWindow.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2022 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"
21 #include "FrameworkWidgetFactory.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 
37 using namespace KDDockWidgets;
38 
39 namespace KDDockWidgets {
40 class MyCentralWidget : public QWidget
41 {
42 public:
43  explicit MyCentralWidget(QWidget *parent = nullptr)
44  : QWidget(parent)
45  {
46  setObjectName(QStringLiteral("MyCentralWidget"));
47  }
48 
49  ~MyCentralWidget() override;
50 };
51 }
52 
53 class MainWindow::Private
54 {
55 public:
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 
87 MyCentralWidget::~MyCentralWidget() = default;
88 
89 
90 MainWindow::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);
100  innerVLayout->addWidget(sideBar(SideBarLocation::North));
101  innerVLayout->addWidget(layoutWidget());
102  innerVLayout->addWidget(sideBar(SideBarLocation::South));
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 
135 void MainWindow::setCentralWidget(QWidget *w)
136 {
138 }
139 
140 SideBar *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 
165 {
166  return centralWidget()->geometry();
167 }
168 
170 {
171  onCloseEvent(ev);
172 }
173 
175 {
176  return d->m_layout;
177 }
QVBoxLayout
KDDockWidgets::MainWindow::setCenterWidgetMargins
void setCenterWidgetMargins(QMargins)
Definition: MainWindow.cpp:156
QRect
QObject::Q_EMIT
Q_EMITQ_EMIT
KDDockWidgets::MainWindow::centerWidgetMargins
QMargins centerWidgetMargins() const override
Definition: MainWindow.cpp:151
KDDockWidgets::MainWindowBase::layoutWidget
LayoutWidget * layoutWidget() const
Definition: MainWindowBase.cpp:205
QWidget::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
QMainWindow::setCentralWidget
void setCentralWidget(QWidget *widget)
QWidget
KDDockWidgets::SideBarLocation::West
@ West
KDDockWidgets::MainWindow::closeEvent
void closeEvent(QCloseEvent *) override
Definition: MainWindow.cpp:169
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Qt::WindowFlags
typedef WindowFlags
QCloseEvent
QString
KDDockWidgets::Config
Singleton to allow to choose certain behaviours of the framework.
Definition: Config.h:75
KDDockWidgets::MainWindow::centralAreaGeometry
QRect centralAreaGeometry() const override
Definition: MainWindow.cpp:164
QWidget::isWindow
bool isWindow() const const
KDDockWidgets::MainWindow::resizeEvent
void resizeEvent(QResizeEvent *) override
Definition: MainWindow.cpp:145
KDDockWidgets::MainWindowBase::onCloseEvent
void onCloseEvent(QCloseEvent *)
Definition: MainWindowBase.cpp:814
QMargins
KDDockWidgets::SideBarLocation
SideBarLocation
Each main window supports 4 sidebars.
Definition: KDDockWidgets.h:208
KDDockWidgets::SideBarLocation::East
@ East
QMainWindow::centralWidget
QWidget * centralWidget() const const
KDDockWidgets::SideBarLocation::South
@ South
Config.h
Application-wide config to tune certain behaviours of the framework.
QWindow::screenChanged
void screenChanged(QScreen *screen)
QWidget::windowHandle
QWindow * windowHandle() const const
MainWindow.h
QMainWindow sub-class to enable KDDockWidgets support.
KDDockWidgets::MainWindow
The QMainwindow sub-class that the application should use to be able to dock KDDockWidget::DockWidget...
Definition: MainWindow.h:37
KDDockWidgets::SideBarLocation::North
@ North
QResizeEvent
QObject::setObjectName
void setObjectName(const QString &name)
QHBoxLayout
KDDockWidgets
Definition: Config.cpp:37
QWidget::geometry
geometry
KDDockWidgets::MainWindowBase
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
Definition: MainWindowBase.h:56
KDDockWidgets::MainWindow::sideBar
SideBar * sideBar(SideBarLocation) const override
returns the sidebar for the specified location
Definition: MainWindow.cpp:140
QHash< SideBarLocation, SideBar * >
KDDockWidgets::MainWindowBase::onResized
void onResized(QResizeEvent *)
Definition: MainWindowBase.cpp:690
KDDockWidgets::MainWindow::~MainWindow
~MainWindow() override
Destructor.
Definition: MainWindow.cpp:130
QWidget::create
void create(WId window, bool initializeWindow, bool destroyOldWindow)
QObject::parent
QObject * parent() const const
KDDockWidgets::MainWindow::MainWindow
MainWindow(const QString &uniqueName, MainWindowOptions options=MainWindowOption_None, QWidget *parent=nullptr, Qt::WindowFlags flags=Qt::WindowFlags())
Constructor. Use it as you would use QMainWindow.
Definition: MainWindow.cpp:90
KDDockWidgets::Config::self
static Config & self()
returns the singleton Config instance
Definition: Config.cpp:84
FrameworkWidgetFactory.h
A factory class for allowing the user to customize some internal widgets.
KDDockWidgets::MainWindow::internalLayout
QHBoxLayout * internalLayout() const
returns the internal layout this is rarely needed unless you want to layout other widgets next to the...
Definition: MainWindow.cpp:174

© 2019-2022 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 Thu Sep 15 2022 00:16:29 for KDDockWidgets API Documentation by doxygen 1.8.20