-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordlistwidget.cc
170 lines (151 loc) · 5.69 KB
/
wordlistwidget.cc
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "wordlistwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListView>
#include <QMenu>
#include <QContextMenuEvent>
#include <QTextEdit>
#include <QListWidget>
#include <QListView>
#include <QStringListModel>
#include <QLabel>
#include <QFileInfo>
#include <QDebug>
#include <QPushButton>
#include <QStringListModel>
#include "wordmodel.h"
#include "words.h"
#include <QCheckBox>
#include <QThread>
void removeSeletedRowsFromView(QAbstractItemView *view) {
qDebug() << __PRETTY_FUNCTION__ << ":" << __LINE__;
auto selections = view->selectionModel()->selectedIndexes();
if (selections.isEmpty()) {
qDebug() << __PRETTY_FUNCTION__ << ":" << __LINE__;
return;
}
// sort indexes with x.row() from big to small.
std::sort(selections.begin(), selections.end(),
[](auto &&l, auto &&r) { return l.row() > r.row(); });
auto model = view->model();
for (auto idx : selections) {
qDebug() << "remove row :" << idx.row();
model->removeRow(idx.row());
}
view->update();
qDebug() << __PRETTY_FUNCTION__ << ":" << __LINE__;
}
struct PrivateData {
QListView *wordlistview{nullptr};
WordItemMap *data;
QStringListModel *model{nullptr};
WordModel *sourceModel{nullptr};
WordSortFilterProxyModel *proxyModel{nullptr};
WordModel::COLUMN_NO sortorder{WordModel::COLUMN_POS_IN_PAGE};
QCheckBox *btn_showdict{nullptr};
QCheckBox *btn_showConciseWordOnly{nullptr};
QCheckBox *btn_showConciseOnly{nullptr};
QPushButton *btn_scanscope;
std::shared_ptr<SqlSave> wordstore{nullptr};
};
WordlistWidget::WordlistWidget(QWidget *parent) : QWidget(parent) {
d = new PrivateData;
auto wordwgt = this;
// layout.
auto lay = new QVBoxLayout;
wordwgt->setLayout(lay);
d->btn_showdict = new QCheckBox("Show word in dict", this);
lay->addWidget(d->btn_showdict);
connect(d->btn_showdict, &QPushButton::clicked, this, &WordlistWidget::updateFilter);
d->btn_showConciseWordOnly = new QCheckBox("Show concise word only", this);
lay->addWidget(d->btn_showConciseWordOnly);
connect(d->btn_showConciseWordOnly, &QPushButton::clicked, this,
&WordlistWidget::updateFilter);
d->wordlistview = new QListView(this);
lay->addWidget(d->wordlistview);
// data.
d->sourceModel = new WordModel(nullptr);
d->proxyModel = new WordSortFilterProxyModel(this);
d->proxyModel->setSourceModel(d->sourceModel);
d->wordlistview->setModel(d->proxyModel);
d->wordlistview->setSelectionMode(QAbstractItemView::ExtendedSelection);
d->wordlistview->setContextMenuPolicy(Qt::CustomContextMenu);
connect(d->wordlistview, &QWidget::customContextMenuRequested, this,
&WordlistWidget::onListViewContextMenu);
}
void WordlistWidget::setWordStore(std::shared_ptr<SqlSave> wordstore) {
d->wordstore = wordstore;
}
void WordlistWidget::setupModel(WordItemMap *data) {
qDebug() << __PRETTY_FUNCTION__;
if (d->sourceModel == nullptr) {
return;
}
d->data = data;
d->sourceModel->setupModelData(data);
}
void WordlistWidget::onListViewContextMenu(const QPoint &pos) {
auto menu = new QMenu;
menu->addAction("test");
menu->addAction("sort by position", [this] {
d->sortorder = WordModel::COLUMN_POS_IN_PAGE;
d->proxyModel->sort(d->sortorder);
});
menu->addAction("sort by alphabeta", [this] {
d->sortorder = WordModel::COLUMN_WORD;
d->proxyModel->sort(d->sortorder);
});
auto sels = d->wordlistview->selectionModel()->selectedIndexes();
if (!sels.isEmpty()) {
menu->addSeparator();
// sort indexes with x.row() from big to small.
menu->addAction("knew", [this]() {
qDebug() << "mark as knew.";
this->markSelectionWithLevel(WORD_IS_KNOWN);
});
menu->addAction("ignore",
[this]() { this->markSelectionWithLevel(WORD_IS_IGNORED); });
menu->addAction("add to dict",
[this]() { this->markSelectionWithLevel(WORD_IS_LEARNING); });
}
menu->exec(mapToGlobal(pos));
}
//NOTE: if should know the level before update:
//1. for sql, add/update, should know.
//2. highlight, doesn't need, highlight use ptr.
void WordlistWidget::markSelectionWithLevel(wordlevel_t lv) {
auto sels = d->wordlistview->selectionModel()->selectedIndexes();
qDebug() << "sels size:" << sels.size();
std::sort(sels.begin(), sels.end(),
[](auto &&l, auto &&r) { return l.row() > r.row(); });
QList<WordItem *> edits;
QStringList words;
for (auto idx : sels) {
auto src_idx = d->proxyModel->mapToSource(idx);
auto *item = static_cast<WordItem *>(src_idx.internalPointer());
auto word = item->word;
edits.push_back(item);
words.push_back(word);
d->wordstore->updateWord(word, item->wordlevel, lv);
// qDebug() << QString("update word %1 from level %2 to level %3")
// .arg(word)
// .arg(levelString(item->wordlevel))
// .arg(levelString(lv));
item->wordlevel = lv;
d->data->value(word)->wordlevel = lv;
}
// TODO: make connections to edit highlight.
emit markItemsLevel(words, lv);
// TODO: mark to
// 1. knew, hide.
// 2. dict, hide or show, checkings. filter should check settings then show/hide.
// 3. ignore, hide.
d->proxyModel->updateFilter();
d->wordlistview->update();
}
void WordlistWidget::onPageLoadBefore() { d->sourceModel->reset_data_before(); }
void WordlistWidget::onPageLoadAfter() {
d->sourceModel->reset_data_after();
d->wordlistview->reset();
d->proxyModel->sort(d->sortorder);
}