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 <QEvent>
|
||||||
|
#include <QPainter>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "iconbutton.h"
|
#include "iconbutton.h"
|
||||||
#include "../lib/rippleoverlay.h"
|
#include "../lib/rippleoverlay.h"
|
||||||
|
@ -19,8 +17,6 @@ IconButton::IconButton(const QIcon &icon, QWidget *parent)
|
||||||
setSizePolicy(policy);
|
setSizePolicy(policy);
|
||||||
|
|
||||||
setGeometryWidget(this);
|
setGeometryWidget(this);
|
||||||
|
|
||||||
setStyle(&Style::instance());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IconButton::~IconButton()
|
IconButton::~IconButton()
|
||||||
|
@ -29,13 +25,7 @@ IconButton::~IconButton()
|
||||||
|
|
||||||
QSize IconButton::sizeHint() const
|
QSize IconButton::sizeHint() const
|
||||||
{
|
{
|
||||||
QStyleOptionButton option(getStyleOption());
|
return iconSize();
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconButton::setGeometryWidget(QWidget *widget)
|
void IconButton::setGeometryWidget(QWidget *widget)
|
||||||
|
@ -52,9 +42,10 @@ void IconButton::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
||||||
QStylePainter painter(this);
|
QPainter painter(this);
|
||||||
QStyleOptionButton option(getStyleOption());
|
const QSize &size = iconSize();
|
||||||
painter.drawControl(QStyle::CE_PushButton, option);
|
QPoint pos(width()/2-size.width()/2, height()/2-size.height()/2);
|
||||||
|
icon().paint(&painter, QRect(pos, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconButton::mousePressEvent(QMouseEvent *event)
|
void IconButton::mousePressEvent(QMouseEvent *event)
|
||||||
|
@ -87,18 +78,6 @@ bool IconButton::eventFilter(QObject *obj, QEvent *event)
|
||||||
return QAbstractButton::eventFilter(obj, 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()
|
void IconButton::updateOverlayGeometry()
|
||||||
{
|
{
|
||||||
if (!_overlay || !_geometryWidget)
|
if (!_overlay || !_geometryWidget)
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define ICONBUTTON_H
|
#define ICONBUTTON_H
|
||||||
|
|
||||||
#include <QAbstractButton>
|
#include <QAbstractButton>
|
||||||
#include <QStyleOptionButton>
|
|
||||||
|
|
||||||
class RippleOverlay;
|
class RippleOverlay;
|
||||||
|
|
||||||
|
@ -23,8 +22,6 @@ protected:
|
||||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
QStyleOptionButton getStyleOption() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateOverlayGeometry();
|
void updateOverlayGeometry();
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,6 @@ IconButtonExamples::IconButtonExamples(QWidget *parent)
|
||||||
);
|
);
|
||||||
frame->setWidget(view);
|
frame->setWidget(view);
|
||||||
|
|
||||||
iconButton->setMinimumSize(50, 50);
|
|
||||||
|
|
||||||
layout->addWidget(frame);
|
layout->addWidget(frame);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
@ -40,7 +38,21 @@ IconButtonExamples::IconButtonExamples(QWidget *parent)
|
||||||
);
|
);
|
||||||
frame->setWidget(view);
|
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);
|
layout->addWidget(frame);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue