KDDockWidgets API Documentation  1.4
FloatingWindowWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2021 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 
12 #include "FloatingWindowWidget_p.h"
13 #include "../DockRegistry_p.h"
14 #include "../DropArea_p.h"
15 #include "../Logging_p.h"
16 #include "../TitleBar_p.h"
17 #include "../Utils_p.h"
18 
19 #include <QApplication>
20 #include <QPainter>
21 #include <QVBoxLayout>
22 #include <QWindow>
23 #include <QWindowStateChangeEvent>
24 
25 using namespace KDDockWidgets;
26 
27 FloatingWindowWidget::FloatingWindowWidget(QRect suggestedGeometry, MainWindowBase *parent)
28  : FloatingWindow(suggestedGeometry, parent)
29  , m_vlayout(new QVBoxLayout(this))
30 {
31  init();
32 }
33 
34 FloatingWindowWidget::FloatingWindowWidget(Frame *frame, QRect suggestedGeometry, MainWindowBase *parent)
35  : FloatingWindow(frame, suggestedGeometry, parent)
36  , m_vlayout(new QVBoxLayout(this))
37 {
38  init();
39 }
40 
41 void FloatingWindowWidget::paintEvent(QPaintEvent *ev)
42 {
43  if (Config::self().disabledPaintEvents() & Config::CustomizableWidget_FloatingWindow) {
45  return;
46  }
47 
48  QPainter p(this);
49  QPen pen(0x666666);
50  pen.setWidth(1);
51  pen.setJoinStyle(Qt::MiterJoin);
52  p.setPen(pen);
53  const qreal halfPenWidth = p.pen().widthF() / 2;
54  const QRectF rectf = rect();
55  p.drawRect(rectf.adjusted(halfPenWidth, halfPenWidth, -halfPenWidth, -halfPenWidth));
56 }
57 
58 bool FloatingWindowWidget::event(QEvent *ev)
59 {
60  if (ev->type() == QEvent::WindowStateChange) {
61  Q_EMIT windowStateChanged(static_cast<QWindowStateChangeEvent *>(ev));
62  } else if (ev->type() == QEvent::NonClientAreaMouseButtonDblClick && (Config::self().flags() & Config::Flag_NativeTitleBar)) {
63  if ((windowFlags() & Qt::Tool) == Qt::Tool) {
64  if (Config::self().flags() & Config::Flag_DoubleClickMaximizes) {
65  // Let's refuse to maximize Qt::Tool. It's not natural.
66  // Just avoid this combination: Flag_NativeTitleBar + Qt::Tool + Flag_DoubleClickMaximizes
67  } else {
68  // Double clicking a Qt::Tool title-bar. Triggers a redocking.
69  if (m_titleBar->isFloating()) { // redocking nested floating windows aren't supported
70  m_titleBar->onFloatClicked();
71  return true;
72  }
73  }
74  } else {
75  // A normal Qt::Window window. The OS handles the double click.
76  // In general this will maximize the window, that's the native behaviour.
77  }
78  } else if (ev->type() == QEvent::Show && !m_screenChangedConnection) {
79  // We connect after QEvent::Show, so we have a QWindow. Qt doesn't offer much API to
80  // intercept screen events
81  m_screenChangedConnection =
82  connect(windowHandle(), &QWindow::screenChanged, DockRegistry::self(),
83  [this] { Q_EMIT DockRegistry::self()->windowChangedScreen(windowHandle()); });
84  }
85 
86  return FloatingWindow::event(ev);
87 }
88 
89 void FloatingWindowWidget::init()
90 {
91  m_vlayout->setSpacing(0);
92  updateMargins();
93  m_vlayout->addWidget(m_titleBar);
94  m_vlayout->addWidget(m_dropArea);
95 
96  connect(DockRegistry::self(), &DockRegistry::windowChangedScreen, this, [this](QWindow *w) {
97  if (w == window()->windowHandle())
98  updateMargins();
99  });
100 }
101 
102 void FloatingWindowWidget::updateMargins()
103 {
104  m_vlayout->setContentsMargins(QMargins(4, 4, 4, 4) * logicalDpiFactor(this));
105 }
QVBoxLayout
QEvent::WindowStateChange
WindowStateChange
QWidget::paintEvent
virtual void paintEvent(QPaintEvent *event)
QRect
Qt::MiterJoin
MiterJoin
QWindow
QRectF::adjusted
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
QPainter
QPen
Qt::Tool
Tool
QWindowStateChangeEvent
QMargins
QWindow::screenChanged
void screenChanged(QScreen *screen)
QEvent::type
QEvent::Type type() const const
QEvent
QPaintEvent
QRectF
KDDockWidgets
Definition: Config.cpp:36
KDDockWidgets::MainWindowBase
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
Definition: MainWindowBase.h:56

© 2019-2021 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 Mon Nov 15 2021 00:17:21 for KDDockWidgets API Documentation by doxygen 1.8.20