implement basic Radio Button functionality

This commit is contained in:
laserpants 2016-06-12 22:36:51 +03:00
parent fbcc7b2bf7
commit a7a4ad9e81
5 changed files with 120 additions and 12 deletions

View File

@ -1,42 +1,115 @@
#include "radiobutton.h"
#include <QPainter>
#include <QEvent>
#include "radiobutton_p.h"
#include "lib/rippleoverlay.h"
RadioButtonPrivate::RadioButtonPrivate(RadioButton *q)
: q_ptr(q)
: q_ptr(q),
checkedIcon(QIcon("../qt-material-widgets/ic_radio_button_checked_black_24px.svg")),
uncheckedIcon(QIcon("../qt-material-widgets/ic_radio_button_unchecked_black_24px.svg"))
{
}
void RadioButtonPrivate::init()
{
//Q_Q(RadioButton);
Q_Q(RadioButton);
ripple = new RippleOverlay(q->parentWidget());
iconWidget = new RadioButtonIcon(q);
QFont f(q->font());
f.setPointSizeF(11);
q->setFont(f);
}
RadioButton::RadioButton(QWidget *parent)
: QRadioButton(parent),
d_ptr(new RadioButtonPrivate(this))
{
d_func()->init();
//QPushButton *b = new QPushButton;
//b->setParent(this);
//b->setIcon(d_func()->checkedIcon);
//b->setIconSize(QSize(12, 12));
//QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect;
//effect->setColor(Qt::blue);
//b->setGraphicsEffect(effect);
}
RadioButton::~RadioButton()
{
}
QSize RadioButton::sizeHint() const
{
QString s(text());
if (s.isEmpty())
return QSize(32, 32);
QFontMetrics fm = fontMetrics();
QSize sz = fm.size(Qt::TextShowMnemonic, s);
return QSize(sz.width() + 44, 32);
}
bool RadioButton::event(QEvent *event)
{
Q_D(RadioButton);
switch (event->type())
{
case QEvent::Resize:
case QEvent::Move:
d->ripple->setGeometry(geometry().adjusted(-8, -8, 8, 8));
d->iconWidget->setGeometry(geometry());
break;
case QEvent::ParentChange:
d->ripple->setParent(parentWidget());
break;
default:
break;
}
return QRadioButton::event(event);
}
void RadioButton::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
Q_D(RadioButton);
d->ripple->addRipple(QPoint(24, 24), 24);
QRadioButton::mousePressEvent(event);
}
void RadioButton::mouseReleaseEvent(QMouseEvent *event)
{
QWidget::mouseReleaseEvent(event);
QRadioButton::mouseReleaseEvent(event);
}
void RadioButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
//Q_D(RadioButton);
QPainter painter(this);
painter.drawRect(rect());
//painter.save();
//QRadioButton::paintEvent(event);
//painter.restore();
QWidget::paintEvent(event);
//painter.drawRect(rect().adjusted(0, 0, -1, -1));
// Icon
//d->checkedIcon.paint(&painter, QRect(4, 4, 24, 24), Qt::AlignCenter, QIcon::Normal);
//d->checkedIcon.pixmap()
// Label
painter.drawText(44, 21, text());
}

View File

@ -13,7 +13,10 @@ public:
explicit RadioButton(QWidget *parent = 0);
~RadioButton();
QSize sizeHint() const;
protected:
bool event(QEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;

View File

@ -2,6 +2,7 @@
#define RADIOBUTTON_P_H
#include <QObject>
#include "radiobutton_internal.h"
class RadioButton;
class RippleOverlay;
@ -16,8 +17,11 @@ public:
void init();
RadioButton *const q_ptr;
RippleOverlay *ripple;
RadioButton *const q_ptr;
RippleOverlay *ripple;
RadioButtonIcon *iconWidget;
QIcon checkedIcon;
QIcon uncheckedIcon;
};
#endif // RADIOBUTTON_P_H

View File

@ -1,5 +1,6 @@
#include <QLayout>
#include <QEvent>
#include <QButtonGroup>
#include "radiobuttonexamples.h"
#include "components/radiobutton.h"
#include "exampleview.h"
@ -11,10 +12,35 @@ RadioButtonExamples::RadioButtonExamples(QWidget *parent)
QLayout *layout = widget()->layout();
{
RadioButton *radioButton = new RadioButton;
QButtonGroup *bg = new QButtonGroup(this);
RadioButton *radioButton1 = new RadioButton;
RadioButton *radioButton2 = new RadioButton;
RadioButton *radioButton3 = new RadioButton;
radioButton1->setText("Auto select");
radioButton2->setText("Option #2");
bg->addButton(radioButton1);
bg->addButton(radioButton2);
bg->addButton(radioButton3);
QWidget *widget = new QWidget;
QVBoxLayout *vbl = new QVBoxLayout;
QSizePolicy policy;
policy.setVerticalPolicy(QSizePolicy::Maximum);
widget->setSizePolicy(policy);
widget->setLayout(vbl);
//widget->setFixedHeight(100);
vbl->addWidget(radioButton1);
vbl->addWidget(radioButton2);
vbl->addWidget(radioButton3);
ExampleView *view = new ExampleView;
view->setWidget(radioButton);
view->setWidget(widget);
Frame *frame = new Frame;
frame->setCodeSnippet(

View File

@ -59,7 +59,8 @@ SOURCES += main.cpp\
components/fab.cpp \
components/badge.cpp \
components/progress.cpp \
components/selectfield.cpp
components/selectfield.cpp \
components/radiobutton_internal.cpp
HEADERS += mainwindow.h \
components/appbar.h \
@ -121,7 +122,8 @@ HEADERS += mainwindow.h \
components/progress.h \
components/selectfield.h \
components/fab_p.h \
components/radiobutton_p.h
components/radiobutton_p.h \
components/radiobutton_internal.h
RESOURCES += \
resources.qrc