RedPanda-CPP/RedPandaIDE/settingsdialog/environmentprogramswidget.cpp

140 lines
5.1 KiB
C++
Raw Permalink 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-12-25 23:38:53 +08:00
#include "environmentprogramswidget.h"
#include "ui_environmentprogramswidget.h"
#include "../settings.h"
#include "../iconsmanager.h"
#include "../systemconsts.h"
#include "../compiler/executablerunner.h"
#include "utils.h"
#include "utils/escape.h"
#include "utils/font.h"
2021-12-25 23:38:53 +08:00
#include <QFileDialog>
#include <QMessageBox>
2021-12-25 23:38:53 +08:00
EnvironmentProgramsWidget::EnvironmentProgramsWidget(const QString& name, const QString& group, QWidget *parent) :
SettingsWidget(name,group,parent),
ui(new Ui::EnvironmentProgramsWidget)
{
ui->setupUi(this);
ui->labelCmdPreviewResult->setFont(defaultMonoFont());
#ifndef Q_OS_WINDOWS
ui->grpUseCustomTerminal->setCheckable(false);
#endif
2021-12-25 23:38:53 +08:00
}
EnvironmentProgramsWidget::~EnvironmentProgramsWidget()
{
delete ui;
}
auto EnvironmentProgramsWidget::resolveExecArguments(const QString &terminalPath, const QString &argsPattern)
-> std::tuple<QString, QStringList, PNonExclusiveTemporaryFileOwner>
{
return wrapCommandForTerminalEmulator(terminalPath, argsPattern, platformCommandForTerminalArgsPreview());
}
void EnvironmentProgramsWidget::updateCommandPreview(const QString &terminalPath, const QString &argsPattern)
{
auto [filename, arguments, fileOwner] = resolveExecArguments(terminalPath, argsPattern);
for (auto &arg : arguments)
arg = escapeArgument(arg, false, platformShellEscapeArgumentRule());
ui->labelCmdPreviewResult->setPlainText(escapeArgument(filename, true, platformShellEscapeArgumentRule()) + " " + arguments.join(' '));
}
void EnvironmentProgramsWidget::autoDetectAndUpdateArgumentsPattern(const QString &terminalPath)
{
const QString &executable = QFileInfo(terminalPath).fileName();
2023-09-21 08:17:07 +08:00
const QString &pattern = pSettings->environment().queryPredefinedTerminalArgumentsPattern(executable);
if (!pattern.isEmpty())
ui->txtArgsPattern->setText(pattern);
else
QMessageBox::warning(nullptr,
QObject::tr("Auto Detection Failed"),
QObject::tr("Failed to detect terminal arguments pattern for “%1”.").arg(executable),
QMessageBox::Ok);
}
2021-12-25 23:38:53 +08:00
void EnvironmentProgramsWidget::doLoad()
{
#ifdef Q_OS_WINDOWS
ui->grpUseCustomTerminal->setChecked(pSettings->environment().useCustomTerminal());
#endif
2021-12-25 23:38:53 +08:00
ui->txtTerminal->setText(pSettings->environment().terminalPath());
ui->txtArgsPattern->setText(pSettings->environment().terminalArgumentsPattern());
2021-12-25 23:38:53 +08:00
}
void EnvironmentProgramsWidget::doSave()
{
#ifdef Q_OS_WINDOWS
pSettings->environment().setUseCustomTerminal(ui->grpUseCustomTerminal->isChecked());
#endif
2021-12-25 23:38:53 +08:00
pSettings->environment().setTerminalPath(ui->txtTerminal->text());
pSettings->environment().setTerminalArgumentsPattern(ui->txtArgsPattern->text());
2021-12-25 23:38:53 +08:00
pSettings->environment().save();
}
void EnvironmentProgramsWidget::updateIcons(const QSize &)
{
pIconsManager->setIcon(ui->btnChooseTerminal,IconsManager::ACTION_FILE_OPEN_FOLDER);
pIconsManager->setIcon(ui->btnAutoDetectArgsPattern,IconsManager::ACTION_EDIT_SEARCH);
pIconsManager->setIcon(ui->btnTest,IconsManager::ACTION_RUN_RUN);
2021-12-25 23:38:53 +08:00
}
void EnvironmentProgramsWidget::on_btnChooseTerminal_clicked()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("Choose Terminal Program"),
QString(),
tr("All files (%1)").arg(ALL_FILE_WILDCARD));
2021-12-25 23:38:53 +08:00
if (!filename.isEmpty() && fileExists(filename) ) {
ui->txtTerminal->setText(filename);
autoDetectAndUpdateArgumentsPattern(filename);
2021-12-25 23:38:53 +08:00
}
}
void EnvironmentProgramsWidget::on_txtTerminal_textChanged(const QString &terminalPath)
{
const QString &argsPattern = ui->txtArgsPattern->text();
updateCommandPreview(terminalPath, argsPattern);
}
void EnvironmentProgramsWidget::on_txtArgsPattern_textChanged(const QString &argsPattern)
{
const QString &terminalPath = ui->txtTerminal->text();
updateCommandPreview(terminalPath, argsPattern);
}
void EnvironmentProgramsWidget::on_btnAutoDetectArgsPattern_clicked()
{
const QString &terminalPath = ui->txtTerminal->text();
autoDetectAndUpdateArgumentsPattern(terminalPath);
}
void EnvironmentProgramsWidget::on_btnTest_clicked()
{
const QString &terminalPath = ui->txtTerminal->text();
const QString &argsPattern = ui->txtArgsPattern->text();
auto [filename, arguments, fileOwner] = resolveExecArguments(terminalPath, argsPattern);
ExecutableRunner runner(filename, arguments, "", nullptr);
runner.start();
runner.wait();
}