KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/ClassicIndicatorsWindow.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
13#include "kddockwidgets/core/indicators/ClassicDropIndicatorOverlay.h"
14#include "kddockwidgets/core/Group.h"
15#include "View.h"
16#include "core/Utils_p.h"
17
18#include <QPainter>
19
20#include <utility>
21
22#ifdef QT_X11EXTRAS_LIB
23#include <QtX11Extras/QX11Info>
24#endif
25
26#define INDICATOR_WIDTH 40
27#define OUTTER_INDICATOR_MARGIN 10
28
29using namespace KDDockWidgets;
30using namespace KDDockWidgets::Core;
31using namespace KDDockWidgets::QtWidgets;
32
33namespace KDDockWidgets {
34
36{
37 if (qEnvironmentVariableIsSet("KDDW_NO_TRANSLUCENCY")
39 return false;
40
41#ifdef QT_X11EXTRAS_LIB
42 if (isXCB())
43 return QX11Info::isCompositingManagerRunning();
44#endif
45
46 // macOS and Windows are fine
47 return true;
48}
49
50class Indicator : public QWidget
51{
53public:
54 typedef QList<Indicator *> List;
55 explicit Indicator(IndicatorWindow *parent, DropLocation location);
56 void paintEvent(QPaintEvent *) override;
57
58 void setHovered(bool hovered);
59 QString iconName(bool active) const;
60 QString iconFileName(bool active) const;
61
62 QImage m_image;
63 QImage m_imageActive;
64 bool m_hovered = false;
65 const DropLocation m_dropLocation;
66};
67
68static QString iconName(DropLocation loc, bool active)
69{
70 QString suffix = active ? QStringLiteral("_active") : QString();
71
72 QString name;
73 switch (loc) {
75 name = QStringLiteral("center");
76 break;
78 name = QStringLiteral("inner_left");
79 break;
81 name = QStringLiteral("inner_right");
82 break;
84 name = QStringLiteral("inner_bottom");
85 break;
87 name = QStringLiteral("inner_top");
88 break;
90 name = QStringLiteral("outter_left");
91 break;
93 name = QStringLiteral("outter_bottom");
94 break;
96 name = QStringLiteral("outter_right");
97 break;
99 name = QStringLiteral("outter_top");
100 break;
106 return QString();
107 }
108
109 return name + suffix;
110}
111}
112
113void Indicator::paintEvent(QPaintEvent *)
114{
115 QPainter p(this);
116 if (m_hovered)
117 p.drawImage(rect(), m_imageActive, rect());
118 else
119 p.drawImage(rect(), m_image, rect());
120}
121
122void Indicator::setHovered(bool hovered)
123{
124 if (hovered != m_hovered) {
125 m_hovered = hovered;
126 update();
127 }
128}
129
130QString Indicator::iconName(bool active) const
131{
132 return KDDockWidgets::iconName(m_dropLocation, active);
133}
134
135QString Indicator::iconFileName(bool active) const
136{
137 const QString name = iconName(active);
139 ? QStringLiteral(":/img/classic_indicators/%1.png").arg(name)
140 : QStringLiteral(":/img/classic_indicators/opaque/%1.png").arg(name);
141}
142
144{
145 // On Wayland it can't be a top-level, as we have no way of positioning it
146
147 return isWayland() ? QtCommon::View_qt::asQWidget(classicIndicators_->view()) : nullptr;
148}
149
154
157 , classicIndicators(classicIndicators_)
158 , m_center(new Indicator(this, DropLocation_Center)) // Each indicator is not a top-level. Otherwise
159 // there's noticeable delay.
160 , m_left(new Indicator(this, DropLocation_Left))
161 , m_right(new Indicator(this, DropLocation_Right))
162 , m_bottom(new Indicator(this, DropLocation_Bottom))
163 , m_top(new Indicator(this, DropLocation_Top))
164 , m_outterLeft(new Indicator(this, DropLocation_OutterLeft))
165 , m_outterRight(new Indicator(this, DropLocation_OutterRight))
166 , m_outterBottom(new Indicator(this, DropLocation_OutterBottom))
167 , m_outterTop(new Indicator(this, DropLocation_OutterTop))
168{
170
172 // Ensure the overlay window is on top
174 }
175
177
178 m_indicators << m_center << m_left << m_right << m_top << m_bottom << m_outterBottom
179 << m_outterTop << m_outterLeft << m_outterRight;
180}
181
182Indicator *IndicatorWindow::indicatorForLocation(DropLocation loc) const
183{
184 switch (loc) {
186 return m_center;
188 return m_left;
190 return m_right;
192 return m_bottom;
193 case DropLocation_Top:
194 return m_top;
196 return m_outterLeft;
198 return m_outterBottom;
200 return m_outterRight;
202 return m_outterTop;
208 return nullptr;
209 }
210
211 return nullptr;
212}
213
214void IndicatorWindow::updateMask()
215{
216 QRegion region;
217
219 for (Indicator *indicator : std::as_const(m_indicators)) {
220 if (indicator->isVisible())
221 region = region.united(QRegion(indicator->geometry(), QRegion::Rectangle));
222 }
223 }
224
225 setMask(region);
226}
227
228void IndicatorWindow::resizeEvent(QResizeEvent *ev)
229{
232}
233
235{
236 for (Indicator *indicator : { m_left, m_right, m_bottom, m_top, m_outterTop, m_outterLeft,
237 m_outterRight, m_outterBottom, m_center })
238 indicator->setVisible(classicIndicators->dropIndicatorVisible(indicator->m_dropLocation));
239
240 updateMask();
241}
242
244{
245 Indicator *indicator = indicatorForLocation(loc);
246 return indicator->mapToGlobal(indicator->rect().center());
247}
248
250{
252
253 for (Indicator *indicator : std::as_const(m_indicators)) {
254 if (indicator->isVisible()) {
255 const bool hovered = indicator->rect().contains(indicator->mapFromGlobal(globalPos));
256 indicator->setHovered(hovered);
257 if (hovered)
258 loc = indicator->m_dropLocation;
259 }
260 }
261
262 return loc;
263}
264
266{
267 QRect r = rect();
268 const int indicatorWidth = m_outterBottom->width();
269 const int halfIndicatorWidth = m_outterBottom->width() / 2;
270
271 m_outterLeft->move(r.x() + OUTTER_INDICATOR_MARGIN, r.center().y() - halfIndicatorWidth);
272 m_outterBottom->move(r.center().x() - halfIndicatorWidth,
273 r.y() + height() - indicatorWidth - OUTTER_INDICATOR_MARGIN);
274 m_outterTop->move(r.center().x() - halfIndicatorWidth, r.y() + OUTTER_INDICATOR_MARGIN);
275 m_outterRight->move(r.x() + width() - indicatorWidth - OUTTER_INDICATOR_MARGIN,
276 r.center().y() - halfIndicatorWidth);
277 Core::Group *hoveredGroup = classicIndicators->hoveredGroup();
278 if (hoveredGroup) {
279 QRect hoveredRect = hoveredGroup->view()->geometry();
280 m_center->move(r.topLeft() + hoveredRect.center()
281 - QPoint(halfIndicatorWidth, halfIndicatorWidth));
282 m_top->move(m_center->pos() - QPoint(0, indicatorWidth + OUTTER_INDICATOR_MARGIN));
283 m_right->move(m_center->pos() + QPoint(indicatorWidth + OUTTER_INDICATOR_MARGIN, 0));
284 m_bottom->move(m_center->pos() + QPoint(0, indicatorWidth + OUTTER_INDICATOR_MARGIN));
285 m_left->move(m_center->pos() - QPoint(indicatorWidth + OUTTER_INDICATOR_MARGIN, 0));
286 }
287}
288
293
298
300{
302}
303
305{
307}
308
313
315{
316 return QWidget::isWindow();
317}
318
319Indicator::Indicator(IndicatorWindow *parent,
320 DropLocation location)
321 : QWidget(parent)
322 , m_dropLocation(location)
323{
324 m_image = QImage(iconFileName(/*active=*/false)).scaled(INDICATOR_WIDTH, INDICATOR_WIDTH);
325 m_imageActive = QImage(iconFileName(/*active=*/true)).scaled(INDICATOR_WIDTH, INDICATOR_WIDTH);
326 setFixedSize(m_image.size());
327 setVisible(true);
328}
329
330#include "ClassicIndicatorsWindow.moc"
@ InternalFlag_DisableTranslucency
Definition Config.h:167
static Config & self()
returns the singleton Config instance
Definition Config.cpp:87
@ Flag_KeepAboveIfNotUtilityWindow
Definition Config.h:119
View * view() const
Returns the view associated with this controller, if any.
virtual bool dropIndicatorVisible(DropLocation) const
Returns whether the specified drop indicator should be visible.
virtual Rect geometry() const =0
IndicatorWindow(Core::ClassicDropIndicatorOverlay *classicIndicators)
Class to abstract QAction, so code still works with QtQuick and Flutter.
DropLocation
Enum describing the different drop indicator types.
static QString iconName(DropLocation loc, bool active)
Definition utils.h:161
QImage scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
Q_OBJECTQ_OBJECT
void setObjectName(const QString &name)
QObject * parent() const const
int x() const const
int y() const const
QPoint center() const const
QPoint topLeft() const const
int x() const const
int y() const const
QRegion united(const QRegion &r) const const
WA_TranslucentBackground
typedef WindowFlags
static Qt::WindowFlags flagsForIndicatorWindow()
static QWidget * parentForIndicatorWindow(ClassicDropIndicatorOverlay *classicIndicators_)
bool isWindow() const const
void raise()
void resize(int w, int h)
virtual void resizeEvent(QResizeEvent *event)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
void setFixedSize(const QSize &s)
void setGeometry(int x, int y, int w, int h)
void setMask(const QBitmap &bitmap)
void setWindowFlag(Qt::WindowType flag, bool on)
virtual void setVisible(bool visible)

© 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