summaryrefslogtreecommitdiff
path: root/widgets/displaytext.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2020-05-25 21:27:22 +0200
committerChristoph Berg <myon@debian.org>2020-05-25 21:27:22 +0200
commit458b033b8ae7818d50e80f51b12c0c43bbd0d3e2 (patch)
tree4aabd646d028edc5e51a71762faeac6c07577642 /widgets/displaytext.cpp
parent32a41b1abe1a4aab0892c9c2051be6ba5b379f1e (diff)
New upstream version 2.2.0~rc2+repackupstream/2.2.0_rc2+repack
Diffstat (limited to 'widgets/displaytext.cpp')
-rw-r--r--widgets/displaytext.cpp114
1 files changed, 76 insertions, 38 deletions
diff --git a/widgets/displaytext.cpp b/widgets/displaytext.cpp
index db00e258..b3ccf8eb 100644
--- a/widgets/displaytext.cpp
+++ b/widgets/displaytext.cpp
@@ -497,60 +497,92 @@ namespace
{
void update_selection (QTextCursor& cursor, QColor const& bg, QColor const& fg)
{
- if (!cursor.isNull ())
+ QTextCharFormat format {cursor.charFormat ()};
+ if (bg.isValid ())
{
- QTextCharFormat format {cursor.charFormat ()};
- if (bg.isValid ())
- {
- format.setBackground (bg);
- }
- else
- {
- format.clearBackground ();
- }
- if (fg.isValid ())
- {
- format.setForeground (fg);
- }
- else
- {
- format.clearForeground ();
- }
- cursor.mergeCharFormat (format);
+ format.setBackground (bg);
}
+ else
+ {
+ format.clearBackground ();
+ }
+ if (fg.isValid ())
+ {
+ format.setForeground (fg);
+ }
+ else
+ {
+ format.clearForeground ();
+ }
+ cursor.mergeCharFormat (format);
}
void reset_selection (QTextCursor& cursor)
{
- if (!cursor.isNull ())
+ // restore previous text format, we rely on the text
+ // char format at he start of the selection being the
+ // old one which should be the case
+ auto c2 = cursor;
+ c2.setPosition (c2.selectionStart ());
+ cursor.setCharFormat (c2.charFormat ());
+ }
+}
+
+namespace
+{
+ QString get_timestamp (QTextCursor& cursor)
+ {
+ QString timestamp;
+ if (cursor.movePosition (QTextCursor::PreviousCharacter)
+ && cursor.movePosition (QTextCursor::StartOfLine)
+ && cursor.movePosition (QTextCursor::EndOfWord, QTextCursor::KeepAnchor)
+ && cursor.hasSelection ())
{
- // restore previous text format, we rely on the text
- // char format at he start of the selection being the
- // old one which should be the case
- auto c2 = cursor;
- c2.setPosition (c2.selectionStart ());
- cursor.setCharFormat (c2.charFormat ());
+ timestamp = cursor.selectedText ();
+ cursor.movePosition (QTextCursor::StartOfLine);
}
+ return timestamp;
}
}
void DisplayText::highlight_callsign (QString const& callsign, QColor const& bg,
- QColor const& fg, bool last_only)
+ QColor const& fg, bool last_period_only)
{
+ // qDebug () << "DisplayText::highlight_callsign: callsign:" << callsign << "last period:" << last_period_only;
+ if (!callsign.size ())
+ {
+ return;
+ }
+ QRegularExpression target {QString {"<?"} + callsign + QString {">?"}, QRegularExpression::DontCaptureOption};
QTextCharFormat old_format {currentCharFormat ()};
QTextCursor cursor {document ()};
- if (last_only)
+ if (last_period_only)
{
+ // highlight each instance of the given callsign (word) in the
+ // current period
cursor.movePosition (QTextCursor::End);
- cursor = document ()->find (callsign, cursor
- , QTextDocument::FindBackward | QTextDocument::FindWholeWords);
- if (bg.isValid () || fg.isValid ())
+ QTextCursor period_start {cursor};
+ QTextCursor prior {cursor};
+ auto period_timestamp = get_timestamp (period_start);
+ while (period_timestamp.size () && period_timestamp == get_timestamp (prior))
{
- update_selection (cursor, bg, fg);
+ period_start = prior;
}
- else
+ while (!cursor.isNull () && cursor > period_start)
{
- reset_selection (cursor);
+ cursor = document ()->find (target, cursor
+ , QTextDocument::FindBackward | QTextDocument::FindWholeWords);
+ if (!cursor.isNull () && cursor.hasSelection ())
+ {
+ if (bg.isValid () || fg.isValid ())
+ {
+ update_selection (cursor, bg, fg);
+ }
+ else
+ {
+ reset_selection (cursor);
+ }
+ }
}
}
else
@@ -569,8 +601,11 @@ void DisplayText::highlight_callsign (QString const& callsign, QColor const& bg,
}
while (!cursor.isNull ())
{
- cursor = document ()->find (callsign, cursor, QTextDocument::FindWholeWords);
- update_selection (cursor, bg, fg);
+ cursor = document ()->find (target, cursor, QTextDocument::FindWholeWords);
+ if (!cursor.isNull () && cursor.hasSelection ())
+ {
+ update_selection (cursor, bg, fg);
+ }
}
}
else if (pos != highlighted_calls_.end ())
@@ -579,8 +614,11 @@ void DisplayText::highlight_callsign (QString const& callsign, QColor const& bg,
QTextCursor cursor {document ()};
while (!cursor.isNull ())
{
- cursor = document ()->find (callsign, cursor, QTextDocument::FindWholeWords);
- reset_selection (cursor);
+ cursor = document ()->find (target, cursor, QTextDocument::FindWholeWords);
+ if (!cursor.isNull () && cursor.hasSelection ())
+ {
+ reset_selection (cursor);
+ }
}
}
}