Skip to content

Commit

Permalink
Flipping dialogs to use a shared_ptr rather than a bare pointer.
Browse files Browse the repository at this point in the history
Refs #9756
  • Loading branch information
martyngigg committed Jun 30, 2014
1 parent 7a09835 commit e4eef3d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 70 deletions.
24 changes: 17 additions & 7 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ void MantidUI::executeSaveNexus(QString algName,int version)
Mantid::API::IAlgorithm_sptr alg;
try
{
alg=Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(),version);
alg = Mantid::API::AlgorithmManager::Instance().create(algName.toStdString(),version);

}
catch(...)
Expand All @@ -1330,7 +1330,7 @@ void MantidUI::executeSaveNexus(QString algName,int version)
{
MantidQt::API::InterfaceManager interfaceManager;
MantidQt::API::AlgorithmDialog *dlg =
interfaceManager.createDialog(alg.get(), m_appWindow);
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*>();
Expand Down Expand Up @@ -1422,16 +1422,25 @@ MantidQt::API::AlgorithmDialog* MantidUI::createAlgorithmDialog(Mantid::API::IA
QString optional_msg(alg->summary().c_str());

MantidQt::API::InterfaceManager interfaceManager;
MantidQt::API::AlgorithmDialog *dlg =
interfaceManager.createDialog(alg.get(), m_appWindow, false, presets, optional_msg,enabled);
MantidQt::API::AlgorithmDialog *dlg = interfaceManager.createDialog(alg, m_appWindow, false, presets, optional_msg,enabled);
return dlg;
}


void MantidUI::executeAlgorithm(MantidQt::API::AlgorithmDialog* dlg,Mantid::API::IAlgorithm_sptr alg)
{
if( !dlg ) return;
if ( dlg->exec() == QDialog::Accepted)

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);
Expand All @@ -1442,6 +1451,7 @@ void MantidUI::executeAlgorithm(MantidQt::API::AlgorithmDialog* dlg,Mantid::API:
AlgorithmManager::Instance().removeById(alg->getAlgorithmID());
delete dlg;
}

}

/**
Expand Down Expand Up @@ -1659,7 +1669,7 @@ void MantidUI::renameWorkspace(QStringList wsName)
using namespace MantidQt::API;
InterfaceManager interfaceManager;
AlgorithmDialog *dialog =
interfaceManager.createDialog(alg.get(), m_appWindow, false, presets,
interfaceManager.createDialog(alg, m_appWindow, false, presets,
QString(alg->summary().c_str()));

executeAlgorithm(dialog,alg);
Expand Down Expand Up @@ -2268,7 +2278,7 @@ bool MantidUI::createPropertyInputDialog(const QString & alg_name, const QString

MantidQt::API::InterfaceManager interfaceManager;
MantidQt::API::AlgorithmDialog *dlg =
interfaceManager.createDialog(alg.get(), m_appWindow->getScriptWindowHandle(),
interfaceManager.createDialog(alg, m_appWindow->getScriptWindowHandle(),
true, presets, optional_msg, enabled, disabled);
return (dlg->exec() == QDialog::Accepted);
}
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/Mantid/MantidUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,11 @@ class MantidUI:public QObject
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 loadraw/load nexus algorithm
/// 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
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/AlgorithmDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class EXPORT_OPT_MANTIDQT_API AlgorithmDialog : public QDialog
friend class InterfaceManager;

/// Get the algorithm pointer
Mantid::API::IAlgorithm* getAlgorithm() const;
Mantid::API::IAlgorithm_sptr getAlgorithm() const;

/// Get a pointer to the named property
Mantid::Kernel::Property* getAlgorithmProperty(const QString & propName) const;
Expand Down Expand Up @@ -230,7 +230,7 @@ protected slots:
/// The following methods were made public for testing in GenericDialogDemo.cpp
public:
/// Set the algorithm associated with this dialog
void setAlgorithm(Mantid::API::IAlgorithm*);
void setAlgorithm(Mantid::API::IAlgorithm_sptr);
/// Set a list of suggested values
void setPresetValues(const QHash<QString,QString> & presetValues);
/// Set whether this is intended for use from a script or not
Expand All @@ -245,7 +245,7 @@ protected slots:
/** @name Member variables. */
//@{
/// The algorithm associated with this dialog
Mantid::API::IAlgorithm *m_algorithm;
Mantid::API::IAlgorithm_sptr m_algorithm;

