/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "systembutton.h"
#include <QDebug>
namespace KylinRubbishClear
{

SystemButton::SystemButton(QWidget *parent) : QPushButton(parent)
{
    status = NORMAL;
    m_mousePressed = false;
    this->setAttribute(Qt::WA_TranslucentBackground);
}


void SystemButton::loadPixmap(QString pic_name)
{
    pixmap = QPixmap(pic_name);
    btn_width = pixmap.width() / 3;
    btn_height = pixmap.height();
    this->setFixedSize(btn_width, btn_height);
}

void SystemButton::enterEvent(QEvent *)
{
    status = ENTER;
    update();
}

void SystemButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_mousePressed = true;
        status = PRESS;
        update();
    }
}

void SystemButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_mousePressed && this->rect().contains(event->pos())) {
        m_mousePressed = false;
        status = ENTER;
        update();
        Q_EMIT clicked();
    }
}

void SystemButton::leaveEvent(QEvent *)
{
    status = NORMAL;
    update();
}

void SystemButton::paintEvent(QPaintEvent *)
{
    QPainter painter;
    //    painter.setBrush(QBrush(pixmap.copy(btn_width * status, 0, btn_width, btn_height)));
    //    QPainterPath path;
    //    path.setFillRule(Qt::WindingFill);
    //    path.addRoundedRect(0,0,this->width(),this->height(),5,5);

    painter.begin(this);
    painter.drawPixmap(this->rect(), pixmap.copy(btn_width * status, 0, btn_width, btn_height));
    //    painter.drawPath(path);
    painter.end();
}

} // namespace KylinRubbishClear
