forked from pgXplorer/pgXplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgxeditor.cpp
545 lines (476 loc) · 20.7 KB
/
pgxeditor.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
/*
LICENSE AND COPYRIGHT INFORMATION - Please read carefully.
Copyright (c) 2011-2012, davyjones <davyjones@github>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "pgxeditor.h"
ulong PgxEditor::editor_widow_id = 0;
PgxEditor::PgxEditor(Database *database, QString editor_name)
{
++editor_widow_id;
this->database = database;
this->editor_name = editor_name;
createActions();
breakpointArea = new BreakPointArea(this);
lineNumberArea = new LineNumberArea(this);
setStyleSheet("QPlainTextEdit{background-color: white; font: bold 14px 'Courier New';}");
highlighter = new Highlighter(document());
toolbar = new QToolBar;
toolbar->setIconSize(QSize(36,36));
toolbar->setObjectName("pgxeditor");
toolbar->setMovable(false);
toolbar->addAction(newpgxeditor_action);
toolbar->addAction(cut_action);
toolbar->addAction(copy_action);
toolbar->addAction(paste_action);
if(!editor_name.isEmpty()) {
toolbar->addSeparator();
toolbar->addAction(save_action);
toolbar->addSeparator();
toolbar->addAction(execute_action);
}
toolbar->addSeparator();
toolbar->addAction(selected_execute_action);
toolbar->addAction(wrap_action);
toolbar->addAction(find_action);
pgxeditor_mainwin = new PgxEditorMainWindow;
pgxeditor_mainwin->addToolBar(toolbar);
pgxeditor_mainwin->setCentralWidget(this);
pgxeditor_mainwin->setAttribute(Qt::WA_DeleteOnClose);
find_bar = new QLineEdit;
find_bar->setPlaceholderText(tr("Find"));
find_bar->setVisible(false);
pgxeditor_mainwin->statusBar()->setSizeGripEnabled(false);
pgxeditor_mainwin->statusBar()->addPermanentWidget(casesensitivity_button, 0);
pgxeditor_mainwin->statusBar()->addPermanentWidget(wholeword_button, 0);
pgxeditor_mainwin->statusBar()->addPermanentWidget(backwards_button, 0);
pgxeditor_mainwin->statusBar()->addPermanentWidget(find_bar);
replace_bar = new QLineEdit;
replace_bar->setPlaceholderText(tr("Replace"));
replace_bar->setVisible(false);
pgxeditor_mainwin->statusBar()->addPermanentWidget(replace_bar);
connect(find_bar, SIGNAL(returnPressed()), this, SLOT(findText()));
connect(replace_bar, SIGNAL(returnPressed()), this, SLOT(replaceText()));
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedSlot()));
connect(this, SIGNAL(textChanged()), this, SLOT(textChangedSlot()));
connect(pgxeditor_mainwin, SIGNAL(pgxeditorClosing()), this, SLOT(pgxeditorClosing()));
updateLineNumberAreaWidth(0);
}
int PgxEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = fontMetrics().width(QLatin1Char('9')) * digits;
return space;
}
void PgxEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(fontMetrics().width(QLatin1Char('9'))+lineNumberAreaWidth(), 0, 0, 0);
}
void PgxEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void PgxEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
/*QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);*/
QTextEdit::ExtraSelection border;
QPen outline(Qt::darkGreen, 2, Qt::SolidLine);
//outline.setJoinStyle(Qt::RoundJoin);
//outline.setCapStyle(Qt::RoundCap);
border.format.setProperty(QTextFormat::OutlinePen, outline);
border.cursor = textCursor();
extraSelections.append(border);
QTextEdit::ExtraSelection back_brush;
QBrush brush(Qt::yellow);
back_brush.format.setProperty(QTextFormat::BackgroundBrush, brush);
back_brush.cursor = textCursor();
extraSelections.append(back_brush);
setExtraSelections(extraSelections);
}
void PgxEditor::removeHighlighting()
{
QList<QTextEdit::ExtraSelection> extraSelections;
setExtraSelections(extraSelections);
}
void PgxEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
breakpointArea->setGeometry(QRect(cr.left(), cr.top(), fontMetrics().width(QLatin1Char('9')), cr.height()));
lineNumberArea->setGeometry(QRect(cr.left()+fontMetrics().width(QLatin1Char('9')), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void PgxEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), QColor(230,230,230,128));
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::darkGray);
painter.drawText(-1, top+2, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
void PgxEditor::breakpointAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(breakpointArea);
painter.fillRect(event->rect(), QColor(230,230,230,128));
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(-1, top+2, breakpointArea->width(), fontMetrics().height(),
Qt::AlignRight, "");
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
void PgxEditor::createActions()
{
newpgxeditor_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/editor.png")), tr("New"), this);
newpgxeditor_action->setShortcuts(QKeySequence::New);
newpgxeditor_action->setStatusTip(tr("New editor"));
connect(newpgxeditor_action, SIGNAL(triggered()), this, SIGNAL(newPgxeditor()));
cut_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/cut.png")), tr("Cut"), this);
cut_action->setShortcuts(QKeySequence::Cut);
cut_action->setStatusTip(tr("Cut selected text and copy to clipboard"));
connect(cut_action, SIGNAL(triggered()), this, SLOT(cut()));
copy_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/copy.png")), tr("Copy"), this);
copy_action->setShortcuts(QKeySequence::Copy);
copy_action->setStatusTip(tr("Copy selected text to clipboard"));
connect(copy_action, SIGNAL(triggered()), this, SLOT(copy()));
paste_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/paste.png")), tr("Paste"), this);
paste_action->setShortcuts(QKeySequence::Paste);
paste_action->setStatusTip(tr("Paste text from clipboard"));
connect(paste_action, SIGNAL(triggered()), this, SLOT(paste()));
if(!editor_name.isEmpty()) {
save_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/save.png")), tr("&Save"), this);
save_action->setShortcuts(QKeySequence::Save);
save_action->setStatusTip(tr("Save function"));
save_action->setEnabled(false);
connect(save_action, SIGNAL(triggered()), this, SLOT(saveFunction()));
execute_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/execute.png")), tr("&Execute"), this);
execute_action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
execute_action->setStatusTip(tr("Execute function"));
connect(execute_action, SIGNAL(triggered()), this, SLOT(executeFunction()));
}
selected_execute_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/selected_execute.png")), tr("&Run"), this);
selected_execute_action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_E));
selected_execute_action->setStatusTip(tr("Execute selected text"));
if(!editor_name.isEmpty())
selected_execute_action->setEnabled(false);
connect(selected_execute_action, SIGNAL(triggered()), this, SLOT(executeText()));
wrap_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/wrap.png")), tr("Wrap/Un-wrap lines"),this);
wrap_action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
wrap_action->setStatusTip(tr("Toggle line wrapping"));
wrap_action->setCheckable(true);
wrap_action->setChecked(true);
connect(wrap_action, SIGNAL(triggered()), this, SLOT(toggleWrap()));
find_action = new QAction(QIcon(qApp->applicationDirPath().append("/icons/find.png")), tr("Find"), this);
find_action->setShortcuts(QKeySequence::Find);
find_action->setStatusTip(tr("Find/replace text"));
connect(find_action, SIGNAL(triggered()), this, SLOT(toggleFindBar()));
casesensitivity_action = new QAction(tr("Cs"), this);
casesensitivity_action->setToolTip(tr("Case sensitive"));
casesensitivity_action->setCheckable(true);
casesensitivity_button = new QToolButton;
casesensitivity_button->setDefaultAction(casesensitivity_action);
casesensitivity_button->setVisible(false);
wholeword_action = new QAction(tr("W"), this);
wholeword_action->setToolTip(tr("Whole word"));
wholeword_action->setCheckable(true);
wholeword_button = new QToolButton;
wholeword_button->setDefaultAction(wholeword_action);
wholeword_button->setVisible(false);
backwards_action = new QAction(tr("B"), this);
backwards_action->setToolTip(tr("Backwards"));
backwards_action->setCheckable(true);
backwards_button = new QToolButton;
backwards_button->setDefaultAction(backwards_action);
backwards_button->setVisible(false);
}
void PgxEditor::saveFunction()
{
bool is_saved = false;
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
{
QSqlDatabase database_connection = QSqlDatabase::addDatabase("QPSQL", QString("save function ").append(editor_name).append(QString::number(editor_widow_id)));
database_connection.setHostName(database->getHost());
database_connection.setPort(database->getPort().toInt());
database_connection.setDatabaseName(database->getName());
database_connection.setUserName(database->getUser());
database_connection.setPassword(database->getPassword());
if (!database_connection.open()) {
QMessageBox::critical(0, tr("Database error"),
tr("Unable to establish a database connection.\n"
"No PostgreSQL support.\n"), QMessageBox::Cancel);
return;
}
QSqlQuery temp_query(database_connection);
is_saved = temp_query.exec(toPlainText().replace(QChar::ParagraphSeparator,"\n"));
if(temp_query.lastError().isValid()) {
QApplication::restoreOverrideCursor();
QMessageBox::critical(0, tr("Database error"),
temp_query.lastError().text(), QMessageBox::Cancel);
}
}
QSqlDatabase::removeDatabase(QString("save function ").append(editor_name).append(QString::number(editor_widow_id)));
QApplication::restoreOverrideCursor();
if(is_saved)
save_action->setEnabled(false);
else
save_action->setEnabled(true);
}
void PgxEditor::executeText()
{
if(toPlainText().simplified().isEmpty())
return;
if(textCursor().selectedText().isEmpty())
emit showQueryView(database, toPlainText().replace(QChar::ParagraphSeparator,"\n"));
else
emit showQueryView(database, textCursor().selectedText().replace(QChar::ParagraphSeparator,"\n"));
}
void PgxEditor::executeFunction()
{
QString query("SELECT ");
query.append(editor_name);
emit newPgxeditor(query);
}
void PgxEditor::selectionChangedSlot()
{
if(!editor_name.isEmpty()) {
if(textCursor().selectedText().isEmpty())
selected_execute_action->setEnabled(false);
else
selected_execute_action->setEnabled(true);
}
}
void PgxEditor::findText()
{
QTextDocument::FindFlags find_flags;
if(casesensitivity_action->isChecked())
find_flags |= QTextDocument::FindCaseSensitively;
if(wholeword_action->isChecked())
find_flags |= QTextDocument::FindWholeWords;
if(backwards_action->isChecked())
find_flags |= QTextDocument::FindBackward;
pgxeditor_mainwin->statusBar()->clearMessage();
if(find_cursor.atEnd() && !backwards_action->isChecked()) {
find_cursor.movePosition(QTextCursor::Start);
setTextCursor(find_cursor);
}
if(find_cursor.atStart() && backwards_action->isChecked()) {
find_cursor.movePosition(QTextCursor::End);
setTextCursor(find_cursor);
}
if(!find(find_bar->text(), find_flags)) {
if(backwards_action->isChecked()) {
pgxeditor_mainwin->statusBar()->showMessage(tr("Reached the top. Continuing from the bottom."));
find_cursor.movePosition(QTextCursor::Start);
}
else {
pgxeditor_mainwin->statusBar()->showMessage(tr("Reached the end. Continuing from the top."));
find_cursor.movePosition(QTextCursor::End);
}
}
else {
if(backwards_action->isChecked()) {
find_cursor.movePosition(QTextCursor::StartOfWord);
}
highlightCurrentLine();
}
}
void PgxEditor::replaceText()
{
QTextDocument::FindFlags find_flags;
if(casesensitivity_action->isChecked())
find_flags |= QTextDocument::FindCaseSensitively;
if(wholeword_action->isChecked())
find_flags |= QTextDocument::FindWholeWords;
if(backwards_action->isChecked())
find_flags |= QTextDocument::FindBackward;
pgxeditor_mainwin->statusBar()->clearMessage();
if(find_cursor.atEnd() && !backwards_action->isChecked()) {
find_cursor.movePosition(QTextCursor::Start);
setTextCursor(find_cursor);
}
if(find_cursor.atStart() && backwards_action->isChecked()) {
find_cursor.movePosition(QTextCursor::End);
setTextCursor(find_cursor);
}
if(!find(find_bar->text(), find_flags)) {
if(backwards_action->isChecked()) {
pgxeditor_mainwin->statusBar()->showMessage(tr("Reached the top. Continuing from the bottom."));
find_cursor.movePosition(QTextCursor::Start);
}
else {
pgxeditor_mainwin->statusBar()->showMessage(tr("Reached the end. Continuing from the top."));
find_cursor.movePosition(QTextCursor::End);
}
}
else {
QTextCursor before_replace = textCursor();
before_replace.movePosition(QTextCursor::WordLeft);
textCursor().insertText(replace_bar->text());
if(backwards_action->isChecked()) {
setTextCursor(before_replace);
find_cursor = before_replace;
}
highlightCurrentLine();
}
}
void PgxEditor::toggleWrap()
{
if(lineWrapMode() == QPlainTextEdit::WidgetWidth)
setLineWrapMode(QPlainTextEdit::NoWrap);
else
setLineWrapMode(QPlainTextEdit::WidgetWidth);
}
void PgxEditor::toggleFindBar()
{
if(find_bar->isVisible()) {
if(find_bar->isActiveWindow()) {
pgxeditor_mainwin->statusBar()->clearMessage();
removeHighlighting();
casesensitivity_button->setVisible(false);
wholeword_button->setVisible(false);
backwards_button->setVisible(false);
find_bar->setVisible(false);
replace_bar->setVisible(false);
}
else
find_bar->setFocus();
}
else {
find_cursor = textCursor();
//find_cursor.movePosition(QTextCursor::Start);
setTextCursor(find_cursor);
find_bar->setFocus();
casesensitivity_button->setVisible(true);
wholeword_button->setVisible(true);
backwards_button->setVisible(true);
find_bar->setVisible(true);
replace_bar->setVisible(true);
}
}
void PgxEditor::textChangedSlot()
{
if(!editor_name.isEmpty())
save_action->setEnabled(true);
}
void PgxEditor::setTitle(QString title)
{
pgxeditor_mainwin->setWindowTitle(title);
}
void PgxEditor::setText(QString editor_text, bool save_state)
{
this->setPlainText(editor_text);
if(!editor_name.isEmpty())
save_action->setEnabled(save_state);
}
void PgxEditor::setResizePos(QSize size, QPoint pos)
{
pgxeditor_mainwin->resize(size);
pgxeditor_mainwin->move(pos);
pgxeditor_mainwin->show();
}
void PgxEditor::pgxeditorClosing()
{
emit pgxeditorClosing(this);
}
void PgxEditor::closeMain()
{
pgxeditor_mainwin->close();
}
void PgxEditor::languageChanged(QEvent *event)
{
if (event->type() == QEvent::LanguageChange) {
newpgxeditor_action->setText(tr("New"));
newpgxeditor_action->setStatusTip(tr("New editor"));
cut_action->setText(tr("Cut"));
cut_action->setStatusTip(tr("Cut selected text and copy to clipboard"));
copy_action->setText(tr("Copy"));
copy_action->setStatusTip(tr("Copy selected text to clipboard"));
paste_action->setText(tr("Paste"));
paste_action->setStatusTip(tr("Paste text from clipboard"));
if(!editor_name.isEmpty()) {
save_action->setText(tr("&Save"));
save_action->setStatusTip(tr("Save function"));
execute_action->setText(tr("&Execute"));
execute_action->setStatusTip(tr("Execute function"));
}
selected_execute_action->setText(tr("&Run"));
selected_execute_action->setStatusTip(tr("Execute selected text"));
wrap_action->setText(tr("Wrap/Un-wrap lines"));
wrap_action->setStatusTip(tr("Toggle line wrapping"));
find_action->setText(tr("Find"));
find_action->setStatusTip(tr("Find/replace text"));
casesensitivity_action->setToolTip(tr("Case sensitive"));
wholeword_action->setToolTip(tr("Whole word"));
backwards_action->setToolTip(tr("Backwards"));
}
}
void PgxEditorMainWindow::closeEvent(QCloseEvent *event)
{
//Clean-up only when there is no active thread.
//However, this will cause a memory leak when the
//TableView is closed when the thread is busy.
//Proper solution is to create a Thread class
//and cancel that before we clean-up. We cannot do
//this now because we are using QFuture (per Qt docs).
emit pgxeditorClosing();
QSettings settings("pgXplorer", "pgXplorer");
if(isMaximized()) {
settings.setValue("pgxeditor_maximized", true);
showNormal();
}
else
settings.setValue("pgxeditor_maximized", false);
settings.setValue("pgxeditor_pos", pos());
settings.setValue("pgxeditor_size", size());
QMainWindow::closeEvent(event);
}