forked from cloose/CuteMarkEd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindreplacewidget.cpp
More file actions
187 lines (151 loc) · 5.01 KB
/
findreplacewidget.cpp
File metadata and controls
187 lines (151 loc) · 5.01 KB
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
/*
* Copyright 2013 Christian Loose <christian.loose@hamburg.de>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "findreplacewidget.h"
#include "ui_findreplacewidget.h"
#include <QMenu>
#include <QPlainTextEdit>
FindReplaceWidget::FindReplaceWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::FindReplaceWidget),
textEditor(0),
findCaseSensitively(false),
findWholeWordsOnly(false),
findUseRegExp(false)
{
ui->setupUi(this);
setupFindOptionsMenu();
setFocusProxy(ui->findLineEdit);
}
FindReplaceWidget::~FindReplaceWidget()
{
delete ui;
}
void FindReplaceWidget::setTextEdit(QPlainTextEdit *editor)
{
textEditor = editor;
}
void FindReplaceWidget::showEvent(QShowEvent *)
{
ui->findLineEdit->selectAll();
}
void FindReplaceWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
event->accept();
close();
emit dialogClosed();
}
}
void FindReplaceWidget::findPreviousClicked()
{
if (!textEditor) return;
find(ui->findLineEdit->text(), QTextDocument::FindBackward);
}
void FindReplaceWidget::findNextClicked()
{
if (!textEditor) return;
find(ui->findLineEdit->text());
}
void FindReplaceWidget::replaceClicked()
{
QString oldText = ui->findLineEdit->text();
QString newText = ui->replaceLineEdit->text();
QTextCursor cursor = textEditor->textCursor();
cursor.beginEditBlock();
if (cursor.hasSelection()) {
cursor.insertText(newText);
}
find(oldText);
cursor.endEditBlock();
}
void FindReplaceWidget::replaceAllClicked()
{
QString oldText = ui->findLineEdit->text();
QString newText = ui->replaceLineEdit->text();
textEditor->moveCursor(QTextCursor::Start);
QTextCursor cursor = textEditor->textCursor();
cursor.beginEditBlock();
bool found = find(oldText);
while (found) {
QTextCursor tc = textEditor->textCursor();
if (tc.hasSelection()) {
tc.insertText(newText);
}
found = find(oldText);
}
cursor.endEditBlock();
}
void FindReplaceWidget::caseSensitiveToggled(bool enabled)
{
findCaseSensitively = enabled;
}
void FindReplaceWidget::wholeWordsOnlyToggled(bool enabled)
{
findWholeWordsOnly = enabled;
}
void FindReplaceWidget::useRegularExpressionsToggled(bool enabled)
{
findUseRegExp = enabled;
}
void FindReplaceWidget::showOptionsMenu()
{
QMenu *findOptionsMenu = qobject_cast<QAction*>(sender())->menu();
// show menu above the line edit
QPoint pos = ui->findLineEdit->mapToGlobal(QPoint(0, 0));
pos.ry() -= findOptionsMenu->sizeHint().height();
findOptionsMenu->exec(pos);
}
void FindReplaceWidget::setupFindOptionsMenu()
{
QMenu *findOptionsMenu = new QMenu(this);
QAction *action = findOptionsMenu->addAction(tr("Case Sensitive"));
action->setCheckable(true);
connect(action, SIGNAL(toggled(bool)), SLOT(caseSensitiveToggled(bool)));
action = findOptionsMenu->addAction(tr("Whole Words Only"));
action->setCheckable(true);
connect(action, SIGNAL(toggled(bool)), SLOT(wholeWordsOnlyToggled(bool)));
action = findOptionsMenu->addAction(tr("Use Regular Expressions"));
action->setCheckable(true);
connect(action, SIGNAL(toggled(bool)), SLOT(useRegularExpressionsToggled(bool)));
// add action to line edit to show the options menu
action = new QAction(tr("Find Options"), this);
action->setIcon(QIcon("fa-search.fontawesome"));
action->setMenu(findOptionsMenu);
connect(action, SIGNAL(triggered(bool)), SLOT(showOptionsMenu()));
ui->findLineEdit->addAction(action, QLineEdit::LeadingPosition);
}
bool FindReplaceWidget::find(const QString &searchString, QTextDocument::FindFlags findOptions) const
{
if (findCaseSensitively)
findOptions |= QTextDocument::FindCaseSensitively;
if (findWholeWordsOnly)
findOptions |= QTextDocument::FindWholeWords;
if (findUseRegExp) {
return findUsingRegExp(searchString, findOptions);
} else {
return textEditor->find(searchString, findOptions);
}
}
bool FindReplaceWidget::findUsingRegExp(const QString &pattern, QTextDocument::FindFlags findOptions) const
{
QRegExp rx(pattern, findCaseSensitively ? Qt::CaseSensitive : Qt::CaseInsensitive);
QTextCursor search = textEditor->document()->find(rx, textEditor->textCursor(), findOptions);
if (search.isNull())
return false;
textEditor->setTextCursor(search);
return true;
}