GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/BroadcastModel.cpp
Date: 2024-12-29 04:08:32
Exec Total Coverage
Lines: 0 92 0.0%
Branches: 0 69 0.0%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2009-2023 Bjarne von Horn <vh@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 "BroadcastModel.h"
23
24 #include <QDateTime>
25 #include <QtPdCom1/Process.h>
26
27 namespace QtPdCom {
28 class BroadcastModelPrivate
29 {
30 friend class BroadcastModel;
31
32
33 struct Broadcast
34 {
35 QString message, timestamp, user;
36 };
37
38 QList<Broadcast> broadcasts;
39 QMetaObject::Connection processConnection;
40 Process *process = nullptr;
41 };
42 } // namespace QtPdCom
43
44 using QtPdCom::BroadcastModel;
45
46 BroadcastModel::BroadcastModel(QObject *parent):
47 QAbstractTableModel(parent), d_ptr(new BroadcastModelPrivate())
48 {}
49
50 BroadcastModel::~BroadcastModel() = default;
51
52 int BroadcastModel::rowCount(const QModelIndex &) const
53 {
54 const Q_D(BroadcastModel);
55
56 return d->broadcasts.count();
57 }
58
59 int BroadcastModel::columnCount(const QModelIndex &) const
60 {
61 return 3;
62 }
63
64 QVariant BroadcastModel::data(const QModelIndex &index, int role) const
65 {
66 const Q_D(BroadcastModel);
67 int col = index.column();
68
69 if (index.row() < 0 || index.row() >= d->broadcasts.count())
70 return QVariant();
71 switch (role) {
72 case Qt::DisplayRole:
73 case DateStringRole:
74 break;
75 case MessageStringRole:
76 col = 1;
77 break;
78 case UsernameRole:
79 col = 2;
80 break;
81 default:
82 return QVariant();
83 }
84 switch (col) {
85 case 0:
86 return d->broadcasts[index.row()].timestamp;
87 case 1:
88 return d->broadcasts[index.row()].message;
89 case 2:
90 return d->broadcasts[index.row()].user;
91 default:
92 return QVariant();
93 }
94 }
95
96 QVariant BroadcastModel::headerData(
97 int section,
98 Qt::Orientation orientation,
99 int role) const
100 {
101 switch (role) {
102 case Qt::DisplayRole:
103 break;
104 case DateStringRole:
105 section = 0;
106 break;
107 case MessageStringRole:
108 section = 1;
109 break;
110 case UsernameRole:
111 section = 2;
112 break;
113 default:
114 return QVariant();
115 }
116 if (orientation == Qt::Horizontal) {
117 switch (section) {
118 case 0:
119 return tr("Date");
120 case 1:
121 return tr("Message");
122 case 2:
123 return tr("User");
124 default:
125 break;
126 }
127 }
128 return QVariant();
129 }
130
131 void BroadcastModel::connectProcess(QtPdCom::Process *p)
132 {
133 Q_D(BroadcastModel);
134
135 d->process = p;
136 if (d->processConnection)
137 disconnect(d->processConnection);
138
139 if (!p)
140 return;
141
142 auto lambda = [this](const QString &message,
143 const QString & /* attr */,
144 uint64_t time_ns,
145 const QString &user) {
146 Q_D(BroadcastModel);
147 const auto sz = d->broadcasts.count();
148 this->beginInsertRows({}, sz, sz);
149 d->broadcasts.push_back(
150 {message,
151 QDateTime::fromMSecsSinceEpoch(time_ns / 1000000).toString(),
152 user});
153 this->endInsertRows();
154 };
155
156 d->processConnection =
157 connect(p, &QtPdCom::Process::broadcastReceived, this, lambda);
158 }
159
160 QtPdCom::Process *BroadcastModel::getProcess() const
161 {
162 const Q_D(BroadcastModel);
163 return d->process;
164 }
165
166 void BroadcastModel::clear()
167 {
168 Q_D(BroadcastModel);
169
170 beginResetModel();
171 d->broadcasts.clear();
172 endResetModel();
173 }
174
175 QHash<int, QByteArray> BroadcastModel::roleNames() const
176 {
177 // default role names
178 auto roles = QAbstractTableModel::roleNames();
179
180 // extended role names
181 roles[DateStringRole] = "dateString";
182 roles[MessageStringRole] = "messageString";
183 roles[UsernameRole] = "username";
184 return roles;
185 }
186