-
Notifications
You must be signed in to change notification settings - Fork 0
/
textedit.cpp
140 lines (132 loc) · 4.59 KB
/
textedit.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
#include "textedit.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <decryptlog.h>
#include <QDesktopWidget>
#include <QApplication>
TextEdit::TextEdit(QWidget *parent)
: QWidget(parent)
{
textEdit = new QTextEdit(this);
textEdit->setReadOnly(true);
this->setAutoFillBackground(true);
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(textEdit);
this->setLayout(mainLayout);
isFirstTime = true;
}
void TextEdit::readDescryptFile(QString& fileName)
{
DecryptLog decrypt;
QString text;
decrypt.Open_Decrypt(fileName, text);
textEdit->append(text);
}
void TextEdit::findButton(const QRegExp searchString)
{
QTextDocument *document = textEdit->document();
if (isFirstTime == false)
document->undo();
highLightRows.clear();
curHighlightRow = -1;
if (searchString.isEmpty())
{
return;
}
else
{
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
QTextCharFormat plainFormat(highlightCursor.charFormat());
QTextCharFormat colorFormat = plainFormat;
colorFormat.setBackground(Qt::yellow);
//qDebug() << searchString.pattern().size();
while (!highlightCursor.isNull() && !highlightCursor.atEnd())
{
//highlightCursor = document->find(searchString, highlightCursor, QTextDocument::FindWholeWords);
highlightCursor = document->find(searchString, highlightCursor);
if (!highlightCursor.isNull())
{
//highlightCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
//highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, searchString.pattern().size());
//highlightCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
highlightCursor.movePosition(QTextCursor::NoMove, QTextCursor::KeepAnchor);
highlightCursor.mergeCharFormat(colorFormat);
if(!highLightRows.contains(highlightCursor.blockNumber()))
highLightRows.append(highlightCursor.blockNumber());
}
}
cursor.endEditBlock();
isFirstTime = false;
if(!highLightRows.isEmpty())
curHighlightRow = highLightRows[0];
select_cur_line();
}
}
void TextEdit::go_to_previous_row()
{
if(-1 == curHighlightRow)
return;
int goLineIndex = highLightRows.indexOf(curHighlightRow)-1;
if(goLineIndex >= 0)
{
curHighlightRow = highLightRows[goLineIndex];
go_to_line(curHighlightRow);
/*
QDesktopWidget* desktopWidget = QApplication::desktop();
//得到客户区矩形
QRect clientRect = desktopWidget->availableGeometry();
//得到应用程序矩形
//QRect applicationRect = desktopWidget->screenGeometry();
int height = clientRect.height();
//int height = applicationRect.height();
qDebug() << height;
textEdit->verticalScrollBar()->setValue(textEdit->verticalScrollBar()->value() - height/2);
*/
}
}
void TextEdit::go_to_next_row()
{
if(-1 == curHighlightRow)
return;
int goLineIndex = highLightRows.indexOf(curHighlightRow)+1;
if(goLineIndex < highLightRows.size())
{
curHighlightRow = highLightRows[goLineIndex];
go_to_line(curHighlightRow);
/*
QDesktopWidget* desktopWidget = QApplication::desktop();
//得到客户区矩形
QRect clientRect = desktopWidget->availableGeometry();
//得到应用程序矩形
//QRect applicationRect = desktopWidget->screenGeometry();
int height = clientRect.height();
//int height = applicationRect.height();
qDebug() << height;
textEdit->verticalScrollBar()->setValue(textEdit->verticalScrollBar()->value() + height/2);
*/
//textEdit->verticalScrollBar()->setValue(textEdit->verticalScrollBar()->value() + 50);
}
}
void TextEdit::go_to_line(int line)
{
QTextDocument *document = textEdit->document();
QTextCursor cursor(document);
int position = document->findBlockByNumber (line).position();
cursor.setPosition(position,QTextCursor::MoveAnchor);
cursor.select(QTextCursor::LineUnderCursor);
textEdit->setTextCursor(cursor);
textEdit->setFocus();
}
void TextEdit::select_cur_line()
{
if(-1 != curHighlightRow)
{
int goLineIndex = highLightRows.indexOf(curHighlightRow);
if(goLineIndex < highLightRows.size())
go_to_line(curHighlightRow);
}
}