1#include "GeneralConfigDlg.h"
2
3#include <GitQlientSettings.h>
4#include <GitQlientStyles.h>
5#include <QLogger.h>
6#include <CheckBox.h>
7#include <ButtonLink.hpp>
8
9#include <QSpinBox>
10#include <QComboBox>
11#include <QLabel>
12#include <QVBoxLayout>
13#include <QPushButton>
14#include <QLineEdit>
15#include <QMessageBox>
16#include <QFileDialog>
17#include <QStandardPaths>
18#include <QJsonDocument>
19#include <QJsonObject>
20
21using namespace QLogger;
22
23GeneralConfigDlg::GeneralConfigDlg(QWidget *parent)
24 : QDialog(parent)
25 , mSettings(new GitQlientSettings())
26 , mDisableLogs(new CheckBox())
27 , mLevelCombo(new QComboBox())
28 , mStylesSchema(new QComboBox())
29 , mGitLocation(new QLineEdit())
30 , mClose(new QPushButton(tr("Close")))
31 , mReset(new QPushButton(tr("Reset")))
32 , mApply(new QPushButton(tr("Apply")))
33
34{
35 mGitLocation->setPlaceholderText(tr("Git location..."));
36
37 mClose->setMinimumWidth(75);
38 mReset->setMinimumWidth(75);
39 mApply->setMinimumWidth(75);
40
41 mDisableLogs->setChecked(mSettings->globalValue("logsDisabled", true).toBool());
42
43 mLevelCombo->addItems({ "Trace", "Debug", "Info", "Warning", "Error", "Fatal" });
44 mLevelCombo->setCurrentIndex(mSettings->globalValue("logsLevel", static_cast<int>(LogLevel::Warning)).toInt());
45
46 mStylesSchema->addItems({ "dark", "bright" });
47
48 const auto currentStyle = mSettings->globalValue("colorSchema", "dark").toString();
49 mStylesSchema->setCurrentText(currentStyle);
50 connect(mStylesSchema, &QComboBox::currentTextChanged, this, [this, currentStyle](const QString &newText) {
51 if (newText != currentStyle)
52 mShowResetMsg = true;
53 });
54
55 connect(mClose, &QPushButton::clicked, this, &GeneralConfigDlg::close);
56 connect(mReset, &QPushButton::clicked, this, &GeneralConfigDlg::resetChanges);
57 connect(mApply, &QPushButton::clicked, this, &GeneralConfigDlg::accept);
58
59 const auto buttonsLayout = new QHBoxLayout();
60 buttonsLayout->setContentsMargins(QMargins());
61 buttonsLayout->setSpacing(20);
62 buttonsLayout->addWidget(mClose);
63 buttonsLayout->addStretch();
64 buttonsLayout->addWidget(mReset);
65 buttonsLayout->addWidget(mApply);
66
67 auto row = 0;
68 const auto layout = new QGridLayout(this);
69 layout->setContentsMargins(20, 20, 20, 20);
70 layout->setSpacing(20);
71 layout->setAlignment(Qt::AlignTop);
72 layout->addWidget(new QLabel(tr("Disable logs")), row, 0);
73 layout->addWidget(mDisableLogs, row, 1);
74 layout->addWidget(new QLabel(tr("Set log level")), ++row, 0);
75 layout->addWidget(mLevelCombo, row, 1);
76 layout->addWidget(new QLabel(tr("Styles schema")), ++row, 0);
77 layout->addWidget(mStylesSchema, row, 1);
78 layout->addWidget(new QLabel(tr("Git location (if not in PATH):")), ++row, 0);
79 layout->addWidget(mGitLocation, row, 1);
80
81 const auto exportLink = new ButtonLink(tr("Export config..."));
82 connect(exportLink, &ButtonLink::clicked, this, &GeneralConfigDlg::exportConfig);
83
84 layout->addWidget(exportLink, ++row, 0, 1, 2);
85
86 const auto importLink = new ButtonLink(tr("Import config..."));
87 connect(importLink, &ButtonLink::clicked, this, &GeneralConfigDlg::importConfig);
88
89 layout->addWidget(importLink, ++row, 0, 1, 2);
90
91 layout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Expanding), ++row, 0, 1, 2);
92 layout->addLayout(buttonsLayout, ++row, 0, 1, 2);
93
94 setFixedSize(500, 300);
95
96 setStyleSheet(GitQlientStyles::getStyles());
97}
98
99void GeneralConfigDlg::resetChanges()
100{
101 mDisableLogs->setChecked(mSettings->globalValue("logsDisabled", false).toBool());
102 mLevelCombo->setCurrentIndex(mSettings->globalValue("logsLevel", 2).toInt());
103 mStylesSchema->setCurrentText(mSettings->globalValue("colorSchema", "bright").toString());
104 mGitLocation->setText(mSettings->globalValue("gitLocation", "").toString());
105}
106
107void GeneralConfigDlg::accept()
108{
109 mSettings->setGlobalValue("logsDisabled", mDisableLogs->isChecked());
110 mSettings->setGlobalValue("logsLevel", mLevelCombo->currentIndex());
111 mSettings->setGlobalValue("colorSchema", mStylesSchema->currentText());
112 mSettings->setGlobalValue("gitLocation", mGitLocation->text());
113
114 if (mShowResetMsg)
115 QMessageBox::information(this, tr("Reset needed!"),
116 tr("You need to restart GitQlient to see the changes in the styles applid."));
117
118 const auto logger = QLoggerManager::getInstance();
119 logger->overwriteLogLevel(static_cast<LogLevel>(mLevelCombo->currentIndex()));
120
121 if (mDisableLogs->isChecked())
122 logger->pause();
123 else
124 logger->resume();
125
126 QDialog::accept();
127}
128
129void GeneralConfigDlg::importConfig()
130{
131 const auto fileDialog
132 = new QFileDialog(this, tr("Select a config file..."),
133 QStandardPaths::writableLocation(QStandardPaths::HomeLocation), "GitQlient.conf");
134 fileDialog->setFileMode(QFileDialog::ExistingFile);
135
136 if (fileDialog->exec())
137 {
138 const auto file = fileDialog->selectedFiles().constFirst();
139
140 QFile f(file);
141
142 if (f.open(QIODevice::ReadOnly))
143 {
144 QJsonDocument doc;
145 doc.fromJson(f.readAll());
146
147 const auto obj = doc.object();
148
149 mDisableLogs->setChecked(obj[QStringLiteral("logsDisabled")].toBool());
150 mLevelCombo->setCurrentIndex(obj[QStringLiteral("logsLevel")].toInt());
151 mStylesSchema->setCurrentText(obj[QStringLiteral("colorSchema")].toString());
152 mGitLocation->setText(obj[QStringLiteral("gitLocation")].toString());
153
154 QMessageBox::information(this, tr("External configuration loaded!"),
155 tr("The configuration has been loaded successfully. Remember to apply the changes."));
156
157 f.close();
158 }
159 }
160}
161
162void GeneralConfigDlg::exportConfig()
163{
164 const auto fileDialog
165 = new QFileDialog(this, tr("Select a folder..."), QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
166 "GitQlient.conf");
167 fileDialog->setOption(QFileDialog::ShowDirsOnly, true);
168 fileDialog->setFileMode(QFileDialog::Directory);
169
170 if (fileDialog->exec())
171 {
172 QJsonObject obj;
173 obj.insert("logsDisabled", mDisableLogs->isChecked());
174 obj.insert("logsLevel", mLevelCombo->currentIndex());
175 obj.insert("colorSchema", mStylesSchema->currentText());
176 obj.insert("gitLocation", mGitLocation->text());
177
178 QJsonDocument doc(obj);
179
180 const auto fullPath = QString("%1/%2").arg(fileDialog->directory().path(), QString::fromUtf8("GitQlient.conf"));
181 QFile f(fullPath);
182
183 if (f.open(QIODevice::WriteOnly))
184 {
185 f.write(doc.toJson());
186 f.close();
187
188 QMessageBox::information(this, tr("Configuration exported!"),
189 tr("The configuration has been stored in {%1}").arg(fullPath));
190 }
191 }
192}
193