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 "editormiscwidget.h"
18#include "ui_editormiscwidget.h"
19#include "../settings.h"
20#include "qt_utils/charsetinfo.h"
21#include "../mainwindow.h"
22
23EditorMiscWidget::EditorMiscWidget(const QString& name, const QString& group,
24 QWidget *parent) :
25 SettingsWidget(name,group,parent),
26 ui(new Ui::EditorMiscWidget)
27{
28 ui->setupUi(this);
29}
30
31EditorMiscWidget::~EditorMiscWidget()
32{
33 delete ui;
34}
35
36void EditorMiscWidget::doLoad()
37{
38 ui->chkReadonlySystemHeaders->setChecked(pSettings->editor().readOnlySytemHeader());
39 ui->chkLoadLastFiles->setChecked(pSettings->editor().autoLoadLastFiles());
40 if (pSettings->editor().defaultFileCpp()) {
41 ui->rbCppFile->setChecked(true);
42 } else {
43 ui->rbCFile->setChecked(true);
44 }
45 ui->chkAutoDetectFileEncoding->setChecked(pSettings->editor().autoDetectFileEncoding());
46
47 QByteArray defaultEncoding = pSettings->editor().defaultEncoding();
48 if (defaultEncoding == ENCODING_AUTO_DETECT
49 || defaultEncoding == ENCODING_SYSTEM_DEFAULT
50 || defaultEncoding == ENCODING_UTF8
51 || defaultEncoding == ENCODING_UTF8_BOM) {
52 int index =ui->cbEncoding->findData(defaultEncoding);
53 ui->cbEncoding->setCurrentIndex(index);
54 ui->cbEncodingDetail->clear();
55 ui->cbEncodingDetail->setVisible(false);
56 } else {
57 QString language = pCharsetInfoManager->findLanguageByCharsetName(defaultEncoding);
58 ui->cbEncoding->setCurrentText(language);
59 ui->cbEncodingDetail->setVisible(true);
60 ui->cbEncodingDetail->clear();
61 QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(language);
62 foreach (const PCharsetInfo& info, infos) {
63 ui->cbEncodingDetail->addItem(info->name);
64 }
65 ui->cbEncodingDetail->setCurrentText(defaultEncoding);
66 }
67 ui->spinMaxUndo->setValue(pSettings->editor().undoLimit());
68}
69
70void EditorMiscWidget::doSave()
71{
72 pSettings->editor().setReadOnlySytemHeader(ui->chkReadonlySystemHeaders->isChecked());
73 pSettings->editor().setAutoLoadLastFiles(ui->chkLoadLastFiles->isChecked());
74 pSettings->editor().setDefaultFileCpp(ui->rbCppFile->isChecked());
75 pSettings->editor().setAutoDetectFileEncoding(ui->chkAutoDetectFileEncoding->isChecked());
76
77 if (ui->cbEncodingDetail->isVisible()) {
78 pSettings->editor().setDefaultEncoding(ui->cbEncodingDetail->currentText().toLocal8Bit());
79 } else {
80 pSettings->editor().setDefaultEncoding(ui->cbEncoding->currentData().toByteArray());
81 }
82 pSettings->editor().setUndoLimit(ui->spinMaxUndo->value());
83 pSettings->editor().save();
84 pMainWindow->updateEditorSettings();
85}
86
87void EditorMiscWidget::init()
88{
89 ui->cbEncodingDetail->setVisible(false);
90 ui->cbEncoding->clear();
91 ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT);
92 ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8);
93 ui->cbEncoding->addItem(tr("UTF-8 BOM"),ENCODING_UTF8_BOM);
94 foreach (const QString& langName, pCharsetInfoManager->languageNames()) {
95 ui->cbEncoding->addItem(langName,langName);
96 }
97 SettingsWidget::init();
98}
99
100void EditorMiscWidget::on_cbEncoding_currentTextChanged(const QString &/*arg1*/)
101{
102 QString userData = ui->cbEncoding->currentData().toString();
103 if (userData == ENCODING_AUTO_DETECT
104 || userData == ENCODING_SYSTEM_DEFAULT
105 || userData == ENCODING_UTF8
106 || userData == ENCODING_UTF8_BOM) {
107 ui->cbEncodingDetail->setVisible(false);
108 ui->cbEncodingDetail->clear();
109 } else {
110 ui->cbEncodingDetail->setVisible(true);
111 ui->cbEncodingDetail->clear();
112 QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(userData);
113 foreach (const PCharsetInfo& info, infos) {
114 ui->cbEncodingDetail->addItem(info->name);
115 }
116 }
117}
118
119