Added the a function to find items containing the specified text. Returning an item selection.
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
#include "generalsortfiltermodel.h"
|
#include "generalsortfiltermodel.h"
|
||||||
#include "metadata.h"
|
#include "metadata.h"
|
||||||
|
|
||||||
|
#include <QItemSelection>
|
||||||
|
|
||||||
GeneralSortFilterModel::GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel)
|
GeneralSortFilterModel::GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel)
|
||||||
: QSortFilterProxyModel{sourceModel.get()}
|
: QSortFilterProxyModel{sourceModel.get()}
|
||||||
, m_tableModel(sourceModel) {
|
, m_tableModel(sourceModel) {
|
||||||
@ -9,6 +11,18 @@ GeneralSortFilterModel::GeneralSortFilterModel(std::shared_ptr<TableModel> sourc
|
|||||||
m_collator.setNumericMode(true);
|
m_collator.setNumericMode(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QItemSelection GeneralSortFilterModel::findItems(const QString& text) const {
|
||||||
|
QItemSelection result;
|
||||||
|
|
||||||
|
for (int i = 0; i < rowCount(); ++i) {
|
||||||
|
const QModelIndex localIndex = index(i, 0);
|
||||||
|
if (indexContainsText(localIndex, text)) {
|
||||||
|
result.select(localIndex, index(i, columnCount() - 1)); /// select entire row
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
||||||
m_tableModel->appendItems(jsonDoc);
|
m_tableModel->appendItems(jsonDoc);
|
||||||
}
|
}
|
||||||
@ -50,3 +64,33 @@ bool GeneralSortFilterModel::lessThan(const QModelIndex& source_left,
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneralSortFilterModel::indexContainsText(const QModelIndex& index,
|
||||||
|
const QString& text) const {
|
||||||
|
// iterate over USER_FACING_ROLES and call roleDataContainsText(...);
|
||||||
|
// ...for each until text is found or no more roles to check;
|
||||||
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
const bool isTextFound = roleDataContainsText(index, role, text);
|
||||||
|
if (isTextFound) {
|
||||||
|
// qInfo() << "Text is found in role:" << role;
|
||||||
|
return true;
|
||||||
|
// } else {
|
||||||
|
// qDebug() << QString("Can't find text in role %1. Continuing search...").arg(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// qWarning() << "Text not found in any role. Returning false...";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneralSortFilterModel::roleDataContainsText(const QModelIndex& index,
|
||||||
|
const int role,
|
||||||
|
const QString& text) const {
|
||||||
|
const QString dataString = index.data(role).toString();
|
||||||
|
if (dataString.contains(text, Qt::CaseInsensitive)) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -11,6 +11,13 @@ class GeneralSortFilterModel : public QSortFilterProxyModel {
|
|||||||
public:
|
public:
|
||||||
explicit GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel = nullptr);
|
explicit GeneralSortFilterModel(std::shared_ptr<TableModel> sourceModel = nullptr);
|
||||||
|
|
||||||
|
/** Returns a QItemSelection with all ModelIndexes which contain the given text.
|
||||||
|
* @brief Returns QItemSelection containing all successful ModelIndex results
|
||||||
|
* @param text To search for
|
||||||
|
* @return QItemSelection containing all successful ModelIndex results
|
||||||
|
*/
|
||||||
|
QItemSelection findItems(const QString& text) const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void appendItems(const QByteArray& jsonDoc);
|
void appendItems(const QByteArray& jsonDoc);
|
||||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||||
@ -21,6 +28,9 @@ class GeneralSortFilterModel : public QSortFilterProxyModel {
|
|||||||
private:
|
private:
|
||||||
std::shared_ptr<TableModel> m_tableModel;
|
std::shared_ptr<TableModel> m_tableModel;
|
||||||
QCollator m_collator; /// for sorting
|
QCollator m_collator; /// for sorting
|
||||||
|
|
||||||
|
bool indexContainsText(const QModelIndex& index, const QString& text) const;
|
||||||
|
bool roleDataContainsText(const QModelIndex& index, const int role, const QString& text) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GENERALSORTFILTERMODEL_H
|
#endif // GENERALSORTFILTERMODEL_H
|
||||||
|
|||||||
Reference in New Issue
Block a user