///The name of the algorithm
QString m_algName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ namespace API

void initLayout();

Mantid::API::IAlgorithm * getAlgorithm();
void setAlgorithm(Mantid::API::IAlgorithm * algo);
Mantid::API::IAlgorithm_sptr getAlgorithm();
void setAlgorithm(Mantid::API::IAlgorithm_sptr algo);

QString getAlgorithmName() const;
void setAlgorithmName(QString name);
Expand Down Expand Up @@ -98,17 +98,14 @@ namespace API
QString m_algoName;

/// Pointer to the algorithm to view
Mantid::API::IAlgorithm * m_algo;
Mantid::API::IAlgorithm_sptr m_algo;

/// The grid widget containing the input boxes
QGridLayout *m_inputGrid;

/// The current grid widget for sub-boxes
QGridLayout *m_currentGrid;

/// We own the m_algo pointer and need to delete it.
bool m_deleteAlgo;

/// A map where key = property name; value = the error for this property (i.e. it is not valid).
QHash<QString, QString> m_errors;

Expand Down
17 changes: 11 additions & 6 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/InterfaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ class EXPORT_OPT_MANTIDQT_API InterfaceManager
public:

/// Create a new instance of the correct type of AlgorithmDialog
AlgorithmDialog* createDialog(Mantid::API::IAlgorithm* alg, QWidget* parent = 0,
bool forScript = false, const QHash<QString,QString> & preset_values = (QHash<QString,QString>()),
const QString & optional_msg = QString(), const QStringList & enabled=QStringList(), const QStringList & disabled=QStringList());

/// Create an algorithm dialog for a given algorithm name.
AlgorithmDialog* createDialogFromName(const QString& algorithmName, bool forScript, QWidget* parent = 0);
AlgorithmDialog* createDialog(boost::shared_ptr<Mantid::API::IAlgorithm> alg, QWidget* parent = 0,
bool forScript = false, const QHash<QString,QString> & presetValues = (QHash<QString,QString>()),
const QString & optional_msg = QString(), const QStringList & enabled = QStringList(),
const QStringList & disabled = QStringList());

/// Create an algorithm dialog for a given name and version
AlgorithmDialog* createDialogFromName(const QString& algorithmName, const int version = -1,
QWidget* parent = 0, bool forScript = false,
const QHash<QString,QString> & presetValues = (QHash<QString,QString>()),
const QString & optionalMsg = QString(), const QStringList & enabled = QStringList(),
const QStringList & disabled = QStringList());

