deep-wine-runner/VM-source/qemusetting.cpp

131 lines
4.3 KiB
C++
Raw Normal View History

2023-04-15 19:14:30 +08:00
#include "qemusetting.h"
#include "ui_qemusetting.h"
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
2023-04-16 22:06:38 +08:00
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include "infoutils.h"
#include <QDir>
#include <sys/sysinfo.h>
2023-04-22 21:30:30 +08:00
#include <QDebug>
2023-04-15 19:14:30 +08:00
QemuSetting::QemuSetting(QWidget *parent) :
2023-04-16 22:06:38 +08:00
QWidget(parent),
2023-04-15 19:14:30 +08:00
ui(new Ui::QemuSetting)
{
ui->setupUi(this);
2023-04-19 21:55:21 +08:00
// 判断是否安装了 Wine 运行器
ui->getrunner->setHidden(QFile::exists(QCoreApplication::applicationDirPath() + "/../mainwindow.py"));
2023-04-16 22:06:38 +08:00
// 设置变量
if(QFile::exists(QDir::homePath() + "/.config/deepin-wine-runner/QemuSetting.json")){
// 读取配置文件
QFile file(QDir::homePath() + "/.config/deepin-wine-runner/QemuSetting.json");
file.open(QIODevice::ReadOnly);
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &error);
file.close();
if(error.error != QJsonParseError::NoError){
QMessageBox::critical(this, "错误", error.errorString());
}
else {
QJsonObject object = document.object();
qDebug() << QJsonDocument(object).toJson();
ui->enableKvm->setChecked(object.value("EnableKVM").toBool());
ui->enableRdp->setChecked(object.value("EnableRDP").toBool());
ui->enableVnc->setChecked(object.value("EnableVNC").toBool());
ui->enableSound->setChecked(object.value("EnableSound").toBool());
ui->memoryNumber->setValue(object.value("Memory").toInt());
ui->vncNumber->setValue(object.value("VNC").toInt());
ui->cpuNumber->setValue(object.value("CPU").toInt());
}
}
else {
// 默认
this->SetDefaultValue();
}
}
void QemuSetting::SetDefaultValue(){
long memory = 0;
long memoryAll = 0;
long swap = 0;
long swapAll = 0;
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
ui->memoryNumber->setValue(memoryAll / 3 / 1024);
ui->vncNumber->setValue(5);
ui->cpuNumber->setValue(get_nprocs());
ui->enableKvm->setChecked(true);
ui->enableRdp->setChecked(true);
ui->enableVnc->setChecked(true);
ui->enableSound->setChecked(true);
2023-04-15 19:14:30 +08:00
}
QemuSetting::~QemuSetting()
{
delete ui;
}
void QemuSetting::on_buttonBox_accepted()
{
QMessageBox::information(NULL, "", "");
acceptDrops();
}
void QemuSetting::on_save_clicked()
{
2023-04-16 22:06:38 +08:00
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson("{}", &error);
if(error.error != QJsonParseError::NoError){
QMessageBox::critical(this, "错误", error.errorString());
return;
}
QJsonObject object = document.object();
//object.insert("1", QJsonValue("aa"));
object.insert("EnableKVM", ui->enableKvm->isChecked());
object.insert("EnableRDP", ui->enableRdp->isChecked());
object.insert("EnableVNC", ui->enableVnc->isChecked());
object.insert("EnableSound", ui->enableSound->isChecked());
object.insert("Memory", ui->memoryNumber->value());
object.insert("VNC", ui->vncNumber->value());
object.insert("CPU", ui->cpuNumber->value());
qDebug() << QJsonDocument(object).toJson();
2023-04-19 21:02:24 +08:00
// 读取配置文件
2023-04-19 21:55:21 +08:00
QDir dir(QDir::homePath() + "/.config/deepin-wine-runner/");
if(!dir.exists()){
dir.mkpath(QDir::homePath() + "/.config/deepin-wine-runner/");
}
2023-04-19 21:02:24 +08:00
QFile file(QDir::homePath() + "/.config/deepin-wine-runner/QemuSetting.json");
file.open(QIODevice::WriteOnly);
file.write(QJsonDocument(object).toJson());
file.close();
QMessageBox::information(this, "提示", "保存完成!");
2023-04-15 19:14:30 +08:00
}
void QemuSetting::on_cancel_clicked()
{
2023-04-16 22:06:38 +08:00
delete this;
2023-04-15 19:14:30 +08:00
}
void QemuSetting::on_getrunner_clicked()
{
QDesktopServices::openUrl(QUrl("https://gitee.com/gfdgd-xi/deep-wine-runner/"));
}
2023-04-19 21:55:21 +08:00
void QemuSetting::on_enableVnc_stateChanged(int arg1)
{
// 控件的开启/关闭
ui->vncNumber->setEnabled(ui->enableVnc->isChecked());
}
void QemuSetting::on_setDefault_clicked()
{
if(QMessageBox::question(this, "提示", "你确定要重置为默认?重置后将无法恢复") == QMessageBox::No){
return;
}
QFile::remove(QDir::homePath() + "/.config/deepin-wine-runner/QemuSetting.json");
this->SetDefaultValue();
QMessageBox::information(this, "提示", "设置完成!");
}