// private slot void RHydroSimtor::Delegate::testButton_clicked() { static const char* mname ("RHydroSimtor::Delegate::testButton_clicked"); // DIAG() << mname; // ********************************************* // *** (1) Open "R" Script File for Writing *** // ********************************************* const QString plugDir = UtilsNS::pluginDir(); const QFileInfo scriptFileInfo (plugDir, "RHydroSimtor.R"); const QString scriptPath = scriptFileInfo.absoluteFilePath(); QFile scriptFile (scriptPath); const bool openOk = scriptFile.open (QIODevice::WriteOnly); if (!openOk) { DIAG() << mname << " ERROR Opening '" << qPrintable (scriptPath) << "'"; QApplication::beep(); return; } // DIAG() << mname << " Opened '" << qPrintable (scriptPath) << "'"; // ***************************************** // *** (2) Prepare Script Response File *** // ***************************************** const QFileInfo scriptRespFileInfo (plugDir, "RHydroSimtor.out"); const QString scriptOutPath = scriptRespFileInfo.absoluteFilePath(); QFile scriptOutFile (scriptOutPath); if (scriptOutFile.exists()) { const bool removeOk = scriptOutFile.remove(); if (!removeOk) { DIAG() << mname << " ERROR Removing '" << qPrintable (scriptOutPath) << "'"; QApplication::beep(); return; } } // ********************************** // *** (3) Write "R" Script File *** // ********************************** // Get Test Parameter const QString userInput = _testLineEdit->text(); // Script Header Comment String QString headerCmtStr = "RHydroSimtor.R script. Generated: "; headerCmtStr += QDateTime::currentDateTime().toString(); static const QString scriptFmt ( "# %1\n" "Sys.sleep (1.5)\n" "resultFile <- file (\"%2\")\n" "resultStr <- if (\"%3\" == \"21\") \"CORRECT\" else \"INCORRECT\"\n" "write (resultStr, resultFile)\n" "close (resultFile)\n"); const QString scriptStr = scriptFmt .arg (headerCmtStr) .arg (scriptOutPath) .arg (userInput); scriptFile.write (qPrintable (scriptStr)); scriptFile.close(); // **************************************** // *** (4) Execute the "R" Script File *** // **************************************** static const QString cmdStrFmt ("Rscript --slave %1"); const QString cmdStr = cmdStrFmt .arg (scriptPath); QProcess cmdProc (this); cmdProc.start (cmdStr); bool didFinish (false); int waitCount (0); while (!didFinish && (waitCount < 30)) { didFinish = cmdProc.waitForFinished (100); // milliseconds ++waitCount; } const QProcess::ExitStatus exStat = cmdProc.exitStatus(); QString resultMsg (""); if (didFinish) { // ******************************* // *** (5) Read Script Result *** // ******************************* const bool openOk = scriptOutFile.open (QIODevice::ReadOnly); if (!openOk) { resultMsg = "ERROR Result Missing '" + scriptOutPath + "'"; } else { const QByteArray resultChars = scriptOutFile.readAll(); resultMsg = QString (resultChars); } } else // (not didFinish) { resultMsg = "Did not finish, ExitStatus: ["; resultMsg += QString::number ((int) exStat) + "] "; resultMsg += ((exStat == QProcess::NormalExit) ? "NORMAL" : "CRASH"); cmdProc.kill(); } const QString waitSecs = QString::number (waitCount / 10.0) + " secs"; DIAG() << mname << "; INPUT '" << qPrintable (userInput) << "'" << "; TIME: " << qPrintable (waitSecs) << "; RESULT: " << qPrintable (resultMsg.trimmed()); }