simplify IconButton code
This commit is contained in:
parent
bce8535b16
commit
685ff75c7c
|
@ -1,7 +1,5 @@
|
|||
#include <QStylePainter>
|
||||
#include <QStyleOptionButton>
|
||||
#include <QApplication>
|
||||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include "iconbutton.h"
|
||||
#include "../lib/rippleoverlay.h"
|
||||
|
@ -19,8 +17,6 @@ IconButton::IconButton(const QIcon &icon, QWidget *parent)
|
|||
setSizePolicy(policy);
|
||||
|
||||
setGeometryWidget(this);
|
||||
|
||||
setStyle(&Style::instance());
|
||||
}
|
||||
|
||||
IconButton::~IconButton()
|
||||
|
@ -29,13 +25,7 @@ IconButton::~IconButton()
|
|||
|
||||
QSize IconButton::sizeHint() const
|
||||
{
|
||||
QStyleOptionButton option(getStyleOption());
|
||||
|
||||
int w = option.iconSize.width() + 4;
|
||||
int h = option.iconSize.height();
|
||||
|
||||
return (style()->sizeFromContents(QStyle::CT_PushButton, &option, QSize(w, h), this).
|
||||
expandedTo(QApplication::globalStrut()));
|
||||
return iconSize();
|
||||
}
|
||||
|
||||
void IconButton::setGeometryWidget(QWidget *widget)
|
||||
|
@ -52,9 +42,10 @@ void IconButton::paintEvent(QPaintEvent *event)
|
|||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
QStylePainter painter(this);
|
||||
QStyleOptionButton option(getStyleOption());
|
||||
painter.drawControl(QStyle::CE_PushButton, option);
|
||||
QPainter painter(this);
|
||||
const QSize &size = iconSize();
|
||||
QPoint pos(width()/2-size.width()/2, height()/2-size.height()/2);
|
||||
icon().paint(&painter, QRect(pos, size));
|
||||
}
|
||||
|
||||
void IconButton::mousePressEvent(QMouseEvent *event)
|
||||
|
@ -87,18 +78,6 @@ bool IconButton::eventFilter(QObject *obj, QEvent *event)
|
|||
return QAbstractButton::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
QStyleOptionButton IconButton::getStyleOption() const
|
||||
{
|
||||
QStyleOptionButton option;
|
||||
option.initFrom(this);
|
||||
option.features = QStyleOptionButton::Flat;
|
||||
if (isChecked())
|
||||
option.state |= QStyle::State_On;
|
||||
option.icon = icon();
|
||||
option.iconSize = iconSize();
|
||||
return option;
|
||||
}
|
||||
|
||||
void IconButton::updateOverlayGeometry()
|
||||
{
|
||||
if (!_overlay || !_geometryWidget)
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
#define ICONBUTTON_H
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QStyleOptionButton>
|
||||
|
||||
class RippleOverlay;
|
||||
|
||||
|
@ -23,8 +22,6 @@ protected:
|
|||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
QStyleOptionButton getStyleOption() const;
|
||||
|
||||
private:
|
||||
void updateOverlayGeometry();
|
||||
|
||||
|
|
|
@ -22,8 +22,6 @@ IconButtonExamples::IconButtonExamples(QWidget *parent)
|
|||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
iconButton->setMinimumSize(50, 50);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
|
@ -40,7 +38,21 @@ IconButtonExamples::IconButtonExamples(QWidget *parent)
|
|||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
iconButton->setMinimumSize(50, 50);
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
IconButton *iconButton = new IconButton(QIcon("../qt-material-widgets/face.svg"));
|
||||
iconButton->setIconSize(QSize(128, 128));
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(iconButton);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"IconButton *iconButton = new IconButton(QIcon(\"face.svg\"));\n"
|
||||
"iconButton->setIconSize(QSize(128, 128));\n"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue