Skip to content

Commit 8fb3405

Browse files
committed
Fix handling of row count changes in device model
1 parent 49e8986 commit 8fb3405

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

syncthingmodel/syncthingdevicemodel.cpp

+57-9
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ using namespace CppUtilities;
1414

1515
namespace Data {
1616

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+
1723
SyncthingDeviceModel::SyncthingDeviceModel(SyncthingConnection &connection, QObject *parent)
1824
: SyncthingModel(connection, parent)
1925
, m_devs(connection.devInfo())
2026
{
27+
updateRowCount();
2128
connect(&m_connection, &SyncthingConnection::devStatusChanged, this, &SyncthingDeviceModel::devStatusChanged);
2229
}
2330

@@ -329,10 +336,8 @@ int SyncthingDeviceModel::rowCount(const QModelIndex &parent) const
329336
{
330337
if (!parent.isValid()) {
331338
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())];
336341
} else {
337342
return 0;
338343
}
@@ -349,19 +354,53 @@ int SyncthingDeviceModel::columnCount(const QModelIndex &parent) const
349354
}
350355
}
351356

352-
void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &, int index)
357+
void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &dev, int index)
353358
{
359+
if (index < 0 || static_cast<std::size_t>(index) >= m_rowCount.size()) {
360+
return;
361+
}
362+
363+
// update top-level indices
354364
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,
356366
DeviceStatusColor, DeviceId, IsThisDevice, IsPinned });
357367
emit dataChanged(modelIndex1, modelIndex1, modelRoles1);
358368
const QModelIndex modelIndex2(this->index(index, 1, QModelIndex()));
359369
static const QVector<int> modelRoles2({ Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole });
360370
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
361389
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();
365404
}
366405

367406
void SyncthingDeviceModel::handleStatusIconsChanged()
@@ -396,4 +435,13 @@ QVariant SyncthingDeviceModel::devStatusColor(const SyncthingDev &dev) const
396435
return QVariant();
397436
}
398437

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+
399447
} // namespace Data

syncthingmodel/syncthingdevicemodel.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ class LIB_SYNCTHING_MODEL_EXPORT SyncthingDeviceModel : public SyncthingModel {
4242

4343
private Q_SLOTS:
4444
void devStatusChanged(const Data::SyncthingDev &, int index);
45+
void handleConfigInvalidated() override;
46+
void handleNewConfigAvailable() override;
4547
void handleStatusIconsChanged() override;
4648
void handleForkAwesomeIconsChanged() override;
4749

4850
private:
4951
QVariant devStatusColor(const SyncthingDev &dev) const;
52+
void updateRowCount();
5053

5154
const std::vector<SyncthingDev> &m_devs;
55+
std::vector<int> m_rowCount;
5256
};
5357

5458
inline const SyncthingDev *SyncthingDeviceModel::info(const QModelIndex &index) const

syncthingmodel/syncthingdirectorymodel.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ int SyncthingDirectoryModel::rowCount(const QModelIndex &parent) const
376376
{
377377
if (!parent.isValid()) {
378378
return static_cast<int>(m_dirs.size());
379-
} else if (!parent.parent().isValid() && static_cast<size_t>(parent.row()) < m_rowCount.size()) {
380-
return m_rowCount[static_cast<size_t>(parent.row())];
379+
} else if (!parent.parent().isValid() && static_cast<std::size_t>(parent.row()) < m_rowCount.size()) {
380+
return m_rowCount[static_cast<std::size_t>(parent.row())];
381381
} else {
382382
return 0;
383383
}
@@ -396,11 +396,11 @@ int SyncthingDirectoryModel::columnCount(const QModelIndex &parent) const
396396

397397
void SyncthingDirectoryModel::dirStatusChanged(const SyncthingDir &dir, int index)
398398
{
399-
if (index < 0 || static_cast<size_t>(index) >= m_rowCount.size()) {
399+
if (index < 0 || static_cast<std::size_t>(index) >= m_rowCount.size()) {
400400
return;
401401
}
402402

403-
// update top-level indizes
403+
// update top-level indices
404404
const QModelIndex modelIndex1(this->index(index, 0, QModelIndex()));
405405
static const QVector<int> modelRoles1({ Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, DirectoryPaused, DirectoryStatus,
406406
DirectoryStatusString, DirectoryStatusColor, DirectoryId, DirectoryPath, DirectoryPullErrorCount });
@@ -410,18 +410,18 @@ void SyncthingDirectoryModel::dirStatusChanged(const SyncthingDir &dir, int inde
410410
emit dataChanged(modelIndex2, modelIndex2, modelRoles2);
411411

412412
// remove/insert detail rows
413-
const auto oldRowCount = m_rowCount[static_cast<size_t>(index)];
413+
const auto oldRowCount = m_rowCount[static_cast<std::size_t>(index)];
414414
const auto newRowCount = computeDirectoryRowCount(dir);
415415
const auto newLastRow = newRowCount - 1;
416416
if (oldRowCount > newRowCount) {
417417
// begin removing rows for statistics
418418
beginRemoveRows(modelIndex1, 2, 3);
419-
m_rowCount[static_cast<size_t>(index)] = newRowCount;
419+
m_rowCount[static_cast<std::size_t>(index)] = newRowCount;
420420
endRemoveRows();
421421
} else if (newRowCount > oldRowCount) {
422422
// begin inserting rows for statistics
423423
beginInsertRows(modelIndex1, 2, 3);
424-
m_rowCount[static_cast<size_t>(index)] = newRowCount;
424+
m_rowCount[static_cast<std::size_t>(index)] = newRowCount;
425425
endInsertRows();
426426
}
427427

tray/gui/qml/DetailsListView.qml

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import Main
66
ListView {
77
id: detailsView
88
Layout.fillWidth: true
9+
Layout.preferredHeight: height
910
visible: false
1011
interactive: false
1112

1213
required property ItemDelegate mainDelegate
1314
property ListView mainView: mainDelegate.mainView
1415

1516
onCountChanged: {
16-
var d = delegate.createObject(detailsView, {detailName: "", detailValue: ""});
17-
height = count * d.height
18-
d.destroy()
17+
if (mainView.mainModel.toString().includes("DeviceModel")) {
18+
console.log("model: " + detailsView.count.toString());
19+
}
20+
const d = detailsView.delegate.createObject(detailsView, {detailName: "", detailValue: ""});
21+
detailsView.height = detailsView.count * d.height;
22+
d.destroy();
1923
}
2024

2125
model: DelegateModel {

0 commit comments

Comments
 (0)