Skip to content

Commit

Permalink
Merge pull request #37611 from robertapplin/37381-use-run-widget-in-s…
Browse files Browse the repository at this point in the history
…imulations

Use RunWidget in Indirect Simulations interface
  • Loading branch information
SilkeSchomann authored Jun 28, 2024
2 parents 782128e + a2c8749 commit 4ce5b3d
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 344 deletions.
2 changes: 2 additions & 0 deletions docs/source/interfaces/indirect/Indirect Simulation.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _interface-indirect-simulation:

Indirect Simulation
===================

Expand Down
1 change: 1 addition & 0 deletions docs/source/release/v6.11.0/Indirect/Bugfixes/37611.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed a crash on the :ref:`interface-indirect-simulation` interface after clicking "Run" without loading data.
101 changes: 35 additions & 66 deletions qt/scientific_interfaces/Indirect/Simulation/DensityOfStates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// SPDX - License - Identifier: GPL - 3.0 +
#include "DensityOfStates.h"

#include "MantidQtWidgets/Common/UserInputValidator.h"

#include "Common/RunWidget/RunView.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/TableRow.h"
#include "MantidQtWidgets/Common/UserInputValidator.h"

#include <QFileInfo>
#include <QString>
Expand All @@ -21,63 +21,56 @@ Mantid::Kernel::Logger g_log("DensityOfStates");
} // namespace

