This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
modern-linbo-gui/sources/qmodernpushbutton.cpp
Dorian Zedler cacd1f5468
- a few new buttons
- started to implement new backend
2020-11-16 18:01:03 +01:00

109 lines
4.2 KiB
C++

/****************************************************************************
** Modern Linbo GUI
** Copyright (C) 2020 Dorian Zedler <dorian@itsblue.de>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as published
** by the Free Software Foundation, either version 3 of the License, 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "../headers/qmodernpushbutton.h"
QModernPushButton::QModernPushButton(QString icon, QWidget* parent) : QAbstractButton(parent)
{
this->svgWidget = new QSvgWidget(this);
this->svgWidget->load(QString(icon));
this->setMouseTracking(true);
this->overlayHoveredWidget = new QSvgWidget(this->svgWidget);
this->overlayHoveredEffect = new QGraphicsOpacityEffect(this);
this->overlayHoveredEffect->setOpacity(0);
this->overlayHoveredWidget->setGraphicsEffect(this->overlayHoveredEffect);
this->overlayPressedWidget = new QSvgWidget(this->svgWidget);
this->overlayPressedWidget->load(QString(":svgIcons/overlayPressed.svg"));
this->overlayPressedEffect = new QGraphicsOpacityEffect(this);
this->overlayPressedEffect->setOpacity(0);
this->overlayPressedWidget->setGraphicsEffect(this->overlayPressedEffect);
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();
}
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->overlayHoveredWidget->load(overlayNormalString.toUtf8());
this->overlayHoveredWidget->setGeometry(QRect(0, 0, event->size().width(), event->size().height()));
this->overlayCheckedWidget->setGeometry(QRect(0, 0, event->size().width(), event->size().height()));
}
void QModernPushButton::paintEvent(QPaintEvent *e) {
Q_UNUSED(e)
}
void QModernPushButton::keyPressEvent(QKeyEvent *e) {
// TODO
return QAbstractButton::keyPressEvent(e);
}
void QModernPushButton::keyReleaseEvent(QKeyEvent *e) {
// TODO
return QAbstractButton::keyReleaseEvent(e);
}
void QModernPushButton::enterEvent(QEvent *e) {
this->overlayHoveredEffect->setOpacity(1);
return QAbstractButton::enterEvent(e);
}
void QModernPushButton::leaveEvent(QEvent *e) {
this->overlayHoveredEffect->setOpacity(0);
return QAbstractButton::leaveEvent(e);
}
void QModernPushButton::mousePressEvent(QMouseEvent *e) {
this->overlayPressedEffect->setOpacity(1);
return QAbstractButton::mousePressEvent(e);
}
void QModernPushButton::mouseReleaseEvent(QMouseEvent *e) {
this->overlayPressedEffect->setOpacity(0);
return QAbstractButton::mouseReleaseEvent(e);
}