Skip to content
Snippets Groups Projects
Commit caf381c2 authored by Janik Zikovsky's avatar Janik Zikovsky
Browse files

Refs #2882: Can manually edit some of the columns of a PeaksWorkspace directly in MantidPlot.

parent b2dc76d7
No related branches found
No related tags found
No related merge requests found
...@@ -91,9 +91,13 @@ public: ...@@ -91,9 +91,13 @@ public:
virtual bool getReadOnly() const virtual bool getReadOnly() const
{ return true; } { return true; }
/// Prints /// Prints out the value to a stream
virtual void print(std::ostream& s, int index) const = 0; virtual void print(std::ostream& s, int index) const = 0;
/// Read in a string and set the value at the given index
virtual void read(const std::string & text, int index)
{ UNUSED_ARG(text);UNUSED_ARG(index); }
/// Specialized type check /// Specialized type check
virtual bool isBool()const = 0; virtual bool isBool()const = 0;
......
...@@ -38,9 +38,13 @@ namespace DataObjects ...@@ -38,9 +38,13 @@ namespace DataObjects
/// Returns typeid for the pointer type to the data element in the column /// Returns typeid for the pointer type to the data element in the column
virtual const std::type_info& get_pointer_type_info()const; virtual const std::type_info& get_pointer_type_info()const;
virtual bool getReadOnly() const;
/// Prints /// Prints
virtual void print(std::ostream& s, int index) const; virtual void print(std::ostream& s, int index) const;
virtual void read(const std::string & text, int index);
/// Specialized type check /// Specialized type check
virtual bool isBool()const; virtual bool isBool()const;
......
#include "MantidDataObjects/PeakColumn.h" #include "MantidDataObjects/PeakColumn.h"
#include "MantidKernel/System.h" #include "MantidKernel/System.h"
#include "MantidKernel/Strings.h"
using namespace Mantid::Kernel;
namespace Mantid namespace Mantid
{ {
...@@ -37,6 +40,7 @@ namespace DataObjects ...@@ -37,6 +40,7 @@ namespace DataObjects
return typeid(double*); return typeid(double*);
} }
//-------------------------------------------------------------------------------------
/** Prints out the column string at the given row index. /** Prints out the column string at the given row index.
* *
* @param s :: stream to output * @param s :: stream to output
...@@ -84,6 +88,65 @@ namespace DataObjects ...@@ -84,6 +88,65 @@ namespace DataObjects
throw std::runtime_error("Unexpected column name"); throw std::runtime_error("Unexpected column name");
} }
//-------------------------------------------------------------------------------------
/** Read in some text and convert to a number in the PeaksWorkspace
*
* @param text :: string to read
* @param index :: index of the peak to modify
*/
void PeakColumn::read(const std::string & text, int index)
{
// Don't modify read-only ones
if (this->getReadOnly())
return;
// Avoid going out of bounds
if (size_t(index) >= peaks.size())
return;
// Reference to the peak in the workspace
Peak & peak = peaks[index];
// Convert to a double
double val = 0;
bool success = Strings::convert(text, val);
int ival = static_cast<int>(val);
if (!success)
{
g_log.error() << "Could not convert string '" << text << "' to a number.\n";
return;
}
if (m_name == "h")
peak.setH(val);
else if (m_name == "k")
peak.setK(val);
else if (m_name == "l")
peak.setL(val);
else if (m_name == "RunNumber")
peak.setRunNumber(ival);
else
throw std::runtime_error("Unexpected column " + m_name + " being set.");
}
//-------------------------------------------------------------------------------------
/** @return true if the column is read-only */
bool PeakColumn::getReadOnly() const
{
if (
(m_name == "h") || (m_name == "k") || (m_name == "l") ||
(m_name == "RunNumber")
)
return false;
else
// Default to true for most columns
return true;
}
//-------------------------------------------------------------------------------------
/// Specialized type check /// Specialized type check
bool PeakColumn::isBool()const bool PeakColumn::isBool()const
{ {
......
...@@ -21,9 +21,11 @@ public: ...@@ -21,9 +21,11 @@ public:
std::vector<Peak> peaks; std::vector<Peak> peaks;
PeakColumn pc(peaks, "h"); PeakColumn pc(peaks, "h");
TS_ASSERT_EQUALS( pc.name(), "h"); TS_ASSERT_EQUALS( pc.name(), "h");
TS_ASSERT_EQUALS( pc.size(), 0);
} }
}; };
......
...@@ -96,6 +96,25 @@ void MantidTable::afterReplaceHandle(const std::string& wsName,const boost::shar ...@@ -96,6 +96,25 @@ void MantidTable::afterReplaceHandle(const std::string& wsName,const boost::shar
} }
//------------------------------------------------------------------------------------------------
/** Called when a cell is edited */
void MantidTable::cellEdited(int row,int col)
{
std::string text = d_table->text(row,col).remove(QRegExp("\\s")).toStdString();
Mantid::API::Column_sptr c = m_ws->getColumn(col);
// Have the column convert the text to a value internally
int index = row;
c->read(text, index);
// Set the table view to be the same text after editing.
// That way, if the string was stupid, it will be reset to the old value.
std::ostringstream s;
c->print(s, index);
d_table->setText(row, col, QString(s.str().c_str()));
}
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
/** Call an algorithm in order to delete table rows /** Call an algorithm in order to delete table rows
* *
......
...@@ -20,6 +20,7 @@ signals: ...@@ -20,6 +20,7 @@ signals:
void needToClose(); void needToClose();
public slots: public slots:
void deleteRows(int startRow, int endRow); void deleteRows(int startRow, int endRow);
void cellEdited(int,int col);
protected slots: protected slots:
void closeTable(); void closeTable();
protected: protected:
......
...@@ -117,7 +117,7 @@ public slots: ...@@ -117,7 +117,7 @@ public slots:
void setRandomValues(); void setRandomValues();
void setAscValues(); void setAscValues();
void cellEdited(int,int col); virtual void cellEdited(int,int col);
void moveCurrentCell(); void moveCurrentCell();
void clearCell(int row, int col); void clearCell(int row, int col);
QString saveText(); QString saveText();
...@@ -275,12 +275,12 @@ public slots: ...@@ -275,12 +275,12 @@ public slots:
void setColumnTypes(const QStringList& ctl); void setColumnTypes(const QStringList& ctl);
void setColumnType(int col, ColType val) { colTypes[col] = val; } void setColumnType(int col, ColType val) { colTypes[col] = val; }
void saveToMemory(double **cells){d_saved_cells = cells;}; void saveToMemory(double **cells){d_saved_cells = cells;};
void saveToMemory(); void saveToMemory();
void freeMemory(); void freeMemory();
bool isReadOnlyColumn(int col); bool isReadOnlyColumn(int col);
void setReadOnlyColumn(int col, bool on = true); void setReadOnlyColumn(int col, bool on = true);
QString columnFormat(int col){return col_format[col];}; QString columnFormat(int col){return col_format[col];};
QStringList getColumnsFormat(){return col_format;}; QStringList getColumnsFormat(){return col_format;};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment