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

Refs #4173: Sort on a PeaksWorkspace table view sorts underlying peaks

but other tables will use the default sorting methods. Sorting columns independently of each other (which does not make sense when sorting peaks) will not work, instead all columns are sorted together
parent d7de0f4f
No related branches found
No related tags found
No related merge requests found
...@@ -182,6 +182,15 @@ public: ...@@ -182,6 +182,15 @@ public:
return getRow(rowCount()-1); return getRow(rowCount()-1);
} }
/** Does this type of TableWorkspace need a custom sorting call (e.g. PeaksWorkspace)
* @return true if the workspace needs custom sorting calls */
virtual bool customSort() const
{ return false; }
/// Overridable method to custom-sort the workspace
virtual void sort(std::vector< std::pair<std::string, bool> > & criteria);
/// Access the column with name \c name trough a ColumnVector object /// Access the column with name \c name trough a ColumnVector object
TableColumnHelper getVector(const std::string& name) TableColumnHelper getVector(const std::string& name)
{ {
......
...@@ -22,6 +22,22 @@ void ITableWorkspace::modified() ...@@ -22,6 +22,22 @@ void ITableWorkspace::modified()
new Kernel::DataService<API::Workspace>::AfterReplaceNotification(this->getName(),tws)); new Kernel::DataService<API::Workspace>::AfterReplaceNotification(this->getName(),tws));
} }
/** Overridable method to custom-sort the workspace
*
* @param criteria : a vector with a list of pairs: column name, bool;
* where bool = true for ascending, false for descending sort.
* The peaks are sorted by the first criterion first, then the 2nd if equal, etc.
* @throw std::runtime_error unless overridden
*/
void ITableWorkspace::sort(std::vector< std::pair<std::string, bool> > & criteria)
{
UNUSED_ARG(criteria);
throw std::runtime_error("This type of ITableWorkspace (" + this->id() + ") has not implemented sort() yet customSort() returns true. Please contact the developers.");
}
} // namespace API } // namespace API
} // Namespace Mantid } // Namespace Mantid
......
...@@ -81,6 +81,10 @@ namespace DataObjects ...@@ -81,6 +81,10 @@ namespace DataObjects
void appendFile( std::string filename, Geometry::Instrument_sptr inst); void appendFile( std::string filename, Geometry::Instrument_sptr inst);
/** @return true because this type of the workspace needs custom sorting calls */
virtual bool customSort() const
{ return true; }
void sort(std::vector< std::pair<std::string, bool> > & criteria); void sort(std::vector< std::pair<std::string, bool> > & criteria);
//--------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------
......
...@@ -261,3 +261,61 @@ void MantidTable::deleteRows(int startRow, int endRow) ...@@ -261,3 +261,61 @@ void MantidTable::deleteRows(int startRow, int endRow)
QMessageBox::critical(this,"MantidPlot - Error", "DeleteTableRow algorithm failed"); QMessageBox::critical(this,"MantidPlot - Error", "DeleteTableRow algorithm failed");
} }
} }
//------------------------------------------------------------------------------------------------
/**\brief Sort the specified column.
* @param col :: the column to be sorted
* @param order :: 0 means ascending, anything else means descending
*/
void MantidTable::sortColumn(int col, int order)
{
if (!m_ws) return;
if (m_ws->customSort())
{
// Customized sorting routine for this TableWorkspace
std::vector< std::pair<std::string, bool> > criteria;
// Only one criterion in sorting
criteria.push_back( std::pair<std::string, bool> (m_ws->getColumn(col)->name(), (order == 0)) );
m_ws->sort(criteria);
// Refresh the table
this->fillTable();
}
else
{
// Fall-back to the default sorting of the table
Table::sortColumn(col, order);
}
}
//------------------------------------------------------------------------------------------------
/**\brief Sort the specified columns.
* @param cols :: the columns to be sorted
* @param type :: 0 means sort individually (as in sortColumn()), anything else means together
* @param order :: 0 means ascending, anything else means descending
* @param leadCol :: for sorting together, the column which determines the permutation
*/
void MantidTable::sortColumns(const QStringList&s, int type, int order, const QString& leadCol)
{
if (!m_ws) return;
if (m_ws->customSort())
{
// Customized sorting routine for this TableWorkspace
std::vector< std::pair<std::string, bool> > criteria;
// Unmangle the column name, which comes as "TableName_ColName"
std::string col = leadCol.toStdString();
size_t n = col.rfind('_');
if (n != std::string::npos && n < col.size()-1)
col = col.substr(n+1, col.size()-n-1);
// Only one criterion in sorting
criteria.push_back( std::pair<std::string, bool> (col, (order == 0)) );
m_ws->sort(criteria);
// Refresh the table
this->fillTable();
}
else
{
// Fall-back to the default sorting of the table
Table::sortColumns(s, type, order, leadCol);
}
}
...@@ -36,6 +36,10 @@ protected: ...@@ -36,6 +36,10 @@ protected:
void deleteHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws); void deleteHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);
void afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws); void afterReplaceHandle(const std::string& wsName,const boost::shared_ptr<Mantid::API::Workspace> ws);
// Reimplemented methods for custom sorting of TableWorkspaces
virtual void sortColumn(int col, int order);
virtual void sortColumns(const QStringList& cols, int type = 0, int order = 0, const QString& leadCol = QString());
private: private:
/// ITableWorkspace being displayed /// ITableWorkspace being displayed
Mantid::API::ITableWorkspace_sptr m_ws; Mantid::API::ITableWorkspace_sptr m_ws;
......
...@@ -162,11 +162,21 @@ public slots: ...@@ -162,11 +162,21 @@ public slots:
* \sa sortColAsc(), sortColumn(), Q3Table::currentColumn() * \sa sortColAsc(), sortColumn(), Q3Table::currentColumn()
*/ */
void sortColDesc(); void sortColDesc();
/**\brief Sort the specified column. /**\brief Sort the specified column.
* @param col :: the column to be sorted * @param col :: the column to be sorted
* @param order :: 0 means ascending, anything else means descending * @param order :: 0 means ascending, anything else means descending
*/ */
void sortColumn(int col = -1, int order = 0); virtual void sortColumn(int col = -1, int order = 0);
/**\brief Sort the specified columns.
* @param cols :: the columns to be sorted
* @param type :: 0 means sort individually (as in sortColumn()), anything else means together
* @param order :: 0 means ascending, anything else means descending
* @param leadCol :: for sorting together, the column which determines the permutation
*/
virtual void sortColumns(const QStringList& cols, int type = 0, int order = 0, const QString& leadCol = QString());
/**\brief Display a dialog with some options for sorting all columns. /**\brief Display a dialog with some options for sorting all columns.
* *
* The sorting itself is done using sort(int,int,const QString&). * The sorting itself is done using sort(int,int,const QString&).
...@@ -174,15 +184,9 @@ public slots: ...@@ -174,15 +184,9 @@ public slots:
void sortTableDialog(); void sortTableDialog();
//! Sort all columns as in sortColumns(const QStringList&,int,int,const QString&). //! Sort all columns as in sortColumns(const QStringList&,int,int,const QString&).
void sort(int type = 0, int order = 0, const QString& leadCol = QString()); void sort(int type = 0, int order = 0, const QString& leadCol = QString());
//! Sort selected columns as in sortColumns(const QStringList&,int,int,const QString&). //! Sort selected columns as in sortColumns(const QStringList&,int,int,const QString&).
void sortColumns(int type = 0, int order = 0, const QString& leadCol = QString()); void sortColumns(int type = 0, int order = 0, const QString& leadCol = QString());
/**\brief Sort the specified columns.
* @param cols :: the columns to be sorted
* @param type :: 0 means sort individually (as in sortColumn()), anything else means together
* @param order :: 0 means ascending, anything else means descending
* @param leadCol :: for sorting together, the column which determines the permutation
*/
void sortColumns(const QStringList& cols, int type = 0, int order = 0, const QString& leadCol = QString());
/**\brief Display a dialog with some options for sorting the selected columns. /**\brief Display a dialog with some options for sorting the selected columns.
* *
* The sorting itself is done using sortColumns(int,int,const QString&). * The sorting itself is done using sortColumns(int,int,const QString&).
......
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