mirror-chatterino2/src/widgets/chatwidgetheaderbutton.cpp

99 lines
1.9 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "widgets/chatwidgetheaderbutton.hpp"
#include "colorscheme.hpp"
2017-01-15 16:38:30 +01:00
#include <QBrush>
#include <QPainter>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
ChatWidgetHeaderButton::ChatWidgetHeaderButton(int spacing)
2017-01-15 16:38:30 +01:00
: QWidget()
2017-04-12 17:46:44 +02:00
, _hbox()
, _label()
, _mouseOver(false)
, _mouseDown(false)
2017-01-15 16:38:30 +01:00
{
2017-04-12 17:46:44 +02:00
setLayout(&_hbox);
2017-01-15 16:38:30 +01:00
2017-04-12 17:46:44 +02:00
_label.setAlignment(Qt::AlignCenter);
2017-01-20 06:10:28 +01:00
2017-04-12 17:46:44 +02:00
_hbox.setMargin(0);
_hbox.addSpacing(spacing);
_hbox.addWidget(&_label);
_hbox.addSpacing(spacing);
2017-01-15 16:38:30 +01:00
QObject::connect(&_label, &SignalLabel::mouseUp, this, &ChatWidgetHeaderButton::labelMouseUp);
2017-04-12 17:46:44 +02:00
QObject::connect(&_label, &SignalLabel::mouseDown, this,
2017-01-20 06:10:28 +01:00
&ChatWidgetHeaderButton::labelMouseDown);
2017-01-15 16:38:30 +01:00
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::paintEvent(QPaintEvent *)
2017-01-15 16:38:30 +01:00
{
QPainter painter(this);
2017-04-12 17:46:44 +02:00
QBrush brush(ColorScheme::getInstance().IsLightTheme ? QColor(0, 0, 0, 32)
: QColor(255, 255, 255, 32));
2017-01-15 16:38:30 +01:00
2017-04-12 17:46:44 +02:00
if (_mouseDown) {
2017-01-15 16:38:30 +01:00
painter.fillRect(rect(), brush);
}
2017-04-12 17:46:44 +02:00
if (_mouseOver) {
2017-01-15 16:38:30 +01:00
painter.fillRect(rect(), brush);
}
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::mousePressEvent(QMouseEvent *event)
2017-01-15 16:38:30 +01:00
{
if (event->button() == Qt::LeftButton) {
2017-04-12 17:46:44 +02:00
_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-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::mouseReleaseEvent(QMouseEvent *event)
2017-01-15 16:38:30 +01:00
{
if (event->button() == Qt::LeftButton) {
2017-04-12 17:46:44 +02:00
_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();
}
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::enterEvent(QEvent *)
2017-01-15 16:38:30 +01:00
{
2017-04-12 17:46:44 +02:00
_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
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::leaveEvent(QEvent *)
2017-01-15 16:38:30 +01:00
{
2017-04-12 17:46:44 +02:00
_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
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::labelMouseUp()
2017-01-15 16:38:30 +01:00
{
2017-04-12 17:46:44 +02:00
_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();
}
2017-04-12 17:46:44 +02:00
void ChatWidgetHeaderButton::labelMouseDown()
2017-01-15 16:38:30 +01:00
{
2017-04-12 17:46:44 +02:00
_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
}
} // namespace widgets
} // namespace chatterino