File: closebutton.cpp

package info (click to toggle)
ukui-control-center 3.22.1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,420 kB
  • sloc: cpp: 77,963; xml: 2,408; sh: 30; makefile: 4
file content (282 lines) | stat: -rw-r--r-- 9,227 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
/* CloseButton by David Peng
 * 2020 10.20
 * Version Beta 1.0
 * 介绍:该控件是用于对话框的关闭按钮,只需指定父对象即可可自动回收。
 * 使用方法:
 * 1、new CloseButton(this); //这样会直接调用主题的关闭按钮。
 * 2、new CloseButton(黑色图标路径,this); //这样会用一个黑色的'X'图标SVG,hover时会渲染成白色。
 * 3、new CloseButton(黑色图标路径,白色图标路径,this) //这样会用两张图片来实现图标显示。
*/

#include "closebutton.h"
#include <QApplication>
#include <QDebug>

#define THEME_QT_SCHEMA "org.ukui.style"
#define THEME_GTK_SCHEMA "org.mate.interface"

CloseButton::CloseButton(QWidget *parent, const QString &filePath, const QString &hoverPath) : QLabel(parent)
{

    //Allocation

    if(filePath != "" && filePath != "window-close-symbolic")
        m_icon = new QIcon(filePath);
    else if(filePath == "window-close-symbolic"){
        QIcon icon = QIcon::fromTheme("window-close-symbolic");
        m_icon = new QIcon(icon);
    } else {
        m_icon = nullptr;
    }
    if(hoverPath != "")
        m_hoverIcon = new QIcon(hoverPath);
    else
        m_hoverIcon = nullptr;

    //Properties
    //setProperty("useIconHighlightEffect", true);
    //setProperty("iconHighlightEffectMode", 1);
    setFocusPolicy(Qt::NoFocus);

    //Initial componentss
    m_bIsChecked = false;
    m_bIsPressed = false;
    m_settedBkg = false;
    m_szHoverIn = "white";
    m_szHoverOut = "default";
    m_cSize = 16;
    m_colorBkg = palette().color(QPalette::Base);
    setAlignment(Qt::AlignCenter);

    if(m_icon != nullptr) {
        setPixmap(renderSvg(*m_icon,m_szHoverOut));
    }
    if(QGSettings::isSchemaInstalled(THEME_GTK_SCHEMA) && QGSettings::isSchemaInstalled(THEME_QT_SCHEMA)) {
        QByteArray qtThemeID(THEME_QT_SCHEMA);
        QByteArray gtkThemeID(THEME_GTK_SCHEMA);

        m_gtkThemeSetting = new QGSettings(gtkThemeID,QByteArray(),this);
        m_qtThemeSetting = new QGSettings(qtThemeID,QByteArray(),this);

        QString style = m_qtThemeSetting->get("styleName").toString();
        if(style == "ukui-black"  || style == "ukui-dark") {
            m_szHoverOut = "white";
        } else {
            m_szHoverOut = "default";
        }

        connect(m_qtThemeSetting,&QGSettings::changed, [this] (const QString &key) {
            QString style = m_qtThemeSetting->get("styleName").toString();
            if(key == "styleName") {
                if(style == "ukui-black" || style == "ukui-dark") {
                    m_szHoverOut = "white";
                } else {
                    m_szHoverOut = "default";
                }
            }
        });
    }
}

