File: custombutton.cpp

package info (click to toggle)
lxqt-panel 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,680 kB
  • sloc: cpp: 30,052; xml: 1,152; makefile: 19
file content (188 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2021 LXQt team
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "custombutton.h"

#include <QMouseEvent>
#include <QPainter>
#include <QStylePainter>
#include <QStyleOptionToolButton>
#include <QProxyStyle>

#include <cmath>
#include <algorithm>

class LeftAlignedTextStyle : public QProxyStyle
{
    using QProxyStyle::QProxyStyle;
public:

    virtual void drawItemText(QPainter * painter, const QRect & rect, int flags
            , const QPalette & pal, bool enabled, const QString & text
            , QPalette::ColorRole textRole = QPalette::NoRole) const override;
};

void LeftAlignedTextStyle::drawItemText(QPainter * painter, const QRect & rect, int flags
            , const QPalette & pal, bool enabled, const QString & text
            , QPalette::ColorRole textRole) const
{
    QString txt = text;
    // get the button text because the text that's given to this function may be middle-elided
    if (const QToolButton *tb = dynamic_cast<const QToolButton*>(painter->device()))
        txt = tb->text();
    txt = QFontMetrics(painter->font()).elidedText(txt, Qt::ElideRight, rect.width());
    QProxyStyle::drawItemText(painter, rect, (flags & ~Qt::AlignHCenter) | Qt::AlignLeft, pal, enabled, txt, textRole);
}


CustomButton::CustomButton(ILXQtPanelPlugin *plugin, QWidget* parent):
        QToolButton(parent),
        mPlugin(plugin),
        mPanel(plugin->panel()),
        mMaxWidth(200)

{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setAutoRaise(true);
    setContentsMargins(0, 0, 0, 0);
    setMinimumWidth(1);
    setMinimumHeight(1);
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    setStyle(new LeftAlignedTextStyle());
    updateWidth();

}

CustomButton::~CustomButton() = default;

void CustomButton::wheelEvent(QWheelEvent *event)
{
    QPoint anglePoint = event->angleDelta();
    bool horizontal(std::abs(event->angleDelta().x()) > std::abs(anglePoint.y()));
    int delta = horizontal ? anglePoint.x() : anglePoint.y();
    emit wheelScrolled(delta);
    event->accept();
}

void CustomButton::setMaxWidth(int maxWidth)
{
    mMaxWidth = maxWidth;
    updateWidth();
}

void CustomButton::updateWidth()
{
    int newWidth = std::min(sizeHint().width(), mMaxWidth);
    if (mOrigin == Qt::TopLeftCorner) {
        setFixedWidth(newWidth);

        setMinimumHeight(1);
        setMaximumHeight(QWIDGETSIZE_MAX);
    }
    else {
        setMinimumWidth(1);
        setMaximumWidth(QWIDGETSIZE_MAX);

        setFixedHeight(newWidth);
    }
    update();
}

void CustomButton::setOrigin(Qt::Corner newOrigin)
{
    if (mOrigin != newOrigin) {
        mOrigin = newOrigin;
        updateWidth();
    }
}

void CustomButton::setAutoRotation(bool value)
{
    if (value) {
        switch (mPanel->position())
        {
        case ILXQtPanel::PositionTop:
        case ILXQtPanel::PositionBottom:
            setOrigin(Qt::TopLeftCorner);
            break;

        case ILXQtPanel::PositionLeft:
            setOrigin(Qt::BottomLeftCorner);
            break;

        case ILXQtPanel::PositionRight:
            setOrigin(Qt::TopRightCorner);
            break;
        }
    }
    else
        setOrigin(Qt::TopLeftCorner);


}

void CustomButton::paintEvent(QPaintEvent *event)
{
    if (mOrigin == Qt::TopLeftCorner) {
        QToolButton::paintEvent(event);
        return;
    }

    QSize sz = size();
    bool transpose = false;
    QTransform transform;

    switch (mOrigin)
    {
    case Qt::TopLeftCorner:
        break;

    case Qt::TopRightCorner:
        transform.rotate(90.0);
        transform.translate(0.0, -sz.width());
        transpose = true;
        break;

    case Qt::BottomRightCorner:
        transform.rotate(180.0);
        transform.translate(-sz.width(), -sz.height());
        break;

    case Qt::BottomLeftCorner:
        transform.rotate(270.0);
        transform.translate(-sz.height(), 0.0);
        transpose = true;
        break;
    }

    QStylePainter painter(this);
    painter.setTransform(transform);
    QStyleOptionToolButton opt;
    initStyleOption(&opt);
    if (transpose)
        opt.rect = opt.rect.transposed();

    painter.drawComplexControl(QStyle::CC_ToolButton, opt);
}