/***************************************************************************
 **
 **  Copyright (C) 2018-2023 wangjianjun <wangjianjun1018@qq.com>
 **  Contact: https://blog.csdn.net/wocanimei007?type=blog
 **
 **
****************************************************************************/
#include "fancywindow.h"
#include "fancybar.h"
#include "fancyscreen.h"
#include "frameless_p.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QStatusBar>
#include <QSettings>
#include <QPainter>



class FancyWindowPrivate
{
public:
    FancyWindowPrivate();

    FancyBar *fancyBar;
};


FancyWindowPrivate::FancyWindowPrivate()
{
    fancyBar = nullptr;
}

FancyWindow::FancyWindow(QWidget *parent)
    : QMainWindow(parent), d(new FancyWindowPrivate())
{
    QMainWindow::setWindowFlags(Qt::FramelessWindowHint
                   | Qt::WindowSystemMenuHint
                   | Qt::WindowMinimizeButtonHint
                   | Qt::WindowMaximizeButtonHint
                   | Qt::Window
                  );
    d->fancyBar = new FancyBar(this);
    connect(d->fancyBar, SIGNAL(maximizationChanged(bool)), this, SIGNAL(resizable(bool)));
    setMenuWidget(d->fancyBar);
    setMouseTracking(true);
    QRect geom = FancyScreen::normalRect();
    resize(geom.width(), geom.height());
    raise();
    activateWindow();
}

FancyWindow::~FancyWindow()
{
    delete d;
}

FancyBar *FancyWindow::fancyBar() const
{
    return d->fancyBar;
}

void FancyWindow::setFixedSize(const QSize &s)
{
    setFixedSize(s.width(), s.height());
}

void FancyWindow::setFixedSize(int w, int h)
{
    d->fancyBar->setWidgetResizable(false);
    QWidget::setFixedSize(w, h);
}

void FancyWindow::setFixedWidth(int w)
{
    setFixedSize(w, this->height());
}

void FancyWindow::setFixedHeight(int h)
{
    setFixedSize(this->width(), h);
}

void FancyWindow::setWindowFlags(Qt::WindowFlags type)
{
    QMainWindow::setWindowFlags(type);
    d->fancyBar->updateWidgetFlags();
}

//void FancyWindow::paintEvent(QPaintEvent *event)
//{
//    Q_UNUSED(event);
//    QSettings settings(FramelessPrivate::g_settingsOrganization,
//                       FramelessPrivate::g_settingsApplication);
//    QString skinName = settings.value("skin").toString();

//    if (skinName.isEmpty()) {
//        skinName = QString(":/skin/default");
//    }

//    QPainter painter(this);
//    painter.setPen(Qt::NoPen);
//    painter.setBrush(Qt::gray);
//    painter.drawPixmap(QRect(0, 0, this->width(), this->height()),
//                       QPixmap(skinName));
//}


