-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.cpp
More file actions
325 lines (265 loc) · 8.72 KB
/
window.cpp
File metadata and controls
325 lines (265 loc) · 8.72 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
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
#include "window.h"
#include <QPainter>
#include <QTextBlock>
#include <QFontDialog>
#include <QTextDocumentFragment>
#include <QPalette>
#include <QStack>
#include <QFileInfo>
#include <QtDebug>
#include <QMainWindow>
#include <QStatusBar>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QFile>
#include <QTextStream>
#include <QUrl>
/* Initializes this Window.
*/
Window::Window(QWidget *parent) : QTextEdit (parent)
{
document()->setModified(false);
installEventFilter(this);
setAcceptDrops(true);
setWindowFont(QFont("Courier"), QFont::Monospace, true);
}
Window::~Window() {
}
/* Resets the window to its default state.
*/
void Window::reset()
{
currentFilePath.clear();
document()->setModified(false);
setPlainText(QString());
}
/* Catch drag event
*/
void Window::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}
/* Program own drag event
* Now when dropped - it loads html structure if its present
* When editor tab is not empty then only text is copied
*/
void Window::dropEvent(QDropEvent *event)
{
const QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
QString filePath = urls.first().toLocalFile();
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray data = file.readAll();
file.close();
QString content = QString::fromUtf8(data);
bool hasContent = !document()->isEmpty();
bool isHtml = Qt::mightBeRichText(content);
// Editor NOT empty → text only
if (hasContent) {
if (isHtml) {
// Extract only <p> text
QTextDocument tmp;
tmp.setHtml(content);
QString plain = tmp.toPlainText();
textCursor().insertText(plain);
} else {
textCursor().insertText(content);
}
return;
}
// Editor empty → allow full HTML
if (isHtml) {
setHtml(content);
} else {
setPlainText(content);
}
}
/* Sets this Window's current file path.
*/
void Window::setCurrentFilePath(QString newPath)
{
currentFilePath = newPath;
fileIsUntitled = false;
}
/* Gets filename fro path
*/
QString Window::getFileNameFromPath()
{
if (currentFilePath.isEmpty())
{
fileIsUntitled = true;
return tr("Untitled document");
}
QFileInfo fileInfo(currentFilePath);
return fileInfo.fileName();
}
/* Returns a QTextDocument::FindFlags representing all the flags a search should be conducted with.
*/
QTextDocument::FindFlags Window::getSearchOptionsFromFlags(bool caseSensitive, bool wholeWords)
{
QTextDocument::FindFlags searchOptions = QTextDocument::FindFlags();
if (caseSensitive)
{
searchOptions |= QTextDocument::FindCaseSensitively;
}
if (wholeWords)
{
searchOptions |= QTextDocument::FindWholeWords;
}
return searchOptions;
}
/* Called when the findDialog object emits its queryReady signal. Initiates the
* actual searching within the window. First searches for a match from the current
* position to the end of the document. If nothing is found, the search proceeds
* from the beginning of the document, but stops once it encounters the first match
* (if any) that was found in prior iterations for the current query.
*/
bool Window::find(QString query, bool caseSensitive, bool wholeWords)
{
// Keep track of the cursor position prior to this search so we can return to it if no match is found
int cursorPositionBeforeCurrentSearch = textCursor().position();
// Specify the options we'll be searching with
QTextDocument::FindFlags searchOptions = getSearchOptionsFromFlags(caseSensitive, wholeWords);
// Search from the current position until the end of the document
bool matchFound = QTextEdit::find(query, searchOptions);
// If we didn't find a match, search from the top of the document
if (!matchFound)
{
moveCursor(QTextCursor::Start);
matchFound = QTextEdit::find(query, searchOptions);
}
// If we found a match...
if (matchFound)
{
int foundPosition = textCursor().position();
bool previouslyFound = search.previouslyFound(query);
// If it's the first time finding this, log the first position at which this query was found in the current document state
// Search history is always reset whenever we do a full cycle back to the first match or start a new search "chain"
if (!previouslyFound)
{
search.add(query, cursorPositionBeforeCurrentSearch, foundPosition);
}
// If term was previously found, check that we didn't cycle back to the first-ever find
else
{
bool loopedBackToFirstMatch = foundPosition == search.firstFoundAt(query);
if (loopedBackToFirstMatch)
{
// It's not really a match that we found; it's a repeat of the very first-ever match
matchFound = false;
// Reset the cursor to its original position prior to first search for this term
int cursorPositionBeforeFirstSearch = search.cursorPositionBeforeFirstSearchFor(query);
moveCursorTo(cursorPositionBeforeFirstSearch);
// Clear search history
search.clear();
// Inform the user of the unsuccessful search (note the "no MORE results found")
emit(findResultReady(tr("No more results found.")));
}
}
}
else
{
// Reset the cursor to its position prior to this particular search
moveCursorTo(cursorPositionBeforeCurrentSearch);
// Inform the user of the unsuccessful search
emit(findResultReady(tr("No results found.")));
}
return matchFound;
}
/* Called when the user clicks the Replace button in FindDialog.*/
void Window::replace(QString what, QString with, bool caseSensitive, bool wholeWords)
{
bool found = find(what, caseSensitive, wholeWords);
if (found)
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
cursor.insertText(with);
cursor.endEditBlock();
}
}
/* Called when the user clicks the Replace All button in FindDialog.*/
void Window::replaceAll(QString what, QString with, bool caseSensitive, bool wholeWords)
{
// Search the entire document from the very beginning
moveCursorTo(0);
// Conduct an initial search; don't rely on our custom find
QTextDocument::FindFlags searchOptions = getSearchOptionsFromFlags(caseSensitive, wholeWords);
bool found = QTextEdit::find(what, searchOptions);
int replacements = 0;
// Keep replacing while there are matches left
QTextCursor cursor(document());
cursor.beginEditBlock();
while (found)
{
QTextCursor currentPosition = textCursor();
currentPosition.insertText(with);
replacements++;
found = QTextEdit::find(what, searchOptions);
}
cursor.endEditBlock();
// End-of-operation feedback
if (replacements == 0)
{
emit(findResultReady(tr("No results found.")));
}
else
{
emit(findResultReady(tr("Document searched. Replaced ") + QString::number(replacements) + tr(" instances.")));
}
}
/* Sets font and size user choosed
*/
void Window::setWindowFont(QFont newFont, QFont::StyleHint styleHint, bool fixedPitch)
{
QFont f = newFont;
f.setStyleHint(styleHint);
f.setFixedPitch(fixedPitch);
QTextEdit::setFont(f);
document()->setDefaultFont(f);
QTextCursor cursor = textCursor();
cursor.select(QTextCursor::Document);
QTextCharFormat fmt;
fmt.setFont(f);
cursor.mergeCharFormat(fmt);
setTextCursor(cursor);
QMainWindow *mw = qobject_cast<QMainWindow*>(window());
if (mw && mw->statusBar()) {
mw->statusBar()->showMessage(tr("Font changed successfully"), 15000);
}
}
/* Sets color user choosed
*/
void Window::setWindowColor(QColor newColor)
{
QTextCharFormat defaultFormat;
defaultFormat.setForeground(newColor);
mergeCurrentCharFormat(defaultFormat);
QTextCursor cursor = textCursor();
cursor.select(QTextCursor::Document);
QTextCharFormat format;
format.setForeground(newColor);
cursor.mergeCharFormat(format);
setTextCursor(cursor);
QMainWindow *mw = qobject_cast<QMainWindow*>(window());
if (mw && mw->statusBar()) {
mw->statusBar()->showMessage(tr("Color changed successfully"), 15000);
}
}
void Window::insertTabs(int numTabs)
{
for (int i = 0; i < numTabs; i++)
{
insertPlainText("\t");
}
}
void Window::moveCursorTo(int positionInText)
{
QTextCursor newCursor = textCursor();
newCursor.setPosition(positionInText);
setTextCursor(newCursor);
}