#include "qmltool.h"
#include <QProcess>
QmlTool* QmlTool::instance = nullptr;

QmlTool::QmlTool(QObject *parent) : QObject(parent)
{
}

QmlTool::~QmlTool()
{
}

QmlTool* QmlTool::getInstance()
{
    if (!instance) {
        instance = new QmlTool();
    }
    return instance;
}


QString QmlTool::readFile(const QString& filePath)
{
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return QString("null");
    }

    QTextStream in(&file);
    in.setCodec("UTF-8");
    QString content = in.readAll();
    file.close();
    return content;
}

bool QmlTool::wirteFile(const QString& filePath,const QString& content){
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qDebug() << "无法打开文件: " << filePath;
        return false;
    }

    QTextStream out(&file);
    out << content;

    file.close();
    return true;
}

void QmlTool::openFile(const QString &fileName) {
    QFile file(fileName);
    if (file.exists()) {
        QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
    }
}


void QmlTool::runExe(const QString &pr){
    // 启动另一个程序
    QProcess process;
    process.startDetached(pr);
    exit(0);
}
