Skip to content

Commit

Permalink
Re #10576. Added custom editor for local parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Mar 19, 2015
1 parent 9211b3c commit f25d687
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
Expand Up @@ -9,6 +9,8 @@
#include "ui_AddWorkspace.h"
#include "ui_EditLocalParameterDialog.h"

#include <QStyledItemDelegate>

#include <boost/shared_ptr.hpp>
#include <QMap>
#include <vector>
Expand Down Expand Up @@ -228,6 +230,36 @@ private slots:
int m_currentIndex;
};

/*==========================================================================================*/
class LocalParameterEditor: public QWidget
{
Q_OBJECT
public:
LocalParameterEditor(QWidget *parent = NULL);
~LocalParameterEditor();
signals:
void setAllValues(double);
private slots:
void buttonPressed();
private:
QLineEdit* m_editor;
};

class LocalParameterItemDelegate: public QStyledItemDelegate
{
Q_OBJECT
public:
LocalParameterItemDelegate(QObject *parent = NULL);
QWidget* createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
void setEditorData(QWidget * editor, const QModelIndex & index) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
signals:
void setAllValues(double);
private:
bool eventFilter(QObject * obj, QEvent * ev);
mutable LocalParameterEditor* m_currentEditor;
};

/*==========================================================================================*/
/**
* A dialog for displaying and editing values of local parameters.
Expand All @@ -238,9 +270,8 @@ class EditLocalParameterDialog: public QDialog
public:
EditLocalParameterDialog(MultiDatasetFit *parent, const QString &parName);
private slots:
//void accept();
//void reject();
void valueChanged(int,int);
void setAllValues(double);
private:
MultiDatasetFit *owner() const {return static_cast<MultiDatasetFit*>(parent());}
Ui::EditLocalParameterDialog m_uiForm;
Expand Down
81 changes: 81 additions & 0 deletions Code/Mantid/MantidQt/CustomInterfaces/src/MultiDatasetFit.cpp
Expand Up @@ -652,6 +652,74 @@ void PlotController::updateRange(int index)
/* EditLocalParameterDialog */
/*==========================================================================================*/

LocalParameterEditor::LocalParameterEditor(QWidget *parent):QWidget(parent)
{
auto layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
layout->setContentsMargins(0,0,0,0);

m_editor = new QLineEdit(parent);
m_editor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
auto button = new QPushButton("All");
button->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Expanding);
this->setFocusPolicy(Qt::NoFocus);
layout->addWidget(m_editor);
layout->addWidget(button);
layout->setStretch(0,1);
layout->setStretch(1,0);
this->setFocusProxy(m_editor);
this->setFocusPolicy(Qt::StrongFocus);
connect(button,SIGNAL(clicked()),this,SLOT(buttonPressed()));
}

LocalParameterEditor::~LocalParameterEditor()
{
}

void LocalParameterEditor::buttonPressed()
{
double value = m_editor->text().toDouble();
emit setAllValues(value);
}

/*==========================================================================================*/
LocalParameterItemDelegate::LocalParameterItemDelegate(QObject *parent):
QStyledItemDelegate(parent),
m_currentEditor(NULL)
{
}

QWidget* LocalParameterItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
m_currentEditor = new LocalParameterEditor(parent);
connect(m_currentEditor,SIGNAL(setAllValues(double)),this,SIGNAL(setAllValues(double)));
m_currentEditor->installEventFilter(const_cast<LocalParameterItemDelegate*>(this));
return m_currentEditor;
}

void LocalParameterItemDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
QStyledItemDelegate::setEditorData(editor->layout()->itemAt(0)->widget(), index);
}

void LocalParameterItemDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
QStyledItemDelegate::setModelData(editor->layout()->itemAt(0)->widget(), model, index);
}

bool LocalParameterItemDelegate::eventFilter(QObject * obj, QEvent * ev)
{
if ( ev->type() == QEvent::WindowDeactivate )
{
emit commitData(m_currentEditor);
return true;
}
return QStyledItemDelegate::eventFilter(obj,ev);
}

/*==========================================================================================*/

EditLocalParameterDialog::EditLocalParameterDialog(MultiDatasetFit *parent, const QString &parName):
QDialog(parent),m_parName(parName)
{
Expand All @@ -671,6 +739,9 @@ EditLocalParameterDialog::EditLocalParameterDialog(MultiDatasetFit *parent, cons
cell = new QTableWidgetItem( QString::number(multifit->getLocalParameterValue(parName,i)) );
m_uiForm.tableWidget->setItem( i, 1, cell );
}
auto deleg = new LocalParameterItemDelegate(this);
m_uiForm.tableWidget->setItemDelegateForColumn(1,deleg);
connect(deleg,SIGNAL(setAllValues(double)),this,SLOT(setAllValues(double)));
}

/**
Expand All @@ -696,6 +767,16 @@ void EditLocalParameterDialog::valueChanged(int row, int col)
}
}

void EditLocalParameterDialog::setAllValues(double value)
{
int n = owner()->getNumberOfSpectra();
for(int i = 0; i < n; ++i)
{
owner()->setLocalParameterValue(m_parName,i,value);
m_uiForm.tableWidget->item(i,1)->setText( QString::number(value) );
}
}

/*==========================================================================================*/
/* MultiDatasetFit */
/*==========================================================================================*/
Expand Down

0 comments on commit f25d687

Please sign in to comment.