Krita Source Code Documentation
Loading...
Searching...
No Matches
KoDockWidgetTitleBar.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 SPDX-FileCopyrightText: 2007 Marijn Kruisselbrink <mkruisselbrink@kde.org>
3 SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
4 SPDX-FileCopyrightText: 2021 Alvin Wong <alvin@alvinhc.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
12
13#include <KoIcon.h>
14#include <kis_icon_utils.h>
15
16#include <WidgetsDebug.h>
17#include <klocalizedstring.h>
18
19#include <QAbstractButton>
20#include <QAction>
21#include <QHBoxLayout>
22#include <QLabel>
23#include <QStyle>
24#include <QStylePainter>
25#include <QStyleOptionFrame>
26
27#include <KSqueezedTextLabel>
28
29static inline bool hasFeature(const QDockWidget *dockwidget, QDockWidget::DockWidgetFeature feature)
30{
31 return (dockwidget->features() & feature) == feature;
32}
33
34constexpr int SPACING = 6;
35
37 : QWidget(dockWidget), d(new Private(this))
38{
39 d->floatIcon = kisIcon("docker_float");
40 d->floatButton = new KoDockWidgetTitleBarButton(this);
41 d->floatButton->setIcon(d->floatIcon);
42 connect(d->floatButton, SIGNAL(clicked()), SLOT(toggleFloating()));
43 d->floatButton->setVisible(true);
44 d->floatButton->setToolTip(i18nc("@info:tooltip", "Float Docker"));
45 d->floatButton->setStyleSheet("border: 0");
46
47 d->removeIcon = kisIcon("docker_close");
48 d->closeButton = new KoDockWidgetTitleBarButton(this);
49 d->closeButton->setIcon(d->removeIcon);
50 connect(d->closeButton, SIGNAL(clicked()), dockWidget, SLOT(close()));
51 d->closeButton->setVisible(true);
52 d->closeButton->setToolTip(i18nc("@info:tooltip", "Close Docker"));
53 d->closeButton->setStyleSheet("border: 0"); // border makes the header busy looking (appears on some OSs)
54
55 d->lockIcon = kisIcon("docker_lock_a");
56 d->lockButton = new KoDockWidgetTitleBarButton(this);
57 d->lockButton->setCheckable(true);
58 d->lockButton->setIcon(d->lockIcon);
59 connect(d->lockButton, SIGNAL(toggled(bool)), SLOT(setLocked(bool)));
60 d->lockButton->setVisible(true);
61 d->lockButton->setToolTip(i18nc("@info:tooltip", "Lock Docker"));
62 d->lockButton->setStyleSheet("border: 0");
63
64 d->updateButtonSizes();
65
66 d->titleLabel = new KSqueezedTextLabel(this);
67 d->titleLabel->setTextElideMode(Qt::ElideRight);
68 d->titleLabel->setText(dockWidget->windowTitle());
69
70 QSizePolicy sizePolicy = d->titleLabel->sizePolicy();
71 sizePolicy.setVerticalPolicy(QSizePolicy::Ignored);
72 d->titleLabel->setSizePolicy(sizePolicy);
73
74 QHBoxLayout *layout = new QHBoxLayout(this);
75 layout->setContentsMargins(2, 0, 2, 0);
76 layout->setSpacing(SPACING);
77 layout->addWidget(d->lockButton);
78 layout->addWidget(d->titleLabel, 1);
79 layout->addWidget(d->floatButton);
80 layout->addWidget(d->closeButton);
81
82 connect(dockWidget, SIGNAL(featuresChanged(QDockWidget::DockWidgetFeatures)), SLOT(featuresChanged(QDockWidget::DockWidgetFeatures)));
83 connect(dockWidget, SIGNAL(topLevelChanged(bool)), SLOT(topLevelChanged(bool)));
84 connect(dockWidget, SIGNAL(windowTitleChanged(const QString &)), SLOT(dockWidgetTitleChanged(const QString &)));
85
86 d->featuresChanged(QDockWidget::NoDockWidgetFeatures);
87}
88
93
95{
96 QStylePainter p(this);
97
98 QDockWidget *q = qobject_cast<QDockWidget*>(parentWidget());
99
100 int fw = q->isFloating() ? q->style()->pixelMetric(QStyle::PM_DockWidgetFrameWidth, 0, q) : 0;
101 int mw = q->style()->pixelMetric(QStyle::PM_DockWidgetTitleMargin, 0, q);
102
103 QStyleOptionDockWidget titleOpt;
104 titleOpt.initFrom(q);
105
106 QSize lockButtonSize(0,0);
107 if (d->lockButton->isVisible()) {
108 lockButtonSize = d->lockButton->size();
109 }
110
111 // To improve the look with Fusion which has weird 13x15 button sizes
112 int fusionTextOffset = 0;
113 QRect styleTestRect = q->style()->subElementRect(QStyle::SE_DockWidgetFloatButton, &titleOpt, q);
114 if (styleTestRect.width() < 16) {
115 fusionTextOffset = d->lockButton->x();
116 }
117
118 titleOpt.rect = QRect(QPoint(fw + mw + lockButtonSize.width() + fusionTextOffset, 0),
119 QSize(geometry().width() - (fw * 2) - mw - lockButtonSize.width(), geometry().height()));
120 // We don't print the title text here. Instead, we have a QLabel.
121 // titleOpt.title = q->windowTitle();
122 // FIXME: Maybe we just shouldn't use a QStylePainter at all?
123 titleOpt.title = QString();
124 titleOpt.closable = hasFeature(q, QDockWidget::DockWidgetClosable);
125 titleOpt.floatable = hasFeature(q, QDockWidget::DockWidgetFloatable);
126 p.drawControl(QStyle::CE_DockWidgetTitle, titleOpt);
127}
128
130{
131 QDockWidget *q = qobject_cast<QDockWidget*>(parentWidget());
132
133 if (q->isFloating() || (width() < (d->closeButton->width() + d->floatButton->width() + d->lockButton->width()) + 32)) {
134 d->lockButton->setVisible(false);
135 } else {
136 d->lockButton->setVisible(true);
137 }
138}
139
141{
142 QDockWidget *q = qobject_cast<QDockWidget*>(parentWidget());
143
144 d->locked = locked;
145 d->lockButton->blockSignals(true);
146 d->lockButton->setChecked(locked);
147 d->lockButton->blockSignals(false);
148
149 if (locked) {
150 d->features = q->features();
151 q->setFeatures(QDockWidget::NoDockWidgetFeatures);
152 }
153 else {
154 q->setFeatures(d->features);
155 }
156
157 q->toggleViewAction()->setEnabled(!locked);
158 d->closeButton->setEnabled(!locked);
159 d->floatButton->setEnabled(!locked);
160 d->floatButton->setVisible(!locked);
161
162 d->updateIcons();
163 q->setProperty("Locked", locked);
164}
165
167{
168 d->updateIcons();
169}
170
171void KoDockWidgetTitleBar::Private::toggleFloating()
172{
173 QDockWidget *q = qobject_cast<QDockWidget*>(thePublic->parentWidget());
174
175 q->setFloating(!q->isFloating());
176 updateIcons();
177}
178
179void KoDockWidgetTitleBar::Private::topLevelChanged(bool topLevel)
180{
181 lockButton->setEnabled(!topLevel);
182 updateIcons();
183}
184
185void KoDockWidgetTitleBar::Private::featuresChanged(QDockWidget::DockWidgetFeatures)
186{
187 QDockWidget *q = qobject_cast<QDockWidget*>(thePublic->parentWidget());
188
189 closeButton->setVisible(hasFeature(q, QDockWidget::DockWidgetClosable));
190 floatButton->setVisible(hasFeature(q, QDockWidget::DockWidgetFloatable));
191
192 updateButtonSizes();
193 thePublic->resizeEvent(0);
194}
195
196void KoDockWidgetTitleBar::Private::dockWidgetTitleChanged(const QString &title)
197{
198 titleLabel->setText(title);
199}
200
201
202void KoDockWidgetTitleBar::Private::updateIcons()
203{
204 lockIcon = (!locked) ? kisIcon("docker_lock_a") : kisIcon("docker_lock_b");
205 lockButton->setIcon(lockIcon);
206
207 // this method gets called when switching themes, so update all of the themed icons now
208 floatButton->setIcon(kisIcon("docker_float"));
209 closeButton->setIcon(kisIcon("docker_close"));
210
211 thePublic->resizeEvent(0);
212}
213
214void KoDockWidgetTitleBar::Private::updateButtonSizes()
215{
216 const QDockWidget *q = qobject_cast<QDockWidget*>(thePublic->parentWidget());
217
218 const int fw = q->isFloating() ? q->style()->pixelMetric(QStyle::PM_DockWidgetFrameWidth, 0, q) : 0;
219
220 QStyleOptionDockWidget opt;
221 opt.initFrom(q);
222 opt.rect = QRect(QPoint(fw, fw), QSize(thePublic->geometry().width() - (fw * 2), thePublic->geometry().height() - (fw * 2)));
223 opt.title = q->windowTitle();
224 // Originally it was:
225 // opt.closable = hasFeature(q, QDockWidget::DockWidgetClosable);
226 // but I think we better just always pretend the close button is visible to
227 // get the button size.
228 opt.closable = true;
229 opt.floatable = hasFeature(q, QDockWidget::DockWidgetFloatable);
230
231 // Again, we just always use the size of the close button, so we don't
232 // need to get the size of the float button...
233 // QRect floatRect = q->style()->subElementRect(QStyle::SE_DockWidgetFloatButton, &opt, q);
234 const QRect closeRect = q->style()->subElementRect(QStyle::SE_DockWidgetCloseButton, &opt, q);
235 QSize buttonSize = closeRect.size();
236 if (buttonSize.width() < 16) {
237 // To improve the look with Fusion which has weird 13x15 button sizes
238 buttonSize = QSize(16, 16 * int(thePublic->isVisible()));
239 } else if (buttonSize.width() != buttonSize.height()) {
240 // Just make sure the button is square...
241 buttonSize.setHeight(buttonSize.width()* int(thePublic->isVisible()));
242 }
243
244 floatButton->setFixedSize(buttonSize);
245 closeButton->setFixedSize(buttonSize);
246 lockButton->setFixedSize(buttonSize);
247}
248
249void KoDockWidgetTitleBar::hideEvent(QHideEvent *event){
250 Q_UNUSED(event);
251 d->updateButtonSizes();
252
259 if (parentWidget() && parentWidget()->layout()) {
260 parentWidget()->layout()->update();
261 }
262}
263
264void KoDockWidgetTitleBar::showEvent(QShowEvent *event){
265 Q_UNUSED(event);
266 d->updateButtonSizes();
267 d->closeButton->setVisible(true);
268
275 if (parentWidget() && parentWidget()->layout()) {
276 parentWidget()->layout()->update();
277 }
278}
279
281{
287 return isVisible() ? QWidget::sizeHint() : QSize();
288}
289
290//have to include this because of Q_PRIVATE_SLOT
291#include "moc_KoDockWidgetTitleBar.cpp"
const Params2D p
static bool hasFeature(const QDockWidget *dockwidget, QDockWidget::DockWidgetFeature feature)
constexpr int SPACING
static int buttonSize(int screen)
Definition KoToolBox.cpp:41
A custom title bar button for dock widgets.
void paintEvent(QPaintEvent *event) override
reimplemented from QWidget
void featuresChanged(QDockWidget::DockWidgetFeatures features)
QSize sizeHint() const override
void hideEvent(QHideEvent *event) override
void resizeEvent(QResizeEvent *event) override
reimplemented from QWidget
void showEvent(QShowEvent *event) override
void dockWidgetTitleChanged(const QString &title)
void topLevelChanged(bool topLevel)
KoDockWidgetTitleBar(QDockWidget *dockWidget)
#define kisIcon(name)
Definition kis_icon.h:26