Debian Codegraph @

src/Gui/Dialogs/DlgRunExternal.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QFileDialog>
24
25 #include "Dialogs/DlgRunExternal.h"
26 #include "ui_DlgRunExternal.h"
27 #include "FileDialog.h"
28
29
30 using namespace Gui::Dialog;
31
32 /* TRANSLATOR Gui::Dialog::DlgRunExternal */
33
34 /**
35 * Constructs a DlgRunExternal which is a child of 'parent', with the
36 * name 'name' and widget flags set to 'f'
37 *
38 * The dialog will by default be modeless, unless you set 'modal' to
39 * true to construct a modal dialog.
40 */
41 DlgRunExternal::DlgRunExternal(QWidget* parent, Qt::WindowFlags fl)
42 : QDialog(parent, fl)
43 , process(this)
44 , advancedHidden(true)
45 , ui(new Ui_DlgRunExternal)
46 {
47 // clang-format off
48 ui->setupUi(this);
49 connect(ui->chooseProgram, &QPushButton::clicked, this, &DlgRunExternal::onChooseProgramClicked);
50 connect(&process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
51 this, qOverload<int, QProcess::ExitStatus>(&DlgRunExternal::finished));
52 connect(ui->buttonAccept, &QPushButton::clicked, this, &DlgRunExternal::accept);
53 connect(ui->buttonDiscard, &QPushButton::clicked, this, &DlgRunExternal::reject);
54 connect(ui->buttonAbort, &QPushButton::clicked, this, &DlgRunExternal::abort);
55 connect(ui->buttonAdvanced, &QPushButton::clicked, this, &DlgRunExternal::advanced);
56 // clang-format on
57
58 ui->gridLayout->setSizeConstraint(QLayout::SetFixedSize);
59 ui->extensionWidget->hide();
60 }
61
62 /**
63 * Destroys the object and frees any allocated resources
64 */
65 DlgRunExternal::~DlgRunExternal() = default;
66
67 void DlgRunExternal::addArgument(const QString& arg)
68 {
69 arguments.append(arg);
70 }
71
72 int DlgRunExternal::runProcess()
73 {
74 QFileInfo ifo(ProcName);
75
76 ui->programName->setText(ifo.baseName());
77 ui->programPath->setText(ProcName);
78 process.start(ProcName, arguments);
79
80 ui->buttonAccept->setEnabled(false);
81 ui->buttonDiscard->setEnabled(false);
82 return exec();
83 }
84
85 void DlgRunExternal::reject()
86 {
87 QDialog::reject();
88 }
89
90 void DlgRunExternal::accept()
91 {
92 QDialog::accept();
93 }
94
95 void DlgRunExternal::abort()
96 {
97 process.terminate();
98 DlgRunExternal::reject();
99 }
100
101 void DlgRunExternal::advanced()
102 {
103 if (advancedHidden) {
104 ui->extensionWidget->show();
105 advancedHidden = false;
106 }
107 else {
108 ui->extensionWidget->hide();
109 advancedHidden = true;
110 }
111 }
112
113 void DlgRunExternal::finished(int exitCode, QProcess::ExitStatus exitStatus)
114 {
115 Q_UNUSED(exitCode);
116 Q_UNUSED(exitStatus);
117 ui->buttonAccept->setEnabled(true);
118 ui->buttonDiscard->setEnabled(true);
119 ui->buttonAbort->setEnabled(false);
120 }
121
122 void DlgRunExternal::onChooseProgramClicked()
123 {
124 QString fn;
125 fn = QFileDialog::getOpenFileName(this, tr("Select a file"), ui->programPath->text());
126 if (!fn.isEmpty()) {
127 ui->programPath->setText(fn);
128 }
129 }
130
131 #include "moc_DlgRunExternal.cpp"