KDDockWidgets API Documentation  1.5
SideBarWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2022 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 "SideBarWidget_p.h"
13 #include "DockWidgetBase.h"
14 #include "MainWindowBase.h"
15 
16 #include <QVBoxLayout>
17 #include <QHBoxLayout>
18 #include <QPainter>
19 #include <QAbstractButton>
20 #include <QStyle>
21 #include <QStyleOptionToolButton>
22 
23 using namespace KDDockWidgets;
24 
25 SideBarWidget::SideBarWidget(SideBarLocation location, MainWindowBase *parent)
26  : SideBar(location, parent)
27  , m_layout(isVertical() ? static_cast<QBoxLayout *>(new QVBoxLayout(this))
28  : static_cast<QBoxLayout *>(new QHBoxLayout(this))) // ternary operator requires static_cast
29 {
30  m_layout->setSpacing(1);
31  m_layout->setContentsMargins(0, 0, 0, 0);
32  m_layout->addStretch();
33 }
34 
35 void SideBarWidget::addDockWidget_Impl(DockWidgetBase *dw)
36 {
37  auto button = createButton(dw, this);
38  button->setText(dw->title());
39  connect(dw, &DockWidgetBase::titleChanged, button, &SideBarButton::setText);
40  connect(dw, &DockWidgetBase::isOverlayedChanged, button, [button] {
41  button->update();
42  });
43  connect(dw, &DockWidgetBase::removedFromSideBar, button, &QObject::deleteLater);
44  connect(dw, &QObject::destroyed, button, &QObject::deleteLater);
45  connect(button, &SideBarButton::clicked, this, [this, dw] {
46  onButtonClicked(dw);
47  });
48 
49  const int count = m_layout->count();
50  m_layout->insertWidget(count - 1, button);
51 }
52 
53 void SideBarWidget::removeDockWidget_Impl(DockWidgetBase *)
54 {
55  // Nothing is needed. Button is removed automatically.
56 }
57 
58 SideBarButton *SideBarWidget::createButton(DockWidgetBase *dw, SideBarWidget *parent) const
59 {
60  return new SideBarButton(dw, parent);
61 }
62 
63 SideBarButton::SideBarButton(DockWidgetBase *dw, SideBarWidget *parent)
64  : QToolButton(parent)
65  , m_sideBar(parent)
66  , m_dockWidget(dw)
67 {
68 }
69 
70 bool SideBarButton::isVertical() const
71 {
72  return m_sideBar->isVertical();
73 }
74 
75 void SideBarButton::paintEvent(QPaintEvent *)
76 {
77  if (!m_dockWidget) {
78  // Can happen during destruction
79  return;
80  }
81 
82  // Draw to an horizontal button, it's easier. Rotate later.
83  QPixmap pixmap((isVertical() ? size().transposed() : size()) * devicePixelRatioF());
84  pixmap.setDevicePixelRatio(devicePixelRatioF());
85 
86  {
87  pixmap.fill(Qt::transparent);
88 
90  initStyleOption(&opt);
91  const bool isHovered = opt.state & QStyle::State_MouseOver;
92  //const bool isOverlayed = m_dockWidget->isOverlayed(); // We could style different if it's open
93  //const bool isHoveredOrOverlayed = isHovered || isOverlayed;
94 
95  QPainter p(&pixmap);
96 
97  const QRect r = isVertical() ? rect().transposed() : rect();
98  const QRect textRect = r.adjusted(3, 0, 5, 0);
99  // p.drawRect(r.adjusted(0, 0, -1, -1));
100  p.setPen(palette().color(QPalette::Text));
101  p.drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, text());
102 
103  QPen pen(isHovered ? palette().color(QPalette::Highlight) : palette().color(QPalette::Highlight).darker());
104  pen.setWidth(isHovered ? 2 : 1);
105  p.setPen(pen);
106  p.drawLine(3, r.bottom() - 1, r.width() - 3 * 2, r.bottom() - 1);
107  }
108 
109  QPainter p(this);
110  if (isVertical()) {
111  pixmap = pixmap.transformed(QTransform().rotate(90));
112  }
113 
114  p.drawPixmap(rect(), pixmap);
115 }
116 
117 QSize SideBarButton::sizeHint() const
118 {
119  const QSize hint = QToolButton::sizeHint();
120  return isVertical() ? (hint.transposed() + QSize(2, 0))
121  : (hint + QSize(0, 2));
122 }
DockWidgetBase.h
The DockWidget base-class that's shared between QtWidgets and QtQuick stack.
Qt::AlignVCenter
AlignVCenter
QVBoxLayout
QStyleOptionToolButton
QRect::transposed
QRect transposed() const const
QPalette::Text
Text
QRect
QRect::width
int width() const const
QSize
QPainter
QPen
QStyle::State_MouseOver
State_MouseOver
KDDockWidgets::DockWidgetBase::title
QString title
Definition: DockWidgetBase.h:66
MainWindowBase.h
The MainWindow base-class that's shared between QtWidgets and QtQuick stack.
QObject::destroyed
void destroyed(QObject *obj)
QPixmap
QObject::deleteLater
void deleteLater()
QRect::bottom
int bottom() const const
QSize::transposed
QSize transposed() const const
QToolButton
KDDockWidgets::SideBarLocation
SideBarLocation
Each main window supports 4 sidebars.
Definition: KDDockWidgets.h:211
QTransform
QHBoxLayout
QPaintEvent
KDDockWidgets::DockWidgetBase
The DockWidget base-class. DockWidget and DockWidgetBase are only split in two so we can share some c...
Definition: DockWidgetBase.h:61
QRect::adjusted
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
QBoxLayout
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
QToolButton::sizeHint
virtual QSize sizeHint() const const override
Qt::transparent
transparent

© 2019-2022 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 Mar 7 2022 02:01:21 for KDDockWidgets API Documentation by doxygen 1.8.20