File indexing completed on 2024-05-12 16:02:30
0001 /* This file is part of the KDE libraries 0002 SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org> 0003 SPDX-FileCopyrightText: 2012 Jean-Nicolas Artaud <jeannicolasartaud@gmail.com> 0004 SPDX-FileCopyrightText: 2012 Jarosław Staniek <staniek@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-only 0007 */ 0008 #include "KoGroupButton.h" 0009 0010 #include <QAction> 0011 #include <QStyleOptionToolButton> 0012 #include <QStylePainter> 0013 0014 #include <KLocalizedString> 0015 0016 class Q_DECL_HIDDEN KoGroupButton::Private 0017 { 0018 public: 0019 Private(KoGroupButton *qq, const GroupPosition position) : groupPosition(position) 0020 { 0021 // Make the policy closer to QPushButton's default but horizontal shouldn't be Fixed, 0022 // otherwise spacing gets broken 0023 qq->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0024 } 0025 GroupPosition groupPosition; 0026 }; 0027 0028 KoGroupButton::KoGroupButton(GroupPosition position, QWidget* parent) 0029 : KisHighlightedToolButton(parent), d(new Private(this, position)) 0030 { 0031 } 0032 0033 KoGroupButton::KoGroupButton(QWidget* parent) 0034 : KisHighlightedToolButton(parent), d(new Private(this, NoGroup)) 0035 { 0036 } 0037 0038 KoGroupButton::~KoGroupButton() 0039 { 0040 delete d; 0041 } 0042 0043 void KoGroupButton::setGroupPosition(KoGroupButton::GroupPosition groupPosition) 0044 { 0045 d->groupPosition = groupPosition; 0046 } 0047 0048 KoGroupButton::GroupPosition KoGroupButton::groupPosition() const 0049 { 0050 return d->groupPosition; 0051 } 0052 0053 void KoGroupButton::paintEvent(QPaintEvent* event) 0054 { 0055 if (groupPosition() == NoGroup) { 0056 QToolButton::paintEvent(event); 0057 return; 0058 } 0059 QStylePainter painter(this); 0060 QStyleOptionToolButton opt; 0061 initStyleOption(&opt); 0062 QStyleOptionToolButton panelOpt = opt; 0063 0064 // Panel 0065 QRect& panelRect = panelOpt.rect; 0066 switch (groupPosition()) { 0067 case GroupLeft: 0068 panelRect.setWidth(panelRect.width() * 2); 0069 break; 0070 case GroupCenter: 0071 panelRect.setLeft(panelRect.left() - panelRect.width()); 0072 panelRect.setWidth(panelRect.width() * 3); 0073 break; 0074 case GroupRight: 0075 panelRect.setLeft(panelRect.left() - panelRect.width()); 0076 break; 0077 case NoGroup: 0078 Q_ASSERT(0); 0079 } 0080 if (autoRaise()) { 0081 if (!isChecked() && !isDown() && !(panelOpt.state & QStyle::State_MouseOver)) { 0082 // Use 'pushed' appearance for all buttons, but those that are not really pushed 0083 // are drawn with less contrast and are toned down. 0084 panelOpt.state |= (QStyle::State_On | QStyle::State_Sunken); 0085 QPalette panelPal(panelOpt.palette); 0086 QColor c; 0087 c = panelPal.color(QPalette::Button); 0088 c.setAlpha(50); 0089 panelPal.setColor(QPalette::Button, c); 0090 c = panelPal.color(QPalette::Window); 0091 c.setAlpha(50); 0092 panelPal.setColor(QPalette::Window, c); 0093 panelOpt.palette = panelPal; 0094 painter.setOpacity(0.5); 0095 } 0096 } else { 0097 0098 if (!isChecked() && !isDown() && !(panelOpt.state & QStyle::State_MouseOver)) { 0099 0100 } else { 0101 // only highlight the selected item 0102 panelOpt.state |= (QStyle::State_On | QStyle::State_Sunken); 0103 QPalette panelPal(panelOpt.palette); 0104 QColor c; 0105 c = panelPal.color(QPalette::Button); 0106 c.setAlpha(50); 0107 panelPal.setColor(QPalette::Button, c); 0108 c = panelPal.color(QPalette::Window); 0109 c.setAlpha(50); 0110 panelPal.setColor(QPalette::Window, c); 0111 panelOpt.palette = panelPal; 0112 painter.setOpacity(0.5); 0113 } 0114 } 0115 0116 0117 painter.drawPrimitive(QStyle::PE_PanelButtonTool, panelOpt); 0118 painter.setOpacity(1.0); 0119 0120 // Separator 0121 //! @todo make specific fixes for styles such as Cleanlooks if there's practical no alternative 0122 const int y1 = opt.rect.top() + 1; 0123 const int y2 = opt.rect.bottom() - 1; 0124 painter.setOpacity(0.4); 0125 if (d->groupPosition != GroupRight) { 0126 const int x = opt.rect.right(); 0127 painter.setPen(QPen(opt.palette.color(QPalette::Dark), 0)); 0128 painter.drawLine(x, y1, x, y2); 0129 } 0130 painter.setOpacity(1.0); 0131 0132 // Text 0133 painter.drawControl(QStyle::CE_ToolButtonLabel, opt); 0134 0135 // Filtering message on tooltip text for CJK to remove accelerators. 0136 // Quoting ktoolbar.cpp: 0137 // """ 0138 // CJK languages use more verbose accelerator marker: they add a Latin 0139 // letter in parenthesis, and put accelerator on that. Hence, the default 0140 // removal of ampersand only may not be enough there, instead the whole 0141 // parenthesis construct should be removed. Provide these filtering i18n 0142 // messages so that translators can use Transcript for custom removal. 0143 // """ 0144 if (!actions().isEmpty()) { 0145 QAction* action = actions().first(); 0146 setToolTip(i18nc("@info:tooltip of custom triple button", "%1", action->toolTip())); 0147 } 0148 }