Newer
Older
Gigg, Martyn Anthony
committed
//----------------------------------
// Includes
//----------------------------------
#include "MantidQtAPI/AlgorithmDialog.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
Gigg, Martyn Anthony
committed
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/FileValidator.h"
Gigg, Martyn Anthony
committed
#include <QIcon>
#include <QLabel>
#include <QMessageBox>
Gigg, Martyn Anthony
committed
#include <QFileDialog>
#include <QFileInfo>
#include <QLineEdit>
Gigg, Martyn Anthony
committed
using namespace MantidQt::API;
//------------------------------------------------------
// Public member functions
//------------------------------------------------------
/**
* Default Constructor
*/
AlgorithmDialog::AlgorithmDialog(QWidget* parent) :
Gigg, Martyn Anthony
committed
QDialog(parent), m_algorithm(NULL), m_algName(""), m_propertyValueMap(), m_forScript(false), m_strMessage(""),
m_msgAvailable(false), m_bIsInitialized(false), m_algProperties(), m_validators()
Gigg, Martyn Anthony
committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{
}
/**
* Destructor
*/
AlgorithmDialog::~AlgorithmDialog()
{
}
/**
* Create the layout for this dialog.
*/
void AlgorithmDialog::initializeLayout()
{
if( isInitialized() ) return;
//Set a common title
setWindowTitle(QString::fromStdString(getAlgorithm()->name()) + " input dialog");
//Set the icon
setWindowIcon(QIcon(":/MantidPlot_Icon_32offset.png"));
createValidatorLabels();
//Calls the derived class function
this->initLayout();
//Check which properties are already valid. This also hides the validation marker
//if a property is valid.
validateProperties();
m_bIsInitialized = true;
}
/**
* Has this dialog been initialized yet
* @returns Whether initialzedLayout has been called yet
*/
bool AlgorithmDialog::isInitialized() const
{
return m_bIsInitialized;
}
//------------------------------------------------------
// Protected member functions
//------------------------------------------------------
/**
* Get the algorithm pointer
* @returns A pointer to the algorithm that is associated with the dialog
*/
Mantid::API::IAlgorithm* AlgorithmDialog::getAlgorithm() const
Gigg, Martyn Anthony
committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{
return m_algorithm;
}
/**
* Are we for a script or not
* @returns A boolean inidcating whether we are being called from a script
*/
bool AlgorithmDialog::isForScript() const
{
return m_forScript;
}
/**
* Return the message string
* @returns the message string
*/
const QString & AlgorithmDialog::getOptionalMessage() const
{
return m_strMessage;
}
/*
* Is there a message string available
* @returns A boolean indicating whether the message string is empty
*/
bool AlgorithmDialog::isMessageAvailable() const
{
return !m_strMessage.isEmpty();
}
/**
* Get a named property for this algorithm
* @param propName The name of the property
*/
Mantid::Kernel::Property* AlgorithmDialog::getAlgorithmProperty(const QString & propName) const
{
if( m_algProperties.contains(propName) ) return m_algProperties[propName];
else return NULL;
}
/**
* Get a property validator label
*/
QLabel* AlgorithmDialog::getValidatorMarker(const QString & propname) const
{
return m_validators[propname];
}
/**
* Adds a property (name,value) pair to the stored map
*/
void AlgorithmDialog::addPropertyValueToMap(const QString & name, const QString & value)
{
if( name.isEmpty() || value.isEmpty() ) return;
m_propertyValueMap.insert(name, value);
}
bool AlgorithmDialog::validateProperties()
{
bool allValid(true);
Gigg, Martyn Anthony
committed
QHash<QString, Mantid::Kernel::Property*>::const_iterator pend = m_algProperties.end();
for( QHash<QString, Mantid::Kernel::Property*>::const_iterator pitr = m_algProperties.begin();
Gigg, Martyn Anthony
committed
pitr != pend; ++pitr )
{
const Mantid::Kernel::Property *prop = pitr.value();
QLabel *validator = getValidatorMarker(pitr.key());
Gigg, Martyn Anthony
committed
// Here we have a property that was able to be set
if( prop->isValid() )
Gigg, Martyn Anthony
committed
{
Gigg, Martyn Anthony
committed
validator->hide();
Gigg, Martyn Anthony
committed
}
else
{
allValid = false;
Gigg, Martyn Anthony
committed
if( validator->parent() ) validator->show();
Gigg, Martyn Anthony
committed
}
}
return allValid;
}
/**
* Set the properties that have been parsed from the dialog.
* @returns A boolean that indicates if the validation was successful.
*/
bool AlgorithmDialog::setPropertyValues()
{
Gigg, Martyn Anthony
committed
QHash<QString, Mantid::Kernel::Property*>::const_iterator pend = m_algProperties.end();
Gigg, Martyn Anthony
committed
QString algName = QString::fromStdString(getAlgorithm()->name());
AlgorithmInputHistory::Instance().clearAlgorithmInput(algName);
Gigg, Martyn Anthony
committed
bool allValid(true);
Gigg, Martyn Anthony
committed
for( QHash<QString, Mantid::Kernel::Property*>::const_iterator pitr = m_algProperties.begin();
Gigg, Martyn Anthony
committed
pitr != pend; ++pitr )
{
Mantid::Kernel::Property *prop = pitr.value();
QString pName = pitr.key();
Gigg, Martyn Anthony
committed
QString value = m_propertyValueMap.value(pName);
QLabel *validator = getValidatorMarker(pitr.key());
if( !value.isEmpty() )
{
prop->setValue(value.toStdString());
}
if( prop->isValid() )
{
validator->hide();
//Store value for future input
AlgorithmInputHistory::Instance().storeNewValue(algName, QPair<QString, QString>(pName, value));
}
else
{
allValid = false;
if( validator->parent() ) validator->show();
}
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
// return validateProperties();
return allValid;
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
/**
* Open a file selection box
* @param The property name that this is associated with
*/
QString AlgorithmDialog::openLoadFileDialog(const QString & propName)
{
if( propName.isEmpty() ) return "";
Gigg, Martyn Anthony
committed
Mantid::Kernel::PropertyWithValue<std::string>* prop =
dynamic_cast< Mantid::Kernel::PropertyWithValue<std::string>* >( getAlgorithmProperty(propName) );
Gigg, Martyn Anthony
committed
if( !prop ) return "";
//The allowed values in this context are file extensions
std::vector<std::string> exts = prop->allowedValues();
QString filter;
if( !exts.empty() )
{
filter = "Files (";
std::vector<std::string>::const_iterator iend = exts.end();
for( std::vector<std::string>::const_iterator itr = exts.begin(); itr != iend; ++itr)
{
filter.append("*." + QString::fromStdString(*itr) + " ");
}
filter.trimmed();
filter.append(QString::fromStdString(")"));
}
else
{
filter = "All Files (*.*)";
}
Gigg, Martyn Anthony
committed
QString filepath;
const Mantid::Kernel::FileValidator *file_checker = dynamic_cast<const Mantid::Kernel::FileValidator*>(prop->getValidator());
if( file_checker && !file_checker->fileMustExist() )
{
filepath = QFileDialog::getSaveFileName(this, tr("Select File"), AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
}
else
{
filepath = QFileDialog::getOpenFileName(this, tr("Select File"), AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
}
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
if( !filepath.isEmpty() ) AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(filepath).absoluteDir().path());
return filepath;
Gigg, Martyn Anthony
committed
}
/**
* Set old input for text edit field
* @param propName The name of the property
* @param field The QLineEdit field
*/
void AlgorithmDialog::setOldLineEditInput(const QString & propName, QLineEdit* field)
{
Mantid::Kernel::Property *prop = getAlgorithmProperty(propName);
if( !prop ) return;
if( isForScript() && prop->isValid() && !prop->isDefault() )
{
field->setText(QString::fromStdString(prop->value()));
field->setEnabled(false);
}
else
{
field->setText(AlgorithmInputHistory::Instance().previousInput(m_algName, propName));
}
}
Gigg, Martyn Anthony
committed
/**
* A slot that can be used to connect a button that accepts the dialog if
* all of the properties are valid
*/
Gigg, Martyn Anthony
committed
void AlgorithmDialog::accept()
Gigg, Martyn Anthony
committed
{
parseInput();
Gigg, Martyn Anthony
committed
if( setPropertyValues() ) QDialog::accept();
Gigg, Martyn Anthony
committed
else
{
QMessageBox::critical(this, "",
"One or more properties are invalid. They are marked with a *");
}
}
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
//------------------------------------------------------
// Private member functions
//------------------------------------------------------
/**
* Set the algorithm pointer
* @param alg A pointer to the algorithm
*/
void AlgorithmDialog::setAlgorithm(Mantid::API::IAlgorithm* alg)
Gigg, Martyn Anthony
committed
{
m_algorithm = alg;
Gigg, Martyn Anthony
committed
m_algName = QString::fromStdString(alg->name());
Gigg, Martyn Anthony
committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
m_algProperties.clear();
std::vector<Mantid::Kernel::Property*>::const_iterator iend = alg->getProperties().end();
for( std::vector<Mantid::Kernel::Property*>::const_iterator itr = alg->getProperties().begin(); itr != iend;
++itr )
{
m_algProperties.insert(QString::fromStdString((*itr)->name()), *itr);
}
}
/**
* Set if we are for a script or not
* @param forScript A boolean inidcating whether we are being called from a script
*/
void AlgorithmDialog::isForScript(bool forScript)
{
m_forScript = forScript;
}
/**
* Set an optional message to be displayed at the top of the widget
* @param message The message string
*/
void AlgorithmDialog::setOptionalMessage(const QString & message)
{
m_strMessage = message;
if( message.isEmpty() ) m_msgAvailable = false;
}
/**
* This sets up the labels that are to be used to mark whether a property is valid. It has
* a default implmentation but can be overridden if some other marker is required
*/
void AlgorithmDialog::createValidatorLabels()
{
Gigg, Martyn Anthony
committed
QHash<QString, Mantid::Kernel::Property*>::const_iterator pend = m_algProperties.end();
for( QHash<QString, Mantid::Kernel::Property*>::const_iterator pitr = m_algProperties.begin();
Gigg, Martyn Anthony
committed
pitr != pend; ++pitr )
{
QLabel *validLbl = new QLabel("*");
QPalette pal = validLbl->palette();
pal.setColor(QPalette::WindowText, Qt::darkRed);
validLbl->setPalette(pal);
m_validators[pitr.key()] = validLbl;
}
}