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 "projectfileswidget.h"
18#include "ui_projectfileswidget.h"
19#include "../mainwindow.h"
20#include "../systemconsts.h"
21#include <qt_utils/charsetinfo.h>
22
23ProjectFilesWidget::ProjectFilesWidget(const QString &name, const QString &group, QWidget *parent) :
24 SettingsWidget(name,group,parent),
25 ui(new Ui::ProjectFilesWidget)
26{
27 ui->setupUi(this);
28}
29
30ProjectFilesWidget::~ProjectFilesWidget()
31{
32 delete ui;
33}
34
35void ProjectFilesWidget::doLoad()
36{
37 std::shared_ptr<Project> project = pMainWindow->project();
38 if (!project)
39 return;
40 copyUnits();
41 ui->treeProject->setModel(project->model());
42 ui->treeProject->expandAll();
43 ui->grpFileOptions->setEnabled(false);
44}
45
46void ProjectFilesWidget::doSave()
47{
48 for (int i=0;i<mUnits.count();i++) {
49 PProjectUnit unitCopy = mUnits[i];
50 PProjectUnit unit = pMainWindow->project()->units()[i];
51 unit->setPriority(unitCopy->priority());
52 unit->setCompile(unitCopy->compile());
53 unit->setLink(unitCopy->link());
54 unit->setCompileCpp(unitCopy->compileCpp());
55 unit->setOverrideBuildCmd(unitCopy->overrideBuildCmd());
56 unit->setBuildCmd(unitCopy->buildCmd());
57 unit->setEncoding(unitCopy->encoding());
58 }
59 pMainWindow->project()->saveUnits();
60 copyUnits();
61 ui->treeProject->expandAll();
62 ui->treeProject->clicked(ui->treeProject->currentIndex());
63}
64
65PProjectUnit ProjectFilesWidget::currentUnit()
66{
67 QModelIndex index = ui->treeProject->currentIndex();
68 if (!index.isValid())
69 return PProjectUnit();
70 ProjectModelNode* node = static_cast<ProjectModelNode*>(index.internalPointer());
71 if (!node)
72 return PProjectUnit();
73 int i = node->unitIndex;
74 if (i>=0) {
75 return mUnits[i];
76 } else
77 return PProjectUnit();
78}
79
80void ProjectFilesWidget::copyUnits()
81{
82 std::shared_ptr<Project> project = pMainWindow->project();
83 if (!project)
84 return;
85 mUnits.clear();
86 foreach (const PProjectUnit& unit, project->units()) {
87 PProjectUnit unitCopy = std::make_shared<ProjectUnit>(project.get());
88 unitCopy->setPriority(unit->priority());
89 unitCopy->setCompile(unit->compile());
90 unitCopy->setLink(unit->link());
91 unitCopy->setCompileCpp(unit->compileCpp());
92 unitCopy->setOverrideBuildCmd(unit->overrideBuildCmd());
93 unitCopy->setBuildCmd(unit->buildCmd());
94 unitCopy->setEncoding(unit->encoding());
95 mUnits.append(unitCopy);
96 }
97}
98
99void ProjectFilesWidget::disableFileOptions()
100{
101 ui->grpFileOptions->setEnabled(false);
102 ui->spinPriority->setValue(0);
103 ui->chkCompile->setChecked(false);
104 ui->chkLink->setChecked(false);
105 ui->chkCompileAsCPP->setChecked(false);
106 ui->chkOverrideBuildCommand->setChecked(false);
107 ui->txtBuildCommand->setPlainText("");
108}
109
110void ProjectFilesWidget::loadUnitEncoding(PProjectUnit unit)
111{
112 if (unit->encoding() == ENCODING_AUTO_DETECT
113 || unit->encoding() == ENCODING_SYSTEM_DEFAULT
114 || unit->encoding() == ENCODING_UTF8) {
115 int index =ui->cbEncoding->findData(unit->encoding());
116 ui->cbEncoding->setCurrentIndex(index);
117 ui->cbEncodingDetail->clear();
118 ui->cbEncodingDetail->setVisible(false);
119 } else {
120 QString encoding = unit->encoding();
121 QString language = pCharsetInfoManager->findLanguageByCharsetName(encoding);
122 ui->cbEncoding->setCurrentText(language);
123 ui->cbEncodingDetail->setVisible(true);
124 ui->cbEncodingDetail->clear();
125 QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(language);
126 foreach (const PCharsetInfo& info, infos) {
127 ui->cbEncodingDetail->addItem(info->name);
128 }
129 ui->cbEncodingDetail->setCurrentText(encoding);
130 }
131}
132
133void ProjectFilesWidget::on_treeProject_doubleClicked(const QModelIndex &index)
134{
135 if (!index.isValid()) {
136 disableFileOptions();
137 return ;
138 }
139 ProjectModelNode* node = static_cast<ProjectModelNode*>(index.internalPointer());
140 if (!node) {
141 disableFileOptions();
142 return;
143 }
144 int i = node->unitIndex;
145 if (i>=0) {
146 PProjectUnit unit = mUnits[i];
147 ui->grpFileOptions->setEnabled(true);
148 ui->spinPriority->setValue(unit->priority());
149 ui->chkCompile->setChecked(unit->compile());
150 ui->chkLink->setChecked(unit->link());
151 ui->chkCompileAsCPP->setChecked(unit->compileCpp());
152 ui->chkOverrideBuildCommand->setChecked(unit->overrideBuildCmd());
153 ui->txtBuildCommand->setPlainText(unit->buildCmd());
154 ui->txtBuildCommand->setEnabled(ui->chkOverrideBuildCommand->isChecked());
155 loadUnitEncoding(unit);
156 } else {
157 disableFileOptions();
158 }
159}
160
161
162void ProjectFilesWidget::on_spinPriority_valueChanged(int)
163{
164 PProjectUnit unit = currentUnit();
165 if(!unit)
166 return;
167 unit->setPriority(ui->spinPriority->value());
168}
169
170
171void ProjectFilesWidget::on_chkCompile_stateChanged(int)
172{
173 PProjectUnit unit = currentUnit();
174 if(!unit)
175 return;
176 unit->setCompile(ui->chkCompile->isChecked());
177}
178
179
180void ProjectFilesWidget::on_chkLink_stateChanged(int)
181{
182 PProjectUnit unit = currentUnit();
183 if(!unit)
184 return;
185 unit->setLink(ui->chkLink->isChecked());
186}
187
188
189void ProjectFilesWidget::on_chkCompileAsCPP_stateChanged(int )
190{
191 PProjectUnit unit = currentUnit();
192 if(!unit)
193 return;
194 unit->setCompileCpp(ui->chkCompileAsCPP->isChecked());
195}
196
197
198void ProjectFilesWidget::on_chkOverrideBuildCommand_stateChanged(int )
199{
200 PProjectUnit unit = currentUnit();
201 if(!unit)
202 return;
203 unit->setOverrideBuildCmd(ui->chkOverrideBuildCommand->isChecked());
204 ui->txtBuildCommand->setEnabled(ui->chkOverrideBuildCommand->isChecked());
205}
206
207
208void ProjectFilesWidget::on_txtBuildCommand_textChanged()
209{
210 PProjectUnit unit = currentUnit();
211 if(!unit)
212 return;
213 unit->setBuildCmd(ui->txtBuildCommand->toPlainText());
214}
215
216
217void ProjectFilesWidget::on_cbEncoding_currentTextChanged(const QString &)
218{
219 QString userData = ui->cbEncoding->currentData().toString();
220 if (userData == ENCODING_AUTO_DETECT
221 || userData == ENCODING_SYSTEM_DEFAULT
222 || userData == ENCODING_UTF8) {
223 PProjectUnit unit = currentUnit();
224 if(!unit)
225 return;
226 unit->setEncoding(userData.toUtf8());
227 ui->cbEncodingDetail->setVisible(false);
228 ui->cbEncodingDetail->clear();
229 } else {
230 ui->cbEncodingDetail->setVisible(true);
231 ui->cbEncodingDetail->clear();
232 QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(userData);
233 foreach (const PCharsetInfo& info, infos) {
234 ui->cbEncodingDetail->addItem(info->name);
235 }
236 }
237}
238
239
240void ProjectFilesWidget::on_treeProject_clicked(const QModelIndex &index)
241{
242 on_treeProject_doubleClicked(index);
243}
244
245void ProjectFilesWidget::init()
246{
247 ui->spinPriority->setMinimum(0);
248 ui->spinPriority->setMaximum(9999);
249 ui->cbEncodingDetail->setVisible(false);
250 ui->cbEncoding->clear();
251 ui->cbEncoding->addItem(tr("Auto detect"),ENCODING_AUTO_DETECT);
252 ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT);
253 ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8);
254 foreach (const QString& langName, pCharsetInfoManager->languageNames()) {
255 ui->cbEncoding->addItem(langName,langName);
256 }
257 SettingsWidget::init();
258}
259
260
261void ProjectFilesWidget::on_cbEncodingDetail_currentTextChanged(const QString &)
262{
263 PProjectUnit unit = currentUnit();
264 if(!unit)
265 return;
266 unit->setEncoding(ui->cbEncodingDetail->currentText().toUtf8());
267}
268
269