@@ -14,10 +14,17 @@ using namespace CppUtilities;
14
14
15
15
namespace Data {
16
16
17
+ static int computeDeviceRowCount (const SyncthingDev &dev)
18
+ {
19
+ // hide connection type, last seen and everything after introducer (eg. traffic) unless connected
20
+ return dev.isConnected () ? 10 : 5 ;
21
+ }
22
+
17
23
SyncthingDeviceModel::SyncthingDeviceModel (SyncthingConnection &connection, QObject *parent)
18
24
: SyncthingModel(connection, parent)
19
25
, m_devs(connection.devInfo())
20
26
{
27
+ updateRowCount ();
21
28
connect (&m_connection, &SyncthingConnection::devStatusChanged, this , &SyncthingDeviceModel::devStatusChanged);
22
29
}
23
30
@@ -329,10 +336,8 @@ int SyncthingDeviceModel::rowCount(const QModelIndex &parent) const
329
336
{
330
337
if (!parent.isValid ()) {
331
338
return static_cast <int >(m_devs.size ());
332
- } else if (!parent.parent ().isValid ()) {
333
- // hide connection type, last seen and everything after introducer (eg. traffic) unless connected
334
- const auto *const dev (devInfo (parent));
335
- return dev && dev->isConnected () ? 10 : 5 ;
339
+ } else if (!parent.parent ().isValid () && static_cast <std::size_t >(parent.row ()) < m_rowCount.size ()) {
340
+ return m_rowCount[static_cast <std::size_t >(parent.row ())];
336
341
} else {
337
342
return 0 ;
338
343
}
@@ -349,19 +354,53 @@ int SyncthingDeviceModel::columnCount(const QModelIndex &parent) const
349
354
}
350
355
}
351
356
352
- void SyncthingDeviceModel::devStatusChanged (const SyncthingDev &, int index)
357
+ void SyncthingDeviceModel::devStatusChanged (const SyncthingDev &dev , int index)
353
358
{
359
+ if (index < 0 || static_cast <std::size_t >(index ) >= m_rowCount.size ()) {
360
+ return ;
361
+ }
362
+
363
+ // update top-level indices
354
364
const QModelIndex modelIndex1 (this ->index (index , 0 , QModelIndex ()));
355
- static const QVector<int > modelRoles1 ({ Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, DevicePaused, DeviceStatus, DeviceStatusString,
365
+ static const QVector<int > modelRoles1 ({ Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, Qt::ForegroundRole, DevicePaused, DeviceStatus, DeviceStatusString,
356
366
DeviceStatusColor, DeviceId, IsThisDevice, IsPinned });
357
367
emit dataChanged (modelIndex1, modelIndex1, modelRoles1);
358
368
const QModelIndex modelIndex2 (this ->index (index , 1 , QModelIndex ()));
359
369
static const QVector<int > modelRoles2 ({ Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole });
360
370
emit dataChanged (modelIndex2, modelIndex2, modelRoles2);
371
+
372
+ // remove/insert detail rows
373
+ const auto oldRowCount = m_rowCount[static_cast <std::size_t >(index )];
374
+ const auto newRowCount = computeDeviceRowCount (dev);
375
+ const auto newLastRow = newRowCount - 1 ;
376
+ if (oldRowCount > newRowCount) {
377
+ // begin removing rows for statistics
378
+ beginRemoveRows (modelIndex1, 2 , 3 );
379
+ m_rowCount[static_cast <std::size_t >(index )] = newRowCount;
380
+ endRemoveRows ();
381
+ } else if (newRowCount > oldRowCount) {
382
+ // begin inserting rows for statistics
383
+ beginInsertRows (modelIndex1, 2 , 3 );
384
+ m_rowCount[static_cast <std::size_t >(index )] = newRowCount;
385
+ endInsertRows ();
386
+ }
387
+
388
+ // update detail rows
361
389
static const QVector<int > modelRoles3 ({ Qt::DisplayRole, Qt::EditRole, Qt::ToolTipRole });
362
- emit dataChanged (this ->index (0 , 1 , modelIndex1), this ->index (5 , 1 , modelIndex1), modelRoles3);
363
- static const QVector<int > modelRoles4 ({ Qt::DisplayRole, Qt::EditRole, DeviceDetail });
364
- emit dataChanged (this ->index (0 , 0 , modelIndex1), this ->index (5 , 0 , modelIndex1), modelRoles4);
390
+ emit dataChanged (this ->index (0 , 1 , modelIndex1), this ->index (newLastRow, 1 , modelIndex1), modelRoles3);
391
+ static const QVector<int > modelRoles4 ({ Qt::DisplayRole, Qt::EditRole, DeviceDetail, DeviceDetailIcon });
392
+ emit dataChanged (this ->index (0 , 0 , modelIndex1), this ->index (newLastRow, 0 , modelIndex1), modelRoles4);
393
+ }
394
+
395
+ void SyncthingDeviceModel::handleConfigInvalidated ()
396
+ {
397
+ beginResetModel ();
398
+ }
399
+
400
+ void SyncthingDeviceModel::handleNewConfigAvailable ()
401
+ {
402
+ updateRowCount ();
403
+ endResetModel ();
365
404
}
366
405
367
406
void SyncthingDeviceModel::handleStatusIconsChanged ()
@@ -396,4 +435,13 @@ QVariant SyncthingDeviceModel::devStatusColor(const SyncthingDev &dev) const
396
435
return QVariant ();
397
436
}
398
437
438
+ void SyncthingDeviceModel::updateRowCount ()
439
+ {
440
+ m_rowCount.clear ();
441
+ m_rowCount.reserve (m_devs.size ());
442
+ for (const auto &dev : m_devs) {
443
+ m_rowCount.emplace_back (computeDeviceRowCount (dev));
444
+ }
445
+ }
446
+
399
447
} // namespace Data
0 commit comments