100 lines
3.5 KiB
C++
100 lines
3.5 KiB
C++
#include "settingsdialog.h"
|
|
#include "ui_settingsdialog.h"
|
|
|
|
#include <settings/settings.h>
|
|
#include <utils/utils.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <QMessageBox>
|
|
#include <outputcolumnmodel.h>
|
|
|
|
SettingsDialog::SettingsDialog(QWidget *parent)
|
|
: QDialog(parent), ui(new Ui::settingsdialog),
|
|
settings(Settings(get_path_relative_to_home(
|
|
".local/share/checks_parser/settings.json"))) {
|
|
|
|
ui->setupUi(this);
|
|
|
|
columns = new std::vector<OutputColumn>;
|
|
|
|
OutputColumnModel *model = new OutputColumnModel(columns, this);
|
|
|
|
ui->outputOptionsListView->setModel(model);
|
|
|
|
for (unsigned short i = 0; i < 6; i ++)
|
|
columns->push_back(OutputColumn(tr("Кто здесь?"), ColumnType::date));
|
|
|
|
for (auto &column : column_names) {
|
|
std::string name = settings.get_all_settings()["output_order"][column.first]["name"];
|
|
unsigned short position = settings.get_all_settings()["output_order"][column.first]["position"];
|
|
ColumnType type = column.second;
|
|
columns->at(position - 1) = (OutputColumn(QString::fromStdString(name), type));
|
|
}
|
|
|
|
ui->printHeaderCheckBox->setChecked(settings.get_all_settings()["print_header"]);
|
|
ui->printTotalCheckBox->setChecked(settings.get_all_settings()["print_total"]);
|
|
|
|
int currentLanguageIndex = 0;
|
|
bool languageSettingPresent = false;
|
|
|
|
languageSettingPresent = settings.get_all_settings().find("language") != settings.get_all_settings().end();
|
|
|
|
if (languageSettingPresent) {
|
|
currentLanguageIndex = ui->languageComboBox->findText(QString::fromStdString(this->settings.get_all_settings()["language"]));
|
|
|
|
} else {
|
|
currentLanguageIndex = ui->languageComboBox->findText(QLocale::system().name());
|
|
if (currentLanguageIndex < 0) {
|
|
currentLanguageIndex = ui->languageComboBox->findText("en_US");
|
|
}
|
|
}
|
|
ui->languageComboBox->setCurrentIndex(currentLanguageIndex);
|
|
|
|
}
|
|
|
|
void SettingsDialog::on_printHeaderCheckBox_stateChanged(int value) {
|
|
this->settings.get_all_settings()["print_header"] = (value? true : false);
|
|
}
|
|
|
|
void SettingsDialog::on_printTotalCheckBox_stateChanged(int value) {
|
|
this->settings.get_all_settings()["print_total"] = (value? true : false);
|
|
}
|
|
|
|
void SettingsDialog::on_buttonBox_accepted() {
|
|
for (int i = 0; i < columns->size(); i ++) {
|
|
OutputColumn &column = columns->at(i);
|
|
std::string key = find_key_by_value(column_names, column.get_column_type());
|
|
settings.get_all_settings()["output_order"][key]["name"] = column.get_text().toStdString();
|
|
settings.get_all_settings()["output_order"][key]["position"] = i + 1;
|
|
}
|
|
|
|
this->settings.flush();
|
|
}
|
|
|
|
void SettingsDialog::on_buttonBox_rejected() { this->close(); }
|
|
|
|
void SettingsDialog::on_languageComboBox_currentTextChanged(const QString &changed) {
|
|
bool languageSettingPresent = false;
|
|
|
|
languageSettingPresent = settings.get_all_settings().find("language") != settings.get_all_settings().end();
|
|
|
|
if (languageSettingPresent) {
|
|
if (changed == QString::fromStdString(this->settings.get_all_settings()["language"])) return;
|
|
} else {
|
|
if (changed == QLocale::system().name()) return;
|
|
}
|
|
|
|
this->settings.get_all_settings()["language"] = changed.toStdString();
|
|
|
|
QMessageBox infoDialog;
|
|
infoDialog.setText(tr("You need to restart program to apply language changes"));
|
|
infoDialog.setIcon(QMessageBox::Information);
|
|
infoDialog.setWindowTitle(tr("Restart required"));
|
|
infoDialog.exec();
|
|
}
|
|
|
|
|
|
SettingsDialog::~SettingsDialog() { delete ui; }
|
|
|