diff --git a/components/toggle.cpp b/components/toggle.cpp index 8e9f474..5dbbb38 100644 --- a/components/toggle.cpp +++ b/components/toggle.cpp @@ -1,13 +1,18 @@ #include "toggle.h" +#include #include "toggle_p.h" TogglePrivate::TogglePrivate(Toggle *q) - : q_ptr(q) + : q_ptr(q), + orientation(Qt::Horizontal) { } void TogglePrivate::init() { + Q_Q(Toggle); + + q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); } Toggle::Toggle(QWidget *parent) @@ -21,12 +26,49 @@ Toggle::~Toggle() { } +QSize Toggle::sizeHint() const +{ + Q_D(const Toggle); + + return Qt::Horizontal == d->orientation + ? QSize(64, 48) + : QSize(48, 64); +} + void Toggle::setOrientation(Qt::Orientation orientation) { + Q_D(Toggle); + + if (d->orientation == orientation) + return; + d->orientation = orientation; } void Toggle::paintEvent(QPaintEvent *event) { + Q_UNUSED(event) + + Q_D(Toggle); + + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush; + brush.setColor(QColor(180, 180, 180)); + brush.setStyle(Qt::SolidPattern); + painter.setBrush(brush); + + painter.setPen(Qt::NoPen); + + if (Qt::Horizontal == d->orientation) { + const int h = height()/2; + const QRect r(0, h/2, width(), h); + painter.drawRoundedRect(r.adjusted(14, 4, -14, -4), h/2-4, h/2-4); + } else { + const int w = width()/2; + const QRect r(w/2, 0, w, height()); + painter.drawRoundedRect(r.adjusted(4, 14, -4, -14), w/2-4, w/2-4); + } } //#include diff --git a/components/toggle.h b/components/toggle.h index 294b458..c2041ca 100644 --- a/components/toggle.h +++ b/components/toggle.h @@ -13,6 +13,8 @@ public: explicit Toggle(QWidget *parent = 0); ~Toggle(); + QSize sizeHint() const; + void setOrientation(Qt::Orientation orientation) Q_DECL_OVERRIDE; protected: diff --git a/components/toggle_p.h b/components/toggle_p.h index e46bb0d..537d417 100644 --- a/components/toggle_p.h +++ b/components/toggle_p.h @@ -14,6 +14,7 @@ public: void init(); Toggle *const q_ptr; + Qt::Orientation orientation; }; #endif // TOGGLE_P_H