mirror-chatterino2/widgets/chatwidgetheaderbutton.cpp

107 lines
1.9 KiB
C++
Raw Normal View History

2017-01-18 21:30:23 +01:00
#include "widgets/chatwidgetheaderbutton.h"
2017-01-15 16:38:30 +01:00
#include "colorscheme.h"
#include <QBrush>
#include <QPainter>
2017-01-18 21:30:23 +01:00
namespace chatterino {
namespace widgets {
ChatWidgetHeaderButton::ChatWidgetHeaderButton(int spacing)
2017-01-15 16:38:30 +01:00
: QWidget()
, hbox()
2017-01-18 04:33:30 +01:00
, label()
, mouseOver(false)
, mouseDown(false)
2017-01-15 16:38:30 +01:00
{
setLayout(&hbox);
2017-01-20 06:10:28 +01:00
label.setAlignment(Qt::AlignCenter);
2017-01-15 16:38:30 +01:00
hbox.setMargin(0);
hbox.addSpacing(spacing);
2017-01-18 04:33:30 +01:00
hbox.addWidget(&this->label);
hbox.addSpacing(spacing);
2017-01-15 16:38:30 +01:00
2017-01-20 06:10:28 +01:00
QObject::connect(&this->label, &SignalLabel::mouseUp, this,
2017-01-15 17:12:19 +01:00
&ChatWidgetHeaderButton::labelMouseUp);
2017-01-20 06:10:28 +01:00
QObject::connect(&this->label, &SignalLabel::mouseDown, this,
&ChatWidgetHeaderButton::labelMouseDown);
2017-01-15 16:38:30 +01:00
}
void
ChatWidgetHeaderButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
2017-01-24 20:15:12 +01:00
QBrush brush(ColorScheme::getInstance().IsLightTheme
2017-01-15 16:38:30 +01:00
? QColor(0, 0, 0, 32)
: QColor(255, 255, 255, 32));
2017-01-18 04:33:30 +01:00
if (this->mouseDown) {
2017-01-15 16:38:30 +01:00
painter.fillRect(rect(), brush);
}
2017-01-18 04:33:30 +01:00
if (this->mouseOver) {
2017-01-15 16:38:30 +01:00
painter.fillRect(rect(), brush);
}
}
void
ChatWidgetHeaderButton::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
2017-01-18 04:33:30 +01:00
this->mouseDown = true;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
}
}
void
ChatWidgetHeaderButton::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
2017-01-18 04:33:30 +01:00
this->mouseDown = false;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
emit clicked();
}
}
void
ChatWidgetHeaderButton::enterEvent(QEvent *)
{
2017-01-18 04:33:30 +01:00
this->mouseOver = true;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
}
void
ChatWidgetHeaderButton::leaveEvent(QEvent *)
{
2017-01-18 04:33:30 +01:00
this->mouseOver = false;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
}
void
ChatWidgetHeaderButton::labelMouseUp()
{
2017-01-18 04:33:30 +01:00
this->mouseDown = false;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
emit clicked();
}
void
ChatWidgetHeaderButton::labelMouseDown()
{
2017-01-18 04:33:30 +01:00
this->mouseDown = true;
2017-01-15 16:38:30 +01:00
2017-01-26 07:10:46 +01:00
update();
2017-01-15 16:38:30 +01:00
}
2017-01-18 21:30:23 +01:00
}
}