From de260d32c5d5eb4266edec34e1383921455d1f38 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Fri, 15 Dec 2017 21:15:39 +0100 Subject: [PATCH] Always open the find/replace dialog with Ctrl+H and option in context menu The shortcut is constrained to the Widget context so it does not conflict with other SqlTextEdit widgets in the application. Now all SqlTextEdit widgets are able of opening the dialog with the same shortcut and behave in the same way (do not block the parent window). The find/replace dialog can now be discovered by the user in the context menu at the SqlTextEdit widgets. --- src/MainWindow.cpp | 19 +++---------------- src/sqltextedit.cpp | 39 +++++++++++++++++++++++++++++---------- src/sqltextedit.h | 5 ++++- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index e19d3d31..43cb0ced 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -145,8 +145,6 @@ void MainWindow::init() connect(shortcutBrowseRefreshF5, SIGNAL(activated()), this, SLOT(refresh())); QShortcut* shortcutBrowseRefreshCtrlR = new QShortcut(QKeySequence("Ctrl+R"), this); connect(shortcutBrowseRefreshCtrlR, SIGNAL(activated()), this, SLOT(refresh())); - QShortcut* shortcutFindReplace = new QShortcut(QKeySequence("Ctrl+H"), this); - connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog())); // Create the actions for the recently opened dbs list for(int i = 0; i < MaxRecentFiles; ++i) { @@ -2719,22 +2717,11 @@ void MainWindow::setFindFrameVisibility(bool show) void MainWindow::openFindReplaceDialog() { - // The slot for the shortcut must discover which sqltexedit widget has the focus and then set it to the dialog. + // The slot for the shortcut must discover which sqltexedit widget has the focus and then open its dialog. SqlExecutionArea* sqlWidget = qobject_cast(ui->tabSqlAreas->currentWidget()); - SqlTextEdit* focusedSqlTextEdit = nullptr; - if (ui->editLogUser->hasFocus()) - focusedSqlTextEdit = ui->editLogUser; - else if (ui->editLogApplication->hasFocus()) - focusedSqlTextEdit = ui->editLogApplication; - - if (sqlWidget && !focusedSqlTextEdit) - focusedSqlTextEdit = sqlWidget->getEditor(); - - if (focusedSqlTextEdit) { - findReplaceDialog->setSqlTextEdit(focusedSqlTextEdit); - findReplaceDialog->show(); - } + if (sqlWidget) + sqlWidget->getEditor()->openFindReplaceDialog(); } void MainWindow::saveAsView(QString query) diff --git a/src/sqltextedit.cpp b/src/sqltextedit.cpp index 27c8ecfd..ba8691b0 100644 --- a/src/sqltextedit.cpp +++ b/src/sqltextedit.cpp @@ -8,12 +8,15 @@ #include #include #include +#include +#include #include SqlUiLexer* SqlTextEdit::sqlLexer = nullptr; SqlTextEdit::SqlTextEdit(QWidget* parent) : - QsciScintilla(parent) + QsciScintilla(parent), + findReplaceDialog(new FindReplaceDialog(this)) { // Create lexer object if not done yet if(sqlLexer == nullptr) @@ -59,12 +62,13 @@ SqlTextEdit::SqlTextEdit(QWidget* parent) : // Connect signals connect(this, SIGNAL(linesChanged()), this, SLOT(updateLineNumberAreaWidth())); - // The ideal shortcut would be QKeySequence::Replace, but that is - // reserved for the Main Window, that contains various SqlTextEdit - // widgets and needs its own shortcut. For other windows - // containing the SqlTextEdit widget we can use this one. - QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+Shift+H")), this); + // The shortcut is constrained to the Widget context so it does not conflict with other SqlTextEdit widgets in the Main Window. + QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+H")), this, nullptr, nullptr, Qt::WidgetShortcut); connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog())); + + // Prepare for adding the find/replace option to the QScintilla context menu + setContextMenuPolicy(Qt::CustomContextMenu); + connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint &))); } SqlTextEdit::~SqlTextEdit() @@ -207,8 +211,23 @@ void SqlTextEdit::clearSelection() void SqlTextEdit::openFindReplaceDialog() { - - FindReplaceDialog dialog(this); - dialog.setSqlTextEdit(this); - dialog.exec(); + findReplaceDialog->setSqlTextEdit(this); + findReplaceDialog->show(); +} + +void SqlTextEdit::showContextMenu(const QPoint &pos) +{ + + QAction* findReplaceAction = new QAction(QIcon(":/icons/text_replace"), tr("Find and Replace..."), this); + findReplaceAction->setShortcut(QKeySequence(tr("Ctrl+H"))); + connect(findReplaceAction, &QAction::triggered, [&]() { + openFindReplaceDialog(); + }); + + // This has to be created here, otherwise the set of enabled options would not update accordingly. + QMenu* editContextMenu = createStandardContextMenu(); + editContextMenu->addSeparator(); + editContextMenu->addAction(findReplaceAction); + + editContextMenu->exec(mapToGlobal(pos)); } diff --git a/src/sqltextedit.h b/src/sqltextedit.h index 5c2b7053..5a080cab 100644 --- a/src/sqltextedit.h +++ b/src/sqltextedit.h @@ -3,6 +3,7 @@ #include "Qsci/qsciscintilla.h" +class FindReplaceDialog; class SqlUiLexer; /** @@ -26,6 +27,7 @@ public slots: void reloadSettings(); void clearErrorIndicators(); void setErrorIndicator(int fromRow, int fromIndex, int toRow, int toIndex); + void openFindReplaceDialog(); protected: void dropEvent(QDropEvent* e); @@ -35,10 +37,11 @@ private: int errorIndicatorNumber; bool showErrorIndicators; + FindReplaceDialog* findReplaceDialog; private slots: void updateLineNumberAreaWidth(); - void openFindReplaceDialog(); + void showContextMenu(const QPoint &pos); }; #endif