1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "editorautosavewidget.h"
18#include "ui_editorautosavewidget.h"
19#include "../settings.h"
20#include "../mainwindow.h"
21
22EditorAutoSaveWidget::EditorAutoSaveWidget(const QString& name, const QString& group,
23 QWidget *parent) :
24 SettingsWidget(name,group,parent),
25 ui(new Ui::EditorAutoSaveWidget)
26{
27 ui->setupUi(this);
28}
29
30EditorAutoSaveWidget::~EditorAutoSaveWidget()
31{
32 delete ui;
33}
34
35void EditorAutoSaveWidget::onAutoSaveStrategyChanged()
36{
37 if (ui->rbOverwrite->isChecked()) {
38 ui->lblFilename->setText(tr("Demo file name: ") + "main.cpp");
39 } else if (ui->rbAppendUNIXTimestamp->isChecked()) {
40 ui->lblFilename->setText(tr("Demo file name: ") +
41 QString("main.%1.cpp").arg(QDateTime::currentSecsSinceEpoch()));
42 } else if (ui->rbAppendFormattedTimestamp->isChecked()) {
43 QDateTime time = QDateTime::currentDateTime();
44 ui->lblFilename->setText(tr("Demo file name: ") +
45 QString("main.%1.cpp").arg(time.toString("yyyy.MM.dd.hh.mm.ss")));
46 }
47}
48
49void EditorAutoSaveWidget::doLoad()
50{
51 //pSettings->editor().load();
52 //font
53 ui->grpEnableAutoSave->setChecked(pSettings->editor().enableAutoSave());
54 ui->spinInterval->setValue(pSettings->editor().autoSaveInterval());
55 switch(pSettings->editor().autoSaveTarget()) {
56 case astCurrentFile:
57 ui->rbCurrentFile->setChecked(true);
58 break;
59 case astAllOpennedFiles:
60 ui->rbAllOpennedFiles->setChecked(true);
61 break;
62 default:
63 ui->rbProjectFiles->setChecked(true);
64 }
65 switch(pSettings->editor().autoSaveStrategy()) {
66 case assOverwrite:
67 ui->rbOverwrite->setChecked(true);
68 break;
69 case assAppendUnixTimestamp:
70 ui->rbAppendUNIXTimestamp->setChecked(true);
71 break;
72 default:
73 ui->rbAppendFormattedTimestamp->setChecked(true);
74 }
75}
76
77void EditorAutoSaveWidget::doSave()
78{
79 pSettings->editor().setEnableAutoSave(ui->grpEnableAutoSave->isChecked());
80 pSettings->editor().setAutoSaveInterval(ui->spinInterval->value());
81 if (ui->rbCurrentFile->isChecked())
82 pSettings->editor().setAutoSaveTarget(astCurrentFile);
83 else if (ui->rbAllOpennedFiles->isChecked())
84 pSettings->editor().setAutoSaveTarget(astAllOpennedFiles);
85 else
86 pSettings->editor().setAutoSaveTarget(astAllProjectFiles);
87 if (ui->rbOverwrite->isChecked())
88 pSettings->editor().setAutoSaveStrategy(assOverwrite);
89 else if (ui->rbAppendUNIXTimestamp->isChecked())
90 pSettings->editor().setAutoSaveStrategy(assAppendUnixTimestamp);
91 else
92 pSettings->editor().setAutoSaveStrategy(assAppendFormatedTimeStamp);
93 pSettings->editor().save();
94 pMainWindow->resetAutoSaveTimer();
95}
96
97void EditorAutoSaveWidget::on_rbOverwrite_toggled(bool)
98{
99 onAutoSaveStrategyChanged();
100}
101
102
103void EditorAutoSaveWidget::on_rbAppendUNIXTimestamp_toggled(bool)
104{
105 onAutoSaveStrategyChanged();
106}
107
108
109void EditorAutoSaveWidget::on_rbAppendFormattedTimestamp_toggled(bool)
110{
111 onAutoSaveStrategyChanged();
112}
113
114