-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cc
307 lines (259 loc) · 8.8 KB
/
mainwindow.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "mainwindow.h"
#include "words.h"
#include "worditem.h"
#include "wordmodel.h"
#include "settings.h"
#include "wordlistwidget.h"
// #include <poppler-qt6.h>
#include <iostream>
#include <algorithm>
#include <format>
#include <QThread>
#include <QCoreApplication>
#include "pagecontainer.h"
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <poppler-qt6.h>
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <poppler-qt5.h>
#else
#include <poppler-qt4.h>
#endif // QT_VERSION
#include <poppler-annotation.h>
#include <QTextEdit>
#include <QListWidget>
#include <QListView>
#include <QStringListModel>
#include <QLabel>
#include <QFileInfo>
#include <QPushButton>
#include <QTransform>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSplitter>
#include <QRadioButton>
#include <QButtonGroup>
#include <QCheckBox>
#include <QToolButton>
#include <QMenu>
#include <QLineEdit>
#include <QDebug>
using namespace std;
namespace {
QString getWord(QString &carried, QString cur) {
auto orig = cur.toStdString();
if (cur.endsWith("-")) {
cur.chop(1);
carried += cur;
return "";
}
if (!carried.isEmpty()) {
cur = carried + cur;
carried.clear();
}
string res;
size_t i = 0;
for (; i < orig.size(); i++) {
if (!isalpha(orig[i])) {
continue;
} else {
break;
}
}
for (; i < orig.size(); i++) {
if (isalpha(orig[i])) {
res.push_back(orig[i]);
} else {
break;
}
}
return QString::fromStdString(res);
}
void removeSeletedRowsFromView(QAbstractItemView *view) {
auto selections = view->selectionModel()->selectedIndexes();
if (selections.isEmpty()) 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) {
model->removeRow(idx.row());
}
}
} // namespace
class Mainwindow::Private {
public:
// settings
Poppler::Document *document{nullptr};
// beware, it's 1-idnexed.
QLineEdit *edit_setPage{nullptr};
QLabel *label_showpage_cur{nullptr};
// gui
PageContainer *page_continer{nullptr};
WordlistWidget *wordwgt{nullptr};
std::shared_ptr<SqlSave> wordstore{nullptr}; // data, should be consistent.
WordItemMap wordItems_in_page;
QStringList words_docu_all; // all words in current page.
~Private() { cout << __PRETTY_FUNCTION__ << endl; }
};
Mainwindow::Mainwindow() {
d = new Private;
auto base = new QWidget;
auto baseVbox = new QVBoxLayout(base);
d->wordstore = make_shared<SqlSave>();
d->wordwgt = new WordlistWidget(this);
d->wordwgt->setWordStore(d->wordstore);
auto pageShower = new QWidget(this);
auto pageLay = new QHBoxLayout;
{
d->page_continer = new PageContainer(this);
pageLay->addWidget(d->page_continer);
pageLay->setStretch(0, 1);
}
d->wordwgt->setupModel(d->page_continer->focus()->getWordItems());
pageShower->setLayout(pageLay);
auto splitter = new QSplitter(this);
splitter->addWidget(d->wordwgt);
splitter->addWidget(pageShower);
auto toolbar = setupToolbar();
baseVbox->addWidget(toolbar, 0);
baseVbox->addWidget(splitter, 1);
this->setCentralWidget(base);
load_settings();
this->resize(100, 100);
connect(d->page_continer->focus(), &PageView::pageLoadBefore, d->wordwgt,
&WordlistWidget::onPageLoadBefore);
connect(d->page_continer->focus(), &PageView::PageLoadDone, d->wordwgt,
&WordlistWidget::onPageLoadAfter);
connect(d->wordwgt, &WordlistWidget::markItemsLevel, this, [this](QStringList words, wordlevel_t lv) {
auto pv = d->page_continer->focus();
pv->update_highlight(words);
});
}
void Mainwindow::openFile(const QString &filename) {
qDebug() << "should load file:" << filename;
if (QFileInfo(filename).exists() == false) {
cout << "load file fail, file not exist" << endl;
return;
}
d->document = Poppler::Document::load(filename);
d->label_showpage_cur->setText(QString(" of %1").arg(d->document->numPages()));
d->edit_setPage->setValidator(new QIntValidator(1, d->document->numPages(), this));
// cout <<"backend: "<< d->document->availableRenderBackends().size()<<endl;
// cout << "backend: now"<< d->document->renderBackend() << endl;
// d->document->setRenderBackend(Poppler::Document::QPainterBackend);
d->document->setRenderHint(Poppler::Document::Antialiasing);
d->document->setRenderHint(Poppler::Document::TextAntialiasing);
// this function will only run once, update later.
d->words_docu_all = words_forDocument();
d->page_continer->setDocument(d->document);
test_load_outline();
test_scan_annotations();
}
Mainwindow::~Mainwindow() {
delete d;
d = nullptr;
cout << __PRETTY_FUNCTION__ << endl;
}
QWidget *Mainwindow::setupToolbar() {
QWidget *base = new QWidget(this);
// as toolbar.
auto toolbar_lay = new QHBoxLayout(base);
auto btn_showoutline = new QToolButton(this);
btn_showoutline->setIcon(QIcon::fromTheme("arrow-left"));
btn_showoutline->setIconSize({32, 32});
toolbar_lay->addWidget(btn_showoutline);
connect(btn_showoutline, &QAbstractButton::clicked, this, [=]() {
// wordwgt->isHidden();
d->wordwgt->setHidden(!d->wordwgt->isHidden());
});
d->edit_setPage = new QLineEdit(this);
d->edit_setPage->setFixedWidth(100);
d->edit_setPage->setAlignment(Qt::AlignRight);
toolbar_lay->addWidget(d->edit_setPage);
// toolbar_lay->addWidget(new QLabel(" of "));
d->label_showpage_cur = new QLabel(this);
toolbar_lay->addWidget(d->label_showpage_cur);
connect(d->edit_setPage, &QLineEdit::returnPressed, this, [this]() {
auto txt = d->edit_setPage->text();
// this->go_to(txt.toInt() - 1);
});
auto btn1 = new QToolButton(this);
btn1->setIcon(QIcon::fromTheme("arrow-left"));
btn1->setIconSize({32, 32});
toolbar_lay->addWidget(btn1);
// connect(btn1, &QAbstractButton::clicked, this, &Mainwindow::go_previous);
auto btn2 = new QToolButton(this);
btn2->setIcon(QIcon::fromTheme("arrow-right"));
btn2->setIconSize({32, 32});
toolbar_lay->addWidget(btn2);
// connect(btn2, &QAbstractButton::clicked, this, &Mainwindow::go_next);
auto btn3 = new QToolButton(this);
btn3->setIcon(QIcon::fromTheme("zoom-in"));
btn3->setIconSize({32, 32});
toolbar_lay->addWidget(btn3);
// connect(btn3, &QAbstractButton::clicked, this, &Mainwindow::scale_bigger);
auto btn4 = new QToolButton(this);
btn4->setIcon(QIcon::fromTheme("zoom-out"));
btn4->setIconSize({32, 32});
toolbar_lay->addWidget(btn4);
// connect(btn4, &QAbstractButton::clicked, this, &Mainwindow::scale_smaller);
auto btn5 = new QPushButton("xx");
btn5->setIconSize({32, 32});
toolbar_lay->addWidget(btn5);
toolbar_lay->addStretch(1);
return base;
}
QStringList Mainwindow::words_forDocument() {
QStringList suffixes{"s", "es", "ed", "ing"};
QStringList res;
QSet<QString> words_cache;
QMap<QString, int> words_cntr;
for (int i = 0; i < d->document->numPages(); i++) {
auto page = d->document->page(i);
QString carried = "";
auto tx = page->textList();
for (auto i = tx.begin(); i < tx.end(); i++) {
auto cur = (*i)->text().simplified();
if (auto res = getWord(carried, cur); res.isEmpty()) {
continue;
} else {
cur = res;
}
bool contains = words_cache.contains(cur);
// deal with word duplication in one page.
if (contains) {
words_cntr[cur]++;
} else {
words_cache.insert(cur);
res.push_back(cur);
words_cntr[cur] = 1;
}
}
}
res = words_cntr.keys();
std::sort(res.begin(), res.end(),
[&](auto &&a, auto &&b) { return words_cntr[a] > words_cntr[b]; });
// test:
// for (auto w : res) {
// cout << words_cntr[w] << ": " << w.toStdString() << endl;
// }
return res;
}
void Mainwindow::test_scan_annotations() {
for (int i = 0; i < d->document->numPages(); i++) {
auto page = d->document->page(i);
auto annos = page->annotations();
if (annos.size()) {
qDebug() << QString("page %1 has %2 annotations.").arg(i).arg(annos.size());
}
}
}
void Mainwindow::load_settings() {
// d->scale = Settings::instance()->pageScale();
}
void Mainwindow::test_load_outline() {
auto outline = Outline();
outline.setDocument(d->document);
outline.load_outlie();
outline.display_outline();
}