implement basic Toggle and Thumb painters

This commit is contained in:
laserpants 2016-03-26 19:26:39 +03:00
parent 0a637bf140
commit 2229b86053
3 changed files with 34 additions and 13 deletions

View File

@ -57,7 +57,7 @@ protected:
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
private: private:
inline bool isOnTrack(int p, int x) const { return (p >= x-2 && p < x+2); } inline bool isOnTrack(int p, int x) const { return (p >= x-2 && p <= x+2); }
bool _drag; bool _drag;
Handle *const _handle; Handle *const _handle;

View File

@ -1,30 +1,52 @@
#include <QAbstractButton> #include <QAbstractButton>
#include <QEvent>
#include <QDebug> #include <QDebug>
#include <QPainter> #include <QPainter>
#include "toggle.h" #include "toggle.h"
#include "../lib/rippleoverlay.h" #include "../lib/rippleoverlay.h"
#include "../lib/customshadoweffect.h" #include "../lib/customshadoweffect.h"
Thumb::Thumb(QWidget *parent) Thumb::Thumb(Toggle *parent)
: QWidget(parent) : QWidget(parent)
{ {
parent->installEventFilter(this);
} }
Thumb::~Thumb() Thumb::~Thumb()
{ {
} }
bool Thumb::eventFilter(QObject *obj, QEvent *event)
{
const QEvent::Type type = event->type();
if (QEvent::Resize == type || QEvent::Move == type) {
setGeometry(parentWidget()->rect());
}
return QWidget::eventFilter(obj, event);
}
void Thumb::paintEvent(QPaintEvent *event) void Thumb::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
QPainter painter(this); qDebug() << rect();
painter.drawRect(rect()); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// QPen pen;
// pen.setColor(Qt::red);
// painter.setPen(pen);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(120, 120, 120));
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
const int d = qMin(width(), height()); const int d = qMin(width(), height());
painter.drawEllipse(0, 0, d, d);
painter.drawEllipse(d, d, d/2, d/2);
} }
Toggle::Toggle(QWidget *parent) Toggle::Toggle(QWidget *parent)
@ -32,8 +54,8 @@ Toggle::Toggle(QWidget *parent)
_overlay(new RippleOverlay(this)), _overlay(new RippleOverlay(this)),
_thumb(new Thumb(this)) _thumb(new Thumb(this))
{ {
CustomShadowEffect *effect = new CustomShadowEffect; // CustomShadowEffect *effect = new CustomShadowEffect;
_thumb->setGraphicsEffect(effect); // _thumb->setGraphicsEffect(effect);
} }
Toggle::~Toggle() Toggle::~Toggle()
@ -45,9 +67,6 @@ void Toggle::paintEvent(QPaintEvent *event)
Q_UNUSED(event) Q_UNUSED(event)
QPainter painter(this); QPainter painter(this);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
const int h = height()/2; const int h = height()/2;
@ -59,5 +78,5 @@ void Toggle::paintEvent(QPaintEvent *event)
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.drawRoundedRect(QRect(0, h-h/2, width(), h+h/2), h, h); painter.drawRoundedRect(QRect(0, h-h/2, width(), h), h/2, h/2);
} }

View File

@ -4,16 +4,18 @@
#include <QAbstractButton> #include <QAbstractButton>
class RippleOverlay; class RippleOverlay;
class Toggle;
class Thumb : public QWidget class Thumb : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit Thumb(QWidget *parent = 0); explicit Thumb(Toggle *parent);
~Thumb(); ~Thumb();
protected: protected:
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
}; };