GCC Code Coverage Report


Directory: ./
File: src/MessageModelImpl.cpp
Date: 2024-11-19 17:00:39
Exec Total Coverage
Lines: 222 245 90.6%
Branches: 311 618 50.3%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2009 - 2022 Florian Pose <fp@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22 #include "MessageModelImpl.h"
23
24 #include "MessageImpl.h"
25 #include "MessageItem.h"
26
27 #include <QDateTime>
28 #include <QFutureWatcher>
29
30 using QtPdCom::MessageModel;
31
32 /****************************************************************************/
33
34 /** Constructor.
35 */
36 10 MessageModel::Impl::Impl(MessageModel *model):
37 parent {model},
38 announcedMessageItem {nullptr},
39 messageManager {nullptr},
40 rowLimit {1000},
41 canFetchMore {false},
42 historicSeqNo {0},
43 10 lessThan {MessageItem::levelNoLessThan}
44 10 {}
45
46 /****************************************************************************/
47
48 /** Destructor.
49 */
50 20 MessageModel::Impl::~Impl()
51 20 {}
52
53 /****************************************************************************/
54
55 /** Insert a message item.
56 */
57 22 void MessageModel::Impl::insertItem(MessageItem *msgItem)
58 {
59 22 int row = messageItemList.indexOf(msgItem);
60
61
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (row >= 0) {
62
1/2
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 parent->beginRemoveRows(QModelIndex(), row, row);
63 2 messageItemList.removeAt(row);
64 2 parent->endRemoveRows();
65 }
66
67 62 row = (messageItemList.empty()
68
4/6
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 6 times.
10 or MessageItem::lessThan(msgItem, messageItemList.first()))
69
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
56 ? 0
70
3/6
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 18 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
34 : (std::lower_bound(
71
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
26 messageItemList.begin(),
72
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
26 messageItemList.end(),
73 msgItem,
74 lessThan)
75
3/6
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
30 - messageItemList.begin());
76
77
1/2
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
22 parent->beginInsertRows(QModelIndex(), row, row);
78 22 messageItemList.insert(row, msgItem);
79 22 parent->endInsertRows();
80
81 // Is this a candidate for the current/announced message?
82 //
83 // Announce this message as current message, if
84 // - there is either no message announced yet,
85 // - the message type is more important than the type of the announced one
86 // - the sequence number is earlier
87
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 10 times.
44 if (msgItem->isActive()
88
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 10 times.
34 and (!announcedMessageItem
89
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
6 or msgItem->getType() > announcedMessageItem->getType()
90
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
4 or (msgItem->getType() == announcedMessageItem->getType()
91
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 and seqNoLessThan(
92 msgItem->seqNo,
93 3 announcedMessageItem->seqNo)))) {
94 12 announcedMessageItem = msgItem;
95 #if PD_DEBUG_MESSAGE_MODEL
96
4/8
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 12 times.
✗ Branch 13 not taken.
24 qDebug() << __func__ << "currentMessage" << msgItem
97
1/2
✓ Branch 5 taken 12 times.
✗ Branch 6 not taken.
24 << msgItem->message;
98 #endif
99 12 emit parent->currentMessage(announcedMessageItem->message);
100 }
101
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
10 else if (msgItem->resetTime and msgItem == announcedMessageItem) {
102 // search for a new message to announce
103 announce();
104 }
105 22 }
106
107 /****************************************************************************/
108
109 /** Called from the PdCom interface, if a new message appears via
110 * processMessage() or in context of activeMessagesReply().
111 */
112 18 void MessageModel::Impl::addProcessMessage(const PdCom::Message &pdComMsg)
113 {
114
1/2
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
36 QString path {QString::fromStdString(pdComMsg.path)};
115 #if PD_DEBUG_MESSAGE_MODEL
116
4/8
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 18 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 18 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 18 times.
✗ Branch 14 not taken.
18 qDebug() << __func__ << "seqno" << pdComMsg.seqNo;
117 #endif
118
119
2/4
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 Message *&msg = messageMap[path][pdComMsg.index];
120
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (!msg) {
121 #if PD_DEBUG_MESSAGE_MODEL
122
3/6
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
9 qDebug() << __func__ << "not in map. creating new message.";
123 #endif
124
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 msg = new Message();
125
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 msg->impl->fromPdComMessage(pdComMsg);
126 }
127
128 18 auto msgItem {msg->impl->currentItem};
129 #if PD_DEBUG_MESSAGE_MODEL
130
4/8
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 18 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 18 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 18 times.
✗ Branch 14 not taken.
18 qDebug() << __func__ << "msgItem" << msgItem;
131
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 11 times.
18 if (msgItem) {
132
4/8
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 7 times.
✗ Branch 14 not taken.
7 qDebug() << __func__ << "currentItem seqNo" << msgItem->seqNo;
133 }
134 #endif
135
136
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 if (pdComMsg.level != PdCom::LogLevel::Reset) { // set
137
138
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
12 if (not msg->impl->announced) {
139
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 emit parent->anyMessage(msg);
140 10 msg->impl->announced = true;
141 }
142
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (msgItem) {
144 // already has current item
145
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!msgItem->seqNo) {
146 // Current item has zero seqNo -> from mixed mode.
147 // Just attach the seqNo.
148 2 msgItem->seqNo = pdComMsg.seqNo;
149 }
150 else if (msgItem->seqNo != pdComMsg.seqNo) {
151 // Current item has different seqNo.
152 // Not ours, thus create a new one.
153 msgItem = new MessageItem(msg, this, pdComMsg.time.count());
154 msgItem->seqNo = pdComMsg.seqNo;
155 }
156 }
157 else { // no current item
158
2/4
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
10 msgItem = new MessageItem(msg, this, pdComMsg.time.count());
159 10 msgItem->seqNo = pdComMsg.seqNo;
160 10 msg->impl->currentItem = msgItem;
161 }
162
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 insertItem(msgItem);
163 }
164 else { // reset
165 #if PD_DEBUG_MESSAGE_MODEL
166
4/8
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
6 qDebug() << __func__ << "reset msgItem" << msgItem;
167 #endif
168 6 msg->impl->announced = false;
169
170
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (msgItem) {
171 5 msgItem->resetTime = pdComMsg.time.count();
172
173 // notify views
174
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 int row = messageItemList.indexOf(msgItem);
175 #if PD_DEBUG_MESSAGE_MODEL
176
4/8
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 5 times.
✗ Branch 14 not taken.
5 qDebug() << __func__ << "reset row" << row;
177 #endif
178
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (row >= 0) {
179
1/2
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 QModelIndex idx0 = parent->index(row, 0);
180
1/2
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 QModelIndex idx1 = parent->index(row, 2);
181
1/2
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
5 emit parent->dataChanged(idx0, idx1);
182 }
183 5 msg->impl->currentItem = nullptr;
184
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (msgItem == announcedMessageItem) {
185
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 announce();
186 }
187 }
188 }
189 18 }
190
191 /****************************************************************************/
192
193 /** Called from the PdCom interface, if a historic message appears via
194 * getMessageReply().
195 */
196 6 void MessageModel::Impl::addHistoricMessage(
197 const PdCom::Message &pdComMsg,
198 const PdCom::Message &resetMsg)
199 {
200
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 QString path {QString::fromStdString(pdComMsg.path)};
201 #if PD_DEBUG_MESSAGE_MODEL
202
4/8
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
6 qDebug() << __func__ << pdComMsg.seqNo << resetMsg.seqNo;
203 #endif
204
205
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 Message *&msg = messageMap[path][pdComMsg.index];
206
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (!msg) {
207
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 msg = new Message();
208
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 msg->impl->fromPdComMessage(pdComMsg);
209 }
210
211
2/4
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
6 auto msgItem = new MessageItem(msg, this, pdComMsg.time.count());
212 6 msgItem->seqNo = pdComMsg.seqNo;
213 6 msgItem->resetTime = resetMsg.time.count();
214
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 insertItem(msgItem);
215 6 }
216
217 /****************************************************************************/
218
219 /** Returns a wrapped version of a string.
220 */
221 37 QString MessageModel::Impl::wrapText(const QString &text, unsigned int width)
222 {
223 37 QString ret;
224 int lineOffset, i;
225
226 37 lineOffset = 0;
227
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 while (lineOffset + (int) width < text.length()) {
228 // search last space before line end
229 for (i = width; i >= 0; i--) {
230 if (text[lineOffset + i].isSpace()) {
231 break; // break at whitespace
232 }
233 }
234 if (i < 0) { // no whitespace found
235 i = width; // "hard" break at line end
236 }
237
238 ret += text.mid(lineOffset, i) + QChar(QChar::LineSeparator);
239 lineOffset += i + 1; // skip line and whitespace
240 }
241
242
2/4
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
37 ret += text.mid(lineOffset); // append remaining string
243 37 return ret;
244 }
245
246 /****************************************************************************/
247
248 13 void MessageModel::Impl::getHistoryMessage()
249 {
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!messageManager) {
251 qWarning() << __func__ << "no message manager";
252 return;
253 }
254
255 #if PD_DEBUG_MESSAGE_MODEL
256
3/6
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
13 qDebug() << __func__ << "setting canFetchMore to false";
257 #endif
258 13 canFetchMore = false; // avoid fetchMore called twice for same seqNo
259
260 13 uint32_t prevSeqNo = historicSeqNo - 1;
261 #if PD_DEBUG_MESSAGE_MODEL
262
4/8
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 13 times.
✗ Branch 14 not taken.
13 qDebug() << __func__ << "fetching" << prevSeqNo;
263 #endif
264
265 try {
266
1/4
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
13 messageManager->getMessage(prevSeqNo, this, &Impl::getMessageReply);
267 }
268 catch (PdCom::Exception &e) {
269 qDebug() << __func__ << e.what();
270 }
271 }
272
273 /****************************************************************************/
274
275 7 void MessageModel::Impl::announce()
276 {
277
3/4
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
9 MessageItemList sortedList(messageItemList);
278
1/2
✓ Branch 3 taken 7 times.
✗ Branch 4 not taken.
21 std::sort(
279
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
14 sortedList.begin(),
280
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
14 sortedList.end(),
281 MessageItem::levelNoLessThan);
282
283 // if there is no active message, announce a nullptr
284
4/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✓ Branch 7 taken 2 times.
7 if (not sortedList.front()->isActive()) {
285
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (announcedMessageItem) {
286 5 announcedMessageItem = nullptr;
287 #if PD_DEBUG_MESSAGE_MODEL
288
3/6
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 5 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
5 qDebug() << __func__ << "currentMessage null";
289 #endif
290
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 emit parent->currentMessage(nullptr);
291 }
292 5 return;
293 }
294
295 // the first active message in the sorted list determines the type
296
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 auto type = sortedList.front()->getType();
297
298 // find the earliest message with this type
299 2 MessageItem *candidate {nullptr};
300
1/2
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 for (MessageItemList::const_iterator it = sortedList.begin();
301
7/16
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 4 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
20 (it != sortedList.end() and (*it)->isActive()
302
6/10
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
18 and (*it)->getType() == type);
303 ++it) {
304
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 candidate = *it;
305 }
306
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (announcedMessageItem == candidate) {
308 // still the same message, no new announcement needed
309 return;
310 }
311 2 announcedMessageItem = candidate;
312
313 2 const QtPdCom::Message *msg {nullptr};
314
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (announcedMessageItem) {
315 2 msg = announcedMessageItem->message;
316 }
317 #if PD_DEBUG_MESSAGE_MODEL
318
5/10
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 2 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 2 times.
✗ Branch 20 not taken.
2 qDebug() << __func__ << "currentMessage" << announcedMessageItem << msg;
319 #endif
320
3/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 5 times.
2 emit parent->currentMessage(msg);
321 }
322
323 /*****************************************************************************
324 * private slots
325 ****************************************************************************/
326
327 /** Reacts on process values changes of all messages to watch.
328 */
329 7 void MessageModel::Impl::stateChanged()
330 {
331
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 Message *msg = (Message *) sender();
332 7 DoubleVariable &var = msg->impl->variable;
333
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 double time {var.hasData() ? var.getValue() : 0.0};
334
335 #if PD_DEBUG_MESSAGE_MODEL
336
7/14
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 7 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 7 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 7 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 7 times.
✗ Branch 23 not taken.
7 qDebug() << __func__ << msg->getPath() << msg->getIndex() << time;
337 #endif
338
339
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 3 times.
7 if (time and not msg->impl->announced) {
340
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 emit parent->anyMessage(msg);
341 4 msg->impl->announced = true;
342 }
343
344 7 MessageItem *msgItem {nullptr};
345
346
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if (time) { // set
347 4 MessageItem *msgItem {msg->impl->currentItem};
348
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!msgItem) {
349
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 msgItem = new MessageItem(msg, this, time * 1e9);
350 #if PD_DEBUG_MESSAGE_MODEL
351
4/8
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
4 qDebug() << __func__ << "new msgItem" << msgItem;
352 #endif
353 4 msg->impl->currentItem = msgItem;
354
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 insertItem(msgItem);
355 }
356 }
357 else { // reset
358 3 msg->impl->announced = false;
359 3 msgItem = msg->impl->currentItem;
360
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (msgItem) {
361
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 auto now {QDateTime::currentDateTime()};
362
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 msgItem->resetTime = now.toMSecsSinceEpoch() * 1000000U;
363
364 // notify views
365
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 int row = messageItemList.indexOf(msgItem);
366 #if PD_DEBUG_MESSAGE_MODEL
367
4/8
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 14 not taken.
3 qDebug() << __func__ << "reset row" << row;
368 #endif
369
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (row >= 0) {
370
1/2
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 QModelIndex idx0 = parent->index(row, 0);
371
1/2
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 QModelIndex idx1 = parent->index(row, 2);
372
1/2
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 emit parent->dataChanged(idx0, idx1);
373 }
374
375 3 msg->impl->currentItem = nullptr;
376
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (msgItem == announcedMessageItem) {
377
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 announce();
378 }
379 }
380 }
381 7 }
382
383 /****************************************************************************/
384
385 17 void MessageModel::Impl::processMessage(PdCom::Message message)
386 {
387 #if PD_DEBUG_MESSAGE_MODEL
388
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
34 auto path = QString::fromStdString(message.path);
389
1/2
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
34 auto text = QString::fromStdString(message.text);
390
2/4
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 17 times.
✗ Branch 8 not taken.
17 qDebug() << __func__;
391
5/10
✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 17 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 17 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 17 times.
✗ Branch 16 not taken.
34 qDebug() << "seqNo" << message.seqNo << "level" << (int) message.level
392
5/10
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 17 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 17 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 17 times.
✗ Branch 15 not taken.
17 << "path" << path << "time" << message.time.count() << "index"
393
3/6
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 17 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 17 times.
✗ Branch 9 not taken.
34 << message.index << "text" << text;
394 #endif
395
396
1/2
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
17 addProcessMessage(message);
397 17 }
398
399 /****************************************************************************/
400
401 11 void MessageModel::Impl::getMessageReply(PdCom::Message message)
402 {
403
3/4
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 2 times.
20 auto path = QString::fromStdString(message.path);
404
405 #if PD_DEBUG_MESSAGE_MODEL
406
3/4
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 2 times.
20 auto text = QString::fromStdString(message.text);
407
2/4
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
11 qDebug() << __func__;
408
5/10
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 11 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 11 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 11 times.
✗ Branch 16 not taken.
22 qDebug() << "seqNo" << message.seqNo << "level" << (int) message.level
409
5/10
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 11 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 11 times.
✗ Branch 15 not taken.
11 << "path" << path << "time" << message.time.count() << "index"
410
3/6
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 11 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 11 times.
✗ Branch 9 not taken.
22 << message.index << "text" << text;
411 #endif
412
413
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9 times.
11 if (path.isEmpty()) {
414 // EOF marker - no more messages from process
415 2 return;
416 }
417
418 9 historicSeqNo = message.seqNo;
419
420 9 canFetchMore = message.level != PdCom::LogLevel::Reset;
421 #if PD_DEBUG_MESSAGE_MODEL
422
4/8
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 9 times.
✗ Branch 14 not taken.
9 qDebug() << __func__ << "setting canFetchMore to" << canFetchMore;
423 #endif
424
425 9 bool stillActive {false};
426
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (canFetchMore) {
427 // found a message that was set in the past. try to find the reset.
428 6 bool found {false};
429 #if PD_DEBUG_MESSAGE_MODEL
430
3/6
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
12 qDebug() << __func__ << "reset msg list size is"
431
1/2
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
12 << resetMessagesList.size();
432 #endif
433
3/6
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
6 for (auto r = resetMessagesList.begin(); r != resetMessagesList.end();
434 r++) {
435
5/10
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 6 times.
✗ Branch 12 not taken.
6 if (r->path == message.path and r->index == message.index) {
436
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 addHistoricMessage(message, *r);
437
1/2
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 resetMessagesList.erase(r);
438 #if PD_DEBUG_MESSAGE_MODEL
439 6 found = true;
440 #endif
441 6 break;
442 }
443 }
444 #if PD_DEBUG_MESSAGE_MODEL
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!found) {
446 qDebug() << __func__ << "reset message not found for"
447 << message.seqNo;
448 // found a message that seems to be still active. Go on with
449 // reading, otherwise views won't ask for more data
450 stillActive = true;
451 }
452 #endif
453 }
454 else {
455
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 resetMessagesList.append(message);
456 }
457
458
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
9 if ((!canFetchMore or stillActive) and historicSeqNo) {
459
3/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 2 times.
5 getHistoryMessage();
460 }
461 }
462
463 /****************************************************************************/
464
465 4 void MessageModel::Impl::activeMessagesReply(
466 std::vector<PdCom::Message> messageList)
467 {
468 4 quint32 maxSeqNo {0};
469
470 #if PD_DEBUG_MESSAGE_MODEL
471
5/10
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 4 times.
✗ Branch 19 not taken.
4 qDebug().nospace() << __func__ << "(" << messageList.size() << ")";
472 #endif
473
3/4
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 4 times.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
8 for (auto message : messageList) {
474 #if PD_DEBUG_MESSAGE_MODEL
475
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 auto path = QString::fromStdString(message.path);
476
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 auto text = QString::fromStdString(message.text);
477
5/10
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
8 qDebug() << "seqNo" << message.seqNo << "level" << (int) message.level
478
4/8
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
4 << "path" << path << "time" << message.time.count()
479
4/8
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
8 << "index" << message.index << "text" << text;
480 #endif
481
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (message.level != PdCom::LogLevel::Reset) {
482
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 addProcessMessage(message);
483 }
484 else {
485 // one entry in the list of active messages can be a reset
486 // message, to announce the current sequence number
487
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 resetMessagesList.append(message);
488 }
489
490
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (message.seqNo > maxSeqNo) {
491 4 maxSeqNo = message.seqNo;
492 }
493 }
494
495
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (maxSeqNo > 0) {
496 // now fetch one historic message
497 4 historicSeqNo = maxSeqNo;
498 4 getHistoryMessage();
499 }
500 4 }
501
502 /****************************************************************************/
503
504 6 void MessageModel::Impl::processReset()
505 {
506 #if PD_DEBUG_MESSAGE_MODEL
507
2/4
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
6 qDebug() << __func__;
508 #endif
509
510 6 canFetchMore = false;
511 6 historicSeqNo = 0;
512 6 resetMessagesList.clear();
513
514 6 parent->beginResetModel();
515 6 messageItemList.clear();
516
517 // reset current item pointers
518
3/10
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 for (auto hash : messageMap) {
519 for (auto msg : hash) {
520 if (msg->impl->currentItem) {
521 msg->impl->currentItem = nullptr;
522 }
523 }
524 }
525 6 parent->endResetModel();
526
527
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (messageManager) {
528 12 QObject::disconnect(
529 6 messageManager,
530 SIGNAL(processMessageSignal(PdCom::Message)),
531 this,
532 SLOT(processMessage(PdCom::Message)));
533 12 QObject::disconnect(
534 6 messageManager,
535 SIGNAL(processResetSignal()),
536 this,
537 SLOT(processReset()));
538 }
539 6 }
540
541 /****************************************************************************/
542
543
544 5 void MessageModel::Impl::reloadActiveMessages()
545 {
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!messageManager) {
547 return;
548 }
549
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 messageManager->activeMessages(this, &Impl::activeMessagesReply);
550 }
551