toggleswitch.cpp 5.56 KB
Newer Older
Andreas Traczyk's avatar
Andreas Traczyk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/**************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux                                *
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>          *
*                                                                         *
* This program is free software; you can redistribute it and/or modify    *
* it under the terms of the GNU General Public License as published by    *
* the Free Software Foundation; either version 3 of the License, or       *
* (at your option) any later version.                                     *
*                                                                         *
* This program is distributed in the hope that it will be useful,         *
* but WITHOUT ANY WARRANTY; without even the implied warranty of          *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
* GNU General Public License for more details.                            *
*                                                                         *
* You should have received a copy of the GNU General Public License       *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/

// Based on: https://stackoverflow.com/questions/14780517/toggle-switch-in-qt

#include "toggleswitch.h"
#include "ringthemeutils.h"

ToggleSwitch::ToggleSwitch(QWidget *parent)
    : QAbstractButton(parent),
    width_(50),
    opacity_(0.000),
    switch_(false),
    margin_(3),
    thumb_("#d5d5d5"),
    textMargin_(24),
    anim_(new QPropertyAnimation(this, "offset", this)),
    style_(SwitchStyle::UWP)
{
    setMouseTracking(true);
    setBrush(QColor(43, 75, 126));
    setOffset(qreal(switch_));

    if (style_ == SwitchStyle::UWP) {
        pinRadius_ = height() * 0.5 - margin_;
        knobRadius_ = pinRadius_ * 0.8;
        height_ = 2 * pinRadius_;
        pinWidth_ = height_ * 2;
        width_ = pinWidth_;
    } else {
        knobRadius_ = height() * 0.5 - margin_;
        pinRadius_ = knobRadius_ * 0.6;
        height_ = 2 * knobRadius_;
        pinWidth_ = height_;
        width_ = pinWidth_ + 2 * knobRadius_;
    }
}

void ToggleSwitch::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    QPainter p(this);
    //p.setOpacity(isEnabled() ? (switch_ ? 0.5 : 0.38) : 0.12);
    //p.setOpacity(isEnabled() ? 1.0 : 0.12);
    p.setOpacity(isEnabled() ? (switch_ ? 1.0 : 0.5) : 0.12);
    p.setRenderHint(QPainter::Antialiasing, true);

    if (style_ == SwitchStyle::UWP) {
        // pin
        QPainterPath path;
        p.setPen(switch_ ? Qt::NoPen : QPen(Qt::black, 2));
        path.addRoundedRect(QRect(1,
                                  margin_,
                                  pinWidth_,
                                  2 * pinRadius_),
                            pinRadius_, pinRadius_);
        p.fillPath(path, (switch_ ? brush() : Qt::white));
        p.drawPath(path);

        // knob
        p.setPen(Qt::NoPen);
        p.setBrush(switch_ ? Qt::white : Qt::black);
        p.drawEllipse(QPoint(1 + pinRadius_ + offset() * pinWidth_ * 0.5,
                             margin_ + pinRadius_),
                      knobRadius_, knobRadius_);
    } else {
        auto pinch = pinWidth_ / 8;
        p.setPen(Qt::NoPen);
        // pin
        p.setBrush(isEnabled() ? (switch_ ? brush() : Qt::black) : Qt::black);
        p.drawRoundedRect(QRect(1 + knobRadius_,
                                margin_ + knobRadius_ - pinRadius_,
                                pinWidth_,
                                2 * pinRadius_),
                          pinRadius_, pinRadius_);

        // knob
        p.setOpacity(1.0);
        p.setBrush(isEnabled() ? thumb_ : QColor("#bdbdbd"));
        p.drawEllipse(QPoint(1 + knobRadius_ + pinch + offset() * (pinWidth_ - pinch * 2),
                             margin_ + knobRadius_),
                      knobRadius_, knobRadius_);
    }
    p.setOpacity(1.0);
    p.setPen(isEnabled() ? Qt::black : Qt::gray);
    p.drawText(width_ + textMargin_, height_ - margin_, text_);
}

void ToggleSwitch::mouseReleaseEvent(QMouseEvent *e)
{
    QRect activeArea(rect());
    auto textWidth = QFontMetrics(font()).size(Qt::TextSingleLine, text_).width();
    activeArea.setWidth(width_ + textMargin_ + textWidth);
    if (e->button() & Qt::LeftButton
        && activeArea.contains(e->pos())) {
        switch_ = switch_ ? false : true;
        thumb_ = switch_ ? brush_ : QBrush("#d5d5d5");
        if (switch_) {
            anim_->setStartValue(0.0);
            anim_->setEndValue(1.0);
            anim_->setDuration(100);
            anim_->start();
        } else {
            anim_->setStartValue(offset());
            anim_->setEndValue(0.0);
            anim_->setDuration(100);
            anim_->start();
        }
        emit clicked(switch_);
    }
}

void ToggleSwitch::mouseMoveEvent(QMouseEvent *e)
{
    QRect activeArea(rect());
    auto textWidth = QFontMetrics(font()).size(Qt::TextSingleLine, text_).width();
    activeArea.setWidth(width_ + textMargin_ + textWidth);
    if (activeArea.contains(e->pos())) {
        setCursor(Qt::PointingHandCursor);
    } else {
        setCursor(Qt::ArrowCursor);
    }
    QAbstractButton::mouseMoveEvent(e);
}

QSize ToggleSwitch::sizeHint() const
{
    auto textWidth = QFontMetrics(font()).size(Qt::TextSingleLine, text_).width();
    return QSize(width_ + textMargin_ + textWidth, height_ + 2 * margin_);
}

void ToggleSwitch::setChecked(bool state)
{
    switch_ = state;
    setOffset(qreal(switch_));
    thumb_ = switch_ ? brush_ : QBrush("#d5d5d5");
}