-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordmodel.h
91 lines (75 loc) · 2.69 KB
/
wordmodel.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include "worditem.h"
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <memory>
// NOTE: implementation notes:
// 1. column 1 is word. use for wordlist of QListView.
// 1. column 2,3,4, for different usage, may contains:
// - word visibility => simple filter.
// - word definition, show in widget.
// - word rect, used for annotation.
class WordModel : public QAbstractItemModel {
Q_OBJECT
public:
using modeldata_t = WordItemMap;
enum COLUMN_NO {
COLUMN_WORD,
COLUMN_VISIBLE,
COLUMN_MEANING,
COLUMN_PAGE,
COLUMN_POS_IN_PAGE,
COLUMN_END
};
WordModel( QObject *parent = nullptr);
void setupModelData(modeldata_t *document);
~WordModel() = default;
QVariant data(const QModelIndex &index, int role) const override;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &child) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
void reset_data_before() { beginResetModel(); }
void reset_data_after() { endResetModel(); }
// Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
modeldata_t *m_data;
};
class WordSortFilterProxyModel : public QSortFilterProxyModel {
Q_OBJECT
public:
WordSortFilterProxyModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent) {}
void updateFilter();
void setSearchPattern(const QString &ptn);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &source_left,
const QModelIndex &source_right) const override;
private:
QString m_match{};
};
// TODO: may be this one is more suitable than WordModel.
class WordListModel : public QAbstractListModel {
public:
using modeldata_t = WordItemMap;
WordListModel(modeldata_t *data, QObject *parent = nullptr)
: QAbstractListModel(parent), m_data(data){};
QVariant data(const QModelIndex &index, int role) const override {
if (role != Qt::DisplayRole) return {};
// if (x < 0 || x >= m_data.length()) return {};
// auto &&cur = m_data[x];
switch (role) {
case Qt::DisplayRole:
return "hello";
default:
break;
}
return {};
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override {
return parent.isValid() ? 0 : m_data->size();
}
private:
const modeldata_t *m_data;
};