Skip to content

Commit

Permalink
Use non-modal calls to show algorithm dialogs
Browse files Browse the repository at this point in the history
The exception is createPropertyInputDialog that is used for scripts and
must remain modal.
Refs #9756
  • Loading branch information
martyngigg committed Jun 30, 2014
1 parent f979a72 commit 00c177b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 193 deletions.
8 changes: 4 additions & 4 deletions Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ MantidDockWidget::MantidDockWidget(MantidUI *mui, ApplicationWindow *parent) :
m_loadMapper->setMapping(loadFileAction,"Load");
connect(liveDataAction,SIGNAL(activated()), m_loadMapper, SLOT(map()));
connect(loadFileAction,SIGNAL(activated()),m_loadMapper,SLOT(map()));
connect(m_loadMapper, SIGNAL(mapped(const QString &)), m_mantidUI, SLOT(executeAlgorithm(const QString&)));
connect(m_loadMapper, SIGNAL(mapped(const QString &)), m_mantidUI, SLOT(showAlgorithmDialog(const QString&)));
m_loadMenu->addAction(loadFileAction);
m_loadMenu->addAction(liveDataAction);
m_loadButton->setMenu(m_loadMenu);
Expand Down Expand Up @@ -1122,15 +1122,15 @@ void MantidDockWidget::treeSelectionChanged()
*/
void MantidDockWidget::convertToMatrixWorkspace()
{
m_mantidUI->executeAlgorithm("ConvertTableToMatrixWorkspace",-1);
m_mantidUI->showAlgorithmDialog(QString("ConvertTableToMatrixWorkspace"),-1);
}

/**
* Convert selected MDHistoWorkspace to a MatrixWorkspace.
*/
void MantidDockWidget::convertMDHistoToMatrixWorkspace()
{
m_mantidUI->executeAlgorithm("ConvertMDHistoToMatrixWorkspace",-1);
m_mantidUI->showAlgorithmDialog(QString("ConvertMDHistoToMatrixWorkspace"),-1);
}

/**
Expand Down Expand Up @@ -1569,7 +1569,7 @@ QDockWidget(w),m_progressBar(NULL),m_algID(),m_mantidUI(mui)
//Add the AlgorithmSelectorWidget
m_selector = new MantidQt::MantidWidgets::AlgorithmSelectorWidget(this);
connect(m_selector,SIGNAL(executeAlgorithm(const QString &, const int)),
m_mantidUI,SLOT(executeAlgorithm(const QString &, const int)));
m_mantidUI,SLOT(showAlgorithmDialog(const QString &, const int)));

m_runningLayout = new QHBoxLayout();
m_runningLayout->setName("testA");
Expand Down
226 changes: 52 additions & 174 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ void MantidUI::init()
m_defaultFitFunction = new MantidQt::MantidWidgets::FitPropertyBrowser(m_appWindow, this);
m_defaultFitFunction->init();
// this make the progress bar work with Fit algorithm running form the fit browser
connect(m_defaultFitFunction,SIGNAL(executeFit(QString,QMap<QString,QString>,Mantid::API::AlgorithmObserver*)),
this,SLOT(executeAlgorithm(QString,QMap<QString,QString>,Mantid::API::AlgorithmObserver*)));
connect(m_defaultFitFunction,SIGNAL(executeFit(QString,QHash<QString,QString>,Mantid::API::AlgorithmObserver*)),
this,SLOT(showAlgorithmDialog(QString,QHash<QString,QString>,Mantid::API::AlgorithmObserver*)));
m_defaultFitFunction->hide();
m_appWindow->addDockWidget( Qt::LeftDockWidgetArea, m_defaultFitFunction );

Expand Down Expand Up @@ -353,7 +353,7 @@ int MantidUI::runningAlgCount() const
*/
void MantidUI::saveNexusWorkspace()
{
executeSaveNexus("SaveNexus",-1);
executeSaveNexus();
}