const QPixmap CloseButton::renderSvg(const QIcon &icon, QString cgColor) {
    int size = m_cSize;
    const auto ratio = qApp->devicePixelRatio();
    if ( 2 == ratio) {
        size = m_cSize * 2;
    } else if (3 == ratio) {
        size = m_cSize * 3;
    }
    QPixmap iconPixmap = icon.pixmap(size,size);
    iconPixmap.setDevicePixelRatio(ratio);
    QImage img = iconPixmap.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if ("white" == cgColor) {
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                } else if ("black" == cgColor) {
                    color.setRed(0);
                    color.setGreen(0);
                    color.setBlue(0);
                    //                    color.setAlpha(0.1);
                    color.setAlphaF(0.12);
                    img.setPixelColor(x, y, color);
                } else if ("gray" == cgColor) {
                    color.setRed(152);
                    color.setGreen(163);
                    color.setBlue(164);
                    img.setPixelColor(x, y, color);
                } else if ("blue" == cgColor){
                    color.setRed(61);
                    color.setGreen(107);
                    color.setBlue(229);
                    img.setPixelColor(x, y, color);
                } else {
                    return iconPixmap;
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

QPixmap CloseButton::drawSymbolicColoredPixmap(const QPixmap &source, QString cgColor)
{
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if ("white" == cgColor) {
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                } else if ("black" == cgColor) {
                    color.setRed(0);
                    color.setGreen(0);
                    color.setBlue(0);
                    color.setAlphaF(0.9);
                    img.setPixelColor(x, y, color);
                } else if ("gray" == cgColor) {
                    color.setRed(152);
                    color.setGreen(163);
                    color.setBlue(164);
                    img.setPixelColor(x, y, color);
                } else if ("blue" == cgColor){
                    color.setRed(61);
                    color.setGreen(107);
                    color.setBlue(229);
                    img.setPixelColor(x, y, color);
                } else {
                    return source;
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

void CloseButton::enterEvent(QEvent *event) {
    Q_UNUSED(event);
    if(m_hoverIcon == nullptr && m_icon != nullptr)
        setPixmap(renderSvg(*m_icon,m_szHoverIn));
    else if(m_hoverIcon != nullptr && m_icon != nullptr)
        setPixmap(m_hoverIcon->pixmap(m_cSize,m_cSize));
    else if(m_customIcon != nullptr)
        setPixmap(renderSvg(*m_customIcon,m_szHoverIn));
    m_colorBkg = QColor("#FA6056");
}

void CloseButton::mousePressEvent(QMouseEvent *event) {
    if(event->button() == Qt::LeftButton)
    {
        m_colorBkg = QColor("#E54A50");
        m_bIsPressed = true;
        update();
    }
}

void CloseButton::mouseReleaseEvent(QMouseEvent *event) {
    Q_UNUSED(event);
    if(m_bIsPressed && this->rect().contains(event->pos()))
    {
        m_bIsChecked = !m_bIsChecked;
        emit clicked(m_bIsChecked);
        m_bIsPressed = false;
    }
}

void CloseButton::leaveEvent(QEvent *event) {
    Q_UNUSED(event);
    m_colorBkg = m_customBkg.isValid() ? m_customBkg : palette().color(QPalette::Base);
    if(m_icon != nullptr)
        setPixmap(renderSvg(*m_icon,m_szHoverOut));
    else if(m_customIcon != nullptr)
        setPixmap(renderSvg(*m_customIcon,m_szHoverOut));

}

void CloseButton::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setPen(Qt::transparent);
    painter.setBrush(QBrush(m_colorBkg));
    painter.drawRoundedRect(rect(), 4, 4);
    painter.end();
    return QLabel::paintEvent(event);
}

void CloseButton::setIconSize(int size) {
    m_cSize = size;
    update();
}

void CloseButton::setIcon(const QIcon &icon) {
    m_customIcon = new QIcon(icon);
    setPixmap(renderSvg(*m_customIcon,m_szHoverOut));
}

void CloseButton::setBkg(const QColor &color) {
    m_settedBkg = true;
    m_customBkg = color;
    m_colorBkg = m_customBkg;
    if(m_icon != nullptr) {
        setPixmap(renderSvg(*m_icon,m_szHoverOut));
    } else if(m_customIcon != nullptr) {
        setPixmap(renderSvg(*m_customIcon,m_szHoverOut));
    }
}

void CloseButton::setHoverIn(const QString &hoverIn) {
    m_szHoverIn = hoverIn;
    update();
}

void CloseButton::setHoverOut(const QString &hoverOut) {
    m_szHoverOut = hoverOut;
    if(m_icon != nullptr) {
        setPixmap(renderSvg(*m_icon,m_szHoverOut));
    } else if(m_customIcon != nullptr) {
        setPixmap(renderSvg(*m_customIcon,m_szHoverOut));
    }
    update();
}

CloseButton::~CloseButton() {
    if(m_icon != nullptr) {
        delete m_icon;
        m_icon = nullptr;
    }
    if(m_hoverIcon != nullptr) {
        delete m_hoverIcon;
        m_hoverIcon = nullptr;
    }
    if(m_customIcon != nullptr) {
        delete m_customIcon;
        m_customIcon = nullptr;
    }
}