diff --git a/components/components.pro b/components/components.pro index fbe8eaa..98e732c 100644 --- a/components/components.pro +++ b/components/components.pro @@ -23,7 +23,9 @@ SOURCES = \ qtmaterialcircularprogress.cpp \ qtmaterialslider_internal.cpp \ qtmaterialslider.cpp \ - qtmaterialradiobutton.cpp + qtmaterialradiobutton.cpp \ + qtmaterialtoggle_internal.cpp \ + qtmaterialtoggle.cpp HEADERS = \ qtmaterialavatar_p.h \ qtmaterialavatar.h \ @@ -62,6 +64,9 @@ HEADERS = \ qtmaterialslider_p.h \ qtmaterialslider.h \ qtmaterialradiobutton_p.h \ - qtmaterialradiobutton.h + qtmaterialradiobutton.h \ + qtmaterialtoggle_internal.h \ + qtmaterialtoggle_p.h \ + qtmaterialtoggle.h RESOURCES += \ resources.qrc diff --git a/components/qtmaterialtoggle.cpp b/components/qtmaterialtoggle.cpp new file mode 100644 index 0000000..de41a50 --- /dev/null +++ b/components/qtmaterialtoggle.cpp @@ -0,0 +1,298 @@ +#include "qtmaterialtoggle.h" +#include "qtmaterialtoggle_p.h" +#include +#include +#include +#include +#include "qtmaterialtoggle_internal.h" +#include "lib/qtmaterialstyle.h" + +/*! + * \class QtMaterialTogglePrivate + * \internal + */ + +QtMaterialTogglePrivate::QtMaterialTogglePrivate(QtMaterialToggle *q) + : q_ptr(q) +{ +} + +QtMaterialTogglePrivate::~QtMaterialTogglePrivate() +{ +} + +void QtMaterialTogglePrivate::init() +{ + Q_Q(QtMaterialToggle); + + track = new QtMaterialToggleTrack(q); + thumb = new QtMaterialToggleThumb(q); + rippleOverlay = new QtMaterialToggleRippleOverlay(thumb, track, q); + stateMachine = new QStateMachine(q); + offState = new QState; + onState = new QState; + orientation = Qt::Horizontal; + useThemeColors = true; + + q->setCheckable(true); + q->setChecked(false); + q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); + + stateMachine->addState(offState); + stateMachine->addState(onState); + stateMachine->setInitialState(offState); + + offState->assignProperty(thumb, "shift", 0); + onState->assignProperty(thumb, "shift", 1); + + QSignalTransition *transition; + QPropertyAnimation *animation; + + // + + transition = new QSignalTransition(q, SIGNAL(toggled(bool))); + transition->setTargetState(onState); + offState->addTransition(transition); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("shift"); + animation->setTargetObject(thumb); + animation->setDuration(200); + animation->setEasingCurve(QEasingCurve::OutQuad); + transition->addAnimation(animation); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("trackColor"); + animation->setTargetObject(track); + animation->setDuration(150); + transition->addAnimation(animation); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("thumbColor"); + animation->setTargetObject(thumb); + animation->setDuration(150); + transition->addAnimation(animation); + + // + + transition = new QSignalTransition(q, SIGNAL(toggled(bool))); + transition->setTargetState(offState); + onState->addTransition(transition); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("shift"); + animation->setTargetObject(thumb); + animation->setDuration(200); + animation->setEasingCurve(QEasingCurve::OutQuad); + transition->addAnimation(animation); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("trackColor"); + animation->setTargetObject(track); + animation->setDuration(150); + transition->addAnimation(animation); + + animation = new QPropertyAnimation(q); + animation->setPropertyName("thumbColor"); + animation->setTargetObject(thumb); + animation->setDuration(150); + transition->addAnimation(animation); + + // + + setupProperties(); + + stateMachine->start(); + QCoreApplication::processEvents(); +} + +void QtMaterialTogglePrivate::setupProperties() +{ + Q_Q(QtMaterialToggle); + + if (q->isEnabled()) { + const qreal shift = thumb->shift(); + if (qFuzzyCompare(shift, 1)) { + thumb->setThumbColor(q->activeColor()); + track->setTrackColor(q->activeColor().lighter(110)); + } else if (qFuzzyCompare(1+shift, 1)) { + thumb->setThumbColor(q->inactiveColor()); + track->setTrackColor(q->trackColor()); + } + } + + offState->assignProperty(track, "trackColor", q->trackColor().lighter(110)); + onState->assignProperty(track, "trackColor", q->activeColor().lighter(110)); + + offState->assignProperty(thumb, "thumbColor", q->inactiveColor()); + onState->assignProperty(thumb, "thumbColor", q->activeColor()); + + q->update(); +} + +/*! + * \class QtMaterialToggle + */ + +QtMaterialToggle::QtMaterialToggle(QWidget *parent) + : QAbstractButton(parent), + d_ptr(new QtMaterialTogglePrivate(this)) +{ + d_func()->init(); +} + +QtMaterialToggle::~QtMaterialToggle() +{ +} + +void QtMaterialToggle::setUseThemeColors(bool value) +{ + Q_D(QtMaterialToggle); + + d->useThemeColors = value; + d->setupProperties(); +} + +bool QtMaterialToggle::useThemeColors() const +{ + Q_D(const QtMaterialToggle); + + return d->useThemeColors; +} + +void QtMaterialToggle::setDisabledColor(const QColor &color) +{ + Q_D(QtMaterialToggle); + + d->disabledColor = color; + + MATERIAL_DISABLE_THEME_COLORS + update(); +} + +QColor QtMaterialToggle::disabledColor() const +{ + Q_D(const QtMaterialToggle); + + if (d->useThemeColors || !d->disabledColor.isValid()) { + return QtMaterialStyle::instance().themeColor("disabled"); + } else { + return d->disabledColor; + } +} + +void QtMaterialToggle::setActiveColor(const QColor &color) +{ + Q_D(QtMaterialToggle); + + d->activeColor = color; + + MATERIAL_DISABLE_THEME_COLORS + update(); +} + +QColor QtMaterialToggle::activeColor() const +{ + Q_D(const QtMaterialToggle); + + if (d->useThemeColors || !d->activeColor.isValid()) { + return QtMaterialStyle::instance().themeColor("primary1"); + } else { + return d->activeColor; + } +} + +void QtMaterialToggle::setInactiveColor(const QColor &color) +{ + Q_D(QtMaterialToggle); + + d->inactiveColor = color; + + MATERIAL_DISABLE_THEME_COLORS + update(); +} + +QColor QtMaterialToggle::inactiveColor() const +{ + Q_D(const QtMaterialToggle); + + if (d->useThemeColors || !d->inactiveColor.isValid()) { + return QtMaterialStyle::instance().themeColor("canvas"); + } else { + return d->inactiveColor; + } +} + +void QtMaterialToggle::setTrackColor(const QColor &color) +{ + Q_D(QtMaterialToggle); + + d->trackColor = color; + + MATERIAL_DISABLE_THEME_COLORS + update(); +} + +QColor QtMaterialToggle::trackColor() const +{ + Q_D(const QtMaterialToggle); + + if (d->useThemeColors || !d->trackColor.isValid()) { + return QtMaterialStyle::instance().themeColor("accent3"); + } else { + return d->trackColor; + } +} + +void QtMaterialToggle::setOrientation(Qt::Orientation orientation) +{ + Q_D(QtMaterialToggle); + + if (d->orientation == orientation) { + return; + } + + d->orientation = orientation; + updateGeometry(); +} + +Qt::Orientation QtMaterialToggle::orientation() const +{ + Q_D(const QtMaterialToggle); + + return d->orientation; +} + +QSize QtMaterialToggle::sizeHint() const +{ + Q_D(const QtMaterialToggle); + + return Qt::Horizontal == d->orientation + ? QSize(64, 48) + : QSize(48, 64); +} + +bool QtMaterialToggle::event(QEvent *event) +{ + Q_D(QtMaterialToggle); + + switch (event->type()) + { + case QEvent::ParentChange: + { + QWidget *widget; + if ((widget = parentWidget())) { + d->rippleOverlay->setParent(widget); + } + break; + } + default: + break; + } + return QAbstractButton::event(event); +} + +void QtMaterialToggle::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) +} diff --git a/components/qtmaterialtoggle.h b/components/qtmaterialtoggle.h new file mode 100644 index 0000000..5d5a557 --- /dev/null +++ b/components/qtmaterialtoggle.h @@ -0,0 +1,52 @@ +#ifndef QTMATERIALTOGGLE_H +#define QTMATERIALTOGGLE_H + +#include + +class QtMaterialTogglePrivate; + +class QtMaterialToggle : public QAbstractButton +{ + Q_OBJECT + + Q_PROPERTY(QColor disabledColor WRITE setDisabledColor READ disabledColor) + Q_PROPERTY(QColor activeColor WRITE setActiveColor READ activeColor) + Q_PROPERTY(QColor inactiveColor WRITE setInactiveColor READ inactiveColor) + Q_PROPERTY(QColor trackColor WRITE setTrackColor READ trackColor) + +public: + explicit QtMaterialToggle(QWidget *parent = 0); + ~QtMaterialToggle(); + + void setUseThemeColors(bool value); + bool useThemeColors() const; + + void setDisabledColor(const QColor &color); + QColor disabledColor() const; + + void setActiveColor(const QColor &color); + QColor activeColor() const; + + void setInactiveColor(const QColor &color); + QColor inactiveColor() const; + + void setTrackColor(const QColor &color); + QColor trackColor() const; + + void setOrientation(Qt::Orientation orientation); + Qt::Orientation orientation() const; + + QSize sizeHint() const Q_DECL_OVERRIDE; + +protected: + bool event(QEvent *event) Q_DECL_OVERRIDE; + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + + const QScopedPointer d_ptr; + +private: + Q_DISABLE_COPY(QtMaterialToggle) + Q_DECLARE_PRIVATE(QtMaterialToggle) +}; + +#endif // QTMATERIALTOGGLE_H diff --git a/components/qtmaterialtoggle_internal.cpp b/components/qtmaterialtoggle_internal.cpp new file mode 100644 index 0000000..9bcf823 --- /dev/null +++ b/components/qtmaterialtoggle_internal.cpp @@ -0,0 +1,231 @@ +#include "qtmaterialtoggle_internal.h" +#include +#include +#include +#include "qtmaterialtoggle.h" +#include "lib/qtmaterialripple.h" + +/*! + * \class QtMaterialToggleRippleOverlay + * \internal + */ + +QtMaterialToggleRippleOverlay::QtMaterialToggleRippleOverlay( + QtMaterialToggleThumb *thumb, + QtMaterialToggleTrack *track, + QtMaterialToggle *parent) + : QtMaterialRippleOverlay(parent->parentWidget()), + m_toggle(parent), + m_thumb(thumb), + m_track(track) +{ + connect(parent, SIGNAL(toggled(bool)), this, SLOT(addToggleRipple())); + + thumb->installEventFilter(this); +} + +QtMaterialToggleRippleOverlay::~QtMaterialToggleRippleOverlay() +{ +} + +void QtMaterialToggleRippleOverlay::addToggleRipple() +{ + if (!m_toggle->isEnabled()) { + return; + } + + int t, w; + + if (Qt::Horizontal == m_toggle->orientation()) { + t = m_toggle->height()/2; + w = m_thumb->height()/2+10; + } else { + t = m_toggle->width()/2; + w = m_thumb->width()/2+10; + } + + QtMaterialRipple *ripple = new QtMaterialRipple(QPoint(10+t, 20+t)); + ripple->setColor(m_track->trackColor()); + ripple->setRadiusEndValue(w); + ripple->setOpacityStartValue(0.8); + + addRipple(ripple); +} + +bool QtMaterialToggleRippleOverlay::eventFilter(QObject *obj, QEvent *event) +{ + if (QEvent::Paint == event->type()) { + setGeometry(overlayGeometry()); + QList::const_iterator i; + QList items = ripples(); + QColor color = m_track->trackColor(); + for (i = items.begin(); i != items.end(); ++i) { + (*i)->setColor(color); + } + } + return QtMaterialRippleOverlay::eventFilter(obj, event); +} + +QRect QtMaterialToggleRippleOverlay::overlayGeometry() const +{ + const qreal offset = m_thumb->offset(); + if (Qt::Horizontal == m_toggle->orientation()) { + return m_toggle->geometry().adjusted(-10+offset, -20, 10+offset, 20); + } else { + return m_toggle->geometry().adjusted(-10, -20+offset, 10, 20+offset); + } +} + +/*! + * \class QtMaterialToggleThumb + * \internal + */ + +QtMaterialToggleThumb::QtMaterialToggleThumb(QtMaterialToggle *parent) + : QWidget(parent), + m_toggle(parent), + m_shift(0), + m_offset(0) +{ + Q_ASSERT(parent); + + QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; + effect->setBlurRadius(6); + effect->setColor(QColor(0, 0, 0, 80)); + effect->setOffset(QPointF(0, 1)); + setGraphicsEffect(effect); + + parent->installEventFilter(this); +} + +QtMaterialToggleThumb::~QtMaterialToggleThumb() +{ +} + +void QtMaterialToggleThumb::setShift(qreal shift) +{ + if (m_shift == shift) { + return; + } + + m_shift = shift; + updateOffset(); +} + +bool QtMaterialToggleThumb::eventFilter(QObject *obj, QEvent *event) +{ + const QEvent::Type type = event->type(); + + if (QEvent::Resize == type || QEvent::Move == type) + { + setGeometry(m_toggle->rect().adjusted(8, 8, -8, -8)); + updateOffset(); + } + return QWidget::eventFilter(obj, event); +} + +void QtMaterialToggleThumb::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) + + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush; + brush.setStyle(Qt::SolidPattern); + brush.setColor(m_toggle->isEnabled() ? m_thumbColor : Qt::white); + + painter.setBrush(brush); + painter.setPen(Qt::NoPen); + + int s; + QRectF r; + + if (Qt::Horizontal == m_toggle->orientation()) { + s = height()-10; + r = QRectF(5+m_offset, 5, s, s); + } else { + s = width()-10; + r = QRectF(5, 5+m_offset, s, s); + } + + painter.drawEllipse(r); + + if (!m_toggle->isEnabled()) { + brush.setColor(m_toggle->disabledColor()); + painter.setBrush(brush); + painter.drawEllipse(r); + } +} + +void QtMaterialToggleThumb::updateOffset() +{ + const QSize s(Qt::Horizontal == m_toggle->orientation() + ? size() : size().transposed()); + m_offset = m_shift*static_cast(s.width()-s.height()); + update(); +} + +/*! + * \class QtMaterialToggleTrack + * \internal + */ + +QtMaterialToggleTrack::QtMaterialToggleTrack(QtMaterialToggle *parent) + : QWidget(parent), + m_toggle(parent) +{ + Q_ASSERT(parent); + + parent->installEventFilter(this); +} + +QtMaterialToggleTrack::~QtMaterialToggleTrack() +{ +} + +void QtMaterialToggleTrack::setTrackColor(const QColor &color) +{ + m_trackColor = color; + update(); +} + +bool QtMaterialToggleTrack::eventFilter(QObject *obj, QEvent *event) +{ + const QEvent::Type type = event->type(); + + if (QEvent::Resize == type || QEvent::Move == type) { + setGeometry(m_toggle->rect()); + } + return QWidget::eventFilter(obj, event); +} + +void QtMaterialToggleTrack::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) + + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush; + if (m_toggle->isEnabled()) { + brush.setColor(m_trackColor); + painter.setOpacity(0.8); + } else { + brush.setColor(m_toggle->disabledColor()); + painter.setOpacity(0.6); + } + brush.setStyle(Qt::SolidPattern); + painter.setBrush(brush); + painter.setPen(Qt::NoPen); + + if (Qt::Horizontal == m_toggle->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); + } +} diff --git a/components/qtmaterialtoggle_internal.h b/components/qtmaterialtoggle_internal.h new file mode 100644 index 0000000..079e369 --- /dev/null +++ b/components/qtmaterialtoggle_internal.h @@ -0,0 +1,120 @@ +#ifndef QTMATERIALTOGGLE_INTERNAL_H +#define QTMATERIALTOGGLE_INTERNAL_H + +#include +#include "lib/qtmaterialrippleoverlay.h" + +class QtMaterialToggle; +class QtMaterialToggleThumb; +class QtMaterialToggleTrack; + +class QtMaterialToggleRippleOverlay : public QtMaterialRippleOverlay +{ + Q_OBJECT + +public: + QtMaterialToggleRippleOverlay(QtMaterialToggleThumb *thumb, + QtMaterialToggleTrack *track, + QtMaterialToggle *parent); + ~QtMaterialToggleRippleOverlay(); + +protected slots: + void addToggleRipple(); + +protected: + bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE; + QRect overlayGeometry() const Q_DECL_OVERRIDE; + +private: + Q_DISABLE_COPY(QtMaterialToggleRippleOverlay) + + QtMaterialToggle *const m_toggle; + QtMaterialToggleThumb *const m_thumb; + QtMaterialToggleTrack *const m_track; +}; + +class QtMaterialToggleThumb : public QWidget +{ + Q_OBJECT + + Q_PROPERTY(qreal shift WRITE setShift READ shift) + Q_PROPERTY(QColor thumbColor WRITE setThumbColor READ thumbColor) + +public: + QtMaterialToggleThumb(QtMaterialToggle *parent); + ~QtMaterialToggleThumb(); + + void setShift(qreal shift); + inline qreal shift() const; + + inline qreal offset() const; + + inline void setThumbColor(const QColor &color); + inline QColor thumbColor() const; + +protected: + bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE; + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + +private: + Q_DISABLE_COPY(QtMaterialToggleThumb) + + void updateOffset(); + + QtMaterialToggle *const m_toggle; + QColor m_thumbColor; + qreal m_shift; + qreal m_offset; +}; + +inline qreal QtMaterialToggleThumb::shift() const +{ + return m_shift; +} + +inline qreal QtMaterialToggleThumb::offset() const +{ + return m_offset; +} + +inline void QtMaterialToggleThumb::setThumbColor(const QColor &color) +{ + m_thumbColor = color; + update(); +} + +inline QColor QtMaterialToggleThumb::thumbColor() const +{ + return m_thumbColor; +} + +class QtMaterialToggleTrack : public QWidget +{ + Q_OBJECT + + Q_PROPERTY(QColor trackColor WRITE setTrackColor READ trackColor) + +public: + QtMaterialToggleTrack(QtMaterialToggle *parent); + ~QtMaterialToggleTrack(); + + void setTrackColor(const QColor &color); + inline QColor trackColor() const; + +protected: + bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE; + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + +private: + Q_DISABLE_COPY(QtMaterialToggleTrack) + + QtMaterialToggle *const m_toggle; + QColor m_trackColor; +}; + +inline QColor QtMaterialToggleTrack::trackColor() const +{ + return m_trackColor; +} + +#endif // QTMATERIALTOGGLE_INTERNAL_H diff --git a/components/qtmaterialtoggle_p.h b/components/qtmaterialtoggle_p.h new file mode 100644 index 0000000..f50d5b3 --- /dev/null +++ b/components/qtmaterialtoggle_p.h @@ -0,0 +1,41 @@ +#ifndef QTMATERIALTOGGLE_P_H +#define QTMATERIALTOGGLE_P_H + +#include + +class QStateMachine; +class QState; +class QColor; +class QtMaterialToggle; +class QtMaterialToggleTrack; +class QtMaterialToggleThumb; +class QtMaterialToggleRippleOverlay; + +class QtMaterialTogglePrivate +{ + Q_DISABLE_COPY(QtMaterialTogglePrivate) + Q_DECLARE_PUBLIC(QtMaterialToggle) + +public: + QtMaterialTogglePrivate(QtMaterialToggle *q); + ~QtMaterialTogglePrivate(); + + void init(); + void setupProperties(); + + QtMaterialToggle *const q_ptr; + QtMaterialToggleTrack *track; + QtMaterialToggleThumb *thumb; + QtMaterialToggleRippleOverlay *rippleOverlay; + QStateMachine *stateMachine; + QState *offState; + QState *onState; + Qt::Orientation orientation; + QColor disabledColor; + QColor activeColor; + QColor inactiveColor; + QColor trackColor; + bool useThemeColors; +}; + +#endif // QTMATERIALTOGGLE_P_H diff --git a/examples/examples.pro b/examples/examples.pro index b297a48..00daeb7 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -13,7 +13,8 @@ SOURCES = mainwindow.cpp \ circularprogresssettingseditor.cpp \ slidersettingseditor.cpp \ ../components/qtmaterialradiobutton.cpp \ - radiobuttonsettingseditor.cpp + radiobuttonsettingseditor.cpp \ + togglesettingseditor.cpp HEADERS = mainwindow.h \ avatarsettingseditor.h \ badgesettingseditor.h \ @@ -27,7 +28,8 @@ HEADERS = mainwindow.h \ slidersettingseditor.h \ ../components/qtmaterialradiobutton_p.h \ ../components/qtmaterialradiobutton.h \ - radiobuttonsettingseditor.h + radiobuttonsettingseditor.h \ + togglesettingseditor.h LIBS += ../components/libcomponents.a INCLUDEPATH += ../components/ TARGET = ../examples-exe @@ -45,4 +47,5 @@ FORMS += \ progresssettingsform.ui \ circularprogresssettingsform.ui \ slidersettingsform.ui \ - radiobuttonsettingsform.ui + radiobuttonsettingsform.ui \ + togglesettingsform.ui diff --git a/examples/mainwindow.cpp b/examples/mainwindow.cpp index f61fb2c..78baccc 100644 --- a/examples/mainwindow.cpp +++ b/examples/mainwindow.cpp @@ -13,6 +13,7 @@ #include "circularprogresssettingseditor.h" #include "slidersettingseditor.h" #include "radiobuttonsettingseditor.h" +#include "togglesettingseditor.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -43,6 +44,7 @@ MainWindow::MainWindow(QWidget *parent) CircularProgressSettingsEditor *circularProgress = new CircularProgressSettingsEditor; SliderSettingsEditor *slider = new SliderSettingsEditor; RadioButtonSettingsEditor *radioButton = new RadioButtonSettingsEditor; + ToggleSettingsEditor *toggle = new ToggleSettingsEditor; stack->addWidget(avatar); stack->addWidget(badge); @@ -55,6 +57,7 @@ MainWindow::MainWindow(QWidget *parent) stack->addWidget(radioButton); stack->addWidget(raisedButton); stack->addWidget(slider); + stack->addWidget(toggle); list->addItem("Avatar"); list->addItem("Badge"); @@ -67,6 +70,7 @@ MainWindow::MainWindow(QWidget *parent) list->addItem("Radio Button"); list->addItem("Raised Button"); list->addItem("Slider"); + list->addItem("Toggle"); list->setCurrentRow(0); diff --git a/examples/togglesettingseditor.cpp b/examples/togglesettingseditor.cpp new file mode 100644 index 0000000..3952f8d --- /dev/null +++ b/examples/togglesettingseditor.cpp @@ -0,0 +1,107 @@ +#include "togglesettingseditor.h" +#include +#include "qtmaterialtoggle.h" + +ToggleSettingsEditor::ToggleSettingsEditor(QWidget *parent) + : QWidget(parent), + ui(new Ui::ToggleSettingsForm), + m_toggle(new QtMaterialToggle) +{ + QVBoxLayout *layout = new QVBoxLayout; + setLayout(layout); + + QWidget *widget = new QWidget; + layout->addWidget(widget); + + QWidget *canvas = new QWidget; + canvas->setStyleSheet("QWidget { background: white; }"); + layout->addWidget(canvas); + + ui->setupUi(widget); + layout->setContentsMargins(20, 20, 20, 20); + + m_toggle->setOrientation(Qt::Vertical); + + layout = new QVBoxLayout; + canvas->setLayout(layout); + layout->addWidget(m_toggle); + layout->setAlignment(m_toggle, Qt::AlignCenter); + + setupForm(); + + connect(ui->disabledCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateWidget())); + connect(ui->checkedCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateWidget())); + connect(ui->orientationComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateWidget())); + connect(ui->useThemeColorsCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateWidget())); + connect(ui->disabledColorToolButton, SIGNAL(pressed()), this, SLOT(selectColor())); + connect(ui->activeColorToolButton, SIGNAL(pressed()), this, SLOT(selectColor())); + connect(ui->inactiveColorToolButton, SIGNAL(pressed()), this, SLOT(selectColor())); + connect(ui->trackColorToolButton, SIGNAL(pressed()), this, SLOT(selectColor())); + + connect(m_toggle, SIGNAL(toggled(bool)), this, SLOT(setupForm())); +} + +ToggleSettingsEditor::~ToggleSettingsEditor() +{ +} + +void ToggleSettingsEditor::setupForm() +{ + switch (m_toggle->orientation()) + { + case Qt::Horizontal: + ui->orientationComboBox->setCurrentIndex(0); + break; + case Qt::Vertical: + ui->orientationComboBox->setCurrentIndex(1); + break; + default: + break; + } + + ui->disabledCheckBox->setChecked(!m_toggle->isEnabled()); + ui->checkedCheckBox->setChecked(m_toggle->isChecked()); + ui->useThemeColorsCheckBox->setChecked(m_toggle->useThemeColors()); +} + +void ToggleSettingsEditor::updateWidget() +{ + switch (ui->orientationComboBox->currentIndex()) + { + case 0: + m_toggle->setOrientation(Qt::Horizontal); + break; + case 1: + m_toggle->setOrientation(Qt::Vertical); + break; + default: + break; + } + + m_toggle->setDisabled(ui->disabledCheckBox->isChecked()); + m_toggle->setChecked(ui->checkedCheckBox->isChecked()); + m_toggle->setUseThemeColors(ui->useThemeColorsCheckBox->isChecked()); +} + +void ToggleSettingsEditor::selectColor() +{ + QColorDialog dialog; + if (dialog.exec()) { + QColor color = dialog.selectedColor(); + QString senderName = sender()->objectName(); + if ("disabledColorToolButton" == senderName) { + m_toggle->setDisabledColor(color); + ui->disabledColorLineEdit->setText(color.name(QColor::HexRgb)); + } else if ("activeColorToolButton" == senderName) { + m_toggle->setActiveColor(color); + ui->activeColorLineEdit->setText(color.name(QColor::HexRgb)); + } else if ("inactiveColorToolButton" == senderName) { + m_toggle->setInactiveColor(color); + ui->inactiveColorLineEdit->setText(color.name(QColor::HexRgb)); + } else if ("trackColorToolButton" == senderName) { + m_toggle->setTrackColor(color); + ui->trackColorLineEdit->setText(color.name(QColor::HexRgb)); + } + } + setupForm(); +} diff --git a/examples/togglesettingseditor.h b/examples/togglesettingseditor.h new file mode 100644 index 0000000..a74553e --- /dev/null +++ b/examples/togglesettingseditor.h @@ -0,0 +1,27 @@ +#ifndef TOGGLESETTINGSEDITOR_H +#define TOGGLESETTINGSEDITOR_H + +#include +#include "ui_togglesettingsform.h" + +class QtMaterialToggle; + +class ToggleSettingsEditor : public QWidget +{ + Q_OBJECT + +public: + explicit ToggleSettingsEditor(QWidget *parent = 0); + ~ToggleSettingsEditor(); + +protected slots: + void setupForm(); + void updateWidget(); + void selectColor(); + +private: + Ui::ToggleSettingsForm *const ui; + QtMaterialToggle *const m_toggle; +}; + +#endif // TOGGLESETTINGSEDITOR_H diff --git a/examples/togglesettingsform.ui b/examples/togglesettingsform.ui new file mode 100644 index 0000000..a6edbb7 --- /dev/null +++ b/examples/togglesettingsform.ui @@ -0,0 +1,166 @@ + + + ToggleSettingsForm + + + + 0 + 0 + 482 + 427 + + + + Form + + + + + 0 + 0 + 441 + 341 + + + + + + + Disabled + + + + + + + + + + Checked + + + + + + + + + + Orientation + + + + + + + + Horizontal + + + + + Vertical + + + + + + + + Active color + + + + + + + Inactive color + + + + + + + Disabled color + + + + + + + Track color + + + + + + + + + + + + ... + + + + + + + + + + + + + + ... + + + + + + + + + + + + + + ... + + + + + + + + + + + + + + ... + + + + + + + + + Use theme colors + + + + + + + + + + + +