KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/SideBar.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
12#include "SideBar.h"
13
14#include "kddockwidgets/core/DockWidget.h"
15#include "kddockwidgets/core/SideBar.h"
16#include "kddockwidgets/core/MainWindow.h"
17#include "core/DockWidget_p.h"
18
19#include <QVBoxLayout>
20#include <QHBoxLayout>
21#include <QPainter>
22#include <QAbstractButton>
23#include <QStyle>
24#include <QStyleOptionToolButton>
25
26using namespace KDDockWidgets;
27using namespace KDDockWidgets::QtWidgets;
28
29namespace KDDockWidgets {
30class SideBarButton::Private
31{
32public:
33 Private(Core::DockWidget *dw, Core::SideBar *sideBar)
34 : m_sideBar(sideBar)
35 , m_dockWidget(dw)
36 {
37 }
38
39 Core::SideBar *const m_sideBar;
40 const QPointer<Core::DockWidget> m_dockWidget;
41
42 // Connections to be disconnected when button is destroyed
43 std::vector<KDBindings::ScopedConnection> m_connections;
44};
45}
46
47SideBar::SideBar(Core::SideBar *controller, QWidget *parent)
48 : View(controller, Core::ViewType::SideBar, parent)
49 , SideBarViewInterface(controller)
50{
51}
52
53void SideBar::init()
54{
55 if (m_sideBar->isVertical())
56 m_layout = new QVBoxLayout(this);
57 else
58 m_layout = new QHBoxLayout(this);
59
60 m_layout->setSpacing(1);
61 m_layout->setContentsMargins(0, 0, 0, 0);
62 m_layout->addStretch();
63}
64
65void SideBar::addDockWidget_Impl(Core::DockWidget *dw)
66{
67 auto button = createButton(dw, this);
68 button->setText(dw->title());
69
70 button->d->m_connections.push_back(dw->d->titleChanged.connect(&SideBarButton::setText, button));
71 button->d->m_connections.push_back(dw->d->isOverlayedChanged.connect([button] { button->update(); }));
72 button->d->m_connections.push_back(dw->d->removedFromSideBar.connect(&QObject::deleteLater, button));
73
75 connect(button, &SideBarButton::clicked, this, [this, dw] { m_sideBar->onButtonClicked(dw); });
76
77 const int count = m_layout->count();
78 m_layout->insertWidget(count - 1, button);
79}
80
81void SideBar::removeDockWidget_Impl(Core::DockWidget *)
82{
83 // Nothing is needed. Button is removed automatically.
84}
85
86SideBarButton *SideBar::createButton(Core::DockWidget *dw,
87 SideBar *parent) const
88{
89 return new SideBarButton(dw, parent);
90}
91
92SideBarButton::SideBarButton(Core::DockWidget *dw, QtWidgets::SideBar *parent)
93 : QToolButton(parent)
94 , d(new Private(dw, parent->sideBar()))
95{
96}
97
99{
100 delete d;
101}
102
103bool SideBarButton::isVertical() const
104{
105 return d->m_sideBar->isVertical();
106}
107
109{
110 if (!d->m_dockWidget) {
111 // Can happen during destruction
112 return;
113 }
114
115 // Draw to an horizontal button, it's easier. Rotate later.
116 QPixmap pixmap((isVertical() ? size().transposed() : size()) * devicePixelRatioF());
118
119 {
120 pixmap.fill(Qt::transparent);
121
123 initStyleOption(&opt);
124 const bool isHovered = opt.state & QStyle::State_MouseOver;
125 // const bool isOverlayed = m_dockWidget->isOverlayed(); // We could style different if it's
126 // open const bool isHoveredOrOverlayed = isHovered || isOverlayed;
127
128 QPainter p(&pixmap);
129
130 const QRect r = isVertical() ? rect().transposed() : rect();
131 const QRect textRect = r.adjusted(3, 0, 5, 0);
132 // p.drawRect(r.adjusted(0, 0, -1, -1));
133 p.setPen(palette().color(QPalette::Text));
134 p.drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text());
135
136 QPen pen(isHovered ? palette().color(QPalette::Highlight)
137 : palette().color(QPalette::Highlight).darker());
138 pen.setWidth(isHovered ? 2 : 1);
139 p.setPen(pen);
140 p.drawLine(3, r.bottom() - 1, r.width() - 3 * 2, r.bottom() - 1);
141 }
142
143 QPainter p(this);
144 if (isVertical()) {
145 pixmap = pixmap.transformed(QTransform().rotate(90));
146 }
147
148 p.drawPixmap(rect(), pixmap);
149}
150
152{
153 const QSize hint = QToolButton::sizeHint();
154 return isVertical() ? (hint.transposed() + QSize(2, 0)) : (hint + QSize(0, 2));
155}
The DockWidget base-class. DockWidget and Core::DockWidget are only split in two so we can share some...
QString title() const
Returns the dock widget's title. This title is visible in title bars and tab bars.
SideBarButton(Core::DockWidget *dw, QtQuick::SideBar *parent)
void paintEvent(QPaintEvent *) override
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
Class to abstract QAction, so code still works with QtQuick and Flutter.
void connect(T &&future, QObjectSubclass *context, Callback func)
Definition qcorotask.h:721
void deleteLater()
void destroyed(QObject *obj)
qreal devicePixelRatioF() const const
void setWidth(int width)
QPixmap transformed(const QMatrix &matrix, Qt::TransformationMode mode) const const
void fill(const QColor &color)
void setDevicePixelRatio(qreal scaleFactor)
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
int bottom() const const
QRect transposed() const const
int width() const const
QSize transposed() const const
AlignVCenter
transparent
void initStyleOption(QStyleOptionToolButton *option) const const
virtual QSize sizeHint() const const override

© 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