From 16d4334c9c194fb8cad68607a746fa2f961c77f9 Mon Sep 17 00:00:00 2001 From: Ben Haller Date: Tue, 25 Jul 2023 17:04:45 -0400 Subject: [PATCH] fix #282, flash the find/jump selection --- QtSLiM/QtSLiMExtras.cpp | 28 ++++++++++++++++++++++++++++ QtSLiM/QtSLiMExtras.h | 3 +++ QtSLiM/QtSLiMFindPanel.cpp | 5 +++++ QtSLiM/QtSLiMScriptTextEdit.cpp | 2 +- QtSLiM/QtSLiMWindow.cpp | 2 ++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/QtSLiM/QtSLiMExtras.cpp b/QtSLiM/QtSLiMExtras.cpp index 50e3a4973..dd690c9c7 100644 --- a/QtSLiM/QtSLiMExtras.cpp +++ b/QtSLiM/QtSLiMExtras.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -1167,6 +1168,33 @@ QPixmap QtSLiMDarkenPixmap(QPixmap p_pixmap) } +// find flashing; see https://bugreports.qt.io/browse/QTBUG-83147 + +static QPalette QtSLiMFlashPalette(QPlainTextEdit *te) +{ + // Returns a palette for QtSLiMTextEdit for highlighting errors, which could depend on platform and dark mode + // Note that this is based on the current palette, and derives only the highlight colors + QPalette p = te->palette(); + p.setColor(QPalette::Highlight, QColor(QColor(Qt::yellow))); + p.setColor(QPalette::HighlightedText, QColor(Qt::black)); + return p; +} + +void QtSLiMFlashHighlightInTextEdit(QPlainTextEdit *te) +{ + const int delayMillisec = 80; // seems good? 12.5 times per second + + // set to the flash color + te->setPalette(QtSLiMFlashPalette(te)); + + // set up timers to flash the color again; we don't worry about being called multiple times, + // cancelling old timers, etc., because this is so quick that it really doesn't matter; + // it sorts itself out more quickly than the user can really notice any discrepancy + QTimer::singleShot(delayMillisec, te, [te]() { te->setPalette(qApp->palette(te)); }); + QTimer::singleShot(delayMillisec * 2, te, [te]() { te->setPalette(QtSLiMFlashPalette(te)); }); + QTimer::singleShot(delayMillisec * 3, te, [te]() { te->setPalette(qApp->palette(te)); }); +} + diff --git a/QtSLiM/QtSLiMExtras.h b/QtSLiM/QtSLiMExtras.h index 7b4842ea8..204120134 100644 --- a/QtSLiM/QtSLiMExtras.h +++ b/QtSLiM/QtSLiMExtras.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -223,6 +224,8 @@ class QtSLiMStatusBar : public QStatusBar // Used to create the dark app icon displayed when running a model QPixmap QtSLiMDarkenPixmap(QPixmap p_pixmap); +void QtSLiMFlashHighlightInTextEdit(QPlainTextEdit *te); + // Incremental sorting // diff --git a/QtSLiM/QtSLiMFindPanel.cpp b/QtSLiM/QtSLiMFindPanel.cpp index d70891c73..66b850a68 100644 --- a/QtSLiM/QtSLiMFindPanel.cpp +++ b/QtSLiM/QtSLiMFindPanel.cpp @@ -238,6 +238,7 @@ bool QtSLiMFindPanel::findForwardWrapBeep(QPlainTextEdit *target, bool forward, if (findResult) { target->centerCursor(); + QtSLiMFlashHighlightInTextEdit(target); } else if (wrap) { @@ -252,7 +253,10 @@ bool QtSLiMFindPanel::findForwardWrapBeep(QPlainTextEdit *target, bool forward, findResult = target->find(findString, findFlags); if (findResult) + { target->centerCursor(); + QtSLiMFlashHighlightInTextEdit(target); + } else target->setTextCursor(originalCursor); } @@ -400,6 +404,7 @@ void QtSLiMFindPanel::jumpToSelection(void) QPlainTextEdit *target = targetTextEditRequireModifiable(false); target->centerCursor(); + QtSLiMFlashHighlightInTextEdit(target); } void QtSLiMFindPanel::jumpToLine(void) diff --git a/QtSLiM/QtSLiMScriptTextEdit.cpp b/QtSLiM/QtSLiMScriptTextEdit.cpp index 6cf6aab9e..ee9a05115 100644 --- a/QtSLiM/QtSLiMScriptTextEdit.cpp +++ b/QtSLiM/QtSLiMScriptTextEdit.cpp @@ -227,7 +227,7 @@ QPalette QtSLiMTextEdit::qtslimStandardPalette(void) QPalette QtSLiMTextEdit::qtslimErrorPalette(void) { // Returns a palette for QtSLiMTextEdit for highlighting errors, which could depend on platform and dark mode - // Note that this is based on the current palette, and derives only the highlight color + // Note that this is based on the current palette, and derives only the highlight colors QPalette p = palette(); p.setColor(QPalette::Highlight, QColor(QColor(Qt::red).lighter(120))); p.setColor(QPalette::HighlightedText, QColor(Qt::black)); diff --git a/QtSLiM/QtSLiMWindow.cpp b/QtSLiM/QtSLiMWindow.cpp index 3ed1f3638..cc553d21c 100644 --- a/QtSLiM/QtSLiMWindow.cpp +++ b/QtSLiM/QtSLiMWindow.cpp @@ -4960,6 +4960,7 @@ void QtSLiMWindow::jumpToPopupButtonRunMenu(void) cursor.setPosition(comment_end, QTextCursor::KeepAnchor); scriptTE->setTextCursor(cursor); scriptTE->centerCursor(); + QtSLiMFlashHighlightInTextEdit(scriptTE); }); QFont action_font = jumpAction->font(); @@ -5104,6 +5105,7 @@ void QtSLiMWindow::jumpToPopupButtonRunMenu(void) cursor.setPosition(decl_end, QTextCursor::KeepAnchor); scriptTE->setTextCursor(cursor); scriptTE->centerCursor(); + QtSLiMFlashHighlightInTextEdit(scriptTE); }); jumpActions.emplace_back(decl_start, jumpAction);