Krita Source Code Documentation
Loading...
Searching...
No Matches
KoGroupButton.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org>
3 SPDX-FileCopyrightText: 2012 Jean-Nicolas Artaud <jeannicolasartaud@gmail.com>
4 SPDX-FileCopyrightText: 2012 Jarosław Staniek <staniek@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8#include "KoGroupButton.h"
9
10#include <QAction>
11#include <QStyleOptionToolButton>
12#include <QStylePainter>
13
14#include <KLocalizedString>
15
16class Q_DECL_HIDDEN KoGroupButton::Private
17{
18public:
19 Private(KoGroupButton *qq, const GroupPosition position) : groupPosition(position)
20 {
21 // Make the policy closer to QPushButton's default but horizontal shouldn't be Fixed,
22 // otherwise spacing gets broken
23 qq->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
24 }
26};
27
29 : KisHighlightedToolButton(parent), d(new Private(this, position))
30{
31}
32
34 : KisHighlightedToolButton(parent), d(new Private(this, NoGroup))
35{
36}
37
39{
40 delete d;
41}
42
44{
45 d->groupPosition = groupPosition;
46}
47
49{
50 return d->groupPosition;
51}
52
53void KoGroupButton::paintEvent(QPaintEvent* event)
54{
55 if (groupPosition() == NoGroup) {
56 QToolButton::paintEvent(event);
57 return;
58 }
59 QStylePainter painter(this);
60 QStyleOptionToolButton opt;
61 initStyleOption(&opt);
62 QStyleOptionToolButton panelOpt = opt;
63
64 // Panel
65 QRect& panelRect = panelOpt.rect;
66 switch (groupPosition()) {
67 case GroupLeft:
68 panelRect.setWidth(panelRect.width() * 2);
69 break;
70 case GroupCenter:
71 panelRect.setLeft(panelRect.left() - panelRect.width());
72 panelRect.setWidth(panelRect.width() * 3);
73 break;
74 case GroupRight:
75 panelRect.setLeft(panelRect.left() - panelRect.width());
76 break;
77 case NoGroup:
78 Q_ASSERT(0);
79 }
80 if (autoRaise()) {
81 if (!isChecked() && !isDown() && !(panelOpt.state & QStyle::State_MouseOver)) {
82 // Use 'pushed' appearance for all buttons, but those that are not really pushed
83 // are drawn with less contrast and are toned down.
84 panelOpt.state |= (QStyle::State_On | QStyle::State_Sunken);
85 QPalette panelPal(panelOpt.palette);
86 QColor c;
87 c = panelPal.color(QPalette::Button);
88 c.setAlpha(50);
89 panelPal.setColor(QPalette::Button, c);
90 c = panelPal.color(QPalette::Window);
91 c.setAlpha(50);
92 panelPal.setColor(QPalette::Window, c);
93 panelOpt.palette = panelPal;
94 painter.setOpacity(0.5);
95 }
96 } else {
97
98 if (!isChecked() && !isDown() && !(panelOpt.state & QStyle::State_MouseOver)) {
99
100 } else {
101 // only highlight the selected item
102 panelOpt.state |= (QStyle::State_On | QStyle::State_Sunken);
103 QPalette panelPal(panelOpt.palette);
104 QColor c;
105 c = panelPal.color(QPalette::Button);
106 c.setAlpha(50);
107 panelPal.setColor(QPalette::Button, c);
108 c = panelPal.color(QPalette::Window);
109 c.setAlpha(50);
110 panelPal.setColor(QPalette::Window, c);
111 panelOpt.palette = panelPal;
112 painter.setOpacity(0.5);
113 }
114 }
115
116
117 painter.drawPrimitive(QStyle::PE_PanelButtonTool, panelOpt);
118 painter.setOpacity(1.0);
119
120 // Separator
122 const int y1 = opt.rect.top() + 1;
123 const int y2 = opt.rect.bottom() - 1;
124 painter.setOpacity(0.4);
125 if (d->groupPosition != GroupRight) {
126 const int x = opt.rect.right();
127 painter.setPen(QPen(opt.palette.color(QPalette::Dark), 0));
128 painter.drawLine(x, y1, x, y2);
129 }
130 painter.setOpacity(1.0);
131
132 // Text
133 painter.drawControl(QStyle::CE_ToolButtonLabel, opt);
134
135 // Filtering message on tooltip text for CJK to remove accelerators.
136 // Quoting ktoolbar.cpp:
137 // """
138 // CJK languages use more verbose accelerator marker: they add a Latin
139 // letter in parenthesis, and put accelerator on that. Hence, the default
140 // removal of ampersand only may not be enough there, instead the whole
141 // parenthesis construct should be removed. Provide these filtering i18n
142 // messages so that translators can use Transcript for custom removal.
143 // """
144 if (!actions().isEmpty()) {
145 QAction* action = actions().first();
146 setToolTip(i18nc("@info:tooltip of custom triple button", "%1", action->toolTip()));
147 }
148}
Private *const d
Private(KoGroupButton *qq, const GroupPosition position)
@ GroupLeft
The button is at the left of the group, so it would have rounded the left part.
@ GroupRight
The button is at the right of the group, so it would have rounded the right part.
@ GroupCenter
The button is on the center of the group, so it would have separators on both sides.
@ NoGroup
No particular position, gives the button unchanged appearance.
GroupPosition groupPosition
KoGroupButton(GroupPosition position, QWidget *parent=0)
void paintEvent(QPaintEvent *event) override
void setGroupPosition(KoGroupButton::GroupPosition groupPosition)
~KoGroupButton() override