| 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 "MessageModel.h" |
| 23 |
|
|
using QtPdCom::MessageModel; |
| 24 |
|
|
|
| 25 |
|
|
#include "MessageImpl.h" |
| 26 |
|
|
#include "MessageItem.h" |
| 27 |
|
|
#include "MessageManager.h" |
| 28 |
|
|
#include "MessageModelImpl.h" |
| 29 |
|
|
#include "Process.h" |
| 30 |
|
|
|
| 31 |
|
|
#include <QEvent> |
| 32 |
|
|
#include <QFile> |
| 33 |
|
|
#include <QDomDocument> |
| 34 |
|
|
#include <QMetaEnum> |
| 35 |
|
|
#include <QtGlobal> |
| 36 |
|
|
|
| 37 |
|
|
/****************************************************************************/ |
| 38 |
|
|
|
| 39 |
|
|
/** Constructor. |
| 40 |
|
|
*/ |
| 41 |
|
✗ |
MessageModel::MessageModel(QObject *parent): |
| 42 |
|
|
QAbstractTableModel(parent), |
| 43 |
|
✗ |
impl(std::unique_ptr<Impl>(new MessageModel::Impl(this))) |
| 44 |
|
|
{} |
| 45 |
|
|
|
| 46 |
|
|
/****************************************************************************/ |
| 47 |
|
|
|
| 48 |
|
|
/** Destructor. |
| 49 |
|
|
*/ |
| 50 |
|
✗ |
MessageModel::~MessageModel() |
| 51 |
|
|
{ |
| 52 |
|
✗ |
clear(); |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
/****************************************************************************/ |
| 56 |
|
|
|
| 57 |
|
|
/** Loads messages from an Xml file. |
| 58 |
|
|
*/ |
| 59 |
|
✗ |
void MessageModel::load( |
| 60 |
|
|
const QString &path, /**< Path to Xml file. */ |
| 61 |
|
|
const QString &lang, /**< Language identifier. */ |
| 62 |
|
|
const QString &pathPrefix /**< Prefix to path (with leading /). */ |
| 63 |
|
|
) |
| 64 |
|
|
{ |
| 65 |
|
✗ |
QFile file(path); |
| 66 |
|
✗ |
QDomDocument doc; |
| 67 |
|
✗ |
QDomElement docElem; |
| 68 |
|
|
|
| 69 |
|
✗ |
if (!file.open(QIODevice::ReadOnly)) { |
| 70 |
|
✗ |
throw Exception(QtPdCom::MessageModel::tr("Failed to open %1.") |
| 71 |
|
✗ |
.arg(file.fileName())); |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
#if QT_VERSION < 0x060500 |
| 75 |
|
|
{ |
| 76 |
|
✗ |
QString errorMessage; |
| 77 |
|
✗ |
int errorRow, errorColumn; |
| 78 |
|
✗ |
if (!doc.setContent(&file, &errorMessage, &errorRow, &errorColumn)) { |
| 79 |
|
|
throw Exception( |
| 80 |
|
✗ |
QtPdCom::MessageModel::tr("Failed to parse %1, line %2," |
| 81 |
|
|
" column %3: %4") |
| 82 |
|
✗ |
.arg(file.fileName()) |
| 83 |
|
✗ |
.arg(errorRow) |
| 84 |
|
✗ |
.arg(errorColumn) |
| 85 |
|
✗ |
.arg(errorMessage)); |
| 86 |
|
|
} |
| 87 |
|
|
} |
| 88 |
|
|
#else |
| 89 |
|
|
{ |
| 90 |
|
|
QDomDocument::ParseResult res = doc.setContent(&file); |
| 91 |
|
|
if (not res) { |
| 92 |
|
|
throw Exception( |
| 93 |
|
|
QtPdCom::MessageModel::tr("Failed to parse %1, line %2," |
| 94 |
|
|
" column %3: %4") |
| 95 |
|
|
.arg(file.fileName()) |
| 96 |
|
|
.arg(res.errorLine) |
| 97 |
|
|
.arg(res.errorColumn) |
| 98 |
|
|
.arg(res.errorMessage)); |
| 99 |
|
|
} |
| 100 |
|
|
} |
| 101 |
|
|
#endif |
| 102 |
|
✗ |
file.close(); |
| 103 |
|
|
|
| 104 |
|
✗ |
docElem = doc.documentElement(); |
| 105 |
|
|
|
| 106 |
|
✗ |
if (docElem.tagName() != "EtherLabPlainMessages") { |
| 107 |
|
|
throw Exception( |
| 108 |
|
✗ |
QtPdCom::MessageModel::tr("Failed to process %1:" |
| 109 |
|
✗ |
" No plain message file (%2)!")); |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
✗ |
QDomNodeList children = docElem.childNodes(); |
| 113 |
|
✗ |
QDomNode node; |
| 114 |
|
✗ |
QDomElement child; |
| 115 |
|
|
|
| 116 |
|
✗ |
for (int i = 0; i < children.size(); i++) { |
| 117 |
|
✗ |
node = children.item(i); |
| 118 |
|
✗ |
if (node.isElement()) { |
| 119 |
|
✗ |
child = node.toElement(); |
| 120 |
|
✗ |
if (child.tagName() == "Message") { |
| 121 |
|
|
try { |
| 122 |
|
✗ |
QString path = Message::Impl::pathFromPlainXmlElement( |
| 123 |
|
|
child, |
| 124 |
|
✗ |
pathPrefix); |
| 125 |
|
✗ |
int index = |
| 126 |
|
✗ |
Message::Impl::indexFromPlainXmlElement(child); |
| 127 |
|
|
|
| 128 |
|
✗ |
Message *&msg = impl->messageMap[path][index]; |
| 129 |
|
✗ |
if (!msg) { |
| 130 |
|
✗ |
msg = new Message(); |
| 131 |
|
|
} |
| 132 |
|
✗ |
msg->impl->fromPlainXmlElement(child, pathPrefix); |
| 133 |
|
✗ |
QObject::connect( |
| 134 |
|
|
msg, |
| 135 |
|
|
SIGNAL(stateChanged()), |
| 136 |
|
✗ |
impl.get(), |
| 137 |
|
|
SLOT(stateChanged())); |
| 138 |
|
|
} |
| 139 |
|
✗ |
catch (Message::Exception &e) { |
| 140 |
|
✗ |
qWarning() << e.msg; |
| 141 |
|
|
} |
| 142 |
|
|
} |
| 143 |
|
|
} |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
✗ |
impl->lang = lang; |
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
/****************************************************************************/ |
| 150 |
|
|
|
| 151 |
|
|
/** Clears the messages. |
| 152 |
|
|
*/ |
| 153 |
|
✗ |
void MessageModel::clear() |
| 154 |
|
|
{ |
| 155 |
|
✗ |
if (impl->announcedMessageItem) { |
| 156 |
|
✗ |
impl->announcedMessageItem = nullptr; |
| 157 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 158 |
|
|
qDebug() << __func__ << "currentMessage null"; |
| 159 |
|
|
#endif |
| 160 |
|
✗ |
emit currentMessage(nullptr); |
| 161 |
|
|
} |
| 162 |
|
|
|
| 163 |
|
✗ |
if (impl->messageItemList.count()) { |
| 164 |
|
✗ |
beginResetModel(); |
| 165 |
|
✗ |
qDeleteAll(impl->messageItemList); |
| 166 |
|
✗ |
impl->messageItemList.clear(); |
| 167 |
|
✗ |
endResetModel(); |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
|
✗ |
for (auto &h : impl->messageMap) { |
| 171 |
|
✗ |
qDeleteAll(h); |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
✗ |
impl->messageMap.clear(); |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
/****************************************************************************/ |
| 178 |
|
|
|
| 179 |
|
|
/** Sets the maximum number of rows to fetch. |
| 180 |
|
|
*/ |
| 181 |
|
✗ |
void MessageModel::setRowLimit(int limit /**< Maximum number of rows. */) |
| 182 |
|
|
{ |
| 183 |
|
✗ |
impl->rowLimit = limit; |
| 184 |
|
|
} |
| 185 |
|
|
|
| 186 |
|
|
/****************************************************************************/ |
| 187 |
|
|
|
| 188 |
|
|
/** Gets the maxium number of rows to fetch. |
| 189 |
|
|
*/ |
| 190 |
|
✗ |
int MessageModel::getRowLimit() const |
| 191 |
|
|
{ |
| 192 |
|
✗ |
return impl->rowLimit; |
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
/****************************************************************************/ |
| 196 |
|
|
|
| 197 |
|
|
/** Connects messages to the given process. |
| 198 |
|
|
*/ |
| 199 |
|
✗ |
void MessageModel::connect(QtPdCom::Process *process /**< PdCom process. */ |
| 200 |
|
|
) |
| 201 |
|
|
{ |
| 202 |
|
✗ |
if (impl->messageManager) { |
| 203 |
|
✗ |
disconnect( |
| 204 |
|
✗ |
impl->messageManager, |
| 205 |
|
|
&MessageManager::processMessageSignal, |
| 206 |
|
✗ |
impl.get(), |
| 207 |
|
|
&Impl::processMessage); |
| 208 |
|
✗ |
disconnect( |
| 209 |
|
|
process, |
| 210 |
|
|
&Process::processConnected, |
| 211 |
|
✗ |
impl.get(), |
| 212 |
|
|
&Impl::reloadActiveMessages); |
| 213 |
|
✗ |
disconnect( |
| 214 |
|
✗ |
impl->messageManager, |
| 215 |
|
|
&MessageManager::processResetSignal, |
| 216 |
|
✗ |
impl.get(), |
| 217 |
|
|
&Impl::processReset); |
| 218 |
|
|
} |
| 219 |
|
|
|
| 220 |
|
✗ |
impl->processReset(); |
| 221 |
|
✗ |
impl->messageManager = nullptr; |
| 222 |
|
✗ |
impl->process = process; |
| 223 |
|
|
|
| 224 |
|
✗ |
if (!process) { |
| 225 |
|
✗ |
return; |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
|
✗ |
impl->messageManager = |
| 229 |
|
✗ |
dynamic_cast<MessageManager *>(process->getMessageManager()); |
| 230 |
|
✗ |
if (impl->messageManager) { |
| 231 |
|
✗ |
QObject::connect( |
| 232 |
|
✗ |
impl->messageManager, |
| 233 |
|
|
&MessageManager::processMessageSignal, |
| 234 |
|
✗ |
impl.get(), |
| 235 |
|
|
&Impl::processMessage); |
| 236 |
|
✗ |
QObject::connect( |
| 237 |
|
|
process, |
| 238 |
|
|
&Process::processConnected, |
| 239 |
|
✗ |
impl.get(), |
| 240 |
|
|
&Impl::reloadActiveMessages); |
| 241 |
|
✗ |
QObject::connect( |
| 242 |
|
✗ |
impl->messageManager, |
| 243 |
|
|
&MessageManager::processResetSignal, |
| 244 |
|
✗ |
impl.get(), |
| 245 |
|
|
&Impl::processReset); |
| 246 |
|
✗ |
if (process->isConnected()) { |
| 247 |
|
✗ |
impl->reloadActiveMessages(); |
| 248 |
|
|
} |
| 249 |
|
|
} |
| 250 |
|
|
else { |
| 251 |
|
✗ |
qWarning() << QtPdCom::MessageModel::tr( |
| 252 |
|
✗ |
"Failed to connect to message manager."); |
| 253 |
|
|
} |
| 254 |
|
|
|
| 255 |
|
✗ |
for (auto h : impl->messageMap) { |
| 256 |
|
✗ |
Impl::MessageHash::iterator i; |
| 257 |
|
✗ |
for (i = h.begin(); i != h.end(); ++i) { |
| 258 |
|
✗ |
if (i.key() != -1) { /* FIXME vector messages */ |
| 259 |
|
✗ |
continue; |
| 260 |
|
|
} |
| 261 |
|
✗ |
Message *msg = i.value(); |
| 262 |
|
✗ |
PdCom::Selector selector; |
| 263 |
|
|
|
| 264 |
|
✗ |
if (msg->getIndex() > -1) { |
| 265 |
|
✗ |
selector = PdCom::ScalarSelector({msg->getIndex()}); |
| 266 |
|
|
} |
| 267 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 268 |
|
|
qDebug() << "Subscribing to" << msg->getPath() << msg->getIndex(); |
| 269 |
|
|
#endif |
| 270 |
|
|
try { |
| 271 |
|
✗ |
msg->impl->variable.setVariable( |
| 272 |
|
|
process, |
| 273 |
|
✗ |
msg->getPath(), |
| 274 |
|
✗ |
selector); |
| 275 |
|
|
} |
| 276 |
|
✗ |
catch (AbstractScalarVariable::Exception &e) { |
| 277 |
|
✗ |
qWarning() << QtPdCom::MessageModel::tr( |
| 278 |
|
|
"Failed to subscribe to %1: %2") |
| 279 |
|
✗ |
.arg(msg->getPath()) |
| 280 |
|
✗ |
.arg(e.msg); |
| 281 |
|
|
} |
| 282 |
|
|
} |
| 283 |
|
|
} |
| 284 |
|
|
} |
| 285 |
|
|
|
| 286 |
|
|
/****************************************************************************/ |
| 287 |
|
|
|
| 288 |
|
✗ |
QtPdCom::Process *MessageModel::getProcess() const |
| 289 |
|
|
{ |
| 290 |
|
✗ |
return impl->process; |
| 291 |
|
|
} |
| 292 |
|
|
|
| 293 |
|
|
/****************************************************************************/ |
| 294 |
|
|
|
| 295 |
|
|
/** Sets a new language and notifies views. |
| 296 |
|
|
*/ |
| 297 |
|
✗ |
void MessageModel::translate(const QString &lang) |
| 298 |
|
|
{ |
| 299 |
|
✗ |
impl->lang = lang; |
| 300 |
|
|
|
| 301 |
|
✗ |
for (int i = 0; i < impl->messageItemList.count(); i++) { |
| 302 |
|
✗ |
QModelIndex idx = index(i, 0); // only text column |
| 303 |
|
✗ |
emit dataChanged(idx, idx); |
| 304 |
|
|
} |
| 305 |
|
|
|
| 306 |
|
✗ |
if (impl->announcedMessageItem) { |
| 307 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 308 |
|
|
qDebug() << __func__ << "currentMessage" << impl->announcedMessageItem |
| 309 |
|
|
<< impl->announcedMessageItem->message; |
| 310 |
|
|
#endif |
| 311 |
|
✗ |
emit currentMessage(impl->announcedMessageItem->message); |
| 312 |
|
|
} |
| 313 |
|
|
} |
| 314 |
|
|
|
| 315 |
|
|
/****************************************************************************/ |
| 316 |
|
|
|
| 317 |
|
|
/** Sets an icon for a specific message type. |
| 318 |
|
|
*/ |
| 319 |
|
✗ |
void MessageModel::setIcon(Message::Type type, const QIcon &icon) |
| 320 |
|
|
{ |
| 321 |
|
✗ |
impl->iconHash[type] = icon; |
| 322 |
|
✗ |
if (rowCount({}) > 0) { |
| 323 |
|
✗ |
emit dataChanged( |
| 324 |
|
✗ |
index(0, TextColumn), |
| 325 |
|
✗ |
index(rowCount({}) - 1, TextColumn), |
| 326 |
|
|
{Qt::DecorationRole}); |
| 327 |
|
|
} |
| 328 |
|
|
} |
| 329 |
|
|
|
| 330 |
|
|
/****************************************************************************/ |
| 331 |
|
|
|
| 332 |
|
|
/** \return Icon for given message type. |
| 333 |
|
|
*/ |
| 334 |
|
✗ |
const QIcon &MessageModel::getIcon(Message::Type type) const |
| 335 |
|
|
{ |
| 336 |
|
✗ |
return impl->iconHash[type]; |
| 337 |
|
|
} |
| 338 |
|
|
|
| 339 |
|
|
/****************************************************************************/ |
| 340 |
|
|
|
| 341 |
|
|
/** Sets the path (url) to an icon for a specific message type. |
| 342 |
|
|
If the model is used with QML views this is the only way |
| 343 |
|
|
to store the icon information. |
| 344 |
|
|
*/ |
| 345 |
|
✗ |
void MessageModel::setIconPath(Message::Type type, const QString &iconPath) |
| 346 |
|
|
{ |
| 347 |
|
✗ |
impl->iconPathHash[type] = iconPath; |
| 348 |
|
|
// and also add it to the iconHash |
| 349 |
|
✗ |
setIcon(type, QIcon(iconPath)); |
| 350 |
|
|
/* vice versa would be nice, but does not work because |
| 351 |
|
|
to a QIcon the Path is not known */ |
| 352 |
|
|
} |
| 353 |
|
|
|
| 354 |
|
|
/****************************************************************************/ |
| 355 |
|
|
|
| 356 |
|
✗ |
QVariantMap QtPdCom::MessageModel::getIconPathMap() const |
| 357 |
|
|
{ |
| 358 |
|
✗ |
const auto metaEnum = QMetaEnum::fromType<Message::Type>(); |
| 359 |
|
✗ |
QVariantMap ans; |
| 360 |
|
✗ |
for (auto it = impl->iconPathHash.keyBegin(); |
| 361 |
|
✗ |
it != impl->iconPathHash.keyEnd(); |
| 362 |
|
|
++it) { |
| 363 |
|
✗ |
ans[QString(metaEnum.valueToKey(*it))] = impl->iconPathHash[*it]; |
| 364 |
|
|
} |
| 365 |
|
✗ |
return ans; |
| 366 |
|
|
} |
| 367 |
|
|
|
| 368 |
|
|
/****************************************************************************/ |
| 369 |
|
|
|
| 370 |
|
✗ |
void QtPdCom::MessageModel::setIconPathMap(QVariantMap const map) |
| 371 |
|
|
{ |
| 372 |
|
✗ |
const auto metaEnum = QMetaEnum::fromType<Message::Type>(); |
| 373 |
|
✗ |
bool ok = false; |
| 374 |
|
✗ |
for (auto it = map.keyBegin(); it != map.keyEnd(); ++it) { |
| 375 |
|
|
const auto key_enum = |
| 376 |
|
✗ |
metaEnum.keyToValue(it->toStdString().c_str(), &ok); |
| 377 |
|
✗ |
if (ok) { |
| 378 |
|
✗ |
setIconPath( |
| 379 |
|
|
static_cast<QtPdCom::Message::Type>(key_enum), |
| 380 |
|
✗ |
map[*it].toString()); |
| 381 |
|
|
} |
| 382 |
|
|
} |
| 383 |
|
|
} |
| 384 |
|
|
|
| 385 |
|
|
/****************************************************************************/ |
| 386 |
|
|
|
| 387 |
|
|
/** Implements the model interface. |
| 388 |
|
|
* |
| 389 |
|
|
* \returns Number of active messages. |
| 390 |
|
|
*/ |
| 391 |
|
✗ |
int MessageModel::rowCount(const QModelIndex &index) const |
| 392 |
|
|
{ |
| 393 |
|
✗ |
if (!index.isValid()) { |
| 394 |
|
✗ |
return impl->messageItemList.count(); |
| 395 |
|
|
} |
| 396 |
|
|
else { |
| 397 |
|
✗ |
return 0; |
| 398 |
|
|
} |
| 399 |
|
|
} |
| 400 |
|
|
|
| 401 |
|
|
/****************************************************************************/ |
| 402 |
|
|
|
| 403 |
|
|
/** Implements the model interface. |
| 404 |
|
|
* |
| 405 |
|
|
* \returns Number of columns. |
| 406 |
|
|
*/ |
| 407 |
|
✗ |
int MessageModel::columnCount(const QModelIndex &index) const |
| 408 |
|
|
{ |
| 409 |
|
|
Q_UNUSED(index); |
| 410 |
|
✗ |
return 3; |
| 411 |
|
|
} |
| 412 |
|
|
|
| 413 |
|
|
/****************************************************************************/ |
| 414 |
|
|
|
| 415 |
|
|
/** Implements the Model interface. |
| 416 |
|
|
*/ |
| 417 |
|
✗ |
QVariant MessageModel::data(const QModelIndex &index, int role) const |
| 418 |
|
|
{ |
| 419 |
|
✗ |
if (!index.isValid()) { |
| 420 |
|
✗ |
return QVariant(); |
| 421 |
|
|
} |
| 422 |
|
|
|
| 423 |
|
✗ |
auto msgItem = impl->messageItemList[index.row()]; |
| 424 |
|
|
|
| 425 |
|
✗ |
switch (index.column()) { |
| 426 |
|
✗ |
case TextColumn: // text |
| 427 |
|
✗ |
switch (role) { |
| 428 |
|
✗ |
case Qt::DisplayRole: |
| 429 |
|
✗ |
return msgItem->getText(impl->lang); |
| 430 |
|
✗ |
case Qt::DecorationRole: |
| 431 |
|
✗ |
return impl->iconHash[msgItem->getType()]; |
| 432 |
|
✗ |
case DecorationPathRole: |
| 433 |
|
✗ |
return impl->iconPathHash[msgItem->getType()]; |
| 434 |
|
✗ |
case TimeStringRole: |
| 435 |
|
|
// it is necessary to return other columns under |
| 436 |
|
|
// column 0 because in QML a View can't address |
| 437 |
|
|
// from column but only from roles |
| 438 |
|
✗ |
return msgItem->getTimeString(); |
| 439 |
|
✗ |
case ResetTimeStringRole: |
| 440 |
|
✗ |
return msgItem->getResetTimeString(); |
| 441 |
|
✗ |
case MessageTypeRole: |
| 442 |
|
✗ |
return msgItem->getType(); |
| 443 |
|
✗ |
case Qt::ToolTipRole: |
| 444 |
|
✗ |
return impl->wrapText( |
| 445 |
|
✗ |
msgItem->getDescription(impl->lang)); |
| 446 |
|
✗ |
default: |
| 447 |
|
✗ |
return QVariant(); |
| 448 |
|
|
} |
| 449 |
|
|
break; |
| 450 |
|
|
|
| 451 |
|
✗ |
case TimeOccurredColumn: // set time |
| 452 |
|
✗ |
switch (role) { |
| 453 |
|
✗ |
case Qt::DisplayRole: |
| 454 |
|
✗ |
return msgItem->getTimeString(); |
| 455 |
|
✗ |
case Qt::UserRole + 1: |
| 456 |
|
✗ |
return ""; |
| 457 |
|
✗ |
default: |
| 458 |
|
✗ |
return QVariant(); |
| 459 |
|
|
} |
| 460 |
|
|
break; |
| 461 |
|
|
|
| 462 |
|
✗ |
case TimeResetColumn: // reset time |
| 463 |
|
✗ |
switch (role) { |
| 464 |
|
✗ |
case Qt::DisplayRole: |
| 465 |
|
✗ |
return msgItem->getResetTimeString(); |
| 466 |
|
✗ |
case Qt::UserRole + 1: |
| 467 |
|
✗ |
return ""; |
| 468 |
|
✗ |
default: |
| 469 |
|
✗ |
return QVariant(); |
| 470 |
|
|
} |
| 471 |
|
|
break; |
| 472 |
|
|
|
| 473 |
|
✗ |
default: |
| 474 |
|
✗ |
return QVariant(); |
| 475 |
|
|
} |
| 476 |
|
|
} |
| 477 |
|
|
|
| 478 |
|
|
/****************************************************************************/ |
| 479 |
|
|
|
| 480 |
|
|
/** Implements the Model interface. |
| 481 |
|
|
*/ |
| 482 |
|
|
QVariant |
| 483 |
|
✗ |
MessageModel::headerData(int section, Qt::Orientation o, int role) const |
| 484 |
|
|
{ |
| 485 |
|
✗ |
if (role == Qt::DisplayRole && o == Qt::Horizontal) { |
| 486 |
|
✗ |
switch (section) { |
| 487 |
|
✗ |
case TextColumn: |
| 488 |
|
✗ |
return QtPdCom::MessageModel::tr("Message"); |
| 489 |
|
|
|
| 490 |
|
✗ |
case TimeOccurredColumn: |
| 491 |
|
✗ |
return QtPdCom::MessageModel::tr("Time"); |
| 492 |
|
|
|
| 493 |
|
✗ |
case TimeResetColumn: |
| 494 |
|
✗ |
return QtPdCom::MessageModel::tr("Reset"); |
| 495 |
|
|
|
| 496 |
|
✗ |
default: |
| 497 |
|
✗ |
return QVariant(); |
| 498 |
|
|
} |
| 499 |
|
|
} |
| 500 |
|
|
else { |
| 501 |
|
✗ |
return QVariant(); |
| 502 |
|
|
} |
| 503 |
|
|
} |
| 504 |
|
|
|
| 505 |
|
|
/****************************************************************************/ |
| 506 |
|
|
|
| 507 |
|
|
/** Implements the Model interface. |
| 508 |
|
|
*/ |
| 509 |
|
✗ |
Qt::ItemFlags MessageModel::flags(const QModelIndex &index) const |
| 510 |
|
|
{ |
| 511 |
|
✗ |
if (!index.isValid()) { |
| 512 |
|
✗ |
return Qt::ItemFlags(); |
| 513 |
|
|
} |
| 514 |
|
|
|
| 515 |
|
✗ |
auto msgItem {impl->messageItemList[index.row()]}; |
| 516 |
|
✗ |
return msgItem->isActive() ? Qt::ItemIsEnabled : Qt::ItemFlags(); |
| 517 |
|
|
} |
| 518 |
|
|
|
| 519 |
|
|
/****************************************************************************/ |
| 520 |
|
|
|
| 521 |
|
|
/** Additional Rolename for decoration for use in QML views |
| 522 |
|
|
*/ |
| 523 |
|
|
|
| 524 |
|
✗ |
QHash<int, QByteArray> MessageModel::roleNames() const |
| 525 |
|
|
{ |
| 526 |
|
|
// default role names |
| 527 |
|
✗ |
QHash<int, QByteArray> roles = QAbstractTableModel::roleNames(); |
| 528 |
|
|
|
| 529 |
|
|
// extended role names |
| 530 |
|
✗ |
roles[DecorationPathRole] = "decorationPath"; |
| 531 |
|
✗ |
roles[TimeStringRole] = "timeString"; |
| 532 |
|
✗ |
roles[ResetTimeStringRole] = "resetTimeString"; |
| 533 |
|
✗ |
roles[MessageTypeRole] = "messageTyp"; |
| 534 |
|
✗ |
return roles; |
| 535 |
|
|
} |
| 536 |
|
|
|
| 537 |
|
|
/****************************************************************************/ |
| 538 |
|
|
|
| 539 |
|
✗ |
void MessageModel::fetchMore(const QModelIndex &parent) |
| 540 |
|
|
{ |
| 541 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 542 |
|
|
qDebug() << __func__ << parent; |
| 543 |
|
|
#endif |
| 544 |
|
✗ |
if (canFetchMore(parent)) { |
| 545 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 546 |
|
|
qDebug() << __func__ << parent << "fetching"; |
| 547 |
|
|
#endif |
| 548 |
|
✗ |
impl->getHistoryMessage(); |
| 549 |
|
|
} |
| 550 |
|
|
} |
| 551 |
|
|
|
| 552 |
|
|
/****************************************************************************/ |
| 553 |
|
|
|
| 554 |
|
✗ |
bool MessageModel::canFetchMore(const QModelIndex &parent) const |
| 555 |
|
|
{ |
| 556 |
|
✗ |
bool ret = !parent.isValid() and impl->canFetchMore |
| 557 |
|
✗ |
and impl->historicSeqNo |
| 558 |
|
✗ |
and impl->messageItemList.count() < impl->rowLimit; |
| 559 |
|
|
#if PD_DEBUG_MESSAGE_MODEL |
| 560 |
|
|
qDebug() << __func__ << parent << "can" << impl->canFetchMore << "hist" |
| 561 |
|
|
<< impl->historicSeqNo << "space" |
| 562 |
|
|
<< (impl->messageItemList.count() < impl->rowLimit) << "ret" |
| 563 |
|
|
<< ret; |
| 564 |
|
|
#endif |
| 565 |
|
✗ |
return ret; |
| 566 |
|
|
} |
| 567 |
|
|
|
| 568 |
|
|
/****************************************************************************/ |
| 569 |
|
|
|
| 570 |
|
|
/** Event handler. |
| 571 |
|
|
*/ |
| 572 |
|
✗ |
bool MessageModel::event(QEvent *event /**< Paint event flags. */ |
| 573 |
|
|
) |
| 574 |
|
|
{ |
| 575 |
|
✗ |
if (event->type() == QEvent::LanguageChange) { |
| 576 |
|
✗ |
emit headerDataChanged(Qt::Horizontal, 0, 1); |
| 577 |
|
|
} |
| 578 |
|
|
|
| 579 |
|
✗ |
return QAbstractTableModel::event(event); |
| 580 |
|
|
} |
| 581 |
|
|
|
| 582 |
|
|
/****************************************************************************/ |
| 583 |
|
|
|