setting loadHidpiIcon in ToolButton is better than ActionManager. ToolButton has less code and its aim also include loading icon.
51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
|
|
//
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "toolbutton.h"
|
|
|
|
#include "opacityhelper.h"
|
|
|
|
#include <QSvgRenderer>
|
|
#include <QGuiApplication>
|
|
#include <QPainter>
|
|
#include <QGraphicsOpacityEffect>
|
|
#include <QPropertyAnimation>
|
|
|
|
ToolButton::ToolButton(bool hoverColor, QWidget *parent)
|
|
: QPushButton(parent)
|
|
, m_opacityHelper(new OpacityHelper(this))
|
|
{
|
|
setFlat(true);
|
|
QString qss = "QPushButton {"
|
|
"background: transparent;"
|
|
"}";
|
|
if (hoverColor) {
|
|
qss += "QPushButton:hover {"
|
|
"background: red;"
|
|
"}";
|
|
}
|
|
setStyleSheet(qss);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void ToolButton::setOpacity(qreal opacity, bool animated)
|
|
{
|
|
m_opacityHelper->setOpacity(opacity, animated);
|
|
}
|