implement basic Toggle and Thumb painters
This commit is contained in:
parent
0a637bf140
commit
2229b86053
|
@ -57,7 +57,7 @@ protected:
|
|||
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
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;
|
||||
Handle *const _handle;
|
||||
|
|
|
@ -1,30 +1,52 @@
|
|||
#include <QAbstractButton>
|
||||
#include <QEvent>
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include "toggle.h"
|
||||
#include "../lib/rippleoverlay.h"
|
||||
#include "../lib/customshadoweffect.h"
|
||||
|
||||
Thumb::Thumb(QWidget *parent)
|
||||
Thumb::Thumb(Toggle *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
parent->installEventFilter(this);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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());
|
||||
|
||||
painter.drawEllipse(d, d, d/2, d/2);
|
||||
painter.drawEllipse(0, 0, d, d);
|
||||
}
|
||||
|
||||
Toggle::Toggle(QWidget *parent)
|
||||
|
@ -32,8 +54,8 @@ Toggle::Toggle(QWidget *parent)
|
|||
_overlay(new RippleOverlay(this)),
|
||||
_thumb(new Thumb(this))
|
||||
{
|
||||
CustomShadowEffect *effect = new CustomShadowEffect;
|
||||
_thumb->setGraphicsEffect(effect);
|
||||
// CustomShadowEffect *effect = new CustomShadowEffect;
|
||||
// _thumb->setGraphicsEffect(effect);
|
||||
}
|
||||
|
||||
Toggle::~Toggle()
|
||||
|
@ -45,9 +67,6 @@ void Toggle::paintEvent(QPaintEvent *event)
|
|||
Q_UNUSED(event)
|
||||
|
||||
QPainter painter(this);
|
||||
|
||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
const int h = height()/2;
|
||||
|
@ -59,5 +78,5 @@ void Toggle::paintEvent(QPaintEvent *event)
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -4,16 +4,18 @@
|
|||
#include <QAbstractButton>
|
||||
|
||||
class RippleOverlay;
|
||||
class Toggle;
|
||||
|
||||
class Thumb : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Thumb(QWidget *parent = 0);
|
||||
explicit Thumb(Toggle *parent);
|
||||
~Thumb();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue