#ifndef DESKTOPBACKGROUND_H
#define DESKTOPBACKGROUND_H

#include <QQuickItem>
#include <QQuickPaintedItem>
#include <QImage>

/*!
 * \brief   背景绘制的Item，导出由QML使用进行背景的绘制
 * \details 方便前端进行背景绘制，并提供虚化、背景图片拉伸的能力
 */
class DesktopBackground : public QQuickPaintedItem
{
    Q_OBJECT

    Q_PROPERTY(qreal blurAlpha READ blurAlpha WRITE setBlurAlpha NOTIFY blurAlphaChanged)

public:
    explicit DesktopBackground(QQuickPaintedItem *parent = nullptr);
    ~DesktopBackground();

    void paint(QPainter *painter);

    qreal blurAlpha(){
        return m_blurAlpha;
    }

    void setBlurAlpha(qreal alpha);

    QRect scaledRect(const QImage &image);

signals:
    void loadPicture(const QString &fileName);
    void blurAlphaChanged();

private slots:
    void onBlurAlphaChanged();

private:
    void init();
    QString getBackgroundFile();
    QString getBackgroundFillStyle();
    double getSystemScalingFactor();
    void updateBlurBackground();

    int edge(int x, int i, int w);
    float gaussFunc1D(int x, float sigma);
    void getKernal(int radius, float* weight);
    void bluredImage(int blur_radius, QImage &blurImage);

    QString m_backgroundFile;
    QString m_backgroundFillStyle = "scaled";
    double m_scalingFactor = 1.5;
    static QImage m_blurBackground;
    static QImage m_nonBlurBackground;
    qreal m_blurAlpha;
    static quint32 m_blurRadius;
};

#endif // DESKTOPBACKGROUND_H
