refactor flat button
This commit is contained in:
parent
51cf801c88
commit
c4a17f9dfa
|
@ -1,6 +1,9 @@
|
||||||
#include "flatbutton.h"
|
#include "flatbutton.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
|
#include <QStylePainter>
|
||||||
|
#include <QStyleOption>
|
||||||
|
#include <QDebug>
|
||||||
#include "lib/style.h"
|
#include "lib/style.h"
|
||||||
#include "lib/rippleoverlay.h"
|
#include "lib/rippleoverlay.h"
|
||||||
|
|
||||||
|
@ -53,9 +56,33 @@ void FlatButton::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
||||||
QPainter painter(this);
|
//painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||||
|
|
||||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
QStylePainter painter(this);
|
||||||
|
|
||||||
|
QStyleOptionButton option;
|
||||||
|
initStyleOption(&option);
|
||||||
|
option.features |= QStyleOptionButton::Flat;
|
||||||
|
|
||||||
|
painter.drawControl(QStyle::CE_PushButtonLabel, option);
|
||||||
|
|
||||||
|
if (testAttribute(Qt::WA_Hover) && underMouse())
|
||||||
|
{
|
||||||
|
QPainter painter(this);
|
||||||
|
QBrush brush;
|
||||||
|
brush.setStyle(Qt::SolidPattern);
|
||||||
|
painter.setOpacity(0.1);
|
||||||
|
painter.fillRect(rect(), brush);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG_LAYOUT
|
||||||
|
QPainter debug(this);
|
||||||
|
QPen pen;
|
||||||
|
pen.setColor(Qt::red);
|
||||||
|
pen.setWidth(2);
|
||||||
|
debug.setPen(pen);
|
||||||
|
debug.drawRect(rect());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlatButton::mousePressEvent(QMouseEvent *event)
|
void FlatButton::mousePressEvent(QMouseEvent *event)
|
||||||
|
@ -65,143 +92,16 @@ void FlatButton::mousePressEvent(QMouseEvent *event)
|
||||||
QPushButton::mousePressEvent(event);
|
QPushButton::mousePressEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
//#include <QDebug>
|
void FlatButton::enterEvent(QEvent *event)
|
||||||
//#include <QStylePainter>
|
{
|
||||||
//#include <QMouseEvent>
|
update();
|
||||||
//#include <QApplication>
|
|
||||||
//#include "flatbutton.h"
|
QPushButton::enterEvent(event);
|
||||||
//#include "lib/style.h"
|
}
|
||||||
//
|
|
||||||
//FlatButton::FlatButton(QWidget *parent)
|
void FlatButton::leaveEvent(QEvent *event)
|
||||||
// : QAbstractButton(parent),
|
{
|
||||||
// _overlay(new RippleOverlay(this))
|
update();
|
||||||
//{
|
|
||||||
// setStyle(&Style::instance());
|
QPushButton::leaveEvent(event);
|
||||||
// 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;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
|
|
|
@ -18,51 +18,11 @@ protected:
|
||||||
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RippleOverlay *const _ripple;
|
RippleOverlay *const _ripple;
|
||||||
};
|
};
|
||||||
|
|
||||||
#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
|
|
||||||
//
|
|
||||||
|
|
|
@ -20,11 +20,24 @@ void Style::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter
|
||||||
{
|
{
|
||||||
switch (pe)
|
switch (pe)
|
||||||
{
|
{
|
||||||
case PE_FrameFocusRect:
|
case PE_FrameFocusRect:
|
||||||
//p->setPen(Qt::blue);
|
//p->setPen(Qt::blue);
|
||||||
//p->drawRect(opt->rect);
|
//p->drawRect(opt->rect);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
QCommonStyle::drawPrimitive(pe, opt, p, w);
|
QCommonStyle::drawPrimitive(pe, opt, p, w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Style::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWidget *widget) const
|
||||||
|
{
|
||||||
|
switch (m)
|
||||||
|
{
|
||||||
|
case PM_ButtonShiftHorizontal:
|
||||||
|
case PM_ButtonShiftVertical:
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return QCommonStyle::pixelMetric(m, opt, widget);
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ public:
|
||||||
void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
|
void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
|
||||||
const QWidget *w = 0) const Q_DECL_OVERRIDE;
|
const QWidget *w = 0) const Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
int pixelMetric(PixelMetric m, const QStyleOption *opt,
|
||||||
|
const QWidget *widget) const Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Style() {}
|
Style() {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue