RedPanda-CPP/RedPandaIDE/settingsdialog/projectcompilerwidget.cpp

193 lines
7.3 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-09-15 08:02:08 +08:00
#include "projectcompilerwidget.h"
#include "ui_projectcompilerwidget.h"
#include "../settings.h"
#include "../project.h"
#include "../mainwindow.h"
#include <qt_utils/charsetinfo.h>
#include <QMessageBox>
2021-09-15 08:02:08 +08:00
ProjectCompilerWidget::ProjectCompilerWidget(const QString &name, const QString &group, QWidget *parent) :
SettingsWidget(name,group,parent),
2021-09-15 08:02:08 +08:00
ui(new Ui::ProjectCompilerWidget)
{
mInitialized=false;
2021-09-15 08:02:08 +08:00
ui->setupUi(this);
}
ProjectCompilerWidget::~ProjectCompilerWidget()
{
delete ui;
}
void ProjectCompilerWidget::refreshOptions()
{
Settings::PCompilerSet pSet = pSettings->compilerSets().getSet(ui->cbCompilerSet->currentIndex());
if (!pSet)
return;
ui->panelAddCharset->setVisible(pSet->compilerType()!=CompilerType::Clang);
//ui->chkAddCharset->setEnabled(pSet->compilerType()!=COMPILER_CLANG);
mOptions = pMainWindow->project()->options().compilerOptions;
2022-05-12 22:42:19 +08:00
if (mOptions.isEmpty())
mOptions = pSet->compileOptions();
ui->tabOptions->resetUI(pSet,mOptions);
ui->chkStaticLink->setChecked(pSet->staticLink());
ui->chkAddCharset->setChecked(pSet->autoAddCharsetParams());
QByteArray execEncoding = pMainWindow->project()->options().execEncoding;
if (execEncoding == ENCODING_AUTO_DETECT
|| execEncoding == ENCODING_SYSTEM_DEFAULT
|| execEncoding == ENCODING_UTF8) {
int index =ui->cbEncoding->findData(execEncoding);
ui->cbEncoding->setCurrentIndex(index);
ui->cbEncodingDetails->clear();
ui->cbEncodingDetails->setVisible(false);
} else {
QString encoding = execEncoding;
QString language = pCharsetInfoManager->findLanguageByCharsetName(encoding);
ui->cbEncoding->setCurrentText(language);
ui->cbEncodingDetails->setVisible(true);
ui->cbEncodingDetails->clear();
QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(language);
foreach (const PCharsetInfo& info, infos) {
ui->cbEncodingDetails->addItem(info->name);
}
ui->cbEncodingDetails->setCurrentText(encoding);
}
}
void ProjectCompilerWidget::doLoad()
{
mOptions = pMainWindow->project()->options().compilerOptions;
ui->cbCompilerSet->setCurrentIndex(pMainWindow->project()->options().compilerSet);
ui->chkAddCharset->setChecked(pMainWindow->project()->options().addCharset);
ui->chkStaticLink->setChecked(pMainWindow->project()->options().staticLink);
mInitialized=true;
}
void ProjectCompilerWidget::doSave()
{
Settings::PCompilerSet pSet = pSettings->compilerSets().getSet(ui->cbCompilerSet->currentIndex());
if (!pSet)
return;
pMainWindow->project()->setCompilerSet(ui->cbCompilerSet->currentIndex());
pMainWindow->project()->options().compilerOptions = ui->tabOptions->arguments(true);
if (pSet->compilerType()!=CompilerType::Clang)
pMainWindow->project()->options().addCharset = ui->chkAddCharset->isChecked();
pMainWindow->project()->options().staticLink = ui->chkStaticLink->isChecked();
if (ui->cbEncodingDetails->isVisible()) {
pMainWindow->project()->options().execEncoding = ui->cbEncodingDetails->currentText().toLocal8Bit();
} else {
pMainWindow->project()->options().execEncoding = ui->cbEncoding->currentData().toString().toLocal8Bit();
}
pMainWindow->project()->saveOptions();
}
void ProjectCompilerWidget::init()
{
ui->cbCompilerSet->clear();
2022-04-25 12:43:23 +08:00
for (size_t i=0;i<pSettings->compilerSets().size();i++) {
2022-03-11 20:51:33 +08:00
ui->cbCompilerSet->addItem(pSettings->compilerSets().getSet(i)->name());
}
ui->cbEncodingDetails->setVisible(false);
ui->cbEncoding->clear();
ui->cbEncoding->addItem(tr("ANSI"),ENCODING_SYSTEM_DEFAULT);
ui->cbEncoding->addItem(tr("UTF-8"),ENCODING_UTF8);
foreach (const QString& langName, pCharsetInfoManager->languageNames()) {
ui->cbEncoding->addItem(langName,langName);
}
SettingsWidget::init();
}
void ProjectCompilerWidget::on_cbCompilerSet_currentIndexChanged(int index)
{
std::shared_ptr<Project> project = pMainWindow->project();
auto action = finally([this]{
this->refreshOptions();
});
if (!mInitialized || index==project->options().compilerSet) {
return;
}
2023-08-17 19:24:49 +08:00
Settings::PCompilerSet pSet=pSettings->compilerSets().getSet(index);
2023-12-17 13:53:22 +08:00
#ifdef ENABLE_SDCC
2023-08-17 19:24:49 +08:00
if (pSet) {
if (project->options().type==ProjectType::MicroController) {
if (pSet->compilerType()!=CompilerType::SDCC) {
QMessageBox::information(this,
tr("Wrong Compiler Type"),
tr("Compiler %1 can't compile a microcontroller project.").arg(pSet->name())
);
ui->cbCompilerSet->setCurrentIndex(project->options().compilerSet);
return;
}
} else {
if (pSet->compilerType()==CompilerType::SDCC) {
QMessageBox::information(this,
tr("Wrong Compiler Type"),
tr("Compiler %1 can only compile microcontroller project.").arg(pSet->name())
);
ui->cbCompilerSet->setCurrentIndex(project->options().compilerSet);
return;
}
}
}
2023-12-17 13:53:22 +08:00
#endif
if (QMessageBox::warning(
this,
2023-08-17 19:24:49 +08:00
tr("Change Project Compiler Set"),
tr("Change the project's compiler set will lose all custom compiler set options.")
+"<br />"
2023-08-17 19:24:49 +08:00
+ tr("Do you really want to do that?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No) != QMessageBox::Yes) {
ui->cbCompilerSet->setCurrentIndex(project->options().compilerSet);
return;
}
project->setCompilerSet(index);
project->saveOptions();
}
2022-07-04 11:39:06 +08:00
void ProjectCompilerWidget::on_cbEncoding_currentTextChanged(const QString &/*arg1*/)
{
QString userData = ui->cbEncoding->currentData().toString();
if (userData == ENCODING_AUTO_DETECT
|| userData == ENCODING_SYSTEM_DEFAULT
|| userData == ENCODING_UTF8) {
ui->cbEncodingDetails->setVisible(false);
ui->cbEncodingDetails->clear();
} else {
ui->cbEncodingDetails->setVisible(true);
ui->cbEncodingDetails->clear();
QList<PCharsetInfo> infos = pCharsetInfoManager->findCharsetsByLanguageName(userData);
foreach (const PCharsetInfo& info, infos) {
ui->cbEncodingDetails->addItem(info->name);
}
}
}
2022-07-04 11:39:06 +08:00
void ProjectCompilerWidget::on_cbEncodingDetails_currentTextChanged(const QString &/*arg1*/)
{
}