1414
1515#include < QFileDialog>
1616#include < QMessageBox>
17+ #include < QStandardPaths>
1718
1819#include < iostream>
1920
@@ -28,55 +29,25 @@ AddCompilerWizard::AddCompilerWizard(QWidget *parent) : QWizard(parent), ui(new
2829 new QRegularExpressionValidator (QRegularExpression (" (\\ w+;)*\\ w+" ), this ));
2930 ui->javaMemoryLimit ->setValidator (new QIntValidator (64 , 2048 , this ));
3031
31- #ifdef Q_OS_WIN
32+ const auto setIfEmpty = [](QLineEdit *target, const QStringList &candidates) {
33+ if (! target->text ().isEmpty ())
34+ return ;
3235
33- for (auto dir : qEnvironmentVariable (" PATH" ).split (' ;' )) {
34- for (auto file : QDir (dir).entryList (QDir::Files)) {
35- if (ui->gccPath ->text ().isEmpty () && file == " gcc.exe" )
36- ui->gccPath ->setText (dir + QDir::separator () + file);
37-
38- if (ui->gppPath ->text ().isEmpty () && file == " g++.exe" )
39- ui->gppPath ->setText (dir + QDir::separator () + file);
40-
41- if (ui->fpcPath ->text ().isEmpty () && file == " fpc.exe" )
42- ui->fpcPath ->setText (dir + QDir::separator () + file);
43-
44- if (ui->javacPath ->text ().isEmpty () && file == " javac.exe" )
45- ui->javacPath ->setText (dir + QDir::separator () + file);
46-
47- if (ui->javaPath ->text ().isEmpty () && file == " java.exe" )
48- ui->javaPath ->setText (dir + QDir::separator () + file);
49-
50- if (ui->pythonPath ->text ().isEmpty () && file == " python.exe" )
51- ui->pythonPath ->setText (dir + QDir::separator () + file);
52- }
53- }
54-
55- #else
56-
57- for (auto dir : qEnvironmentVariable (" PATH" ).split (' :' )) {
58- for (auto file : QDir (dir).entryList (QDir::Files)) {
59- if (ui->gccPath ->text ().isEmpty () && file == " gcc" )
60- ui->gccPath ->setText (dir + QDir::separator () + file);
61-
62- if (ui->gppPath ->text ().isEmpty () && file == " g++" )
63- ui->gppPath ->setText (dir + QDir::separator () + file);
64-
65- if (ui->fpcPath ->text ().isEmpty () && file == " fpc" )
66- ui->fpcPath ->setText (dir + QDir::separator () + file);
67-
68- if (ui->javacPath ->text ().isEmpty () && file == " javac" )
69- ui->javacPath ->setText (dir + QDir::separator () + file);
70-
71- if (ui->javaPath ->text ().isEmpty () && file == " java" )
72- ui->javaPath ->setText (dir + QDir::separator () + file);
73-
74- if (ui->pythonPath ->text ().isEmpty () && file == " python" )
75- ui->pythonPath ->setText (dir + QDir::separator () + file);
36+ for (const auto &candidate : candidates) {
37+ const auto executable = QStandardPaths::findExecutable (candidate);
38+ if (! executable.isEmpty ()) {
39+ target->setText (QDir::toNativeSeparators (executable));
40+ return ;
41+ }
7642 }
77- }
43+ };
7844
79- #endif
45+ setIfEmpty (ui->gccPath , {" gcc" });
46+ setIfEmpty (ui->gppPath , {" g++" });
47+ setIfEmpty (ui->fpcPath , {" fpc" });
48+ setIfEmpty (ui->javacPath , {" javac" });
49+ setIfEmpty (ui->javaPath , {" java" });
50+ setIfEmpty (ui->pythonPath , {" python" });
8051 connect (ui->typeSelect , qOverload<int >(&QComboBox::currentIndexChanged), this ,
8152 &AddCompilerWizard::compilerTypeChanged);
8253 connect (ui->compilerSelectButton , &QToolButton::clicked, this ,
0 commit comments