namespace MantidQt::CustomInterfaces {

enum class DensityOfStates::InputFormat : int { Unsupported = 0, Phonon, Castep, ForceConstants };

DensityOfStates::DensityOfStates(QWidget *parent) : SimulationTab(parent) {
m_uiForm.setupUi(parent);
m_runPresenter = std::make_unique<RunPresenter>(this, new RunView(m_uiForm.runWidget));
setOutputPlotOptionsPresenter(
std::make_unique<OutputPlotOptionsPresenter>(m_uiForm.ipoPlotOptions, PlotWidget::Spectra));

connect(m_uiForm.mwInputFile, SIGNAL(filesFound()), this, SLOT(handleFileChange()));

connect(m_uiForm.pbRun, SIGNAL(clicked()), this, SLOT(runClicked()));
connect(m_uiForm.pbSave, SIGNAL(clicked()), this, SLOT(saveClicked()));

m_uiForm.lwIons->setSelectionMode(QAbstractItemView::MultiSelection);
}

void DensityOfStates::setup() {}

/**
* Validate the form to check the program can be run.
*
* @return Whether the form was valid
*/
bool DensityOfStates::validate() {
UserInputValidator uiv;
void DensityOfStates::handleValidation(IUserInputValidator *validator) const {
auto const filename = m_uiForm.mwInputFile->getFirstFilename().toStdString();
if (filename.empty()) {
validator->addErrorMessage("A data file has not been loaded.");
return;
}
auto const format = filenameToFormat(filename);
if (format == InputFormat::Unsupported) {
validator->addErrorMessage("The provided file format is unsupported. The supported extensions are 'phonon', "
"'castep', 'castep_bin' and 'yaml'.");
return;
}

const auto filename = m_uiForm.mwInputFile->getFirstFilename();
InputFormat format = filenameToFormat(filename);
QString specType = m_uiForm.cbSpectrumType->currentText();
auto items = m_uiForm.lwIons->selectedItems();
auto const specType = m_uiForm.cbSpectrumType->currentText();
auto const items = m_uiForm.lwIons->selectedItems();

if (specType == "DensityOfStates" && isPdosFile(format) && items.size() < 1)
uiv.addErrorMessage("Must select at least one ion for DensityOfStates.");

// Give error message when there are errors
if (!uiv.isAllInputValid())
emit showMessageBox(uiv.generateErrorMessage());

return uiv.isAllInputValid();
validator->addErrorMessage("Must select at least one ion for DensityOfStates.");
}

/**
* Configures and executes the DensityOfStates algorithm.
*/
void DensityOfStates::run() {
setRunIsRunning(true);
void DensityOfStates::handleRun() {
clearOutputPlotOptionsWorkspaces();

// Get the SimulatedDensityOfStates algorithm
auto dosAlgo = AlgorithmManager::Instance().create("SimulatedDensityOfStates");

const auto filename = m_uiForm.mwInputFile->getFirstFilename();
const auto filename = m_uiForm.mwInputFile->getFirstFilename().toStdString();
const auto specType = m_uiForm.cbSpectrumType->currentText();
const auto filePropName = formatToFilePropName(filenameToFormat(filename));

// QFileInfo inputFileInfo(filename);

m_outputWsName = QFileInfo(filename).baseName() + "_" + specType;
m_outputWsName = QFileInfo(QString::fromStdString(filename)).baseName() + "_" + specType;

// Set common properties
dosAlgo->setProperty(filePropName, filename.toStdString());
dosAlgo->setProperty(filePropName, filename);
dosAlgo->setProperty("OutputWorkspace", m_outputWsName.toStdString());

const auto peakShape = m_uiForm.cbPeakShape->currentText().toStdString();
Expand Down Expand Up @@ -145,26 +138,25 @@ void DensityOfStates::run() {
void DensityOfStates::dosAlgoComplete(bool error) {
disconnect(m_batchAlgoRunner, SIGNAL(batchComplete(bool)), this, SLOT(dosAlgoComplete(bool)));

setRunIsRunning(false);
if (error)
setSaveEnabled(false);
else
m_runPresenter->setRunEnabled(true);
setSaveEnabled(!error);
if (!error)
setOutputPlotOptionsWorkspaces({m_outputWsName.toStdString()});
}

/**
* Handles a new file being selected by the browser.
*/
void DensityOfStates::handleFileChange() {
QString filename = m_uiForm.mwInputFile->getFirstFilename();
auto const filename = m_uiForm.mwInputFile->getFirstFilename().toStdString();
InputFormat fileFormat = filenameToFormat(filename);
bool pdosAvailable = isPdosFile(fileFormat);

if (pdosAvailable) {
// Load the ion table to populate the list of ions
IAlgorithm_sptr ionTableAlgo = AlgorithmManager::Instance().create("SimulatedDensityOfStates");
ionTableAlgo->initialize();
ionTableAlgo->setProperty(formatToFilePropName(fileFormat), filename.toStdString());
ionTableAlgo->setProperty(formatToFilePropName(fileFormat), filename);
ionTableAlgo->setProperty("SpectrumType", "IonTable");
ionTableAlgo->setProperty("OutputWorkspace", "__dos_ions");

Expand Down Expand Up @@ -227,11 +219,6 @@ void DensityOfStates::ionLoadComplete(bool error) {
*/
void DensityOfStates::loadSettings(const QSettings &settings) { m_uiForm.mwInputFile->readSettings(settings.group()); }

void DensityOfStates::runClicked() {
clearOutputPlotOptionsWorkspaces();
runTab();
}

/**
* Handle saving of workspace
*/
Expand All @@ -241,28 +228,10 @@ void DensityOfStates::saveClicked() {
m_batchAlgoRunner->executeBatchAsync();
}

void DensityOfStates::setRunIsRunning(bool running) {
m_uiForm.pbRun->setText(running ? "Running..." : "Run");
setButtonsEnabled(!running);
}

void DensityOfStates::setButtonsEnabled(bool enabled) {
setRunEnabled(enabled);
setSaveEnabled(enabled);
}

void DensityOfStates::setRunEnabled(bool enabled) { m_uiForm.pbRun->setEnabled(enabled); }

void DensityOfStates::setSaveEnabled(bool enabled) { m_uiForm.pbSave->setEnabled(enabled); }

/**
* Handle file formats
*/

enum class DensityOfStates::InputFormat : int { Unsupported = 0, Phonon, Castep, ForceConstants };

DensityOfStates::InputFormat DensityOfStates::filenameToFormat(QString filename) {
QFileInfo inputFileInfo(filename);
DensityOfStates::InputFormat DensityOfStates::filenameToFormat(std::string const &filename) const {
QFileInfo inputFileInfo(QString::fromStdString(filename));
const auto suffix = inputFileInfo.suffix().toStdString();

InputFormat format;
Expand All @@ -282,7 +251,7 @@ DensityOfStates::InputFormat DensityOfStates::filenameToFormat(QString filename)
return format;
}

std::string DensityOfStates::formatToFilePropName(InputFormat format) {
std::string DensityOfStates::formatToFilePropName(InputFormat const &format) const {
std::string filePropName;

switch (format) {
Expand All @@ -302,7 +271,7 @@ std::string DensityOfStates::formatToFilePropName(InputFormat format) {
return filePropName;
}

bool DensityOfStates::isPdosFile(InputFormat dosFileFormat) {
bool DensityOfStates::isPdosFile(InputFormat const &dosFileFormat) const {
return (dosFileFormat == InputFormat::Phonon) || (dosFileFormat == InputFormat::ForceConstants);
}

Expand Down
24 changes: 10 additions & 14 deletions qt/scientific_interfaces/Indirect/Simulation/DensityOfStates.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,40 @@
#pragma once

#include "../DllConfig.h"
#include "Common/RunWidget/IRunSubscriber.h"
#include "Common/RunWidget/RunPresenter.h"
#include "SimulationTab.h"
#include "ui_DensityOfStates.h"

namespace MantidQt {
namespace CustomInterfaces {
class MANTIDQT_INDIRECT_DLL DensityOfStates : public SimulationTab {
class MANTIDQT_INDIRECT_DLL DensityOfStates : public SimulationTab, public IRunSubscriber {
Q_OBJECT

public:
DensityOfStates(QWidget *parent = nullptr);

QString help() { return "DensityOfStates"; };

void setup() override;
bool validate() override;
void run() override;

/// Load default settings into the interface
void loadSettings(const QSettings &settings) override;

void handleValidation(IUserInputValidator *validator) const override;
void handleRun() override;

private slots:
void dosAlgoComplete(bool error);
void handleFileChange();
void ionLoadComplete(bool error);
void runClicked();
void saveClicked();

private:
void setRunIsRunning(bool running);
void setButtonsEnabled(bool enabled);
void setRunEnabled(bool enabled);
void setSaveEnabled(bool enabled);

enum class InputFormat : int;
InputFormat filenameToFormat(QString filename);
std::string formatToFilePropName(InputFormat format);
bool isPdosFile(InputFormat dosFileFormat);
InputFormat filenameToFormat(std::string const &filename) const;
std::string formatToFilePropName(InputFormat const &format) const;
bool isPdosFile(InputFormat const &dosFileFormat) const;

std::unique_ptr<IRunPresenter> m_runPresenter;
/// The ui form
Ui::DensityOfStates m_uiForm;
/// Name of output workspace
Expand Down
57 changes: 3 additions & 54 deletions qt/scientific_interfaces/Indirect/Simulation/DensityOfStates.ui
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>480</width>
<height>329</height>
<width>500</width>
<height>381</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -515,58 +515,7 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="gbRun">
<property name="title">
<string>Run</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>7</number>
</property>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>194</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pbRun">
<property name="text">
<string>Run</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>193</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="runWidget" native="true"/>
</item>
<item>
<widget class="QGroupBox" name="gbOutput">
Expand Down
Loading

0 comments on commit 4ce5b3d

Please sign in to comment.