-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindreplacearea.cpp
572 lines (492 loc) · 18.4 KB
/
findreplacearea.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
This file is part of cPad Project.
cPad is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cPad is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cPad. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include "findreplacearea.h"
#include "cpadpalette.h"
FindReplaceArea::FindReplaceArea(QWidget *parent, CodeEditor *editor,
QAction *find, QAction *replace):
QDockWidget(parent)
{
codeEditor = editor;
findAction = find;
replaceAction = replace;
createWidget();
generalSetting();
findFlag = 0; // for the check box;
}
void FindReplaceArea::closeEvent(QCloseEvent *event)
{
replaceAction->setChecked(false);
findAction->setChecked(false);
event->accept();
}
void FindReplaceArea::createWidget()
{
//中央控件和布局控件
findWidget = new QWidget();
replaceWidget = new QWidget();
findLayout = new QGridLayout();
findWidget->setLayout(findLayout);
replaceLayout = new QGridLayout();
replaceWidget->setLayout(replaceLayout);
findLabel = new QLabel(tr("查找内容(&S):"));
replaceLabel = new QLabel(tr(" 替换为(&P):"));
findLineEdit = new QLineEdit();
replaceLineEdit = new QLineEdit();
previousButton = new QPushButton(QIcon(":/resources/previous.png"),
tr("上一个(&U)"));
nextButton = new QPushButton(QIcon(":/resources/next.png"),
tr("下一个(&D)"));
closeButton = new QPushButton(tr("关闭(&X)"));
replaceButton = new QPushButton(tr("替换(&R)"));
replaceAllButton = new QPushButton(tr("替换全部(&A)"));
caseSensitive = new QCheckBox(tr("区分大小写(&C)"));
consistentMatch = new QCheckBox(tr("全字匹配(&W)"));
highLightResult = new QCheckBox(tr("高亮结果(&L)"));
}
void FindReplaceArea::transLayout(bool topLevel)
{
if (topLevel) {
floatingLayout();
}
else {
dockedLayout();
}
}
void FindReplaceArea::openFindArea(bool open)
{
if (open) {
replaceAction->setChecked(false);
findAction->setChecked(true);
currentMode = FindMode;
transLayout(isFloating());
show();
}
else {
close();
clearAll();
}
findLineEdit->setFocus();
}
void FindReplaceArea::openReplaceArea(bool open)
{
if (open) {
findAction->setChecked(false);
replaceAction->setChecked(true);
currentMode = ReplaceMode;
transLayout(isFloating());
show();
}
else {
close();
clearAll();
}
findLineEdit->setFocus();
}
void FindReplaceArea::floatingLayout()
{
if (currentMode == FindMode) {
setWidget(findWidget);//中心区域为查找
setWindowTitle(tr("查找"));
findLineEdit->setFixedSize(195, 22);
findLayout->addWidget(findLabel, 0, 0);
findLayout->addWidget(findLineEdit, 0, 1, 1, 2);
findLayout->addWidget(previousButton, 0, 3);
findLayout->addWidget(nextButton, 0, 4);
findLayout->addWidget(caseSensitive, 1, 1);
findLayout->addWidget(consistentMatch, 1, 2);
findLayout->addWidget(highLightResult, 1, 3);
findLayout->addWidget(closeButton, 1, 4);
findLayout->setContentsMargins(11, 2, 11, 0);
//设置固定大小
setFixedSize(480, 80);
}
else {
setWidget(replaceWidget);//中心区域为替换
setWindowTitle(tr("替换"));
findLineEdit->setFixedSize(195, 22);
replaceLineEdit->setFixedSize(195, 22);
replaceLayout->addWidget(findLabel, 0, 0);
replaceLayout->addWidget(findLineEdit, 0, 1, 1, 2);
replaceLayout->addWidget(previousButton, 0, 3);
replaceLayout->addWidget(nextButton, 0, 4);
replaceLayout->addWidget(replaceLabel, 1, 0);
replaceLayout->addWidget(replaceLineEdit, 1, 1, 1, 2);
replaceLayout->addWidget(replaceButton, 1, 3);
replaceLayout->addWidget(replaceAllButton, 1, 4);
replaceLayout->addWidget(caseSensitive, 2, 1);
replaceLayout->addWidget(consistentMatch, 2, 2);
replaceLayout->addWidget(highLightResult, 2, 3);
replaceLayout->addWidget(closeButton, 2, 4);
replaceLayout->setContentsMargins(11, 2, 11, 0);
//设置固定大小
setFixedSize(480, 105);
}
}
void FindReplaceArea::dockedLayout()
{
setWindowTitle(tr(""));//附着时不用标题
if (currentMode == FindMode) {
setWidget(findWidget);//中心区域为查找
findLineEdit->setFixedSize(150, 22);
findLayout->addWidget(findLabel, 0, 0);
findLayout->addWidget(findLineEdit, 0, 1);
findLayout->addWidget(previousButton, 0, 2);
findLayout->addWidget(nextButton, 0, 3);
findLayout->addWidget(caseSensitive, 0, 5);
findLayout->addWidget(consistentMatch, 0, 4);
findLayout->addWidget(highLightResult, 1, 4);
findLayout->setContentsMargins(7, 2, 11, 7);
findLayout->setRowMinimumHeight(1, 23);
findLayout->addWidget(closeButton, 1, 5);
//设置固定大小
setFixedSize(650, 65);
}
else {
setWidget(replaceWidget);//中心区域为替换
findLineEdit->setFixedSize(150, 22);
replaceLineEdit->setFixedSize(150, 22);
replaceLayout->addWidget(findLabel, 0, 0);
replaceLayout->addWidget(findLineEdit, 0, 1);
replaceLayout->addWidget(previousButton, 0, 2);
replaceLayout->addWidget(nextButton, 0, 3);
replaceLayout->addWidget(replaceLabel, 1, 0);
replaceLayout->addWidget(replaceLineEdit, 1, 1, 1, 2);
replaceLayout->addWidget(replaceButton, 1, 2);
replaceLayout->addWidget(replaceAllButton, 1, 3);
replaceLayout->addWidget(caseSensitive, 0, 5);
replaceLayout->addWidget(consistentMatch, 0, 4);
replaceLayout->addWidget(highLightResult, 1, 4);
replaceLayout->setContentsMargins(7, 2, 11, 7);
replaceLayout->addWidget(closeButton, 1, 5);
//设置固定大小
setFixedSize(650, 65);
}
}
void FindReplaceArea::generalSetting()
{
//设置 Dock 可移动区域为上下
setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
//查找栏 可关 可移 可浮出 竖向标题
setFeatures(QDockWidget::DockWidgetClosable |
QDockWidget::DockWidgetMovable |
QDockWidget::DockWidgetFloatable |
QDockWidget::DockWidgetVerticalTitleBar
);
//Dock[ Widget[ Layout[ ... ] ] ]
//设置中央控件
//设置label的伙伴(Buddy),作用:
//当按下label快捷键时,findLineEdit将获取到焦点
findLabel->setBuddy(findLineEdit);
replaceLabel->setBuddy(replaceLineEdit);
setWindowTitle(tr("查找和替换"));//附着时不用标题
//任意付个初值
currentMode = FindMode;
QMainWindow *main = qobject_cast<QMainWindow *>(parent());
main->addDockWidget(Qt::BottomDockWidgetArea, this);
hide();
connect(findAction, SIGNAL(toggled(bool)),
this, SLOT(openFindArea(bool)));
connect(replaceAction, SIGNAL(toggled(bool)),
this, SLOT(openReplaceArea(bool)));
connect(this, SIGNAL(topLevelChanged(bool)),
this, SLOT(transLayout(bool)));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));
//实时高亮
connect(findLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(runtimeSearchToggled(QString)));
//点击下一个,上一个时调用的函数
connect(nextButton, SIGNAL(clicked()),
this, SLOT(searchNext()));
connect(previousButton, SIGNAL(clicked()),
this, SLOT(searchPrevious()));
//勾选区分大小写和全字匹配
connect(caseSensitive, SIGNAL(stateChanged(int)),
this, SLOT(checkButtonToggled(int)));
connect(consistentMatch, SIGNAL(stateChanged(int)),
this, SLOT(checkButtonToggled(int)));
//全部替换
connect(replaceAllButton, SIGNAL(clicked()),
this, SLOT(replaceAll()));
//替换
connect(replaceButton, SIGNAL(clicked()),
this, SLOT(replace()));
//高亮
connect(highLightResult, SIGNAL(stateChanged(int)),
this, SLOT(highlightResultChecked(int)));
//user change text
connect(codeEditor, SIGNAL(textChanged()),
this, SLOT(userChangedText()));
}
/* 在搜索过程中,何时会出现 cursor.isNull 何时会出现 cursor.atEnd
* isNull 为真的情况是,光标位置到文档末尾都不存在要找的字符,和之前是否找到过关键字无关。
* atEnd 就是光标位于文档末尾
*/
/* always search from the start */
void FindReplaceArea::runtimeSearch(QString text)
{
QString searchString = text;
if ( searchString == "" )
{
codeEditor->setTextCursor(QTextCursor(codeEditor->document()));
return;
}
QTextDocument::FindFlags findFlag(0); // init to 0, this is important
if ( consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords;
else if ( consistentMatch->isChecked() && !caseSensitive->isChecked() )
findFlag = QTextDocument::FindWholeWords;
else if ( ! consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively;
QTextDocument *document = codeEditor->document();
//下面初始化 cursor,初始化后位置都在文档前面
QTextCursor highlightCursor(document);
// 移动光标去找
highlightCursor = document->find(searchString, highlightCursor, findFlag);
if (!highlightCursor.isNull() )
{
codeEditor->setTextCursor(highlightCursor);
}
else if ( highlightCursor.isNull() )
{
codeEditor->setTextCursor(QTextCursor(codeEditor->document()));
}
highlightCursor.clearSelection();
}
/* 改变文字的时候调用这里,如果勾选了高亮,再高亮一次 */
void FindReplaceArea::runtimeSearchToggled(QString text)
{
runtimeSearch(text);
if ( highLightResult->isChecked() )
clearAndHighlightAll(text);
}
//查找下一个对应的函数
void FindReplaceArea::searchNext()
{
QString searchString = findLineEdit->text();
if ( searchString == "" )
return;
QTextDocument::FindFlags findFlag(0); // init to 0, this is important
if ( consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords;
else if ( consistentMatch->isChecked() && !caseSensitive->isChecked() )
findFlag = QTextDocument::FindWholeWords;
else if ( ! consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively;
QTextDocument *document = codeEditor->document();
//下面初始化两个 cursor,初始化后位置都在文档前面
QTextCursor highlightCursor(codeEditor->textCursor());
// 移动光标去找
highlightCursor = document->find(searchString, highlightCursor, findFlag);
if (!highlightCursor.isNull() )
{
codeEditor->setTextCursor(highlightCursor);
}
else if ( highlightCursor.isNull() ) //如果没有找到,从文档起始位置开始再找一次
{
// find one more time
highlightCursor = document->find(searchString, QTextCursor(document), findFlag);
if (!highlightCursor.isNull() )
{
codeEditor->setTextCursor(highlightCursor);
}
}
highlightCursor.clearSelection();
}
//查找上一个对应的函数
void FindReplaceArea::searchPrevious()
{
QString searchString = findLineEdit->text();
if ( searchString == "" )
return;
QTextDocument::FindFlags findFlag(QTextDocument::FindBackward); // init to 0, this is important
if ( consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords | QTextDocument::FindBackward;
else if ( consistentMatch->isChecked() && !caseSensitive->isChecked() )
findFlag = QTextDocument::FindWholeWords | QTextDocument::FindBackward;
else if ( ! consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindBackward;
QTextDocument *document = codeEditor->document();
QTextCursor highlightCursor(codeEditor->textCursor());
highlightCursor = document->find(searchString, highlightCursor, findFlag);
if ( !highlightCursor.isNull() ) //如果找到了
{
codeEditor->setTextCursor(highlightCursor);
}
else if ( highlightCursor.isNull() )//如果没有找到就再从文档末尾往回找一次
{
QTextCursor end(document->lastBlock());
end.setPosition(end.position()+document->lastBlock().text().length());
// find one more time
highlightCursor = document->find(searchString, end, findFlag);
if (!highlightCursor.isNull() )
{
codeEditor->setTextCursor(highlightCursor);
}
}
highlightCursor.clearSelection();
}
//当勾选高亮全部的按钮时候,调用这个函数
void FindReplaceArea::highlightResultChecked(int state)
{
if ( state == Qt::Checked )
{
clearAndHighlightAll( findLineEdit->text() );
}
else
{
clearAll();
}
}
/*在查找高亮之前,需要清除以前的背景
* text 是上一次查找的词*/
void FindReplaceArea::clearAll()
{
QList<QTextEdit::ExtraSelection> extraSelection;
CpadPalette cpadPalette;
QColor lineColor = cpadPalette.defaultGroup[10];
if (!codeEditor->isReadOnly()) {
QTextEdit::ExtraSelection selection;
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = codeEditor->textCursor();
selection.cursor.clearSelection();
extraSelection.append(selection);
}
codeEditor->setExtraSelections(extraSelection);
}
void FindReplaceArea::clearAndHighlightAll(QString text)
{
QTextDocument *document = codeEditor->document();
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
CpadPalette cpadPalette;
////////////////////////
clearAll();
QList<QTextEdit::ExtraSelection> selections = codeEditor->extraSelections();
QTextEdit::ExtraSelection selection;
QTextCharFormat format = selection.format;
format.setBackground(cpadPalette.highlightForSearch);
selection.format = format;
if ( consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords;
else if ( consistentMatch->isChecked() && !caseSensitive->isChecked() )
findFlag = QTextDocument::FindWholeWords;
else if ( ! consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively;
else
findFlag = 0;
cursor.beginEditBlock();
while ( !highlightCursor.isNull() && !highlightCursor.atEnd() )
{
highlightCursor = document->find(text, highlightCursor,findFlag);
if (!highlightCursor.isNull())
{
QTextCursor cursor = highlightCursor;
cursor.setPosition(highlightCursor.position()-text.length(),QTextCursor::MoveAnchor);
cursor.setPosition(highlightCursor.position(), QTextCursor::KeepAnchor);
selection.cursor = cursor;
selections.append(selection);
}
}
cursor.endEditBlock();
codeEditor->setExtraSelections(selections);
}
//当区分大小写和全字匹配勾选或取消勾选的时候,调用这里重新搜索
void FindReplaceArea::checkButtonToggled(int state)
{
QString text = findLineEdit->text();
if ( highLightResult->isChecked() == true )
clearAndHighlightAll(text);
runtimeSearch(text);
}
//替换全部
void FindReplaceArea::replaceAll()
{
QString searchString = findLineEdit->text();
if ( searchString == "" )
return;
QString replaceWith = replaceLineEdit->text();
if ( consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively | QTextDocument::FindWholeWords;
else if ( consistentMatch->isChecked() && !caseSensitive->isChecked() )
findFlag = QTextDocument::FindWholeWords;
else if ( ! consistentMatch->isChecked() && caseSensitive->isChecked() )
findFlag = QTextDocument::FindCaseSensitively;
QTextDocument *document = codeEditor->document();
//下面初始化两个 cursor,初始化后位置都在文档前面
QTextCursor highlightCursor(document);
QTextCursor cursor(document);
cursor.beginEditBlock();
// 移动光标去找
while ( !highlightCursor.isNull() && !highlightCursor.atEnd() )
{
highlightCursor = document->find(searchString, highlightCursor, findFlag);
if (!highlightCursor.isNull())
{
highlightCursor.removeSelectedText();
highlightCursor.insertText(replaceWith);
}
}
cursor.endEditBlock();
}
void FindReplaceArea::replace()
{
QString replaceWith = replaceLineEdit->text();
QTextCursor cursor = codeEditor->textCursor();
if ( cursor.hasSelection() )
{
cursor.removeSelectedText();
cursor.insertText(replaceWith);
}
//替换之后再查找一次
searchNext();
}
/*
* 用户更改文本时候调用这个函数
* 作用是在用户输入时,判断输入的位置是否有查找的高亮存在,有则把高亮取消掉
*/
void FindReplaceArea::userChangedText()
{
QTextCursor cursor = codeEditor->textCursor();
int listPosition = inExtraSelection(cursor.position());
if ( listPosition == -1 )
return;
else
{
QList <QTextEdit::ExtraSelection> extraSelection = codeEditor->extraSelections();
extraSelection.removeAt(listPosition);
codeEditor->setExtraSelections(extraSelection);
}
}
/*
* 查找给定光标是否在已有的 extraSelection 中。
*/
int FindReplaceArea::inExtraSelection(int cursorPosition)
{
QList <QTextEdit::ExtraSelection> extraSelection = codeEditor->extraSelections();
int listLength = extraSelection.length();
if ( listLength == 1 ) // 这个里面只存了当前的行高亮
return -1;
int searchLength = findLineEdit->text().length();
for ( int i = 1; i < listLength; i++ )
{
int end = extraSelection[i].cursor.position();
int start = end - searchLength;
if ( cursorPosition < end && cursorPosition > start )
return i;
}
return -1;
}