From a7a4ad9e81a8485ee0ed02fd5a0f139a5143a09c Mon Sep 17 00:00:00 2001 From: laserpants Date: Sun, 12 Jun 2016 22:36:51 +0300 Subject: [PATCH] implement basic Radio Button functionality --- components/radiobutton.cpp | 85 +++++++++++++++++++++++++++++--- components/radiobutton.h | 3 ++ components/radiobutton_p.h | 8 ++- examples/radiobuttonexamples.cpp | 30 ++++++++++- qt-material-widgets.pro | 6 ++- 5 files changed, 120 insertions(+), 12 deletions(-) diff --git a/components/radiobutton.cpp b/components/radiobutton.cpp index 1766733..a0ebdec 100644 --- a/components/radiobutton.cpp +++ b/components/radiobutton.cpp @@ -1,42 +1,115 @@ #include "radiobutton.h" #include +#include #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()); } diff --git a/components/radiobutton.h b/components/radiobutton.h index 5b1dec7..117e24e 100644 --- a/components/radiobutton.h +++ b/components/radiobutton.h @@ -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; diff --git a/components/radiobutton_p.h b/components/radiobutton_p.h index a9723a2..a119bda 100644 --- a/components/radiobutton_p.h +++ b/components/radiobutton_p.h @@ -2,6 +2,7 @@ #define RADIOBUTTON_P_H #include +#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 diff --git a/examples/radiobuttonexamples.cpp b/examples/radiobuttonexamples.cpp index 81c5d60..e1a0223 100644 --- a/examples/radiobuttonexamples.cpp +++ b/examples/radiobuttonexamples.cpp @@ -1,5 +1,6 @@ #include #include +#include #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( diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index 26b824d..3c148f9 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -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