Files
sarasacw-picture/app/toolbutton.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.2 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
//
// SPDX-License-Identifier: MIT
2025-07-23 21:20:34 +08:00
2019-10-06 14:12:52 +08:00
#include "toolbutton.h"
2025-07-23 21:20:34 +08:00
2019-10-06 14:58:01 +08:00
#include "opacityhelper.h"
2025-07-23 21:20:34 +08:00
#include <QSvgRenderer>
#include <QGuiApplication>
2019-10-06 14:12:52 +08:00
#include <QPainter>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
2025-07-23 21:20:34 +08:00
2020-07-04 13:49:12 +08:00
ToolButton::ToolButton(bool hoverColor, QWidget *parent)
2019-10-06 14:12:52 +08:00
: QPushButton(parent)
2019-10-06 14:58:01 +08:00
, m_opacityHelper(new OpacityHelper(this))
2019-10-06 14:12:52 +08:00
{
setFlat(true);
2020-07-04 13:49:12 +08:00
QString qss = "QPushButton {"
2019-10-06 14:12:52 +08:00
"background: transparent;"
2020-07-04 13:49:12 +08:00
"}";
if (hoverColor) {
qss += "QPushButton:hover {"
"background: red;"
"}";
}
setStyleSheet(qss);
2019-10-06 14:12:52 +08:00
}
2025-07-23 21:20:34 +08:00
void ToolButton::setIconResourcePath(const QString &iconp)
{
this->setIcon(ToolButton::loadHidpiIcon(iconp, this->iconSize()));
}
QIcon ToolButton::loadHidpiIcon(const QString &resp, QSize sz)
{
QSvgRenderer r(resp);
QPixmap pm = QPixmap(sz * qApp->devicePixelRatio());
pm.fill(Qt::transparent);
QPainter p(&pm);
r.render(&p);
pm.setDevicePixelRatio(qApp->devicePixelRatio());
return QIcon(pm);
}
2025-07-23 21:20:34 +08:00
2019-10-06 14:58:01 +08:00
void ToolButton::setOpacity(qreal opacity, bool animated)
2019-10-06 14:12:52 +08:00
{
2019-10-06 14:58:01 +08:00
m_opacityHelper->setOpacity(opacity, animated);
2019-10-06 14:12:52 +08:00
}