refactor flat button code
This commit is contained in:
parent
b0d2a56905
commit
d39db592a8
|
@ -1,139 +1,157 @@
|
|||
#include <QDebug>
|
||||
#include <QStylePainter>
|
||||
#include <QMouseEvent>
|
||||
#include <QApplication>
|
||||
#include "flatbutton.h"
|
||||
#include "lib/style.h"
|
||||
|
||||
FlatButton::FlatButton(QWidget *parent)
|
||||
: QAbstractButton(parent),
|
||||
_overlay(new RippleOverlay(this))
|
||||
: QPushButton(parent)
|
||||
{
|
||||
setStyle(&Style::instance());
|
||||
setAttribute(Qt::WA_Hover);
|
||||
|
||||
QFont font(this->font());
|
||||
font.setCapitalization(QFont::AllUppercase);
|
||||
font.setPointSizeF(10.5);
|
||||
font.setStyleName("Medium");
|
||||
setFont(font);
|
||||
}
|
||||
|
||||
FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||
: QAbstractButton(parent),
|
||||
_overlay(new RippleOverlay(this))
|
||||
: QPushButton(parent)
|
||||
{
|
||||
setText(text);
|
||||
|
||||
setStyle(&Style::instance());
|
||||
setAttribute(Qt::WA_Hover);
|
||||
|
||||
QFont font(this->font());
|
||||
font.setCapitalization(QFont::AllUppercase);
|
||||
font.setPointSizeF(10.5);
|
||||
font.setStyleName("Medium");
|
||||
setFont(font);
|
||||
}
|
||||
|
||||
FlatButton::~FlatButton()
|
||||
{
|
||||
}
|
||||
|
||||
QSize FlatButton::sizeHint() const
|
||||
{
|
||||
// Mostly lifted from QPushButton
|
||||
|
||||
ensurePolished();
|
||||
|
||||
int w = 0,
|
||||
h = 0;
|
||||
|
||||
QStyleOptionButton option(getStyleOption());
|
||||
|
||||
#ifndef QT_NO_ICON
|
||||
if (!icon().isNull()) {
|
||||
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());
|
||||
return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this)
|
||||
.expandedTo(QApplication::globalStrut()));
|
||||
}
|
||||
|
||||
void FlatButton::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
updateOverlayGeometry();
|
||||
}
|
||||
|
||||
void FlatButton::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
QStylePainter painter(this);
|
||||
|
||||
painter.drawControl(QStyle::CE_PushButton, getStyleOption());
|
||||
|
||||
if (testAttribute(Qt::WA_Hover) && underMouse()) {
|
||||
QRect r(rect());
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
painter.setOpacity(0.1);
|
||||
painter.fillRect(r, brush);
|
||||
}
|
||||
}
|
||||
|
||||
void FlatButton::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (!_overlay)
|
||||
return;
|
||||
|
||||
_overlay->addRipple(event->pos());
|
||||
}
|
||||
|
||||
void FlatButton::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
emit clicked();
|
||||
}
|
||||
|
||||
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);
|
||||
option.features = QStyleOptionButton::Flat;
|
||||
if (isChecked())
|
||||
option.state |= QStyle::State_On;
|
||||
option.text = text();
|
||||
option.icon = icon();
|
||||
option.iconSize = iconSize();
|
||||
return option;
|
||||
}
|
||||
//#include <QDebug>
|
||||
//#include <QStylePainter>
|
||||
//#include <QMouseEvent>
|
||||
//#include <QApplication>
|
||||
//#include "flatbutton.h"
|
||||
//#include "lib/style.h"
|
||||
//
|
||||
//FlatButton::FlatButton(QWidget *parent)
|
||||
// : QAbstractButton(parent),
|
||||
// _overlay(new RippleOverlay(this))
|
||||
//{
|
||||
// setStyle(&Style::instance());
|
||||
// setAttribute(Qt::WA_Hover);
|
||||
//
|
||||
// QFont font(this->font());
|
||||
// font.setCapitalization(QFont::AllUppercase);
|
||||
// font.setPointSizeF(10.5);
|
||||
// font.setStyleName("Medium");
|
||||
// setFont(font);
|
||||
//}
|
||||
//
|
||||
//FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||
// : QAbstractButton(parent),
|
||||
// _overlay(new RippleOverlay(this))
|
||||
//{
|
||||
// setText(text);
|
||||
// setStyle(&Style::instance());
|
||||
// setAttribute(Qt::WA_Hover);
|
||||
//
|
||||
// QFont font(this->font());
|
||||
// font.setCapitalization(QFont::AllUppercase);
|
||||
// font.setPointSizeF(10.5);
|
||||
// font.setStyleName("Medium");
|
||||
// setFont(font);
|
||||
//}
|
||||
//
|
||||
//FlatButton::~FlatButton()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//QSize FlatButton::sizeHint() const
|
||||
//{
|
||||
// // Mostly lifted from QPushButton
|
||||
//
|
||||
// ensurePolished();
|
||||
//
|
||||
// int w = 0,
|
||||
// h = 0;
|
||||
//
|
||||
// QStyleOptionButton option(getStyleOption());
|
||||
//
|
||||
//#ifndef QT_NO_ICON
|
||||
// if (!icon().isNull()) {
|
||||
// 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());
|
||||
// return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this)
|
||||
// .expandedTo(QApplication::globalStrut()));
|
||||
//}
|
||||
//
|
||||
//void FlatButton::resizeEvent(QResizeEvent *event)
|
||||
//{
|
||||
// Q_UNUSED(event)
|
||||
//
|
||||
// updateOverlayGeometry();
|
||||
//}
|
||||
//
|
||||
//void FlatButton::paintEvent(QPaintEvent *event)
|
||||
//{
|
||||
// Q_UNUSED(event)
|
||||
//
|
||||
// QStylePainter painter(this);
|
||||
//
|
||||
// painter.drawControl(QStyle::CE_PushButton, getStyleOption());
|
||||
//
|
||||
// if (testAttribute(Qt::WA_Hover) && underMouse()) {
|
||||
// QRect r(rect());
|
||||
// QBrush brush;
|
||||
// brush.setStyle(Qt::SolidPattern);
|
||||
// painter.setOpacity(0.1);
|
||||
// painter.fillRect(r, brush);
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void FlatButton::mousePressEvent(QMouseEvent *event)
|
||||
//{
|
||||
// if (!_overlay)
|
||||
// return;
|
||||
//
|
||||
// _overlay->addRipple(event->pos());
|
||||
//}
|
||||
//
|
||||
//void FlatButton::mouseReleaseEvent(QMouseEvent *event)
|
||||
//{
|
||||
// Q_UNUSED(event)
|
||||
//
|
||||
// emit clicked();
|
||||
//}
|
||||
//
|
||||
//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);
|
||||
// option.features = QStyleOptionButton::Flat;
|
||||
// if (isChecked())
|
||||
// option.state |= QStyle::State_On;
|
||||
// option.text = text();
|
||||
// option.icon = icon();
|
||||
// option.iconSize = iconSize();
|
||||
// return option;
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
#ifndef FLATBUTTON_H
|
||||
#define FLATBUTTON_H
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QStyleOptionButton>
|
||||
#include "../lib/rippleoverlay.h"
|
||||
#include <QPushButton>
|
||||
|
||||
class FlatButton : public QAbstractButton
|
||||
class FlatButton : public QPushButton
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
|
@ -13,28 +11,48 @@ public:
|
|||
explicit FlatButton(QWidget *parent = 0);
|
||||
explicit FlatButton(const QString &text, QWidget *parent = 0);
|
||||
~FlatButton();
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||
void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
QStyleOptionButton getStyleOption() const;
|
||||
|
||||
private:
|
||||
inline void updateOverlayGeometry()
|
||||
{
|
||||
if (_overlay) {
|
||||
_overlay->setGeometry(rect());
|
||||
}
|
||||
}
|
||||
|
||||
RippleOverlay *const _overlay;
|
||||
};
|
||||
|
||||
#endif // FLATBUTTON_H
|
||||
|
||||
//#ifndef FLATBUTTON_H
|
||||
//#define FLATBUTTON_H
|
||||
//
|
||||
//#include <QAbstractButton>
|
||||
//#include <QStyleOptionButton>
|
||||
//#include "../lib/rippleoverlay.h"
|
||||
//
|
||||
//class FlatButton : public QAbstractButton
|
||||
//{
|
||||
// Q_OBJECT
|
||||
//
|
||||
//public:
|
||||
// explicit FlatButton(QWidget *parent = 0);
|
||||
// explicit FlatButton(const QString &text, QWidget *parent = 0);
|
||||
// ~FlatButton();
|
||||
//
|
||||
// QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
//
|
||||
//protected:
|
||||
// void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
||||
// void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
// void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
// void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
// void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||
// void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||
//
|
||||
// QStyleOptionButton getStyleOption() const;
|
||||
//
|
||||
//private:
|
||||
// inline void updateOverlayGeometry()
|
||||
// {
|
||||
// if (_overlay) {
|
||||
// _overlay->setGeometry(rect());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// RippleOverlay *const _overlay;
|
||||
//};
|
||||
//
|
||||
//#endif // FLATBUTTON_H
|
||||
//
|
||||
|
|
Loading…
Reference in New Issue