/***************************************************************************
 **
 **  Copyright (C) 2018-2023 wangjianjun <wangjianjun1018@qq.com>
 **  Contact: https://blog.csdn.net/wocanimei007?type=blog
 **
 **
****************************************************************************/
#include "fancynavbutton.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>
#include <QAction>



FancyNavButton::FancyNavButton(QWidget *parent)
    : QToolButton(parent)
{
    setToolButtonStyle(Qt::ToolButtonTextOnly);
    setAutoRaise(false);
    setArrowType(Qt::NoArrow);
    setFocusPolicy(Qt::NoFocus);
    setIconSize(QSize(16, 16));
    m_isMouseOver = false;
    m_isMousePress = false;
    m_type = Button;
    m_hoverColor = QColor(255, 255, 255, 50);
    m_pressColor = QColor(0, 0, 0, 100);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setStyleSheet("QToolButton{background-color: transparent;border:none;color: white;}");
}

void FancyNavButton::select(bool selected)
{
    m_isMousePress = selected;
    if (!selected) {
        this->m_isMouseOver = false;
    }
    update();
}

void FancyNavButton::setTextColor(const QColor &color)
{
    setStyleSheet(QString("QToolButton{background-color: transparent;border:none;color: rgba(%1,%2,%3,%4);}")
                  .arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha())
                 );
}

void FancyNavButton::enterEvent(QEvent *event)
{
    Q_UNUSED(event);

    if (!isEnabled()) {
        return;
    }
    m_isMouseOver = true;
}

void FancyNavButton::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    m_isMouseOver = false;
}

void FancyNavButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (m_type == Action) {
            m_isMousePress = true;
            update();
        } else {
            if (this->isCheckable()) {
                if (this->isChecked()) {
                    m_isMousePress = false;
                } else {
                    m_isMousePress = true;
                }

                this->setChecked(m_isMousePress);
                update();
            } else {
                m_isMousePress = true;
                emit clicked();
            }
        }
    }
}

void FancyNavButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        if (m_type == Action) {
            m_isMousePress = false; // add for action
            update();

            if (this->rect().contains(event->pos())) { // don't emit when move out
                emit clicked();
            }
        }
    }
}

void FancyNavButton::paintEvent(QPaintEvent *event)
{
    if (m_isMousePress) {
        painterInfo(m_pressColor);
    } else if (m_isMouseOver) {
        painterInfo(m_hoverColor);
    }

    QToolButton::paintEvent(event);
}

void FancyNavButton::painterInfo(QColor &color)
{
    QPainter painter(this);
    QPen pen(Qt::NoBrush, 1);
    painter.setPen(pen);
    painter.setBrush(color);
    painter.drawRect(rect());
}
