overlaybutton.cpp 2.95 KB
Newer Older
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
/**************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux                                *
* Author: Yang Wang <yang.wang@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/>.   *
**************************************************************************/
#include "overlaybutton.h"

#include "utils.h"

#include <QEvent>

OverlayButton::OverlayButton(QWidget *parent)
    : QPushButton(parent)
{
27 28
    connect(this, SIGNAL(toggled(bool)),
            this, SLOT(slotToggled(bool)));
29 30 31
}

OverlayButton::~OverlayButton()
32
{}
33 34 35 36

void
OverlayButton::setOriginPix(QPixmap originPixPath)
{
37 38
    normalPixmap_ = originPixPath;
    setIcon(normalPixmap_);
39 40 41 42 43
}

QPixmap
OverlayButton::getOriginPix() const
{
44
    return normalPixmap_;
45 46 47 48 49
}

void
OverlayButton::setCheckedPix(QPixmap checkedPixPath)
{
50
    checkedPixmap_ = checkedPixPath;
51 52 53 54 55
}

QPixmap
OverlayButton::getCheckedPix() const
{
56
    return checkedPixmap_;
57 58 59 60 61 62
}

void
OverlayButton::setTintColor(QColor tint_color)
{
    tintColor_ = tint_color;
63 64
    tintNormalPixmap_ = Utils::generateTintedPixmap(normalPixmap_, tintColor_);
    tintCheckedPixmap_ = Utils::generateTintedPixmap(checkedPixmap_, tintColor_);
65 66 67 68 69 70 71 72 73
}

QColor
OverlayButton::getTintColor() const
{
    return tintColor_;
}

void
74
OverlayButton::updateIcon()
75
{
76 77 78 79
    if (isSelected_) {
        setIcon(isHovered_ ? tintCheckedPixmap_ : checkedPixmap_);
    } else {
        setIcon(isHovered_ ? tintNormalPixmap_ : normalPixmap_);
80 81 82 83
    }
}

void
84
OverlayButton::enterEvent(QEvent* event)
85
{
86 87 88
    Q_UNUSED(event);
    isHovered_ = true;
    updateIcon();
89 90
}

91 92
void
OverlayButton::leaveEvent(QEvent* event)
93
{
94 95 96
    Q_UNUSED(event);
    isHovered_ = false;
    updateIcon();
97 98 99
}

void
100
OverlayButton::slotToggled(bool checked)
101 102
{
    isSelected_ = checked;
103
    updateIcon();
104 105 106 107 108
}

void
OverlayButton::resetToOriginal()
{
109
    setIcon(normalPixmap_);
110 111 112 113 114 115 116
}

void
OverlayButton::setOverlayButtonChecked(bool checked)
{
    Utils::whileBlocking(this)->setChecked(checked);
    isSelected_ = checked;
117
    updateIcon();
118
}