qt-material-widgets/components/flatbutton.cpp

126 lines
2.5 KiB
C++
Raw Normal View History

2016-03-18 14:07:20 +00:00
#include <QDebug>
#include <QStylePainter>
2016-03-18 14:36:02 +00:00
#include <QMouseEvent>
#include <QApplication>
2016-03-18 11:14:06 +00:00
#include "flatbutton.h"
2016-03-20 16:43:05 +00:00
#include "style.h"
2016-03-18 11:14:06 +00:00
FlatButton::FlatButton(QWidget *parent)
2016-03-18 14:36:02 +00:00
: QAbstractButton(parent),
_overlay(new RippleOverlay(this))
2016-03-18 11:14:06 +00:00
{
2016-03-20 16:43:05 +00:00
setStyle(&Style::instance());
2016-03-18 11:14:06 +00:00
}
FlatButton::FlatButton(const QString &text, QWidget *parent)
2016-03-18 14:36:02 +00:00
: QAbstractButton(parent),
_overlay(new RippleOverlay(this))
2016-03-18 11:14:06 +00:00
{
setText(text);
2016-03-20 16:43:05 +00:00
setStyle(&Style::instance());
2016-03-18 11:14:06 +00:00
}
FlatButton::~FlatButton()
{
}
2016-03-18 14:07:20 +00:00
QSize FlatButton::sizeHint() const
{
// Mostly lifted from QPushButton
ensurePolished();
int w = 0,
h = 0;
2016-03-19 07:50:48 +00:00
QStyleOptionButton option(getStyleOption());
#ifndef QT_NO_ICON
if (!icon().isNull()) {
2016-03-19 07:50:48 +00:00
int ih = option.iconSize.height();
int iw = option.iconSize.width() + 4;
w += iw;
h = qMax(h, ih);
}
#endif
QString s(text());
bool empty = s.isEmpty();
if (empty)
s = QString::fromLatin1("XXXX");
QFontMetrics fm = fontMetrics();
QSize sz = fm.size(Qt::TextShowMnemonic, s);
if (!empty || !w)
w += sz.width();
if (!empty || !h)
h = qMax(h, sz.height());
2016-03-20 16:43:05 +00:00
return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this)
.expandedTo(QApplication::globalStrut()));
}
2016-03-18 14:36:02 +00:00
void FlatButton::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
updateOverlayGeometry();
}
2016-03-18 14:07:20 +00:00
void FlatButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QStylePainter painter(this);
2016-03-20 16:43:05 +00:00
painter.drawControl(QStyle::CE_PushButton, getStyleOption());
2016-03-18 14:07:20 +00:00
2016-03-20 16:43:05 +00:00
if (testAttribute(Qt::WA_Hover) && underMouse()) {
2016-03-19 07:50:48 +00:00
QRect r(rect());
2016-03-18 14:07:20 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
painter.setOpacity(0.1);
painter.fillRect(r, brush);
}
}
2016-03-18 14:36:02 +00:00
void FlatButton::mousePressEvent(QMouseEvent *event)
{
2016-03-19 07:50:48 +00:00
if (!_overlay)
return;
2016-03-18 14:36:02 +00:00
_overlay->addRipple(event->pos());
}
void FlatButton::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event)
emit clicked();
}
2016-03-18 14:07:20 +00:00
void FlatButton::enterEvent(QEvent *event)
{
Q_UNUSED(event)
update();
}
void FlatButton::leaveEvent(QEvent *event)
{
Q_UNUSED(event)
update();
}
QStyleOptionButton FlatButton::getStyleOption() const
{
QStyleOptionButton option;
option.initFrom(this);
2016-03-19 07:50:48 +00:00
option.features = QStyleOptionButton::Flat;
if (isChecked())
option.state |= QStyle::State_On;
option.text = text();
option.icon = icon();
option.iconSize = iconSize();
return option;
}