1
0
Fork 0

GUI: remove sorting related functionality from AbstractTableView

This commit is contained in:
Duncan Ogilvie 2017-12-19 16:54:08 +01:00
parent ae5bb70203
commit 83005bdcda
No known key found for this signature in database
GPG Key ID: FC89E0AAA0C1AAD8
4 changed files with 64 additions and 66 deletions

View File

@ -1009,10 +1009,9 @@ int AbstractTableView::getLineToPrintcount()
*
* @param[in] width Width of the column in pixel
* @param[in] isClickable Boolean that tells whether the header is clickable or not
* @param[in] sortFn The sort function to use for this column. Defaults to case insensitve text search
* @return Nothing.
*/
void AbstractTableView::addColumnAt(int width, const QString & title, bool isClickable, SortBy::t sortFn)
void AbstractTableView::addColumnAt(int width, const QString & title, bool isClickable)
{
HeaderButton_t wHeaderButton;
Column_t wColumn;
@ -1030,7 +1029,6 @@ void AbstractTableView::addColumnAt(int width, const QString & title, bool isCli
wColumn.width = width;
wColumn.hidden = false;
wColumn.title = title;
wColumn.sortFunction = sortFn;
wCurrentCount = mColumnList.length();
mColumnList.append(wColumn);
mColumnOrder.append(wCurrentCount);
@ -1104,13 +1102,6 @@ bool AbstractTableView::getColumnHidden(int col)
return true;
}
AbstractTableView::SortBy::t AbstractTableView::getColumnSortBy(int col) const
{
if(col < getColumnCount() && col >= 0)
return mColumnList[col].sortFunction;
return SortBy::AsText;
}
void AbstractTableView::setColumnHidden(int col, bool hidden)
{
if(col < getColumnCount() && col >= 0)
@ -1254,31 +1245,3 @@ void AbstractTableView::prepareData()
dsint wRemainingRowsCount = getRowCount() - mTableOffset;
mNbrOfLineToPrint = (dsint)wRemainingRowsCount > (dsint)wViewableRowsCount ? (int)wViewableRowsCount : (int)wRemainingRowsCount;
}
bool AbstractTableView::SortBy::AsText(const QString & a, const QString & b)
{
auto i = QString::compare(a, b);
if(i < 0)
return true;
if(i > 0)
return false;
return duint(&a) < duint(&b);
}
bool AbstractTableView::SortBy::AsInt(const QString & a, const QString & b)
{
if(a.toLongLong() < b.toLongLong())
return true;
if(a.toLongLong() > b.toLongLong())
return false;
return duint(&a) < duint(&b);
}
bool AbstractTableView::SortBy::AsHex(const QString & a, const QString & b)
{
if(a.toLongLong(0, 16) < b.toLongLong(0, 16))
return true;
if(a.toLongLong(0, 16) > b.toLongLong(0, 16))
return false;
return duint(&a) < duint(&b);
}

View File

@ -73,16 +73,8 @@ public:
int getViewableRowsCount();
virtual int getLineToPrintcount();
struct SortBy
{
typedef std::function<bool(const QString &, const QString &)> t;
static bool AsText(const QString & a, const QString & b);
static bool AsInt(const QString & a, const QString & b);
static bool AsHex(const QString & a, const QString & b);
};
// New Columns/New Size
virtual void addColumnAt(int width, const QString & title, bool isClickable, SortBy::t sortFn = SortBy::AsText);
virtual void addColumnAt(int width, const QString & title, bool isClickable);
virtual void setRowCount(dsint count);
virtual void deleteAllColumns();
void setColTitle(int index, const QString & title);
@ -105,7 +97,6 @@ public:
int getCharWidth();
bool getColumnHidden(int col);
void setColumnHidden(int col, bool hidden);
SortBy::t getColumnSortBy(int idx) const;
// UI customization
void loadColumnFromConfig(const QString & viewName);
@ -166,7 +157,6 @@ private:
bool hidden;
HeaderButton_t header;
QString title;
SortBy::t sortFunction;
} Column_t;
typedef struct _Header_t

View File

