Skip to content

Commit

Permalink
Unified the code for handling Browse buttons.
Browse files Browse the repository at this point in the history
Will be easier to add another one now.

Refs #7229
  • Loading branch information
arturbekasov committed Oct 14, 2013
1 parent 1df4cac commit 2c0eadf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ui_PlotAsymmetryByLogValueDialog.h"

#include <QString>
#include <QSignalMapper>

//---------------------------
// Qt Forward declarations
Expand Down Expand Up @@ -70,16 +71,18 @@ class PlotAsymmetryByLogValueDialog : public MantidQt::API::AlgorithmDialog

private slots:

/// A slot for the browse button clicked signal
void browseFirstClicked();
void browseLastClicked();
/// Opens a file dialog. Updates the QLineEdit provided when the dialog is closed.
void openFileDialog(const QString& filePropName);
void fillLogBox(const QString&);

private:


// The form generated with Qt Designer
Ui::PlotAsymmetryByLogValueDialog m_uiForm;

/// Maps Browse buttons to file properties
QSignalMapper* browseButtonMapper;
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QFileInfo>
#include <QDir>
#include <QCheckBox>
#include <QSignalMapper>

//Add this class to the list of specialised dialogs in this namespace
namespace MantidQt
Expand All @@ -27,8 +28,8 @@ namespace CustomDialogs
}
}

// Just to save writing this everywhere
using namespace MantidQt::CustomDialogs;
using namespace MantidQt::API;

//---------------------------------------
// Public member functions
Expand All @@ -38,13 +39,17 @@ using namespace MantidQt::CustomDialogs;
*/
PlotAsymmetryByLogValueDialog::PlotAsymmetryByLogValueDialog(QWidget *parent) : AlgorithmDialog(parent)
{
browseButtonMapper = new QSignalMapper();

connect(browseButtonMapper, SIGNAL(mapped(const QString&)), this, SLOT(openFileDialog(const QString&)));
}

/**
*Destructor
*/
PlotAsymmetryByLogValueDialog::~PlotAsymmetryByLogValueDialog()
{
delete browseButtonMapper;
}

//---------------------------------------
Expand All @@ -70,8 +75,14 @@ void PlotAsymmetryByLogValueDialog::initLayout()
tie(m_uiForm.timeMinBox, "TimeMin");
tie(m_uiForm.timeMaxBox, "TimeMax");

connect( m_uiForm.browseFirstButton, SIGNAL(clicked()), this, SLOT(browseFirstClicked()) );
connect( m_uiForm.browseLastButton, SIGNAL(clicked()), this, SLOT(browseLastClicked()) );
// Set-up browse button mapping
browseButtonMapper->setMapping(m_uiForm.browseFirstButton, "FirstRun");
browseButtonMapper->setMapping(m_uiForm.browseLastButton, "LastRun");

// Connect Browse buttons to the mapper
connect(m_uiForm.browseFirstButton, SIGNAL(clicked()), browseButtonMapper, SLOT(map()));
connect(m_uiForm.browseLastButton, SIGNAL(clicked()), browseButtonMapper, SLOT(map()));

connect( m_uiForm.firstRunBox, SIGNAL(textChanged(const QString&)), this, SLOT(fillLogBox(const QString&)) );
connect( m_uiForm.btnOK,SIGNAL(clicked()),this,SLOT(accept()));
connect( m_uiForm.btnCancel,SIGNAL(clicked()),this,SLOT(reject()));
Expand All @@ -89,45 +100,25 @@ void PlotAsymmetryByLogValueDialog::initLayout()
}

/**
* A slot for the browse button "clicked" signal
*/
void PlotAsymmetryByLogValueDialog::browseFirstClicked()
* Opens a file dialog. Updates the QLineEdit provided when the dialog is closed.
*/
void PlotAsymmetryByLogValueDialog::openFileDialog(const QString& filePropName)
{
if( !m_uiForm.firstRunBox->text().isEmpty() )
{
MantidQt::API::AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(m_uiForm.firstRunBox->text()).absoluteDir().path());
}
QString selectedPath = AlgorithmDialog::openFileDialog(filePropName);

QString filepath = this->openFileDialog("FirstRun");
if( !filepath.isEmpty() )
if(!selectedPath.isEmpty())
{
m_uiForm.firstRunBox->clear();
m_uiForm.firstRunBox->setText(filepath.trimmed());
}
// Save used directory for the next time
AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(selectedPath).absoluteDir().path());

// //Add a suggestion for workspace name
// if( m_wsBox->isEnabled() && !filepath.isEmpty() ) m_wsBox->setText(QFileInfo(filepath).baseName());
}
// Get the widget for the file property
QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(m_tied_properties[filePropName]);

/**
* A slot for the browse button "clicked" signal
*/
void PlotAsymmetryByLogValueDialog::browseLastClicked()
{
if( !m_uiForm.firstRunBox->text().isEmpty() )
{
MantidQt::API::AlgorithmInputHistory::Instance().setPreviousDirectory(QFileInfo(m_uiForm.firstRunBox->text()).absoluteDir().path());
}
if(!lineEdit)
throw std::runtime_error("Widget of the file property was not found");

QString filepath = this->openFileDialog("LastRun");
if( !filepath.isEmpty() )
{
m_uiForm.lastRunBox->clear();
m_uiForm.lastRunBox->setText(filepath.trimmed());
lineEdit->setText(selectedPath.trimmed());
}

// //Add a suggestion for workspace name
// if( m_wsBox->isEnabled() && !filepath.isEmpty() ) m_wsBox->setText(QFileInfo(filepath).baseName());
}

/**
Expand Down

0 comments on commit 2c0eadf

Please sign in to comment.