1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
3 Widget taken from Gwenview
4 SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org>
5 
6 SPDX-License-Identifier: GPL-2.0-or-later
7 
8 */
9 // Self
10 #include "statusbartoolbutton.h"
11 
12 // Qt
13 #include <QAction>
14 #include <QStyleOptionToolButton>
15 #include <QStylePainter>
16 #include <QToolButton>
17 
18 // KDE
19 #include <KLocalizedString>
20 
StatusBarToolButton(QWidget * parent)21 StatusBarToolButton::StatusBarToolButton(QWidget* parent)
22   : QToolButton(parent),
23     mGroupPosition(NotGrouped)
24 {
25     setToolButtonStyle(Qt::ToolButtonTextOnly);
26     setFocusPolicy(Qt::NoFocus);
27     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
28 }
29 
30 
minimumSizeHint() const31 QSize StatusBarToolButton::minimumSizeHint() const
32 {
33     return sizeHint();
34 }
35 
36 
sizeHint() const37 QSize StatusBarToolButton::sizeHint() const
38 {
39     QSize sh = QToolButton::sizeHint();
40     sh.setHeight(fontMetrics().height());
41     return sh;
42 }
43 
44 
setGroupPosition(StatusBarToolButton::GroupPosition groupPosition)45 void StatusBarToolButton::setGroupPosition(StatusBarToolButton::GroupPosition groupPosition)
46 {
47     mGroupPosition = groupPosition;
48 }
49 
50 
paintEvent(QPaintEvent * event)51 void StatusBarToolButton::paintEvent(QPaintEvent* event)
52 {
53     if (mGroupPosition == NotGrouped) {
54         QToolButton::paintEvent(event);
55         return;
56     }
57     QStylePainter painter(this);
58     QStyleOptionToolButton opt;
59     initStyleOption(&opt);
60     QStyleOptionToolButton panelOpt = opt;
61 
62     // Panel
63     QRect& panelRect = panelOpt.rect;
64     switch (mGroupPosition) {
65     case GroupLeft:
66         panelRect.setWidth(panelRect.width() * 2);
67         break;
68     case GroupCenter:
69         panelRect.setLeft(panelRect.left() - panelRect.width());
70         panelRect.setWidth(panelRect.width() * 3);
71         break;
72     case GroupRight:
73         panelRect.setLeft(panelRect.left() - panelRect.width());
74         break;
75     case NotGrouped:
76         Q_ASSERT(0);
77     }
78     painter.drawPrimitive(QStyle::PE_PanelButtonTool, panelOpt);
79 
80     // Separator
81     const int y1 = opt.rect.top() + 6;
82     const int y2 = opt.rect.bottom() - 6;
83     if (mGroupPosition & GroupRight) {
84         const int x = opt.rect.left();
85         painter.setPen(opt.palette.color(QPalette::Light));
86         painter.drawLine(x, y1, x, y2);
87     }
88     if (mGroupPosition & GroupLeft) {
89         const int x = opt.rect.right();
90         painter.setPen(opt.palette.color(QPalette::Mid));
91         painter.drawLine(x, y1, x, y2);
92     }
93 
94     // Text
95     painter.drawControl(QStyle::CE_ToolButtonLabel, opt);
96 
97     // Filtering message on tooltip text for CJK to remove accelerators.
98     // Quoting ktoolbar.cpp:
99     // """
100     // CJK languages use more verbose accelerator marker: they add a Latin
101     // letter in parenthesis, and put accelerator on that. Hence, the default
102     // removal of ampersand only may not be enough there, instead the whole
103     // parenthesis construct should be removed. Provide these filtering i18n
104     // messages so that translators can use Transcript for custom removal.
105     // """
106     if (!actions().isEmpty()) {
107         QAction* action = actions().first();
108         setToolTip(i18nc("@info:tooltip of custom toolbar button", "%1", action->toolTip()));
109     }
110 }
111