@ -411,12 +411,43 @@ bool StdTable::scrollSelect(int offset)
return true;
}
/************************************************************************************
Sorting
************************************************************************************/
bool StdTable::SortBy::AsText(const QString & a, const QString & b)
{
auto i = QString::compare(a, b);
if(i < 0)
return true;
if(i > 0)
return false;
return duint(&a) < duint(&b);
}
bool StdTable::SortBy::AsInt(const QString & a, const QString & b)
{
if(a.toLongLong() < b.toLongLong())
return true;
if(a.toLongLong() > b.toLongLong())
return false;
return duint(&a) < duint(&b);
}
bool StdTable::SortBy::AsHex(const QString & a, const QString & b)
{
if(a.toLongLong(0, 16) < b.toLongLong(0, 16))
return true;
if(a.toLongLong(0, 16) > b.toLongLong(0, 16))
return false;
return duint(&a) < duint(&b);
}
/************************************************************************************
Data Management
************************************************************************************/
void StdTable::addColumnAt(int width, QString title, bool isClickable, QString copyTitle, SortBy::t sortFn)
{
AbstractTableView::addColumnAt(width, title, isClickable, sortFn);
AbstractTableView::addColumnAt(width, title, isClickable);
//append empty column to list of rows
for(size_t i = 0; i < mData.size(); i++)
@ -427,9 +458,20 @@ void StdTable::addColumnAt(int width, QString title, bool isClickable, QString c
mCopyTitles.push_back(title);
else
mCopyTitles.push_back(copyTitle);
//append column sort function
mColumnSortFunctions.push_back(sortFn);
}
void StdTable::setRowCount(int count)
void StdTable::deleteAllColumns()
{
setRowCount(0);
AbstractTableView::deleteAllColumns();
mCopyTitles.clear();
mColumnSortFunctions.clear();
}
void StdTable::setRowCount(dsint count)
{
int wRowToAddOrRemove = count - int(mData.size());
for(int i = 0; i < qAbs(wRowToAddOrRemove); i++)
@ -446,13 +488,6 @@ void StdTable::setRowCount(int count)
AbstractTableView::setRowCount(count);
}
void StdTable::deleteAllColumns()
{
setRowCount(0);
AbstractTableView::deleteAllColumns();
mCopyTitles.clear();
}
void StdTable::setCellContent(int r, int c, QString s)
{
if(isValidIndex(r, c))
@ -776,7 +811,7 @@ void StdTable::reloadData()
{
if(mSort.first != -1) //re-sort if the user wants to sort
{
auto sortFn = getColumnSortBy(mSort.first);
auto sortFn = mColumnSortFunctions.at(mSort.first);
std::stable_sort(mData.begin(), mData.end(), [this, &sortFn](const std::vector<CellData> & a, const std::vector<CellData> & b)
{
auto less = sortFn(a.at(mSort.first).text, b.at(mSort.first).text);

View File

@ -11,11 +11,11 @@ public:
QString paintContent(QPainter* painter, dsint rowBase, int rowOffset, int col, int x, int y, int w, int h);
void reloadData();
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseDoubleClickEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void keyPressEvent(QKeyEvent* event);
void mouseMoveEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseDoubleClickEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
void keyPressEvent(QKeyEvent* event) override;
void enableMultiSelection(bool enabled);
void enableColumnSorting(bool enabled);
@ -37,10 +37,19 @@ public:
bool isSelected(int base, int offset);
bool scrollSelect(int offset);
// Sorting
struct SortBy
{
typedef std::function<bool(const QString &, const QString &)> t;
static bool AsText(const QString & a, const QString & b);
static bool AsInt(const QString & a, const QString & b);
static bool AsHex(const QString & a, const QString & b);
};
// Data Management
void addColumnAt(int width, QString title, bool isClickable, QString copyTitle = "", SortBy::t sortFn = SortBy::AsText);
void setRowCount(int count);
void deleteAllColumns();
void deleteAllColumns() override;
void setRowCount(dsint count) override;
void setCellContent(int r, int c, QString s);
QString getCellContent(int r, int c);
void setCellUserdata(int r, int c, duint userdata);
@ -120,6 +129,7 @@ protected:
bool mIsColumnSortingAllowed;
std::vector<std::vector<CellData>> mData; //listof(row) where row = (listof(col) where col = string)
std::vector<SortBy::t> mColumnSortFunctions;
std::vector<QString> mCopyTitles;
QPair<int, bool> mSort;
};