Added a QSortFilterProxyModel subclass to enable sorting by different columns in the views.

This commit is contained in:
2026-01-12 09:56:50 +01:00
parent 3597fcf0b0
commit a6512f2c67
5 changed files with 77 additions and 1 deletions

View File

@ -33,6 +33,7 @@ add_library(${TARGET_APP} STATIC
formats/csvparser.h formats/csvparser.cpp formats/csvparser.h formats/csvparser.cpp
# 3rd party libraries # 3rd party libraries
../3rdParty/rapidcsv/src/rapidcsv.h ../3rdParty/rapidcsv/src/rapidcsv.h
model/generalsortfiltermodel.h model/generalsortfiltermodel.cpp
) )
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@ -12,6 +12,7 @@
#include "CoreConfig.h" #include "CoreConfig.h"
#include "constants.h" #include "constants.h"
#include "data/filehandler.h" #include "data/filehandler.h"
#include "model/generalsortfiltermodel.h"
#include "model/metadata.h" #include "model/metadata.h"
#include "model/tablemodel.h" #include "model/tablemodel.h"
@ -89,6 +90,10 @@ QUndoStack* GenericCore::getModelUndoStack() const { return m_modelUndoStack; }
std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; } std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; }
std::shared_ptr<GeneralSortFilterModel> GenericCore::getSortFilterModel() const {
return m_sortFilterModel;
}
/** /**
* Save items to default file (in standard location). * Save items to default file (in standard location).
* @brief GenericCore::saveItems Saves item fo file. * @brief GenericCore::saveItems Saves item fo file.
@ -127,7 +132,9 @@ bool GenericCore::exportCSVFile(const QString& filePath) {
} }
void GenericCore::setupModels() { void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this); m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
m_sortFilterModel = make_shared<GeneralSortFilterModel>(m_mainModel);
// TODO add QAbstractItemModelTester // TODO add QAbstractItemModelTester
initModelData(); initModelData();
} }

View File

@ -8,6 +8,7 @@ class QAbstractItemModel;
class QString; class QString;
class TableModel; class TableModel;
class GeneralSortFilterModel;
class GenericCore : public QObject { class GenericCore : public QObject {
Q_OBJECT Q_OBJECT
@ -24,6 +25,7 @@ class GenericCore : public QObject {
QUndoStack* getModelUndoStack() const; QUndoStack* getModelUndoStack() const;
std::shared_ptr<TableModel> getModel() const; std::shared_ptr<TableModel> getModel() const;
std::shared_ptr<GeneralSortFilterModel> getSortFilterModel() const;
void saveItems(); void saveItems();
void importCSVFile(const QString& filePath); void importCSVFile(const QString& filePath);
@ -35,6 +37,7 @@ class GenericCore : public QObject {
private: private:
QUndoStack* m_modelUndoStack; QUndoStack* m_modelUndoStack;
std::shared_ptr<TableModel> m_mainModel; std::shared_ptr<TableModel> m_mainModel;
std::shared_ptr<GeneralSortFilterModel> m_sortFilterModel;
void setupModels(); void setupModels();
void initModelData(); void initModelData();

View File

@ -0,0 +1,42 @@
#include "generalsortfiltermodel.h"
#include "metadata.h"
GeneralSortFilterModel::GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel)
: QSortFilterProxyModel{sourceModel.get()}
, m_sourceModel(sourceModel) {
setSourceModel(sourceModel.get());
m_collator.setNumericMode(true);
}
bool GeneralSortFilterModel::lessThan(const QModelIndex& source_left,
const QModelIndex& source_right) const {
if (source_left.column() != source_right.column()) {
return false;
}
QAbstractItemModel* localSourceModel = sourceModel();
const int role = GET_ROLE_FOR_COLUMN(source_left.column());
const QVariant leftData = localSourceModel->data(source_left);
const QVariant rightData = localSourceModel->data(source_right);
const bool isText = STRING_ROLES.contains(role);
if (isText) {
const QString leftString = leftData.toString();
const QString rightString = rightData.toString();
return m_collator.compare(leftString, rightString) > 0;
}
const bool isInt = INT_ROLES.contains(role);
if (isInt) {
const int leftInt = leftData.toInt();
const int rightInt = rightData.toInt();
return leftInt > rightInt;
}
const bool isDouble = DOUBLE_ROLES.contains(role);
if (isDouble) {
const int leftInt = leftData.toDouble();
const int rightInt = rightData.toDouble();
return leftInt > rightInt;
}
return false;
}

View File

@ -0,0 +1,23 @@
#ifndef GENERALSORTFILTERMODEL_H
#define GENERALSORTFILTERMODEL_H
#include <QCollator>
#include <QSortFilterProxyModel>
#include "tablemodel.h"
class GeneralSortFilterModel : public QSortFilterProxyModel {
Q_OBJECT
public:
explicit GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel = nullptr);
/// QSortFilterProxyModel interface
protected:
bool lessThan(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override;
private:
std::shared_ptr<TableModel> m_sourceModel;
QCollator m_collator; /// for sorting
};
#endif // GENERALSORTFILTERMODEL_H