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 "projectcompilerwidget.h" |
18 | #include "ui_projectcompilerwidget.h" |
19 | #include "../settings.h" |
20 | #include "../project.h" |
21 | #include "../mainwindow.h" |
22 | #include <qt_utils/charsetinfo.h> |
23 | |
24 | ProjectCompilerWidget::ProjectCompilerWidget(const QString &name, const QString &group, QWidget *parent) : |
25 | SettingsWidget(name,group,parent), |
26 | ui(new Ui::ProjectCompilerWidget) |
27 | { |
28 | ui->setupUi(this); |
29 | } |
30 | |
31 | ProjectCompilerWidget::~ProjectCompilerWidget() |
32 | { |
33 | delete ui; |
34 | } |
35 | |
36 | void ProjectCompilerWidget::refreshOptions() |
37 | { |
38 | Settings::PCompilerSet pSet = pSettings->compilerSets().getSet(ui->cbCompilerSet->currentIndex()); |
39 | if (!pSet) |
40 | return; |
41 | ui->panelAddCharset->setVisible(pSet->compilerType()!=COMPILER_CLANG); |
42 | //ui->chkAddCharset->setEnabled(pSet->compilerType()!=COMPILER_CLANG); |
43 | mOptions = pMainWindow->project()->options().compilerOptions; |
44 | if (mOptions.isEmpty()) |
45 | mOptions = pSet->compileOptions(); |
46 | |
47 | ui->tabOptions->resetUI(pSet,mOptions); |
48 | |
49 | ui->chkStaticLink->setChecked(pSet->staticLink()); |
50 | |
51 | QByteArray execEncoding = pMainWindow->project()->options().execEncoding; |
52 | if (execEncoding == ENCODING_AUTO_DETECT |
53 | || execEncoding == ENCODING_SYSTEM_DEFAULT |
54 | || execEncoding == ENCODING_UTF8) { |
55 | int index =ui->cbEncoding->findData(execEncoding); |
56 | ui->cbEncoding->setCurrentIndex(index); |
57 | ui->cbEncodingDetails->clear(); |
58 | ui->cbEncodingDetails->setVisible(false); |
59 | } else { |
60 | QString encoding = execEncoding; |
61 | QString language = pCharsetInfoManager->findLanguageByCharsetName(encoding); |
62 | ui->cbEncoding->setCurrentText(language); |
63 | ui->cbEncodingDetails->setVisible(true); |
64 | ui->cbEncodingDetails->clear(); |
65 | QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(language); |
66 | foreach (const PCharsetInfo& info, infos) { |
67 | ui->cbEncodingDetails->addItem(info->name); |
68 | } |
69 | ui->cbEncodingDetails->setCurrentText(encoding); |
70 | } |
71 | } |
72 | |
73 | void ProjectCompilerWidget::doLoad() |
74 | { |
75 | ui->chkAddCharset->setChecked(pMainWindow->project()->options().addCharset); |
76 | ui->chkStaticLink->setChecked(pMainWindow->project()->options().staticLink); |
77 | |
78 | mOptions = pMainWindow->project()->options().compilerOptions; |
79 | ui->cbCompilerSet->setCurrentIndex(pMainWindow->project()->options().compilerSet); |
80 | } |
81 | |
82 | void ProjectCompilerWidget::doSave() |
83 | { |
84 | Settings::PCompilerSet pSet = pSettings->compilerSets().getSet(ui->cbCompilerSet->currentIndex()); |
85 | if (!pSet) |
86 | return; |
87 | |
88 | pMainWindow->project()->setCompilerSet(ui->cbCompilerSet->currentIndex()); |
89 | pMainWindow->project()->options().compilerOptions = ui->tabOptions->arguments(true); |
90 | if (pSet->compilerType()!=COMPILER_CLANG) |
91 | pMainWindow->project()->options().addCharset = ui->chkAddCharset->isChecked(); |
92 | pMainWindow->project()->options().staticLink = ui->chkStaticLink->isChecked(); |
93 | |
94 | if (ui->cbEncodingDetails->isVisible()) { |
95 | pMainWindow->project()->options().execEncoding = ui->cbEncodingDetails->currentText().toLocal8Bit(); |
96 | } else { |
97 | pMainWindow->project()->options().execEncoding = ui->cbEncoding->currentData().toString().toLocal8Bit(); |
98 | } |
99 | pMainWindow->project()->saveOptions(); |
100 | } |
101 | |
102 | void ProjectCompilerWidget::init() |
103 | { |
104 | ui->cbCompilerSet->clear(); |
105 | for (size_t i=0;i<pSettings->compilerSets().size();i++) { |
106 | ui->cbCompilerSet->addItem(pSettings->compilerSets().getSet(i)->name()); |
107 | } |
108 | ui->cbEncodingDetails->setVisible(false); |
109 | ui->cbEncoding->clear(); |
110 | ui->cbEncoding->addItem(tr("ANSI" ),ENCODING_SYSTEM_DEFAULT); |
111 | ui->cbEncoding->addItem(tr("UTF-8" ),ENCODING_UTF8); |
112 | foreach (const QString& langName, pCharsetInfoManager->languageNames()) { |
113 | ui->cbEncoding->addItem(langName,langName); |
114 | } |
115 | SettingsWidget::init(); |
116 | } |
117 | |
118 | void ProjectCompilerWidget::on_cbCompilerSet_currentIndexChanged(int) |
119 | { |
120 | refreshOptions(); |
121 | } |
122 | |
123 | void ProjectCompilerWidget::on_cbEncoding_currentTextChanged(const QString &/*arg1*/) |
124 | { |
125 | QString userData = ui->cbEncoding->currentData().toString(); |
126 | if (userData == ENCODING_AUTO_DETECT |
127 | || userData == ENCODING_SYSTEM_DEFAULT |
128 | || userData == ENCODING_UTF8) { |
129 | ui->cbEncodingDetails->setVisible(false); |
130 | ui->cbEncodingDetails->clear(); |
131 | } else { |
132 | ui->cbEncodingDetails->setVisible(true); |
133 | ui->cbEncodingDetails->clear(); |
134 | QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(userData); |
135 | foreach (const PCharsetInfo& info, infos) { |
136 | ui->cbEncodingDetails->addItem(info->name); |
137 | } |
138 | } |
139 | } |
140 | |
141 | |
142 | void ProjectCompilerWidget::on_cbEncodingDetails_currentTextChanged(const QString &/*arg1*/) |
143 | { |
144 | |
145 | } |
146 | |
147 | |