RedPanda-CPP/RedPandaIDE/settingsdialog/projectcompilerwidget.cpp

191 lines
7.3 KiB
C++

/*
* 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/>.
*/
#include "projectcompilerwidget.h"
#include "ui_projectcompilerwidget.h"
#include "../settings.h"
#include "../project.h"
#include "../mainwindow.h"
#include <qt_utils/charsetinfo.h>
#include <QMessageBox>
ProjectCompilerWidget::ProjectCompilerWidget(const QString &name, const QString &group, QWidget *parent) :
SettingsWidget(name,group,parent),
ui(new Ui::ProjectCompilerWidget)
{
mInitialized=false;
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;
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();
for (size_t i=0;i<pSettings->compilerSets().size();i++) {
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;
}
Settings::PCompilerSet pSet=pSettings->compilerSets().getSet(index);
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;
}
}
}
if (QMessageBox::warning(
this,
tr("Change Project Compiler Set"),
tr("Change the project's compiler set will lose all custom compiler set options.")
+"<br />"
+ 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();
}
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);
}
}
}
void ProjectCompilerWidget::on_cbEncodingDetails_currentTextChanged(const QString &/*arg1*/)
{
}