/**
Expand Down Expand Up @@ -1312,58 +1312,12 @@ saveNexus Input Dialog is a generic dialog.Below code is added to remove
the workspaces except the selected workspace from the InputWorkspace combo
*/
void MantidUI::executeSaveNexus(QString algName,int version)
void MantidUI::executeSaveNexus()
{
QString selctedWsName = getSelectedWorkspaceName();
Mantid::API::IAlgorithm_sptr alg;
try
{
alg = Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(),version);

}
catch(...)
{
QMessageBox::critical(appWindow(),"MantidPlot - Algorithm error","Cannot create algorithm "+algName+" version "+QString::number(version));
return;
}
if (alg)
{
MantidQt::API::InterfaceManager interfaceManager;
MantidQt::API::AlgorithmDialog *dlg =
interfaceManager.createDialog(alg, m_appWindow);
if( !dlg ) return;
//getting the combo box which has input workspaces and removing the workspaces except the selected one
QComboBox *combo = dlg->findChild<QComboBox*>();
if(combo)
{
int count=combo->count();
int index=count-1;
while(count>1)
{
int selectedIndex=combo->findText(selctedWsName,Qt::MatchExactly );
if(selectedIndex!=index)
{
combo->removeItem(index);
count=combo->count();
}
index=index-1;

}

}//end of if loop for combo
if ( dlg->exec() == QDialog::Accepted)
{
delete dlg;
executeAlgorithmAsync(alg);
}
else
{
delete dlg;
}
}



QString wsName = getSelectedWorkspaceName();
QHash<QString,QString> presets;
presets["InputWorkspace"] = wsName;
showAlgorithmDialog("SaveNexus", presets);
}

//-----------------------------------------------------------------------------
Expand All @@ -1373,17 +1327,53 @@ void MantidUI::executeSaveNexus(QString algName,int version)
* @param version :: version number, -1 for latest
* @return true if sucessful.
*/
bool MantidUI::executeAlgorithm(const QString & algName, int version)
void MantidUI::showAlgorithmDialog(const QString & algName, int version)
{
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, version);
if( !alg ) return false;
MantidQt::API::AlgorithmDialog* dlg=createAlgorithmDialog(alg);
executeAlgorithm(dlg,alg);
return true;
if( !alg ) return;
MantidQt::API::AlgorithmDialog* dlg = createAlgorithmDialog(alg);
dlg->show();
dlg->raise();
dlg->activateWindow();
}

/**
* Execute an algorithm. Show the algorithm dialog before executing. The property widgets will be preset
* with values in paramList.
* @param algName :: The algorithm name
* @param paramList :: A list of algorithm properties to be passed to Algorithm::setProperties
* @param obs :: A pointer to an instance of AlgorithmObserver which will be attached to the finish notification
*/
void MantidUI::showAlgorithmDialog(QString algName, QHash<QString,QString> paramList,Mantid::API::AlgorithmObserver* obs)
{
//Get latest version of the algorithm
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, -1);
if( !alg ) return;
if (obs)
{
obs->observeFinish(alg);
}
for(QHash<QString,QString>::Iterator it = paramList.begin(); it != paramList.end(); ++it)
{
alg->setPropertyValue(it.key().toStdString(),it.value().toStdString());
}
MantidQt::API::AlgorithmDialog* dlg = createAlgorithmDialog(alg);
dlg->show();
dlg->raise();
dlg->activateWindow();
}

/**
* Slot for executing an algorithm.
* @param alg :: Shared pointer to an algorithm to execute with all properties already set.
*/
void MantidUI::executeAlgorithm(Mantid::API::IAlgorithm_sptr alg)
{
executeAlgorithmAsync(alg);
}


/**
/**
* This creates an algorithm dialog (the default property entry thingie).
*/
MantidQt::API::AlgorithmDialog* MantidUI::createAlgorithmDialog(Mantid::API::IAlgorithm_sptr alg)
Expand Down Expand Up @@ -1426,108 +1416,6 @@ MantidQt::API::AlgorithmDialog* MantidUI::createAlgorithmDialog(Mantid::API::IA
return dlg;
}


