62 lines
2.2 KiB
C++
62 lines
2.2 KiB
C++
#include "../headers/qmodernpushbutton.h"
|
|
|
|
QModernPushButton::QModernPushButton(QString icon, QWidget* parent) : QPushButton(parent)
|
|
{
|
|
this->setStyleSheet(this->generateStyleSheet());
|
|
this->setCheckable(true);
|
|
|
|
this->svgWidget = new QSvgWidget(this);
|
|
this->svgWidget->load(QString(icon));
|
|
|
|
this->overlayNormalWidget = new QSvgWidget(this->svgWidget);
|
|
|
|
this->overlayCheckedWidget = new QSvgWidget(this->svgWidget);
|
|
this->overlayCheckedWidget->load(QString(":svgIcons/overlayChecked.svg"));
|
|
|
|
this->overlayCheckedEffect = new QGraphicsOpacityEffect(this);
|
|
this->overlayCheckedEffect->setOpacity(0);
|
|
this->overlayCheckedWidget->setGraphicsEffect(this->overlayCheckedEffect);
|
|
|
|
connect(this, SIGNAL(toggled(bool)), this, SLOT(handleToggled(bool)));
|
|
}
|
|
|
|
void QModernPushButton::handleToggled(bool checked) {
|
|
int startValue = 1;
|
|
if(checked) {
|
|
startValue = 0;
|
|
}
|
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this->overlayCheckedEffect, "opacity");
|
|
animation->setDuration(400);
|
|
animation->setStartValue(startValue);
|
|
animation->setEndValue(1-startValue);
|
|
animation->setEasingCurve(QEasingCurve(QEasingCurve::InOutQuad));
|
|
animation->start();
|
|
}
|
|
|
|
QString QModernPushButton::generateStyleSheet() {
|
|
return "QPushButton {\n"
|
|
" background-color: transparent;\n"
|
|
"}\n"
|
|
"QPushButton:hover {"
|
|
"background-color: lightblue;"
|
|
"}"
|
|
"QPushButton:active {\n"
|
|
" outline: none;"
|
|
"}";
|
|
}
|
|
|
|
void QModernPushButton::resizeEvent(QResizeEvent *event) {
|
|
this->svgWidget->setGeometry(QRect(0, 0, event->size().width(), event->size().height()));
|
|
|
|
// make border width match button size
|
|
QFile file(":svgIcons/overlayNormal.svg");
|
|
file.open(QFile::ReadOnly);
|
|
QString overlayNormalString = file.readAll();
|
|
overlayNormalString.replace("stroke-width:15px", "stroke-width:" + QString::number(500/event->size().width() * 5) + "px");
|
|
//this->overlayNormalWidget->load(overlayNormalString.toUtf8());
|
|
|
|
this->overlayNormalWidget->setGeometry(QRect(0, 0, event->size().width(), event->size().height()));
|
|
|
|
this->overlayCheckedWidget->setGeometry(QRect(0, 0, event->size().width(), event->size().height()));
|
|
}
|