Warning, file /multimedia/stopmotion/src/application/externalcommand.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2017 by Linuxstopmotion contributors;              *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "externalcommand.h"
0021 
0022 #include <QByteArray>
0023 #include <QChar>
0024 #include <QHBoxLayout>
0025 #include <QLabel>
0026 #include <QMessageBox>
0027 #include <QTextCursor>
0028 #include <QVBoxLayout>
0029 
0030 
0031 ExternalCommand::ExternalCommand(QWidget *parent)
0032     : QWidget(parent)
0033 {
0034     vboxLayout = new QVBoxLayout(this);
0035     vboxLayout->setSpacing(6);
0036     vboxLayout->setContentsMargins(9, 9, 9, 9);
0037     
0038     textBrowser = new QTextBrowser(this);
0039     vboxLayout->addWidget(textBrowser);
0040 
0041     hboxLayout = new QHBoxLayout();
0042     hboxLayout->setSpacing(6);
0043     hboxLayout->setContentsMargins(0, 0, 0, 0);
0044     
0045     label = new QLabel(this);
0046     label->setText(tr("Input to program:"));
0047     hboxLayout->addWidget(label);
0048 
0049     lineEdit = new QLineEdit(this);
0050     connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(submitInputToProgram())); 
0051     lineEdit->setEchoMode(QLineEdit::Normal);
0052     hboxLayout->addWidget(lineEdit);
0053 
0054     submitButton = new QPushButton(this);
0055     connect(submitButton, SIGNAL(clicked()), this, SLOT(submitInputToProgram()));
0056     submitButton->setText(tr("Submit"));
0057     hboxLayout->addWidget(submitButton);
0058 
0059     closeButton = new QPushButton(this);
0060     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
0061     closeButton->setEnabled(false);
0062     closeButton->setText(tr("Close"));
0063     hboxLayout->addWidget(closeButton);
0064 
0065     vboxLayout->addLayout(hboxLayout);
0066     
0067     process = new QProcess(this);
0068     connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(readFromStandardOutput())); 
0069     connect(process, SIGNAL(readyReadStandardError()), this, SLOT(readFromStandardError())); 
0070     connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(displayExitStatus(int, QProcess::ExitStatus)));
0071     
0072     resize(QSize(593, 363).expandedTo(minimumSizeHint()));
0073     
0074     setAttribute(Qt::WA_DeleteOnClose);
0075     setWindowTitle( tr("Output from external command") );
0076 }
0077 
0078 
0079 void ExternalCommand::run(const QString &command)
0080 {
0081 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
0082     QStringList arguments = QProcess::splitCommand(command);
0083     const QString executalbe = arguments.takeFirst();
0084     process->start(executalbe, arguments);
0085 #else
0086     process->start(command);
0087 #endif
0088 }
0089 
0090 
0091 void ExternalCommand::readFromStandardOutput()
0092 {
0093     textBrowser->ensureCursorVisible();
0094     QTextCursor curs = textBrowser->textCursor();
0095     curs.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
0096     curs.insertText(process->readAllStandardOutput());
0097 }
0098 
0099 
0100 void ExternalCommand::readFromStandardError()
0101 {
0102     textBrowser->ensureCursorVisible();
0103     QTextCursor curs = textBrowser->textCursor();
0104     curs.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
0105     curs.insertText(process->readAllStandardError());
0106 }
0107 
0108 
0109 void ExternalCommand::submitInputToProgram()
0110 {
0111     if ( lineEdit->isModified() ) {
0112         QString input = lineEdit->text();
0113         input.append('\n');
0114         process->write(input.toLocal8Bit());
0115         lineEdit->setText("");
0116     }
0117 }
0118 
0119 
0120 void ExternalCommand::displayExitStatus(int exitCode, QProcess::ExitStatus)
0121 {
0122     if (exitCode != 0) {
0123         QMessageBox::warning(this, tr("Result"), tr("Failed!"));
0124     }
0125     else {                                                     
0126         QMessageBox::information(this, tr("Result"), tr("Successful!"));
0127     }
0128     
0129     lineEdit->setEnabled(false);
0130     submitButton->setEnabled(false);
0131     closeButton->setEnabled(true);
0132 }
0133