/*
 * Copyright (C) 2025 KylinSoftCo., 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, 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 <http://www.gnu.org/licenses/>.
 *
 **/

#ifndef QT_DESKTOP_COMPAT_H
#define QT_DESKTOP_COMPAT_H

#include <QApplication>
#include <QRect>

// QDesktopWidget 兼容性处理
#if QT_VERSION_MAJOR == 6
    #include <QScreen>
    
    // Qt6中QDesktopWidget已被移除，使用QScreen替代
    inline QScreen* getDesktopWidget() {
        return QApplication::primaryScreen();
    }
    
    inline QRect getDesktopGeometry() {
        QScreen* screen = QApplication::primaryScreen();
        return screen ? screen->geometry() : QRect();
    }
    
    inline int getScreenCount() {
        return QApplication::screens().count();
    }
    
    // 连接屏幕变化信号的兼容性函数
    template<typename Receiver, typename Slot>
    void connectScreenSignals(Receiver* receiver, Slot slot) {
        QApplication* app = qApp;
        QObject::connect(app, &QApplication::primaryScreenChanged, receiver, slot);
        QObject::connect(app, &QApplication::screenAdded, receiver, slot);
        QObject::connect(app, &QApplication::screenRemoved, receiver, slot);
    }
    
    template<typename Receiver, typename Slot>
    void disconnectScreenSignals(Receiver* receiver, Slot slot) {
        QApplication* app = qApp;
        QObject::disconnect(app, &QApplication::primaryScreenChanged, receiver, slot);
        QObject::disconnect(app, &QApplication::screenAdded, receiver, slot);
        QObject::disconnect(app, &QApplication::screenRemoved, receiver, slot);
    }
#else
    #include <QDesktopWidget>
    
    inline QDesktopWidget* getDesktopWidget() {
        return QApplication::desktop();
    }
    
    inline QRect getDesktopGeometry() {
        QDesktopWidget* desktop = QApplication::desktop();
        return desktop ? desktop->geometry() : QRect();
    }
    
    inline int getScreenCount() {
        QDesktopWidget* desktop = QApplication::desktop();
        return desktop ? desktop->screenCount() : 0;
    }
    
    // 连接屏幕变化信号的兼容性函数
    template<typename Receiver, typename Slot>
    void connectScreenSignals(Receiver* receiver, Slot slot) {
        QDesktopWidget* desktop = QApplication::desktop();
        if (desktop) {
            QObject::connect(desktop, SIGNAL(resized(int)), receiver, slot);
            QObject::connect(desktop, SIGNAL(workAreaResized(int)), receiver, slot);
            QObject::connect(desktop, SIGNAL(primaryScreenChanged(QScreen*)), receiver, slot);
            QObject::connect(desktop, SIGNAL(screenCountChanged(int)), receiver, slot);
        }
    }
    
    template<typename Receiver, typename Slot>
    void disconnectScreenSignals(Receiver* receiver, Slot slot) {
        QDesktopWidget* desktop = QApplication::desktop();
        if (desktop) {
            QObject::disconnect(desktop, SIGNAL(resized(int)), receiver, slot);
            QObject::disconnect(desktop, SIGNAL(workAreaResized(int)), receiver, slot);
            QObject::disconnect(desktop, SIGNAL(primaryScreenChanged(QScreen*)), receiver, slot);
            QObject::disconnect(desktop, SIGNAL(screenCountChanged(int)), receiver, slot);
        }
    }
#endif

#endif // QT_DESKTOP_COMPAT_H
