#include "AllHead.h"

QSettings* getQSetting()
{

    QString fileName = QCoreApplication::applicationDirPath() + "/Config.ini";
    QSettings* setting = new QSettings(fileName, QSettings::IniFormat);
    setting->setIniCodec(QTextCodec::codecForName("UTF-8"));

    if (QFile::exists(fileName))
    {
        return setting;
    }
    else {// 文件不存在，写入配置项，生成配置文件
        setting->setValue("application", u8"微波测量仿真分系统");
        setting->sync();
    }

	return setting;
}

QString getOpenFilePath(QWidget* parent, const QString& caption, const QString& filter)
{
    QSettings* setting = getQSetting();
    QString curpath = u8".";
    if (setting->contains("FileSelectPath")) {
        curpath = setting->value("FileSelectPath").toString();
    }
    else {

    }

    QString filepath = QFileDialog::getOpenFileName(  parent,caption, curpath, filter);
    if (!filepath.isEmpty()&&QFile::exists(filepath)) {
        setting->setValue("FileSelectPath", getParantFromPath(filepath));
        setting->sync();
    }
	return filepath;
}

QString getSaveFilePath(QWidget* parent, const QString& caption, const QString& filter)
{
    QSettings* setting = getQSetting();
    QString curpath = u8".";
    if (setting->contains("FileSelectPath")) {
        curpath = setting->value("FileSelectPath").toString();
    }
    else {

    }

    QString filepath = QFileDialog::getSaveFileName(parent, caption, curpath, filter);
    if (!filepath.isEmpty() && isExists(getParantFromPath(filepath))) {
        setting->setValue("FileSelectPath", getParantFromPath(filepath));
        setting->sync();
    }
    return filepath;
}
