#include "basewidget.h"

#include <QPainter>
#include <QStyleOption>
#include <QDebug>
#include <QApplication>
#include <QScreen>
#include <QPainterPath>
#include <QLabel>

#define THEME_UKFD_TRANS "org.ukui.control-center.personalise"

baseWidget::baseWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint |Qt::X11BypassWindowManagerHint); //Qt::ToolTip设置新窗口是一个提示窗口，没有标题栏和窗口边框,设置后独立与窗口存在
    initOpacityGSettings();
    installEventFilter(this);
    QLabel *tiplabel = new QLabel(this);
    tiplabel->move(16,16);
    tiplabel->setFixedWidth(200);
    tiplabel->setText(tr("removable storage device"));
    tiplabel->show();
    setAttribute(Qt::WA_AlwaysShowToolTips);
    setAttribute(Qt::WA_TranslucentBackground);
    setFocusPolicy(Qt::NoFocus);
    setProperty("useSystemStyleBlur", true);
    mainWindowVboxLayout = new QVBoxLayout();
    mainWindowVboxLayout->setContentsMargins(16,52,16,16);
    mainWindowVboxLayout->setSpacing(5);
    this->setLayout(mainWindowVboxLayout);
}

baseWidget::~baseWidget()
{
    if (m_gsTransOpacity) {
        delete m_gsTransOpacity;
        m_gsTransOpacity = nullptr;
    }
}

void baseWidget::initOpacityGSettings()
{
    const QByteArray idtrans(THEME_UKFD_TRANS);
    if(QGSettings::isSchemaInstalled(idtrans)) {
        m_gsTransOpacity = new QGSettings(idtrans);
    }

    if (!m_gsTransOpacity) {
        m_curTransOpacity = 1;
        return;
    }

    connect(m_gsTransOpacity, &QGSettings::changed, this, [=](const QString &key) {
        if (key == "transparency") {
            QStringList keys = m_gsTransOpacity->keys();
            if (keys.contains("transparency")) {
                m_curTransOpacity = m_gsTransOpacity->get("transparency").toString().toDouble();
                repaint();
            }
        }
    });

    QStringList keys = m_gsTransOpacity->keys();
    if(keys.contains("transparency")) {
        m_curTransOpacity = m_gsTransOpacity->get("transparency").toString().toDouble();
    }
}

void baseWidget::paintEvent(QPaintEvent * event)
{
    QPainterPath path;

    QPainter painter(this);
    painter.setOpacity(m_curTransOpacity);  //设置不透明度
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setClipping(true);
    painter.setPen(Qt::transparent);

    path.addRoundedRect(this->rect(), 12, 12);
    path.setFillRule(Qt::WindingFill);   //填充规则
    painter.setBrush(this->palette().color(QPalette::Base));
    painter.setPen(Qt::transparent);

    painter.drawPath(path); //绘制底色
    //QFrame::paintEvent(event);
}

bool baseWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == this)
    {
        if(event->type() == QEvent::Enter) {
           qDebug()<<"鼠标进入";
           return false;

        } else if (event->type() == QEvent::WindowDeactivate)  {

            qDebug()<<"激活外部窗口";
            this->hide();
            return true;
        } else if (event->type() == QEvent::ActivationChange) {
            qDebug()<<"焦点变更";
            //this->hide();
            return false;
        }
    }

    if (!isActiveWindow())
    {
        activateWindow();
    }

    return false;


}

