KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtquick/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
18#include <QVBoxLayout>
19#include <QHBoxLayout>
20#include <QPainter>
21#include <QAbstractButton>
22#include <QStyle>
23#include <QStyleOptionToolButton>
24
25using namespace KDDockWidgets;
26using namespace KDDockWidgets::Core;
27
28SideBar::SideBar(Core::SideBar *controller, QWidget *parent)
29 : View(controller, Type::SideBar, parent)
30 , Core::SideBarViewInterface(controller)
31{
32}
33
34void SideBar::init()
35{
36 if (m_sideBar->isVertical())
37 m_layout = new QVBoxLayout(this);
38 else
39 m_layout = new QHBoxLayout(this);
40
41 m_layout->setSpacing(1);
42 m_layout->setContentsMargins(0, 0, 0, 0);
43 m_layout->addStretch();
44}
45
46void SideBar::addDockWidget_Impl(Core::DockWidget *dw)
47{
48 auto button = createButton(dw, this);
49 button->setText(dw->title());
50 connect(dw, &Core::DockWidget::titleChanged, button, &SideBarButton::setText);
51 connect(dw, &Core::DockWidget::isOverlayedChanged, button,
52 [button] { button->update(); });
53 connect(dw, &Core::DockWidget::removedFromSideBar, button, &QObject::deleteLater);
55 connect(button, &SideBarButton::clicked, this, [this, dw] { m_sideBar->onButtonClicked(dw); });
56
57 const int count = m_layout->count();
58 m_layout->insertWidget(count - 1, button);
59}
60
61void SideBar::removeDockWidget_Impl(Core::DockWidget *)
62{
63 // Nothing is needed. Button is removed automatically.
64}
65
66bool SideBar::isVertical() const
67{
68 return m_sideBar->isVertical();
69}
70
71SideBarButton *SideBar::createButton(Core::DockWidget *dw,
72 SideBar *parent) const
73{
74 return new SideBarButton(dw, parent);
75}
76
78 : QToolButton(parent)
79 , m_sideBar(parent)
80 , m_dockWidget(dw)
81{
82}
83
85{
86 return m_sideBar->isVertical();
87}
88
90{
91 if (!m_dockWidget) {
92 // Can happen during destruction
93 return;
94 }
95
96 // Draw to an horizontal button, it's easier. Rotate later.
97 QPixmap pixmap((isVertical() ? size().transposed() : size()) * devicePixelRatioF());
99
100 {
101 pixmap.fill(Qt::transparent);
102
104 initStyleOption(&opt);
105 const bool isHovered = opt.state & QStyle::State_MouseOver;
106 // const bool isOverlayed = m_dockWidget->isOverlayed(); // We could style different if it's
107 // open const bool isHoveredOrOverlayed = isHovered || isOverlayed;
108
109 QPainter p(&pixmap);
110
111 const QRect r = isVertical() ? rect().transposed() : rect();
112 const QRect textRect = r.adjusted(3, 0, 5, 0);
113 // p.drawRect(r.adjusted(0, 0, -1, -1));
114 p.setPen(palette().color(QPalette::Text));
116
117 QPen pen(isHovered ? palette().color(QPalette::Highlight)
118 : palette().color(QPalette::Highlight).darker());
119 pen.setWidth(isHovered ? 2 : 1);
120 p.setPen(pen);
121 p.drawLine(3, r.bottom() - 1, r.width() - 3 * 2, r.bottom() - 1);
122 }
123
124 QPainter p(this);
125 if (isVertical()) {
126 pixmap = pixmap.transformed(QTransform().rotate(90));
127 }
128
129 p.drawPixmap(rect(), pixmap);
130}
131
133{
134 const QSize hint = QToolButton::sizeHint();
135 return isVertical() ? (hint.transposed() + QSize(2, 0)) : (hint + QSize(0, 2));
136}
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.
The interface that SideBar views should implement.
bool isVertical() const
returns if this side bar has vertical orientation
SideBar(SideBarLocation, MainWindow *parent=nullptr)
SideBarButton(Core::DockWidget *dw, QtQuick::SideBar *parent)
void paintEvent(QPaintEvent *) override
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 clicked(bool checked)
void setText(const QString &text)
void deleteLater()
void destroyed(QObject *obj)
qreal devicePixelRatioF() const const
void drawLine(const QLineF &line)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void drawText(const QPointF &position, const QString &text)
void setPen(const QColor &color)
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