Limit number of changes to keep
This commit is contained in:
parent
f9100bb0b4
commit
e832ccf51d
|
@ -9,19 +9,17 @@
|
|||
|
||||
#include <QStringBuilder>
|
||||
|
||||
#include <limits>
|
||||
|
||||
using namespace std;
|
||||
using namespace CppUtilities;
|
||||
|
||||
namespace Data {
|
||||
|
||||
SyncthingRecentChangesModel::SyncthingRecentChangesModel(SyncthingConnection &connection, QObject *parent)
|
||||
SyncthingRecentChangesModel::SyncthingRecentChangesModel(SyncthingConnection &connection, int maxRows, QObject *parent)
|
||||
: SyncthingModel(connection, parent)
|
||||
, m_maxRows(maxRows)
|
||||
{
|
||||
for (const auto &dir : connection.dirInfo()) {
|
||||
for (const auto &fileChange : dir.recentChanges) {
|
||||
fileChanged(dir, -1, fileChange);
|
||||
}
|
||||
}
|
||||
connect(&m_connection, &SyncthingConnection::fileChanged, this, &SyncthingRecentChangesModel::fileChanged);
|
||||
}
|
||||
|
||||
|
@ -91,7 +89,7 @@ QVariant SyncthingRecentChangesModel::data(const QModelIndex &index, int role) c
|
|||
return QVariant();
|
||||
}
|
||||
|
||||
const SyncthingRecentChange &change = m_changes[m_changes.size() - static_cast<size_t>(index.row()) - 1];
|
||||
const SyncthingRecentChange &change = m_changes[static_cast<size_t>(index.row())];
|
||||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
|
@ -184,7 +182,7 @@ void SyncthingRecentChangesModel::fileChanged(const SyncthingDir &dir, int index
|
|||
if (index >= 0) {
|
||||
beginInsertRows(QModelIndex(), 0, 0);
|
||||
}
|
||||
m_changes.emplace_back(SyncthingRecentChange{
|
||||
m_changes.emplace_front(SyncthingRecentChange{
|
||||
.directoryId = dir.id,
|
||||
.directoryName = dir.displayName(),
|
||||
.fileChange = change,
|
||||
|
@ -192,6 +190,8 @@ void SyncthingRecentChangesModel::fileChanged(const SyncthingDir &dir, int index
|
|||
if (index >= 0) {
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
ensureWithinLimit();
|
||||
}
|
||||
|
||||
void SyncthingRecentChangesModel::handleConfigInvalidated()
|
||||
|
@ -202,4 +202,20 @@ void SyncthingRecentChangesModel::handleNewConfigAvailable()
|
|||
{
|
||||
}
|
||||
|
||||
void SyncthingRecentChangesModel::setMaxRows(int maxRows)
|
||||
{
|
||||
m_maxRows = maxRows < 0 ? std::numeric_limits<int>::max() : maxRows;
|
||||
}
|
||||
|
||||
void SyncthingRecentChangesModel::ensureWithinLimit()
|
||||
{
|
||||
const auto rowsToDelete = static_cast<int>(m_changes.size()) - m_maxRows;
|
||||
if (rowsToDelete <= 0) {
|
||||
return;
|
||||
}
|
||||
beginRemoveRows(QModelIndex(), m_maxRows, static_cast<int>(m_changes.size()) - 1);
|
||||
m_changes.erase(m_changes.begin() + m_maxRows, m_changes.end());
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
} // namespace Data
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "../connector/syncthingdir.h"
|
||||
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
|
||||
namespace Data {
|
||||
|
||||
|
@ -17,6 +17,7 @@ struct LIB_SYNCTHING_MODEL_EXPORT SyncthingRecentChange {
|
|||
|
||||
class LIB_SYNCTHING_MODEL_EXPORT SyncthingRecentChangesModel : public SyncthingModel {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int maxRows READ maxRows WRITE setMaxRows)
|
||||
public:
|
||||
enum SyncthingRecentChangesModelRole {
|
||||
Action = Qt::UserRole + 1,
|
||||
|
@ -29,8 +30,7 @@ public:
|
|||
ExtendedAction,
|
||||
ItemType,
|
||||
};
|
||||
|
||||
explicit SyncthingRecentChangesModel(SyncthingConnection &connection, QObject *parent = nullptr);
|
||||
explicit SyncthingRecentChangesModel(SyncthingConnection &connection, int maxRows = 200, QObject *parent = nullptr);
|
||||
|
||||
public Q_SLOTS:
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
@ -42,6 +42,8 @@ public Q_SLOTS:
|
|||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
int rowCount(const QModelIndex &parent) const override;
|
||||
int columnCount(const QModelIndex &parent) const override;
|
||||
int maxRows() const;
|
||||
void setMaxRows(int maxRows);
|
||||
|
||||
private Q_SLOTS:
|
||||
void fileChanged(const SyncthingDir &dir, int index, const SyncthingFileChange &change);
|
||||
|
@ -49,9 +51,17 @@ private Q_SLOTS:
|
|||
void handleNewConfigAvailable() override;
|
||||
|
||||
private:
|
||||
std::vector<SyncthingRecentChange> m_changes;
|
||||
void ensureWithinLimit();
|
||||
|
||||
std::deque<SyncthingRecentChange> m_changes;
|
||||
int m_maxRows;
|
||||
};
|
||||
|
||||
inline int SyncthingRecentChangesModel::maxRows() const
|
||||
{
|
||||
return m_maxRows;
|
||||
}
|
||||
|
||||
} // namespace Data
|
||||
|
||||
Q_DECLARE_METATYPE(Data::SyncthingRecentChange)
|
||||
|
|
Loading…
Reference in New Issue