Commit 0b00eb8d authored by John Tourtellott's avatar John Tourtellott
Browse files

Add logic to get working directory, plus some refactoring

parent 2c4144b3
......@@ -21,14 +21,17 @@
// Qt
#include <QAction>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QProcess>
#include <QStringList>
#include <QWidget>
pqCubitToolBar::pqCubitToolBar(QWidget* parent)
: Superclass("CUBIT Application", parent)
, m_cubitPID(0)
{
this->setObjectName("CUBIT Executable");
......@@ -39,6 +42,114 @@ pqCubitToolBar::pqCubitToolBar(QWidget* parent)
pqCubitToolBar::~pqCubitToolBar() = default;
void pqCubitToolBar::onLaunch()
{
QWidget* mainWidget = pqCoreUtilities::mainWidget();
// Have we been here before?
if (m_cubitPID != 0)
{
QString text = "You have already started CUBIT from modelbuilder before."
" Do you want to continue anyway?";
int choice = QMessageBox::question(mainWidget, "CUBIT Already Running?", text);
if (choice != QMessageBox::Yes)
{
return;
}
}
QString cubitPath;
if (!this->getCubitPath(cubitPath))
{
return;
}
QString workspacePath;
if (!this->getWorkspacePath(workspacePath))
{
return;
}
// Make sure workspace/cubit folder exists
QDir workspaceDir(workspacePath);
QDir workingDir(workspaceDir);
if (!workingDir.cd("cubit"))
{
if (!workspaceDir.mkdir("cubit"))
{
QMessageBox::warning(mainWidget, "Cannot Create working Directory",
"Error creating working directory " + workingDir.canonicalPath());
return;
}
workingDir.setPath(workspaceDir.path());
workingDir.cd("cubit");
}
// Start cubit as a detached process
bool success = this->runCubit(cubitPath, workingDir.canonicalPath());
if (!success)
{
QMessageBox::warning(mainWidget, "CUBIT LAUNCH FAILED", "The system was unable to launch CUBIT.");
return;
}
}
void pqCubitToolBar::askUserForPath(const QString& headline, const QString& details, QString& result) const
{
QWidget* mainWidget = pqCoreUtilities::mainWidget();
QMessageBox msgBox(mainWidget);
msgBox.setText(headline);
msgBox.setInformativeText(details);
msgBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
msgBox.setDefaultButton(QMessageBox::Yes);
int msgRet = msgBox.exec();
if (msgRet != QMessageBox::Yes)
{
result.clear();
return;
}
QFileDialog fileDialog(mainWidget, "CUBIT executable");
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setFileMode(QFileDialog::ExistingFile);
bool fileRet = fileDialog.exec();
if (fileRet != QDialog::Accepted)
{
result.clear();
}
else
{
result = fileDialog.selectedFiles().at(0);
}
}
bool pqCubitToolBar::checkCubitPath(const QString& path, QString& reason) const
{
QFileInfo fileInfo(path);
if (!fileInfo.exists())
{
reason = "File was not found at " + path + ".";
return false;
}
if (!fileInfo.isExecutable())
{
reason = "The file is not executable: " + path + ".";
return false;
}
if (!fileInfo.permission(QFileDevice::ExeUser))
{
reason = "The current user does not have permission to run " + path + ".";
return false;
}
return true;
}
bool pqCubitToolBar::getCubitPath(QString& usePath) const
{
// Get the CUBIT path from application settings
QWidget* mainWidget = pqCoreUtilities::mainWidget();
......@@ -48,16 +159,16 @@ void pqCubitToolBar::onLaunch()
{
QMessageBox::warning(
mainWidget, "Internal Error", "Internal Error: Settings proxy for ACE3P not found.");
return;
return false;
}
vtkSMProperty* cubitProp = proxy->GetProperty("CubitPath");
auto* cubitStringProp = vtkSMStringVectorProperty::SafeDownCast(cubitProp);
if (!cubitStringProp)
{
QMessageBox::warning(
QMessageBox::critical(
mainWidget, "Internal Error", "Internal Error: CubitPath not found in ACE3P settings.");
return;
return false;
}
std::string cubitPath = cubitStringProp->GetElement(0);
......@@ -83,7 +194,7 @@ void pqCubitToolBar::onLaunch()
// Check path
QString reason;
if (this->checkPath(path, reason))
if (this->checkCubitPath(path, reason))
{
done = true;
break;
......@@ -99,91 +210,89 @@ void pqCubitToolBar::onLaunch()
if (path.isEmpty())
{
return;
return false;
}
// (else)
usePath = path;
cubitPath = path.toStdString();
// Update settings
cubitStringProp->SetElement(0, cubitPath.c_str());
proxy->UpdateVTKObjects();
//QMessageBox::information(mainWidget, "CUBIT PATH FOUND", path);
// Run the process
bool success = this->runCubit(path);
if (!success)
{
QMessageBox::warning(mainWidget, "CUBIT LAUNCH FAILED", "The system was unable to launch CUBIT.");
}
return true;
}
void pqCubitToolBar::askUserForPath(const QString& headline, const QString& details, QString& result) const
bool pqCubitToolBar::getWorkspacePath(QString& usePath) const
{
// Get the Workspace path from application settings
QWidget* mainWidget = pqCoreUtilities::mainWidget();
QMessageBox msgBox(mainWidget);
msgBox.setText(headline);
msgBox.setInformativeText(details);
msgBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
msgBox.setDefaultButton(QMessageBox::Yes);
int msgRet = msgBox.exec();
if (msgRet != QMessageBox::Yes)
{
result.clear();
return;
}
QFileDialog fileDialog(mainWidget, "CUBIT executable");
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setFileMode(QFileDialog::ExistingFile);
bool fileRet = fileDialog.exec();
if (fileRet != QDialog::Accepted)
{
result.clear();
}
else
pqServer* server = pqActiveObjects::instance().activeServer();
vtkSMProxy* proxy = server->proxyManager()->GetProxy("settings", "SMTKSettings");
if (!proxy)
{
result = fileDialog.selectedFiles().at(0);
QMessageBox::critical(
mainWidget, "Internal Error", "Internal Error: Settings proxy for SMTK not found.");
return false;
}
}
bool pqCubitToolBar::checkPath(const QString& path, QString& reason) const
{
QFileInfo fileInfo(path);
if (!fileInfo.exists())
vtkSMProperty* folderProp = proxy->GetProperty("ProjectsRootFolder");
auto* folderStringProp = vtkSMStringVectorProperty::SafeDownCast(folderProp);
if (!folderStringProp)
{
reason = "File was not found at " + path + ".";
QMessageBox::critical(
mainWidget, "Internal Error", "Internal Error: ProjectsRootFolder not found in SMTK settings.");
return false;
}
std::string folderPath = folderStringProp->GetElement(0);
QString path = QString::fromStdString(folderPath);
if (!fileInfo.isExecutable())
if (path.isEmpty())
{
reason = "The file is not executable: " + path + ".";
return false;
}
QString headline = "The projects workspace folder has not been specified.";
QString details = "This setting is required before you can run CUBIT."
" Do you want to set it now?";
this->askUserForPath(headline, details, path);
if (path.isEmpty())
{
return false;
}
} // if (path empty)
if (!fileInfo.permission(QFileDevice::ExeUser))
// Check that path exists
QDir projectsFolder(path);
if (!projectsFolder.exists() && !projectsFolder.mkpath(path))
{
reason = "The current user does not have permission to run " + path + ".";
QString details = "The system could not create a folder at " + path;
QMessageBox::warning(mainWidget, "Unable to create projects folder", details);
return false;
}
// (else)
usePath = path;
// Update settings
folderStringProp->SetElement(0, path.toStdString().c_str());
proxy->UpdateVTKObjects();
return true;
}
bool pqCubitToolBar::runCubit(const QString& path) const
bool pqCubitToolBar::runCubit(const QString& path, const QString& workingDir)
{
QProcess process;
process.setProgram(path);
// process.setArguments();
// process.setWorkingDirectory();
QStringList args;
args << "-workingdir" << workingDir;
process.setArguments(args);
process.setWorkingDirectory(workingDir); // backup
qint64 pid;
qInfo() << "Starting" << path;
bool success = process.startDetached(&pid);
if (success)
{
qInfo() << "Started process" << pid;
qInfo() << "Started" << path << "as process" << pid;
qInfo() << "Working directory" << workingDir;
m_cubitPID = pid;
}
return success;
}
......@@ -13,6 +13,7 @@
#include "smtk/PublicPointerDefs.h"
#include <QString>
#include <QtGlobal> // qint64
#include <QToolBar>
......@@ -25,13 +26,18 @@ public:
pqCubitToolBar(QWidget* parent = nullptr);
~pqCubitToolBar() override;
protected slots:
void onLaunch();
protected:
void askUserForPath(const QString& headline, const QString& details, QString& result) const;
bool checkPath(const QString& path, QString& reason) const;
bool runCubit(const QString& path) const;
bool checkCubitPath(const QString& path, QString& reason) const;
bool getCubitPath(QString& path) const;
bool getWorkspacePath(QString& path) const;
bool runCubit(const QString& path, const QString& workingDir);
protected slots:
void onLaunch();
// Remember if we already started CUBIT before
qint64 m_cubitPID;
private:
Q_DISABLE_COPY(pqCubitToolBar);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment