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.
This commit is contained in:
mgrojo
2017-12-15 21:15:39 +01:00
parent cb9712769e
commit de260d32c5
3 changed files with 36 additions and 27 deletions

View File

@@ -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<SqlExecutionArea*>(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)

View File

@@ -8,12 +8,15 @@
#include <QUrl>
#include <QMimeData>
#include <QShortcut>
#include <QAction>
#include <QMenu>
#include <cmath>
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));
}

View File

@@ -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