KDDockWidgets API Documentation 1.7
Loading...
Searching...
No Matches
FloatingWindowWidget.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
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#include "KDDockWidgets.h"
19
20#include <QApplication>
21#include <QPainter>
22#include <QVBoxLayout>
23#include <QWindow>
24#include <QWindowStateChangeEvent>
25
26using namespace KDDockWidgets;
27
28FloatingWindowWidget::FloatingWindowWidget(QRect suggestedGeometry, MainWindowBase *parent,
29 FloatingWindowFlags flags)
30 : FloatingWindow(suggestedGeometry, parent, flags)
31 , m_vlayout(new QVBoxLayout(this))
32{
33 init();
34}
35
36FloatingWindowWidget::FloatingWindowWidget(Frame *frame, QRect suggestedGeometry, MainWindowBase *parent)
37 : FloatingWindow(frame, suggestedGeometry, parent)
38 , m_vlayout(new QVBoxLayout(this))
39{
40 init();
41}
42
43void FloatingWindowWidget::paintEvent(QPaintEvent *ev)
44{
45 if (Config::self().disabledPaintEvents() & Config::CustomizableWidget_FloatingWindow) {
47 return;
48 }
49
50 QPainter p(this);
51 QPen pen(0x666666);
52 pen.setWidth(1);
53 pen.setJoinStyle(Qt::MiterJoin);
54 p.setPen(pen);
55 const qreal halfPenWidth = p.pen().widthF() / 2;
56 const QRectF rectf = rect();
57 p.drawRect(rectf.adjusted(halfPenWidth, halfPenWidth, -halfPenWidth, -halfPenWidth));
58}
59
60bool FloatingWindowWidget::event(QEvent *ev)
61{
63 if ((windowFlags() & Qt::Tool) == Qt::Tool) {
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 windowHandle()->installEventFilter(this);
86 }
87
88 return FloatingWindow::event(ev);
89}
90
91bool FloatingWindowWidget::eventFilter(QObject *o, QEvent *ev)
92{
93 if (ev->type() == QEvent::WindowStateChange) {
94
95 // TODOv2: Have this fix for QtQuick too
96
97 // QWidget::windowState() is not reliable as it's emitted both for the spontaneous (async) event and non-spontaneous (sync)
98 // The sync one being useless, as the window manager can still have the old state.
99 // Only emit windowStateChanged once the window manager tells us the state has actually changed
100 // See also QTBUG-102430
101 if (ev->spontaneous()) {
102 m_lastWindowManagerState = windowHandle()->windowState();
103 Q_EMIT windowStateChanged();
104 }
105 }
106
107 return FloatingWindow::eventFilter(o, ev);
108}
109
110void FloatingWindowWidget::init()
111{
112 m_vlayout->setSpacing(0);
113 updateMargins();
114 m_vlayout->addWidget(m_titleBar);
115 m_vlayout->addWidget(m_dropArea);
116
117 connect(DockRegistry::self(), &DockRegistry::windowChangedScreen, this, [this](QWindow *w) {
118 if (w == window()->windowHandle())
119 updateMargins();
120 });
121}
122
123void FloatingWindowWidget::updateMargins()
124{
125 m_vlayout->setContentsMargins(QMargins(4, 4, 4, 4) * logicalDpiFactor(this));
126}
File with KDDockWidgets namespace-level enums and methods.
@ CustomizableWidget_FloatingWindow
A top-level window. The container for 1 or more Frame nested side by side.
Definition Config.h:122
static Config & self()
returns the singleton Config instance
Definition Config.cpp:84
Flags flags() const
returns the chosen flags
Definition Config.cpp:95
@ Flag_DoubleClickMaximizes
Double clicking the titlebar will maximize a floating window instead of re-docking it.
Definition Config.h:96
@ Flag_NativeTitleBar
Enables the Native OS title bar on OSes that support it (Windows 10, macOS), ignored otherwise.
Definition Config.h:89
The MainWindow base-class. MainWindow and MainWindowBase are only split in two so we can share some c...
NonClientAreaMouseButtonDblClick
bool spontaneous() const const
QEvent::Type type() const const
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
MiterJoin
virtual void paintEvent(QPaintEvent *event)
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