1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#ifndef JTTY_RECEIVE_LINE_HPP
#define JTTY_RECEIVE_LINE_HPP
#include <QColor>
#include <QDateTime>
#include <QFont>
#include <QPointer>
#include <QTextBlock>
#include <QTextBlockFormat>
#include <QTextCharFormat>
#include <QTextCursor>
#include <QTextDocument>
#include <memory>
#include <vector>
#include "widgets/JttyMessages.hpp"
class JttyReceiveLine
{
public:
struct Presentation {
quint64 group {};
qint64 messageId {};
QDateTime startUtc;
double startSeconds {};
int frequency {};
QString message;
int snr {-10};
bool blankSnrColumn {false}; // a sent message has no measured SNR
QColor background {}; // invalid QColor() means "no override"
QColor foreground {};
};
struct Options {
bool lowerCase {};
bool includeTime {};
};
// Removed history is never recreated by a delayed update of the same message.
QTextCursor render (QTextDocument& document, Presentation const& presentation,
Options const& options, QFont const& font,
bool chronological = false)
{
if (pruned_) return {};
auto record = record_.lock ();
if (rendered_) {
if (!record || record->document != &document
|| record->presentation.group != presentation.group
|| record->presentation.messageId != presentation.messageId
|| !intact (record)) {
pruned_ = true;
record_.reset ();
return {};
}
} else {
QTextBlock before;
QTextBlock after;
if (chronological) {
for (auto block = document.begin (); block.isValid (); block = block.next ()) {
auto const * tag = dynamic_cast<Tag const *> (block.userData ());
if (!tag || tag->index || tag->record->presentation.group != presentation.group
|| !intact (tag->record)) continue;
auto const& known = tag->record->presentation;
if (presentation.startSeconds < known.startSeconds
|| (presentation.startSeconds == known.startSeconds
&& presentation.messageId < known.messageId)) {
before = block;
break;
}
after = tag->record->last;
}
}
if (!before.isValid () && after.isValid ()) before = after.next ();
if (before == document.begin () && document.maximumBlockCount () > 0
&& document.blockCount () >= document.maximumBlockCount ()) {
pruned_ = true;
return {};
}
auto cursor = insertBlock (document, before);
record = std::make_shared<Record> ();
record->document = &document;
record->first = record->last = cursor.block ();
record->first.setUserData (new Tag (record, 0));
record_ = record;
rendered_ = true;
}
record->presentation = presentation;
return rewrite (record, options, font);
}
static void refresh (QTextDocument& document, Options const& options,
QFont const& font)
{
std::vector<std::shared_ptr<Record>> records;
for (auto block = document.begin (); block.isValid (); block = block.next ()) {
auto const * tag = dynamic_cast<Tag const *> (block.userData ());
if (tag && !tag->index) records.push_back (tag->record);
}
for (auto const& record : records) {
if (intact (record)) rewrite (record, options, font);
}
}
private:
struct Record {
QPointer<QTextDocument> document;
Presentation presentation;
QTextBlock first, last;
QString text;
QFont font;
int blockCount {1};
};
struct Tag : QTextBlockUserData {
Tag (std::shared_ptr<Record> const& source, int blockIndex)
: record (source), index (blockIndex) {}
std::shared_ptr<Record> record;
int index;
};
static bool intact (std::shared_ptr<Record> const& record)
{
if (!record->document || !record->first.isValid () || !record->last.isValid ()) return false;
int index = 0;
for (auto block = record->first; block.isValid (); block = block.next (), ++index) {
auto const * tag = dynamic_cast<Tag const *> (block.userData ());
if (!tag || tag->record != record || tag->index != index) return false;
if (block == record->last) {
if (index + 1 != record->blockCount) return false;
auto cursor = range (record);
auto text = cursor.selectedText ();
text.replace (QChar::ParagraphSeparator, QChar ('\n'));
return text == record->text;
}
}
return false;
}
static QTextCursor range (std::shared_ptr<Record> const& record)
{
QTextCursor cursor (record->first);
QTextCursor end (record->last);
end.movePosition (QTextCursor::EndOfBlock);
cursor.setPosition (end.position (), QTextCursor::KeepAnchor);
return cursor;
}
static QTextCursor insertBlock (QTextDocument& document, QTextBlock const& before)
{
QTextCursor cursor (&document);
if (before.isValid ()) {
auto previous = before.previous ();
if (previous.isValid ()) {
cursor = QTextCursor (previous);
cursor.movePosition (QTextCursor::EndOfBlock);
cursor.insertBlock (QTextBlockFormat {});
} else {
auto const * tag = dynamic_cast<Tag const *> (before.userData ());
auto record = tag ? tag->record : std::shared_ptr<Record> {};
int const index = tag ? tag->index : 0;
auto const blockFormat = before.blockFormat ();
auto const charFormat = before.charFormat ();
cursor.insertBlock (blockFormat, charFormat);
// Inserting at the first block moves its text into the new block.
if (record) {
auto moved = cursor.block ();
moved.setUserData (new Tag (record, index));
if (record->first == before) record->first = moved;
if (record->last == before) record->last = moved;
}
cursor.movePosition (QTextCursor::PreviousBlock);
cursor.setBlockFormat (QTextBlockFormat {});
}
} else {
cursor.movePosition (QTextCursor::End);
if (cursor.position ()) cursor.insertBlock (QTextBlockFormat {});
}
return cursor;
}
static QString formatted (Presentation const& presentation, Options const& options)
{
QString const message = options.lowerCase
? presentation.message.toLower () : presentation.message;
QString text = presentation.blankSnrColumn
? Jtty::formatJttyTxLine (presentation.frequency, Jtty::wrapMessage (message))
: Jtty::formatJttyDecodeLine (presentation.frequency, presentation.snr, Jtty::wrapMessage (message));
if (options.includeTime) {
auto const time = Jtty::jttyLineTimeLabel (presentation.startUtc);
if (!time.isEmpty ()) text.prepend (time + " ");
}
return text;
}
static QTextCursor rewrite (std::shared_ptr<Record> const& record,
Options const& options, QFont const& font)
{
QString const text = formatted (record->presentation, options);
if (record->text == text && record->font == font) return {};
auto cursor = range (record);
cursor.removeSelectedText ();
record->first = cursor.block ();
record->first.setUserData (new Tag (record, 0));
QTextCharFormat format;
format.setFont (font);
if (record->presentation.background.isValid ()) format.setBackground (record->presentation.background);
if (record->presentation.foreground.isValid ()) format.setForeground (record->presentation.foreground);
cursor.insertText (text, format);
auto const * firstTag = record->first.isValid ()
? dynamic_cast<Tag const *> (record->first.userData ()) : nullptr;
if (!firstTag || firstTag->record != record || firstTag->index) return {};
record->last = cursor.block ();
int index = 0;
for (auto block = record->first; block.isValid (); block = block.next (), ++index) {
block.setUserData (new Tag (record, index));
if (block == record->last) break;
}
record->blockCount = index + 1;
record->text = text;
record->font = font;
return cursor;
}
std::weak_ptr<Record> record_;
bool rendered_ = false;
bool pruned_ = false;
};
#endif
|