void MantidUI::executeAlgorithm(MantidQt::API::AlgorithmDialog* dlg,Mantid::API::IAlgorithm_sptr alg)
{
if( !dlg ) return;

dlg->setModal(false);
dlg->setAttribute(Qt::WA_DeleteOnClose, false); // the result() method only works with this off
dlg->show();
dlg->activateWindow();

while(dlg->isVisible())
{
QCoreApplication::processEvents();
}
if(dlg->result() == QDialog::Accepted)
{
delete dlg;
executeAlgorithmAsync(alg);
}
else
{
using Mantid::API::AlgorithmManager;
AlgorithmManager::Instance().removeById(alg->getAlgorithmID());
delete dlg;
}

}

/**
* Execute an algorithm
* @param algName :: The algorithm name
* @param paramList :: A list of algorithm properties to be passed to Algorithm::setProperties
* @param obs :: A pointer to an instance of AlgorithmObserver which will be attached to the finish notification
*/
void MantidUI::executeAlgorithm(const QString & algName, const QString & paramList,Mantid::API::AlgorithmObserver* obs)
{
//Get latest version of the algorithm
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, -1);
if( !alg ) return;
if (obs)
{
obs->observeFinish(alg);
}
alg->setProperties(paramList.toStdString());
executeAlgorithmAsync(alg);
}

/**
* Execute an algorithm
* @param algName :: The algorithm name
* @param paramList :: A list of algorithm properties to be passed to Algorithm::setProperties
* @param obs :: A pointer to an instance of AlgorithmObserver which will be attached to the finish notification
*/
void MantidUI::executeAlgorithm(QString algName, QMap<QString,QString> paramList,Mantid::API::AlgorithmObserver* obs)
{
//Get latest version of the algorithm
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, -1);
if( !alg ) return;
if (obs)
{
obs->observeFinish(alg);
}
for(QMap<QString,QString>::Iterator it = paramList.begin(); it != paramList.end(); ++it)
{
alg->setPropertyValue(it.key().toStdString(),it.value().toStdString());
}
executeAlgorithmAsync(alg);
}

/**
* Execute an algorithm. Show the algorithm dialog before executing. The property widgets will be preset
* with values in paramList.
* @param algName :: The algorithm name
* @param paramList :: A list of algorithm properties to be passed to Algorithm::setProperties
* @param obs :: A pointer to an instance of AlgorithmObserver which will be attached to the finish notification
*/
void MantidUI::executeAlgorithmDlg(QString algName, QMap<QString,QString> paramList,Mantid::API::AlgorithmObserver* obs)
{
//Get latest version of the algorithm
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, -1);
if( !alg ) return;
if (obs)
{
obs->observeFinish(alg);
}
for(QMap<QString,QString>::Iterator it = paramList.begin(); it != paramList.end(); ++it)
{
alg->setPropertyValue(it.key().toStdString(),it.value().toStdString());
}
MantidQt::API::AlgorithmDialog* dlg=createAlgorithmDialog(alg);
executeAlgorithm(dlg,alg);
}

/**
* Slot for executing an algorithm.
* @param alg :: Shared pointer to an algorithm to execute with all properties already set.
*/
void MantidUI::executeAlgorithm(Mantid::API::IAlgorithm_sptr alg)
{
executeAlgorithmAsync(alg);
}

