Skip to content

Commit

Permalink
Refs #10392 Sort runs list in ReflSearchModel
Browse files Browse the repository at this point in the history
This commit fundamentally changes the behaviour of ReflSearchModel.
Instead of merely wrapping a ITableWorkspace, it copies the data it
cares about out of the workspace and then forgets about it. The main
reason for doing this is the lack of sorting functionality in the
TableWorkspace class. Weighing things up, the simplest way forward is
simply to look after the data we care about ourselves and manage it
internally.
  • Loading branch information
Harry Jeffery committed Nov 6, 2014
1 parent b27e98e commit 44ea276
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Expand Up @@ -51,8 +51,11 @@ namespace MantidQt
Qt::ItemFlags flags(const QModelIndex &index) const;

protected:
//The table we're wrapping
Mantid::API::ITableWorkspace_sptr m_tWS;
//vector of the run numbers
std::vector<std::string> m_runs;

//maps each run number to its description
std::map<std::string,std::string> m_descriptions;
};

/// Typedef for a shared pointer to \c ReflSearchModel
Expand Down
32 changes: 26 additions & 6 deletions Code/Mantid/MantidQt/CustomInterfaces/src/ReflSearchModel.cpp
Expand Up @@ -10,10 +10,21 @@ namespace MantidQt

//----------------------------------------------------------------------------------------------
/** Constructor
@param tableWorkspace : The table workspace to wrap
@param tableWorkspace : The table workspace to copy data from
*/
ReflSearchModel::ReflSearchModel(ITableWorkspace_sptr tableWorkspace) : m_tWS(tableWorkspace)
ReflSearchModel::ReflSearchModel(ITableWorkspace_sptr tableWorkspace)
{
//Copy the data from the input table workspace
for(size_t i = 0; i < tableWorkspace->rowCount(); ++i)
{
const std::string run = tableWorkspace->String(i,0);
const std::string description = tableWorkspace->String(i,6);
m_runs.push_back(run);
m_descriptions[run] = description;
}

//By sorting the vector of runs, we sort the entire table
std::sort(m_runs.begin(), m_runs.end());
}

//----------------------------------------------------------------------------------------------
Expand All @@ -28,7 +39,7 @@ namespace MantidQt
*/
int ReflSearchModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(m_tWS->rowCount());
return static_cast<int>(m_runs.size());
}

/**
Expand All @@ -52,9 +63,18 @@ namespace MantidQt
const int colNumber = index.column();
const int rowNumber = index.row();

TableRow tableRow = m_tWS->getRow(rowNumber);
//run column, or description column
return QString::fromStdString(tableRow.cell<std::string>(colNumber == 0 ? 0 : 6));
if(rowNumber < 0 || rowNumber >= static_cast<int>(m_runs.size()))
return QVariant();

const std::string run = m_runs[rowNumber];

if(colNumber == 0)
return QString::fromStdString(run);

if(colNumber == 1)
return QString::fromStdString(m_descriptions.find(run)->second);

return QVariant();
}

/**
Expand Down

0 comments on commit 44ea276

Please sign in to comment.