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 "flatbutton.h"
|
||||||
#include "lib/style.h"
|
#include "lib/style.h"
|
||||||
|
|
||||||
FlatButton::FlatButton(QWidget *parent)
|
FlatButton::FlatButton(QWidget *parent)
|
||||||
: QAbstractButton(parent),
|
: QPushButton(parent)
|
||||||
_overlay(new RippleOverlay(this))
|
|
||||||
{
|
{
|
||||||
setStyle(&Style::instance());
|
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)
|
FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||||
: QAbstractButton(parent),
|
: QPushButton(parent)
|
||||||
_overlay(new RippleOverlay(this))
|
|
||||||
{
|
{
|
||||||
setText(text);
|
setText(text);
|
||||||
|
|
||||||
setStyle(&Style::instance());
|
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()
|
//#include <QDebug>
|
||||||
{
|
//#include <QStylePainter>
|
||||||
}
|
//#include <QMouseEvent>
|
||||||
|
//#include <QApplication>
|
||||||
QSize FlatButton::sizeHint() const
|
//#include "flatbutton.h"
|
||||||
{
|
//#include "lib/style.h"
|
||||||
// Mostly lifted from QPushButton
|
//
|
||||||
|
//FlatButton::FlatButton(QWidget *parent)
|
||||||
ensurePolished();
|
// : QAbstractButton(parent),
|
||||||
|
// _overlay(new RippleOverlay(this))
|
||||||
int w = 0,
|
//{
|
||||||
h = 0;
|
// setStyle(&Style::instance());
|
||||||
|
// setAttribute(Qt::WA_Hover);
|
||||||
QStyleOptionButton option(getStyleOption());
|
//
|
||||||
|
// QFont font(this->font());
|
||||||
#ifndef QT_NO_ICON
|
// font.setCapitalization(QFont::AllUppercase);
|
||||||
if (!icon().isNull()) {
|
// font.setPointSizeF(10.5);
|
||||||
int ih = option.iconSize.height();
|
// font.setStyleName("Medium");
|
||||||
int iw = option.iconSize.width() + 4;
|
// setFont(font);
|
||||||
w += iw;
|
//}
|
||||||
h = qMax(h, ih);
|
//
|
||||||
}
|
//FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||||
#endif
|
// : QAbstractButton(parent),
|
||||||
|
// _overlay(new RippleOverlay(this))
|
||||||
QString s(text());
|
//{
|
||||||
bool empty = s.isEmpty();
|
// setText(text);
|
||||||
if (empty)
|
// setStyle(&Style::instance());
|
||||||
s = QString::fromLatin1("XXXX");
|
// setAttribute(Qt::WA_Hover);
|
||||||
QFontMetrics fm = fontMetrics();
|
//
|
||||||
QSize sz = fm.size(Qt::TextShowMnemonic, s);
|
// QFont font(this->font());
|
||||||
if (!empty || !w)
|
// font.setCapitalization(QFont::AllUppercase);
|
||||||
w += sz.width();
|
// font.setPointSizeF(10.5);
|
||||||
if (!empty || !h)
|
// font.setStyleName("Medium");
|
||||||
h = qMax(h, sz.height());
|
// setFont(font);
|
||||||
return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this)
|
//}
|
||||||
.expandedTo(QApplication::globalStrut()));
|
//
|
||||||
}
|
//FlatButton::~FlatButton()
|
||||||
|
//{
|
||||||
void FlatButton::resizeEvent(QResizeEvent *event)
|
//}
|
||||||
{
|
//
|
||||||
Q_UNUSED(event)
|
//QSize FlatButton::sizeHint() const
|
||||||
|
//{
|
||||||
updateOverlayGeometry();
|
// // Mostly lifted from QPushButton
|
||||||
}
|
//
|
||||||
|
// ensurePolished();
|
||||||
void FlatButton::paintEvent(QPaintEvent *event)
|
//
|
||||||
{
|
// int w = 0,
|
||||||
Q_UNUSED(event)
|
// h = 0;
|
||||||
|
//
|
||||||
QStylePainter painter(this);
|
// QStyleOptionButton option(getStyleOption());
|
||||||
|
//
|
||||||
painter.drawControl(QStyle::CE_PushButton, getStyleOption());
|
//#ifndef QT_NO_ICON
|
||||||
|
// if (!icon().isNull()) {
|
||||||
if (testAttribute(Qt::WA_Hover) && underMouse()) {
|
// int ih = option.iconSize.height();
|
||||||
QRect r(rect());
|
// int iw = option.iconSize.width() + 4;
|
||||||
QBrush brush;
|
// w += iw;
|
||||||
brush.setStyle(Qt::SolidPattern);
|
// h = qMax(h, ih);
|
||||||
painter.setOpacity(0.1);
|
// }
|
||||||
painter.fillRect(r, brush);
|
//#endif
|
||||||
}
|
//
|
||||||
}
|
// QString s(text());
|
||||||
|
// bool empty = s.isEmpty();
|
||||||
void FlatButton::mousePressEvent(QMouseEvent *event)
|
// if (empty)
|
||||||
{
|
// s = QString::fromLatin1("XXXX");
|
||||||
if (!_overlay)
|
// QFontMetrics fm = fontMetrics();
|
||||||
return;
|
// QSize sz = fm.size(Qt::TextShowMnemonic, s);
|
||||||
|
// if (!empty || !w)
|
||||||
_overlay->addRipple(event->pos());
|
// w += sz.width();
|
||||||
}
|
// if (!empty || !h)
|
||||||
|
// h = qMax(h, sz.height());
|
||||||
void FlatButton::mouseReleaseEvent(QMouseEvent *event)
|
// return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this)
|
||||||
{
|
// .expandedTo(QApplication::globalStrut()));
|
||||||
Q_UNUSED(event)
|
//}
|
||||||
|
//
|
||||||
emit clicked();
|
//void FlatButton::resizeEvent(QResizeEvent *event)
|
||||||
}
|
//{
|
||||||
|
// Q_UNUSED(event)
|
||||||
void FlatButton::enterEvent(QEvent *event)
|
//
|
||||||
{
|
// updateOverlayGeometry();
|
||||||
Q_UNUSED(event)
|
//}
|
||||||
|
//
|
||||||
update();
|
//void FlatButton::paintEvent(QPaintEvent *event)
|
||||||
}
|
//{
|
||||||
|
// Q_UNUSED(event)
|
||||||
void FlatButton::leaveEvent(QEvent *event)
|
//
|
||||||
{
|
// QStylePainter painter(this);
|
||||||
Q_UNUSED(event)
|
//
|
||||||
|
// painter.drawControl(QStyle::CE_PushButton, getStyleOption());
|
||||||
update();
|
//
|
||||||
}
|
// if (testAttribute(Qt::WA_Hover) && underMouse()) {
|
||||||
|
// QRect r(rect());
|
||||||
QStyleOptionButton FlatButton::getStyleOption() const
|
// QBrush brush;
|
||||||
{
|
// brush.setStyle(Qt::SolidPattern);
|
||||||
QStyleOptionButton option;
|
// painter.setOpacity(0.1);
|
||||||
option.initFrom(this);
|
// painter.fillRect(r, brush);
|
||||||
option.features = QStyleOptionButton::Flat;
|
// }
|
||||||
if (isChecked())
|
//}
|
||||||
option.state |= QStyle::State_On;
|
//
|
||||||
option.text = text();
|
//void FlatButton::mousePressEvent(QMouseEvent *event)
|
||||||
option.icon = icon();
|
//{
|
||||||
option.iconSize = iconSize();
|
// if (!_overlay)
|
||||||
return option;
|
// 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
|
#ifndef FLATBUTTON_H
|
||||||
#define FLATBUTTON_H
|
#define FLATBUTTON_H
|
||||||
|
|
||||||
#include <QAbstractButton>
|
#include <QPushButton>
|
||||||
#include <QStyleOptionButton>
|
|
||||||
#include "../lib/rippleoverlay.h"
|
|
||||||
|
|
||||||
class FlatButton : public QAbstractButton
|
class FlatButton : public QPushButton
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
@ -13,28 +11,48 @@ public:
|
||||||
explicit FlatButton(QWidget *parent = 0);
|
explicit FlatButton(QWidget *parent = 0);
|
||||||
explicit FlatButton(const QString &text, QWidget *parent = 0);
|
explicit FlatButton(const QString &text, QWidget *parent = 0);
|
||||||
~FlatButton();
|
~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
|
#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