/**
* Find the first input workspace for an algorithm
* @param algorithm :: A pointer to the algorithm instance
Expand Down Expand Up @@ -1649,12 +1537,9 @@ void MantidUI::renameWorkspace(QStringList wsName)
}
}

//Determine the algorithm
// Determine the algorithm
QString algName("RenameWorkspace");
if(wsName.size() > 1) algName = "RenameWorkspaces";
int version=-1;
auto alg = createAlgorithm(algName, version);
if(!alg) return;

QHash<QString,QString> presets;
if(wsName.size() > 1 )
Expand All @@ -1665,14 +1550,7 @@ void MantidUI::renameWorkspace(QStringList wsName)
{
presets["InputWorkspace"] = wsName[0];
}

using namespace MantidQt::API;
InterfaceManager interfaceManager;
AlgorithmDialog *dialog =
interfaceManager.createDialog(alg, m_appWindow, false, presets,
QString(alg->summary().c_str()));

executeAlgorithm(dialog,alg);
showAlgorithmDialog(algName, presets);
}

void MantidUI::setFitFunctionBrowser(MantidQt::MantidWidgets::FitPropertyBrowser* newBrowser)
Expand Down
17 changes: 4 additions & 13 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,12 @@ class MantidUI:public QObject
void convertToWaterfall(MultiLayer* ml);

// Execute algorithm given name and version
bool executeAlgorithm(const QString & algName, int version = -1);
void showAlgorithmDialog(const QString & algName, int version = -1);
//Execute an algorithm with the given parameter list
void executeAlgorithm(const QString & algName, const QString & paramList,Mantid::API::AlgorithmObserver* obs = NULL);
//Execute an algorithm with the given parameter list
void executeAlgorithm(QString algName, QMap<QString,QString> paramList,Mantid::API::AlgorithmObserver* obs = NULL);
//Execute an algorithm with the given parameter list
void executeAlgorithmDlg(QString algName, QMap<QString,QString> paramList,Mantid::API::AlgorithmObserver* obs = NULL);
void showAlgorithmDialog(QString algName, QHash<QString, QString> paramList, Mantid::API::AlgorithmObserver* obs = NULL);
// Execute an algorithm
void executeAlgorithm(Mantid::API::IAlgorithm_sptr alg);

// Find the name of the first input workspace for an algorithm
QString findInputWorkspaceProperty(Mantid::API::IAlgorithm_sptr algorithm) const;
// Show Qt critical error message box
Expand All @@ -393,8 +390,6 @@ class MantidUI:public QObject

void manageMantidWorkspaces();



//Python related functions
InstrumentWindow* getInstrumentView(const QString & wsName, int tab = -1);

Expand Down Expand Up @@ -481,18 +476,14 @@ class MantidUI:public QObject

//#678
//for savenexus algorithm
void executeSaveNexus(QString algName,int version);
void executeSaveNexus();

void copyWorkspacestoVector(const QList<QTreeWidgetItem*> &list,std::vector<std::string> &inputWS);
void PopulateData(Mantid::API::Workspace_sptr ws_ptr,QTreeWidgetItem* wsid_item);


/// This creates an algorithm dialog.
MantidQt::API::AlgorithmDialog * createAlgorithmDialog(Mantid::API::IAlgorithm_sptr alg);

/// This method accepts user inputs and executes algorithm
void executeAlgorithm(MantidQt::API::AlgorithmDialog* dlg,Mantid::API::IAlgorithm_sptr alg);

/// This method accepts user inputs and executes loadraw/load nexus algorithm
std::string extractLogTime(Mantid::Kernel::DateAndTime value,bool useAbsoluteDate, Mantid::Kernel::DateAndTime start);

Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/MantidQt/API/src/InterfaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ InterfaceManager::createDialog(boost::shared_ptr<Mantid::API::IAlgorithm> alg, Q

// The parent so that the dialog appears on top of it
dlg->setParent(parent);
dlg->setAttribute(Qt::WA_DeleteOnClose, true);

// Set the QDialog window flags to ensure the dialog ends up on top
Qt::WindowFlags flags = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include "WidgetDllOption.h"

#include <QDockWidget>
#include <QMap>
#include <QHash>
#include <QList>
#include <QMap>

#include "MantidQtAPI/WorkspaceObserver.h"
#include "MantidAPI/CompositeFunction.h"
Expand Down Expand Up @@ -258,7 +259,7 @@ public slots:
void removePlotSignal(MantidQt::MantidWidgets::PropertyHandler*);
void removeFitCurves();

void executeFit(QString,QMap<QString,QString>,Mantid::API::AlgorithmObserver*);
void executeFit(QString,QHash<QString,QString>,Mantid::API::AlgorithmObserver*);
void multifitFinished();

/// signal which can optionally be caught for customization after a fit has
Expand Down

0 comments on commit 00c177b

Please sign in to comment.