Skip to content

Commit

Permalink
Refs #4173: Sort on a PeaksWorkspace table view sorts underlying peaks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Janik Zikovsky committed Dec 6, 2011
1 parent d7de0f4 commit 8142f2f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ITableWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ class ITableWorkspace_DllExport ITableWorkspace: public API::Workspace
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
TableColumnHelper getVector(const std::string& name)
{
Expand Down
16 changes: 16 additions & 0 deletions Code/Mantid/Framework/API/src/ITableWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ void ITableWorkspace::modified()
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 Mantid

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ namespace DataObjects

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);

//---------------------------------------------------------------------------------------------
Expand Down
58 changes: 58 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/MantidTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,61 @@ void MantidTable::deleteRows(int startRow, int endRow)
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);
}
}
4 changes: 4 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/MantidTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ protected slots:
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);

// 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:
/// ITableWorkspace being displayed
Mantid::API::ITableWorkspace_sptr m_ws;
Expand Down
24 changes: 14 additions & 10 deletions Code/Mantid/MantidPlot/src/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,31 @@ public slots:
* \sa sortColAsc(), sortColumn(), Q3Table::currentColumn()
*/
void sortColDesc();

/**\brief Sort the specified column.
* @param col :: the column to be sorted
* @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.
*
* The sorting itself is done using sort(int,int,const QString&).
*/
void sortTableDialog();
//! Sort all columns as in sortColumns(const QStringList&,int,int,const 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&).
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());
//! Sort selected columns as in sortColumns(const QStringList&,int,int,const QString&).
void sortColumns(int type = 0, int order = 0, const QString& leadCol = QString());

/**\brief Display a dialog with some options for sorting the selected columns.
*
* The sorting itself is done using sortColumns(int,int,const QString&).
Expand Down

0 comments on commit 8142f2f

Please sign in to comment.