/// Create a new instance of the correct type of UserSubWindow
UserSubWindow* createSubWindow(const QString & interface_name, QWidget* parent = 0);
Expand Down
8 changes: 4 additions & 4 deletions Code/Mantid/MantidQt/API/src/AlgorithmDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using Mantid::Kernel::DateAndTime;
* Default Constructor
*/
AlgorithmDialog::AlgorithmDialog(QWidget* parent) :
QDialog(parent), m_algorithm(NULL), m_algName(""), m_algProperties(),
QDialog(parent), m_algorithm(), m_algName(""), m_algProperties(),
m_propertyValueMap(), m_tied_properties(), m_forScript(false), m_python_arguments(),
m_enabled(), m_disabled(), m_strMessage(""), m_msgAvailable(false), m_isInitialized(false), m_autoParseOnInit(true),
m_validators(), m_noValidation(), m_inputws_opts(), m_outputws_fields(), m_wsbtn_tracker()
Expand Down Expand Up @@ -139,7 +139,7 @@ void AlgorithmDialog::saveInput()
* Set the algorithm pointer
* @param alg :: A pointer to the algorithm
*/
void AlgorithmDialog::setAlgorithm(Mantid::API::IAlgorithm* alg)
void AlgorithmDialog::setAlgorithm(Mantid::API::IAlgorithm_sptr alg)
{
m_algorithm = alg;
m_algName = QString::fromStdString(alg->name());
Expand All @@ -164,7 +164,7 @@ void AlgorithmDialog::setAlgorithm(Mantid::API::IAlgorithm* alg)
* Get the algorithm pointer
* @returns A pointer to the algorithm that is associated with the dialog
*/
Mantid::API::IAlgorithm* AlgorithmDialog::getAlgorithm() const
Mantid::API::IAlgorithm_sptr AlgorithmDialog::getAlgorithm() const
{
return m_algorithm;
}
Expand Down Expand Up @@ -452,7 +452,7 @@ bool AlgorithmDialog::isWidgetEnabled(const QString & propName) const
// Regular C++ algo. Let the property tell us,
// possibly using validators, if it is to be shown enabled
if (property->getSettings())
return property->getSettings()->isEnabled(m_algorithm);
return property->getSettings()->isEnabled(getAlgorithm().get());
else
return true;
}
Expand Down
23 changes: 8 additions & 15 deletions Code/Mantid/MantidQt/API/src/AlgorithmPropertiesWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace API
AlgorithmPropertiesWidget::AlgorithmPropertiesWidget(QWidget * parent)
: QWidget(parent),
m_algoName(""),
m_algo(NULL), m_deleteAlgo(false),
m_algo(),
m_inputHistory(NULL)
{
// Create the grid layout that will have all the widgets
Expand Down Expand Up @@ -73,7 +73,6 @@ namespace API
*/
AlgorithmPropertiesWidget::~AlgorithmPropertiesWidget()
{
if (m_deleteAlgo) delete m_algo;
}

//----------------------------------------------------------------------------------------------
Expand All @@ -89,7 +88,7 @@ namespace API

//----------------------------------------------------------------------------------------------
///@return the algorithm being viewed
Mantid::API::IAlgorithm * AlgorithmPropertiesWidget::getAlgorithm()
Mantid::API::IAlgorithm_sptr AlgorithmPropertiesWidget::getAlgorithm()
{
return m_algo;
}
Expand All @@ -98,16 +97,13 @@ namespace API
/** Directly set the algorithm to view. Sets the name to match
*
* @param algo :: IAlgorithm bare ptr */
void AlgorithmPropertiesWidget::setAlgorithm(Mantid::API::IAlgorithm * algo)
void AlgorithmPropertiesWidget::setAlgorithm(Mantid::API::IAlgorithm_sptr algo)
{
if (!algo) return;
saveInput();
if (m_deleteAlgo) delete m_algo;
m_algo = algo;
m_algoName = QString::fromStdString(m_algo->name());
this->initLayout();
// Caller should replace this value as needed
m_deleteAlgo = false;
}

//----------------------------------------------------------------------------------------------
Expand All @@ -127,14 +123,11 @@ namespace API
try
{
Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(m_algoName.toStdString());
AlgorithmProxy * algProxy = new AlgorithmProxy(alg);
auto algProxy = boost::shared_ptr<AlgorithmProxy>(new AlgorithmProxy(alg));
algProxy->initialize();

// Set the algorithm ptr. This will redo the layout
this->setAlgorithm(algProxy);

// Take ownership of the pointer
m_deleteAlgo = true;
}
catch (std::runtime_error & )
{
Expand Down Expand Up @@ -398,7 +391,7 @@ namespace API
// Regular C++ algo. Let the property tell us,
// possibly using validators, if it is to be shown enabled
if (property->getSettings())
return property->getSettings()->isEnabled(m_algo);
return property->getSettings()->isEnabled(m_algo.get());
else
return true;
}
Expand Down Expand Up @@ -430,13 +423,13 @@ namespace API
if (settings)
{
// Set the visible flag
visible = settings->isVisible(m_algo);
visible = settings->isVisible(m_algo.get());

// Dynamic PropertySettings objects allow a property to change validators.
// This removes the old widget and creates a new one instead.
if (settings->isConditionChanged(m_algo))
if (settings->isConditionChanged(m_algo.get()))
{
settings->applyChanges(m_algo, prop);
settings->applyChanges(m_algo.get(), prop);

// Delete the old widget
int row = widget->getGridRow();
Expand Down
52 changes: 28 additions & 24 deletions Code/Mantid/MantidQt/API/src/InterfaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "MantidKernel/Logger.h"
#include "MantidKernel/LibraryManager.h"
#include "MantidKernel/ConfigService.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/Exception.h"

#include <QStringList>
Expand All @@ -26,6 +26,7 @@ namespace
// static logger
Mantid::Kernel::Logger g_log("InterfaceManager");
}

// initialise VATES factory
Mantid::Kernel::AbstractInstantiator<VatesViewerInterface> *InterfaceManager::m_vatesGuiFactory = NULL;
// initialise HelpWindow factory
Expand All @@ -39,46 +40,44 @@ Mantid::Kernel::AbstractInstantiator<MantidHelpInterface> *InterfaceManager::m_h
* @param alg :: A pointer to the algorithm
* @param parent :: An optional parent widget
* @param forScript :: A boolean indicating if this dialog is to be use for from a script or not
* @param preset_values :: TODO: Write description of this variable.
* @param optional_msg :: An optional message string to be placed at the top of the dialog
* @param enabled :: TODO: Write description of this variable.
* @param disabled :: TODO: Write description of this variable.
* @param presetValues :: A hash of property names to preset values for the dialog
* @param optionalMsg :: An optional message string to be placed at the top of the dialog
* @param enabled :: These properties will be left enabled
* @param disabled :: These properties will be left disabled
* @returns An AlgorithmDialog object
*/
AlgorithmDialog* InterfaceManager::createDialog(Mantid::API::IAlgorithm* alg, QWidget* parent,
bool forScript, const QHash<QString,QString> & preset_values,
const QString & optional_msg, const QStringList & enabled, const QStringList & disabled)
AlgorithmDialog*
InterfaceManager::createDialog(boost::shared_ptr<Mantid::API::IAlgorithm> alg, QWidget* parent,
bool forScript, const QHash<QString,QString> & presetValues,
const QString & optionalMsg, const QStringList & enabled, const QStringList & disabled)
{
AlgorithmDialog* dlg = NULL;
if( AlgorithmDialogFactory::Instance().exists(alg->name() + "Dialog") )
{
g_log.debug() << "Creating a specialised dialog for " << alg->name() << std::endl;
dlg = AlgorithmDialogFactory::Instance().createUnwrapped(alg->name() + "Dialog");
}
}
else
{
dlg = new GenericDialog;
g_log.debug() << "No specialised dialog exists for the " << alg->name()
<< " algorithm: a generic one has been created" << std::endl;
<< " algorithm: a generic one has been created" << std::endl;
}

// The parent so that the dialog appears on top of it
dlg->setParent(parent);

// MG 20/07/2009: I have to set the QDialog window flag manually for some reason.
//I assumed that the QDialog
// base class would define this but it doesn't. This should mean that the dialog
//will pop up on top of the relevant widget
// Set the QDialog window flags to ensure the dialog ends up on top
Qt::WindowFlags flags = 0;
flags |= Qt::Dialog;
flags |= Qt::WindowContextHelpButtonHint;
dlg->setWindowFlags(flags);

// Set the content
dlg->setAlgorithm(alg);
dlg->setPresetValues(preset_values);
dlg->setPresetValues(presetValues);
dlg->isForScript(forScript);
dlg->setOptionalMessage(optional_msg);
dlg->setOptionalMessage(optionalMsg);
dlg->addEnabledAndDisableLists(enabled, disabled);

// Setup the layout
Expand All @@ -88,19 +87,24 @@ AlgorithmDialog* InterfaceManager::createDialog(Mantid::API::IAlgorithm* alg, QW
}

/**
* Create an algorithm dialog for a given algorithm name.
* @param algorithmName : Name of the algorithm
* @param forScript : True if this is being run from a script.
* @param parent : Parent widget
* @return new AlgorithmDialog
* @param algorithmName :: Name of AlgorithmDialog
* @param version :: Version number
* @param parent :: An optional parent widget
* @param forScript :: A boolean indicating if this dialog is to be use for from a script or not
* @param presetValues :: A hash of property names to preset values for the dialog
* @param optionalMsg :: An optional message string to be placed at the top of the dialog
* @param enabled :: These properties will be left enabled
* @param disabled :: These properties will be left disabled
*/
AlgorithmDialog* InterfaceManager::createDialogFromName(const QString& algorithmName, bool forScript, QWidget* parent)
AlgorithmDialog* InterfaceManager::createDialogFromName(const QString& algorithmName, const int version, QWidget* parent, bool forScript,
const QHash<QString, QString> &presetValues,
const QString &optionalMsg, const QStringList &enabled, const QStringList &disabled)
{
// Create the algorithm. This should throw if the algorithm can't be found.
auto alg = Mantid::API::FrameworkManager::Instance().createAlgorithm(algorithmName.toStdString());
auto alg = Mantid::API::AlgorithmManager::Instance().create(algorithmName.toStdString(), version);

// Forward call.
return createDialog(alg, parent, forScript);
return createDialog(alg, parent, forScript, presetValues, optionalMsg, enabled, disabled);
}

/**
Expand Down

0 comments on commit e4eef3d

Please sign in to comment.