Skip to content

Commit

Permalink
fix #282, flash the find/jump selection
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaller committed Jul 25, 2023
1 parent 2088547 commit 16d4334
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
28 changes: 28 additions & 0 deletions QtSLiM/QtSLiMExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QTextDocument>
#include <QAbstractTextDocumentLayout>
#include <QPalette>
#include <QApplication>
#include <QDebug>
#include <cmath>

Expand Down Expand Up @@ -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)); });
}




Expand Down
3 changes: 3 additions & 0 deletions QtSLiM/QtSLiMExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QSplitter>
#include <QSplitterHandle>
#include <QStatusBar>
#include <QPlainTextEdit>

#include <cmath>
#include <algorithm>
Expand Down Expand Up @@ -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
//
Expand Down
5 changes: 5 additions & 0 deletions QtSLiM/QtSLiMFindPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ bool QtSLiMFindPanel::findForwardWrapBeep(QPlainTextEdit *target, bool forward,
if (findResult)
{
target->centerCursor();
QtSLiMFlashHighlightInTextEdit(target);
}
else if (wrap)
{
Expand All @@ -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);
}
Expand Down Expand Up @@ -400,6 +404,7 @@ void QtSLiMFindPanel::jumpToSelection(void)

QPlainTextEdit *target = targetTextEditRequireModifiable(false);
target->centerCursor();
QtSLiMFlashHighlightInTextEdit(target);
}

void QtSLiMFindPanel::jumpToLine(void)
Expand Down
2 changes: 1 addition & 1 deletion QtSLiM/QtSLiMScriptTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions QtSLiM/QtSLiMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 16d4334

Please sign in to comment.