borrow implementation of sizeHint from QPushButton
This commit is contained in:
parent
9baf066abc
commit
9f640ff6ca
|
@ -1,7 +1,7 @@
|
|||
#include <QDebug>
|
||||
#include <QStylePainter>
|
||||
#include <QStyleOptionButton>
|
||||
#include <QMouseEvent>
|
||||
#include <QApplication>
|
||||
#include "flatbutton.h"
|
||||
|
||||
FlatButton::FlatButton(QWidget *parent)
|
||||
|
@ -21,6 +21,39 @@ FlatButton::~FlatButton()
|
|||
{
|
||||
}
|
||||
|
||||
QSize FlatButton::sizeHint() const
|
||||
{
|
||||
// Mostly lifted from QPushButton
|
||||
|
||||
ensurePolished();
|
||||
|
||||
int w = 0,
|
||||
h = 0;
|
||||
|
||||
QStyleOptionButton opt = getStyleOption();
|
||||
|
||||
#ifndef QT_NO_ICON
|
||||
if (!icon().isNull()) {
|
||||
int ih = opt.iconSize.height();
|
||||
int iw = opt.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, &opt, QSize(w, h), this).
|
||||
expandedTo(QApplication::globalStrut()));
|
||||
}
|
||||
|
||||
void FlatButton::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
@ -34,15 +67,7 @@ void FlatButton::paintEvent(QPaintEvent *event)
|
|||
|
||||
QStylePainter painter(this);
|
||||
|
||||
QStyleOptionButton option;
|
||||
option.initFrom(this);
|
||||
option.features = QStyleOptionButton::None;
|
||||
option.features |= QStyleOptionButton::Flat;
|
||||
if (isChecked())
|
||||
option.state |= QStyle::State_On;
|
||||
option.text = text();
|
||||
option.icon = icon();
|
||||
option.iconSize = iconSize();
|
||||
QStyleOptionButton option = getStyleOption();
|
||||
|
||||
painter.drawControl(QStyle::CE_PushButton, option);
|
||||
|
||||
|
@ -80,3 +105,17 @@ void FlatButton::leaveEvent(QEvent *event)
|
|||
|
||||
update();
|
||||
}
|
||||
|
||||
QStyleOptionButton FlatButton::getStyleOption() const
|
||||
{
|
||||
QStyleOptionButton option;
|
||||
option.initFrom(this);
|
||||
option.features = QStyleOptionButton::None;
|
||||
option.features |= QStyleOptionButton::Flat;
|
||||
if (isChecked())
|
||||
option.state |= QStyle::State_On;
|
||||
option.text = text();
|
||||
option.icon = icon();
|
||||
option.iconSize = iconSize();
|
||||
return option;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define FLATBUTTON_H
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QStyleOptionButton>
|
||||
#include "rippleoverlay.h"
|
||||
|
||||
class FlatButton : public QAbstractButton
|
||||
|
@ -13,6 +14,8 @@ public:
|
|||
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;
|
||||
|
@ -23,6 +26,7 @@ protected:
|
|||
|
||||
private:
|
||||
inline void updateOverlayGeometry() { _overlay->setGeometry(rect()); }
|
||||
QStyleOptionButton getStyleOption() const;
|
||||
|
||||
RippleOverlay *const _overlay;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue