-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectWidgetSortModel.cpp
68 lines (59 loc) · 2.19 KB
/
ProjectWidgetSortModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "ProjectWidgetSortModel.h"
#include "ProjectWidget.h"
ProjectWidgetSortModel::ProjectWidgetSortModel(QObject* parent) :
QSortFilterProxyModel(parent)
{
}
void ProjectWidgetSortModel::setFilteredContent(const QString& content)
{
m_filteredContent = content;
invalidateFilter();
}
bool ProjectWidgetSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (!left.parent().isValid() && !right.parent().isValid())
{
//top items (summaries) are sorted alphabethically
QModelIndex leftSummaryIndex = left.sibling(left.row(), ProjectWidget::TAG_COLUMN);
QString leftSummary = leftSummaryIndex.data().toString();
QModelIndex rightSummaryIndex = right.sibling(right.row(), ProjectWidget::TAG_COLUMN);
QString rightSummary = rightSummaryIndex.data().toString();
return (leftSummary < rightSummary);
}
if (left.parent().isValid() && right.parent().isValid())
{
//tags stay unsorted
QModelIndex leftTagIndex = left.sibling(left.row(), ProjectWidget::TAG_COLUMN);
QModelIndex rightTagIndex = right.sibling(right.row(), ProjectWidget::TAG_COLUMN);
return (leftTagIndex.data(Qt::UserRole).toInt() < rightTagIndex.data(Qt::UserRole).toInt());
}
return QSortFilterProxyModel::lessThan(left, right);
}
bool ProjectWidgetSortModel::filterAcceptsColumn(int, const QModelIndex&) const
{
return true;
}
bool ProjectWidgetSortModel::filterAcceptsRow(int source_row,
const QModelIndex& source_parent) const
{
if (m_filteredContent.isEmpty())
{
return true;
}
if (source_parent.isValid())
{
return true;
}
QAbstractItemModel* dataModel = sourceModel();
QModelIndex vCardIndex = dataModel->index(source_row, ProjectWidget::TAG_COLUMN);
for (int childRow = 0; childRow < dataModel->rowCount(vCardIndex); ++childRow)
{
QModelIndex contentIndex = dataModel->index(childRow, ProjectWidget::CONTENT_COLUMN, vCardIndex);
QString content = contentIndex.data().toString();
if (content == m_filteredContent)
{
return true;
}
}
return false;
}