Skip to content

Commit 5ca95a5

Browse files
committed
Find dialog based on the Find/Replace dialog for the Scintilla widgets
A new find dialog, bound to Ctrl+F, has been added to all the Scintilla editors. The dialog is basically the Find/Replace dialog, whose replace related controls have been set to invisible. This shortcut and the new menu contextual entry are disabled for the Execute SQL editors, since there the shortcut is already assigned to the search bar and it would also be redundant. See issue #1746
1 parent eb2a6e0 commit 5ca95a5

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

src/ExtendedScintilla.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ ExtendedScintilla::ExtendedScintilla(QWidget* parent) :
6060
// The shortcuts are constrained to the Widget context so they do not conflict with other SqlTextEdit widgets in the Main Window.
6161
QShortcut* shortcutFindReplace = new QShortcut(QKeySequence(tr("Ctrl+H")), this, nullptr, nullptr, Qt::WidgetShortcut);
6262
connect(shortcutFindReplace, SIGNAL(activated()), this, SLOT(openFindReplaceDialog()));
63+
shortcutFind = new QShortcut(QKeySequence(tr("Ctrl+F")), this, nullptr, nullptr, Qt::WidgetShortcut);
64+
connect(shortcutFind, SIGNAL(activated()), this, SLOT(openFindDialog()));
6365

6466
#ifdef Q_OS_MACX
6567
// Alt+Backspace on Mac is expected to delete one word to the left,
@@ -245,6 +247,11 @@ bool ExtendedScintilla::findText(QString text, bool regexp, bool caseSensitive,
245247
/* line */ -1, /* index */ -1, /* show */ true, /* posix */ true);
246248
}
247249

250+
void ExtendedScintilla::setEnabledFindDialog(bool value)
251+
{
252+
shortcutFind->setEnabled(value);
253+
}
254+
248255
void ExtendedScintilla::clearSelection()
249256
{
250257
setSelection(-1,-1,-1,-1);
@@ -253,12 +260,27 @@ void ExtendedScintilla::clearSelection()
253260
void ExtendedScintilla::openFindReplaceDialog()
254261
{
255262
findReplaceDialog->setExtendedScintilla(this);
256-
findReplaceDialog->show();
263+
findReplaceDialog->showFindReplaceDialog(true);
264+
}
265+
266+
void ExtendedScintilla::openFindDialog()
267+
{
268+
findReplaceDialog->setExtendedScintilla(this);
269+
findReplaceDialog->showFindReplaceDialog(false);
257270
}
258271

259272
void ExtendedScintilla::showContextMenu(const QPoint &pos)
260273
{
261274

275+
// This has to be created here, otherwise the set of enabled options would not update accordingly.
276+
QMenu* editContextMenu = createStandardContextMenu();
277+
editContextMenu->addSeparator();
278+
if (shortcutFind->isEnabled()) {
279+
QAction* findAction = new QAction(QIcon(":/icons/find"), tr("Find..."), this);
280+
findAction->setShortcut(shortcutFind->key());
281+
connect(findAction, &QAction::triggered, this, &ExtendedScintilla::openFindDialog);
282+
editContextMenu->addAction(findAction);
283+
}
262284
QAction* findReplaceAction = new QAction(QIcon(":/icons/text_replace"), tr("Find and Replace..."), this);
263285
findReplaceAction->setShortcut(QKeySequence(tr("Ctrl+H")));
264286
connect(findReplaceAction, &QAction::triggered, this, &ExtendedScintilla::openFindReplaceDialog);
@@ -267,10 +289,8 @@ void ExtendedScintilla::showContextMenu(const QPoint &pos)
267289
printAction->setShortcut(QKeySequence(tr("Ctrl+P")));
268290
connect(printAction, &QAction::triggered, this, &ExtendedScintilla::openPrintDialog);
269291

270-
// This has to be created here, otherwise the set of enabled options would not update accordingly.
271-
QMenu* editContextMenu = createStandardContextMenu();
272-
editContextMenu->addSeparator();
273292
editContextMenu->addAction(findReplaceAction);
293+
editContextMenu->addSeparator();
274294
editContextMenu->addAction(printAction);
275295

276296
editContextMenu->exec(mapToGlobal(pos));

src/ExtendedScintilla.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Qsci/qsciscintilla.h"
55

66
class FindReplaceDialog;
7+
class QShortcut;
78

89
/**
910
* @brief The ExtendedScintilla class
@@ -18,6 +19,7 @@ class ExtendedScintilla : public QsciScintilla
1819
~ExtendedScintilla() override;
1920

2021
bool findText(QString text, bool regexp, bool caseSensitive, bool words, bool wrap, bool forward);
22+
void setEnabledFindDialog(bool value);
2123
void clearSelection();
2224
// Override parent setLexer
2325
void setLexer(QsciLexer *lexer) override;
@@ -34,6 +36,7 @@ public slots:
3436
// Set error indicator from position to end of line
3537
void setErrorIndicator(int position);
3638
void openFindReplaceDialog();
39+
void openFindDialog();
3740
void openPrintDialog();
3841

3942
protected:
@@ -46,6 +49,7 @@ public slots:
4649
int errorIndicatorNumber;
4750
bool showErrorIndicators;
4851
FindReplaceDialog* findReplaceDialog;
52+
QShortcut* shortcutFind;
4953

5054
private slots:
5155
void updateLineNumberAreaWidth();

src/FindReplaceDialog.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ bool FindReplaceDialog::findNext()
8686

8787
}
8888

89+
void FindReplaceDialog::showFindReplaceDialog(bool hasReplace)
90+
{
91+
ui->replaceWithText->setVisible(hasReplace);
92+
ui->replaceButton->setVisible(hasReplace);
93+
ui->replaceWithLabel->setVisible(hasReplace);
94+
ui->replaceAllButton->setVisible(hasReplace);
95+
show();
96+
}
97+
8998
void FindReplaceDialog::show()
9099
{
91100
ui->findText->setFocus();

src/FindReplaceDialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class FindReplaceDialog : public QDialog
2020
~FindReplaceDialog() override;
2121
void setExtendedScintilla(ExtendedScintilla* scintilla);
2222
void show();
23+
void showFindReplaceDialog(bool hasReplace);
2324

2425
private slots:
2526
bool findNext();

src/FindReplaceDialog.ui

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
<property name="text">
142142
<string>&amp;Find Next</string>
143143
</property>
144+
<property name="shortcut">
145+
<string>F3</string>
146+
</property>
144147
</widget>
145148
</item>
146149
<item>

src/MainWindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,9 @@ int MainWindow::openSqlTab(bool resetCounter)
22582258
int index = ui->tabSqlAreas->addTab(w, QString("SQL %1").arg(++tabNumber));
22592259
ui->tabSqlAreas->setCurrentIndex(index);
22602260
w->setFindFrameVisibility(ui->actionSqlFind->isChecked());
2261+
// Disable the find dialog in the SQL tabs, since the shortcut
2262+
// would interfere with the search bar and it'd be anyway redundant.
2263+
w->getEditor()->setEnabledFindDialog(false);
22612264
w->getEditor()->setFocus();
22622265
connect(w, SIGNAL(findFrameVisibilityChanged(bool)), ui->actionSqlFind, SLOT(setChecked(bool)));
22632266

0 commit comments

Comments
 (0)