forked from guangmingwan/rs97-txtReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtxtreader.cpp
500 lines (428 loc) · 17.7 KB
/
qtxtreader.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
** Copyright (C) 2013 Jiří Procházka (Hobrasoft)
** Contact: http://www.hobrasoft.cz/
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file is under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing
** in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the
** GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
*/
#include "qtxtreader.h"
#include <QApplication>
#include <QClipboard>
#include <QMimeData>
#include <QFontDatabase>
#include <QInputDialog>
#include <QColorDialog>
#include <QTextList>
#include <QtDebug>
#include <QFileDialog>
#include <QImageReader>
#include <QSettings>
#include <QBuffer>
#include <QUrl>
#include <QPlainTextEdit>
#include <QString>
#include <QFont>
#include <QMenu>
#include <QDialog>
#include<QScrollBar>
#include <stdlib.h>
#include <math.h>
QString qtxtReader::styleSheetFromFile(QString file, QString folderForUrl)
{
qDebug("styleSheetFromFile %ls %ls", reinterpret_cast<const wchar_t *>(file.utf16()), reinterpret_cast<const wchar_t *>(folderForUrl.utf16()));
QFile f(file);
f.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(f.readAll());
f.close();
styleSheet.replace("url(","url("+folderForUrl+"/");
return styleSheet;
}
qtxtReader::qtxtReader(QWidget *parent) : QWidget(parent) , scrollValue(0), fontSize(12), textColor(QString("#000000"))
{
setupUi(this);
#ifdef __APPLE__
#else
this->setWindowFlags(Qt::FramelessWindowHint);//Remove the title bar
#endif
//this->setGeometry(QRect(0, 0, 320, 240));//The orientation and size of the window display can be set
m_lastBlockList = 0;
f_textedit->setTabStopWidth(40);
connect(f_textedit->verticalScrollBar(),SIGNAL(valueChanged(int)),this, SLOT(setValue(int)));
f_textedit->installEventFilter(this);
f_textedit->verticalScrollBar()->setStyleSheet(
"QScrollBar:vertical"
"{"
"width:4px;"
"background:rgba(0,0,0,0%);"
"margin:0px,0px,0px,0px;"
"padding-top:9px;"
"padding-bottom:9px;"
"}"
"QScrollBar::handle:vertical"
"{"
"width:4px;"
"background:rgba(0,0,0,25%);"
" border-radius:4px;"
"min-height:20;"
"}"
"QScrollBar::handle:vertical:hover"
"{"
"width:4px;"
"background:rgba(0,0,0,50%);"
" border-radius:4px;"
"min-height:20;"
"}"
"QScrollBar::add-line:vertical"
"{"
"height:9px;width:4px;"
"border-image:url(:/images/a/3.png);"
"subcontrol-position:bottom;"
"}"
"QScrollBar::sub-line:vertical"
"{"
"height:9px;width:4px;"
"border-image:url(:/images/a/1.png);"
"subcontrol-position:top;"
"}"
"QScrollBar::add-line:vertical:hover"
"{"
"height:9px;width:4px;"
"border-image:url(:/images/a/4.png);"
"subcontrol-position:bottom;"
"}"
"QScrollBar::sub-line:vertical:hover"
"{"
"height:9px;width:4px;"
"border-image:url(:/images/a/2.png);"
"subcontrol-position:top;"
"}"
"QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
"{"
"background:rgba(0,0,0,10%);"
"border-radius:4px;"
"}"
);
m_fontsize_h1 = 18;
m_fontsize_h2 = 16;
m_fontsize_h3 = 14;
m_fontsize_h4 = 12;
m_paragraphItems << tr("Standard")
<< tr("Heading 1")
<< tr("Heading 2")
<< tr("Heading 3")
<< tr("Heading 4")
<< tr("Monospace");
// undo & redo
// cut, copy & paste
#ifndef QT_NO_CLIPBOARD
//connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(slotClipboardDataChanged()));
#endif
// link
// bold, italic & underline
QAction *actFullscreen = new QAction(QString::fromUtf8("Fullscreen"), this);
//actReloadByUTF8->setShortcut(QKeySequence("CTRL+M"));
connect(actFullscreen, SIGNAL(triggered()), this, SLOT(fullscreen()));
//f_textedit->addAction(actFullscreen);
QAction *actReloadByUTF8 = new QAction(QString::fromUtf8("Force UTF8"), this);
//actReloadByUTF8->setShortcut(QKeySequence("CTRL+M"));
connect(actReloadByUTF8, SIGNAL(triggered()), this, SLOT(reloadByUtf8()));
//f_textedit->addAction(actReloadByUTF8);
QAction *actReloadByGBK = new QAction(QString::fromUtf8("Force GBK"), this);
connect(actReloadByGBK, SIGNAL(triggered()), this, SLOT(reloadByGBK()));
//f_textedit->addAction(actReloadByGBK);
QAction *textsource = new QAction(QString::fromUtf8("Quit"), this);
textsource->installEventFilter(this);
//textsource->setShortcut(QKeySequence("CTRL+O"));
connect(textsource, SIGNAL(triggered()), this, SLOT(slotQuit()));
//f_textedit->addAction(textsource);
f_textedit->setFocus();
f_textedit->setFrameStyle(QFrame::NoFrame);
menu = new QMenu(this);
menu->installEventFilter(this);
menu->addAction(actFullscreen);
QMenu *menuReload = menu->addMenu(QString::fromUtf8("Encoding"));
menuReload->installEventFilter(this);
//menu->addAction(actReloadByGBK);
menuReload->addAction(actReloadByGBK);
menuReload->addAction(actReloadByUTF8);
QMenu *subMenu = menu->addMenu(QString::fromUtf8("Font"));
QAction *actFontMini = new QAction(QString::fromUtf8("Smallest(12)"), this);
connect(actFontMini, SIGNAL(triggered()), this, SLOT(actFontMini()));
QAction *actFontSmall = new QAction(QString::fromUtf8("Small(13)"), this);
connect(actFontSmall, SIGNAL(triggered()), this, SLOT(actFontSmall()));
QAction *actFontMedium = new QAction(QString::fromUtf8("Medium(14)"), this);
connect(actFontMedium, SIGNAL(triggered()), this, SLOT(actFontMedium()));
QAction *actFontBig = new QAction(QString::fromUtf8("Big(15)"), this);
connect(actFontBig, SIGNAL(triggered()), this, SLOT(actFontBig()));
subMenu->addAction(actFontMini);
subMenu->addAction(actFontSmall);
subMenu->addAction(actFontMedium);
subMenu->addAction(actFontBig);
subMenu->installEventFilter(this);
//menu->addAction(menuReload);
menu->addAction(textsource);
f_menu->setMenu(menu);
f_menu->setPopupMode(QToolButton::InstantPopup);
f_menu->setShortcut(QKeySequence(Qt::Key_Escape));
f_menu->setFocusPolicy(Qt::NoFocus);
f_toolbar->setWindowOpacity(0.5);
QFontDatabase db;
}
void qtxtReader::setValue(int v) {
this->scrollValue = v;
//qDebug("setValue %d",v);
}
void qtxtReader::readConfig() {
QString appPath = QDir::currentPath();
QString iniFileName = appPath +"/qtxtreader.ini";
qDebug("read from ini: %s", qPrintable(iniFileName));
QSettings *settings = new QSettings(iniFileName, QSettings::IniFormat);
//Write content to the ini file, the two parameters of the setValue function are key-value pairs
//Write content to the first section of the ini file, the first parameter under the ip section
//QString scrollValue = settings->getValue(tr("/progress/")+ tr(this->fileName));
QString textColor = settings->value("/skin/textcolor").toString();
int scrollValuel = settings->value(tr("/book_progress/")+ tr(this->fileName)).toInt();
int book_fontsize = settings->value(tr("/book_fontsize/")+ tr(this->fileName)).toInt();
if(textColor.length() >0) {
this->textColor = textColor;
f_textedit->setStyleSheet( tr("QTextEdit{color:%1}").arg(textColor) );
qDebug("read textColor: %s", qPrintable(this->textColor));
}
if(book_fontsize > 0) {
this->fontSize = book_fontsize;
QFont font = QFont();
font.setPointSize(book_fontsize);
f_textedit->setFont(font);
qDebug("read book fontsize: %d", book_fontsize);
}
else {
int fontsize = settings->value(tr("/skin/fontsize")).toInt();
if(fontsize >0) {
this->fontSize = fontsize;
QFont font2 = QFont();
font2.setPointSize(fontsize);
f_textedit->setFont(font2);
qDebug("read fontsize: %d", fontsize);
}
}
if(scrollValuel > 0) {
qDebug("read scrollValue: %d", scrollValuel);
this->scrollValue = scrollValuel;
f_textedit->verticalScrollBar()->setValue(this->scrollValue);
}
}
void qtxtReader::writeConfig() {
QString appPath = QDir::currentPath();
QString iniFileName = appPath +"/qtxtreader.ini";
qDebug("save to ini: %s", qPrintable(iniFileName));
qDebug("set fontsize: %d", this->fontSize);
qDebug("set textColor: %s", qPrintable(this->textColor));
qDebug("set scrollValue: %d", this->scrollValue);
QSettings *settings = new QSettings(iniFileName, QSettings::IniFormat);
//Write content to the ini file, the two parameters of the setValue function are key-value pairs
//Write content to the first section of the ini file, the first parameter under the ip section
//this->fontSize = 12;
settings->setValue(tr("/skin/fontsize"), this->fontSize);
settings->setValue(tr("/skin/textcolor"), this->textColor);
settings->setValue(tr("/book_progress/")+ tr(this->fileName), this->scrollValue);
settings->setValue(tr("/book_fontsize/")+ tr(this->fileName), this->fontSize);
settings->sync();
}
bool qtxtReader::eventFilter(QObject* obj, QEvent* event)
{
if (event->type()==QEvent::KeyPress) {
QKeyEvent* key = static_cast<QKeyEvent*>(event);
//qDebug("qtxtReader::eventFilter : key:0x%04x", key->key());
if(obj->metaObject()->className() == QString("QMenu")) { //a button ,when menu focus
if( key->key() == Qt::Key_Control) {
QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_Return,Qt::NoModifier,QString());
QCoreApplication::postEvent((QObject*)obj, eve1);
}
return false;
}
if( key->key()==Qt::Key_Tab || key->key()==Qt::Key_Left) {
//QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_PageUp, Qt::NoModifier, QString());
QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_PageUp,Qt::NoModifier,QString());
QCoreApplication::postEvent((QObject*)f_textedit, eve1);
}
else if ( key->key()==Qt::Key_Backspace || key->key()==Qt::Key_Right ) {
QKeyEvent * eve1 = new QKeyEvent (QEvent::KeyPress,Qt::Key_PageDown,Qt::NoModifier,QString());
QCoreApplication::postEvent((QObject*)f_textedit, eve1);
//qDebug("send key:0x%04x", eve1->key());
} else if ( key->key()==Qt::Key_Escape) {
if(!f_toolbar->isVisible()) {
f_toolbar->setVisible(true);
}
}
else {
return QObject::eventFilter(obj, event);
}
//return true;
} else {
return QObject::eventFilter(obj, event);
}
return false;
}
void qtxtReader::slotQuit()
{
this->writeConfig();
QApplication::quit();
}
void qtxtReader::fullscreen() {
f_toolbar->setVisible(false);
}
void qtxtReader::reloadByUtf8()
{
QString fileName = QString::fromUtf8(this->fileName);
//QTextCodec *codec = QTextCodec::codecForName("utf-8");
QFile file(this->fileName);
const wchar_t *encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
if (file.open(QIODevice::ReadOnly))
{
//reader->fileName = this->fileName;
this->setWindowTitle(fileName);
QTextStream stream(&file);
stream.setCodec("UTF-8");
QString str = stream.readAll();
qDebug("open file : %ls", encodedName);
this->setText(str);
}
else
{
QString str = QString("open file fail:%1 \n %2").arg(fileName, file.errorString());
this->setText(str);
}
}
void qtxtReader::actFontMini() {
//f_textedit->setFont
QFont font = QFont();
font.setPointSize(12);
this->fontSize = 12;
f_textedit->setFont(font);
}
void qtxtReader::actFontSmall() {
//f_textedit->setFont
QFont font = QFont();
font.setPointSize(13);
this->fontSize = 13;
f_textedit->setFont(font);
}
void qtxtReader::actFontMedium() {
QFont font = QFont();
font.setPointSize(14);
this->fontSize = 14;
f_textedit->setFont(font);
}
void qtxtReader::actFontBig() {
QFont font = QFont();
font.setPointSize(15);
this->fontSize = 15;
f_textedit->setFont(font);
}
void qtxtReader::reloadByGBK()
{
QString fileName = QString::fromUtf8(this->fileName);
//QTextCodec *codec = QTextCodec::codecForName("utf-8");
QFile file(this->fileName);
const wchar_t *encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
if (file.open(QIODevice::ReadOnly))
{
//reader->fileName = this->fileName;
this->setWindowTitle(fileName);
QTextStream stream(&file);
stream.setCodec("GBK");
QString str = stream.readAll();
qDebug("open file : %ls", encodedName);
this->setText(str);
}
else
{
QString str = QString("open file fail:%1 \n %2").arg(fileName, file.errorString());
this->setText(str);
}
}
// void qtxtReader::fontChanged(const QFont &f)
// {
// }
QString qtxtReader::toHtml() const
{
QString s = f_textedit->toHtml();
// convert emails to links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)([a-zA-Z\\d]+@[a-zA-Z\\d]+\\.[a-zA-Z]+)"), "\\1<a href=\"mailto:\\2\">\\2</a>");
// convert links
s = s.replace(QRegExp("(<[^a][^>]+>(?:<span[^>]+>)?|\\s)((?:https?|ftp|file)://[^\\s'\"<>]+)"), "\\1<a href=\"\\2\">\\2</a>");
// see also: Utils::linkify()
return s;
}
// Prints to the provided buffer a nice number of bytes (KB, MB, GB, etc)
void pretty_bytes(char* buf, uint bytes)
{
const char* suffixes[7];
suffixes[0] = "B";//"B";
suffixes[1] = "KB";//"KB";
suffixes[2] = "MB";//"MB";
uint s = 0; // which suffix to use
double count = bytes;
if(bytes>1000) {
s++;
count = bytes / 1000.0;
}
if(bytes>10000) {
s++;
count = bytes / 10000.0;
}
if(s<=0)
sprintf(buf, "%d%s", (int)count, suffixes[s]);
else
sprintf(buf, "%.1f%s", count, suffixes[s]);
}
void qtxtReader::setText(const QString &text)
{
if (text.isEmpty())
{
setPlainText(text);
return;
}
if (text[0] == '<')
{
setHtml(text);
}
else
{
setPlainText(text);
}
char cb[1024]="";
pretty_bytes(cb,text.length());
QString wc = QString::fromUtf8("字:") + QString::fromUtf8(cb);
this->l_word_count->setText( wc );
int bc = f_textedit->document()->blockCount();
qDebug("block count: %d", bc);
this->readConfig();
}
void qtxtReader::setCode(const QString &text)
{
if (text.isEmpty())
{
l_encode_name->setText(QString::fromUtf8("Unrecognized,Use default utf8"));
return;
}
else
{
l_encode_name->setText(QString::fromUtf8("Encoding:") + text);
}
}