Skip to content

Commit

Permalink
Add highestPriorityType property to NotificationSortFilterProxyModel
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTrahearn-Qinetic committed Dec 23, 2024
1 parent 6869e62 commit bc325b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
51 changes: 45 additions & 6 deletions src/notificationsortfilterproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QQmlContext>

#include "notificationsortfilterproxymodel.h"
#include "notificationsmodel.h"

namespace Victron {

Expand All @@ -28,20 +29,27 @@ NotificationSortFilterProxyModel::NotificationSortFilterProxyModel(QObject *pare

sort(0, Qt::AscendingOrder);

connect(this, &QSortFilterProxyModel::rowsInserted, this, &NotificationSortFilterProxyModel::countChanged);
connect(this, &QSortFilterProxyModel::rowsRemoved, this, &NotificationSortFilterProxyModel::countChanged);
connect(this, &QSortFilterProxyModel::modelReset, this, &NotificationSortFilterProxyModel::countChanged);
connect(this, &QSortFilterProxyModel::layoutChanged, this, &NotificationSortFilterProxyModel::countChanged);
connect(this, &QSortFilterProxyModel::rowsInserted, this, &NotificationSortFilterProxyModel::updateStats);
connect(this, &QSortFilterProxyModel::rowsRemoved, this, &NotificationSortFilterProxyModel::updateStats);
connect(this, &QSortFilterProxyModel::modelReset, this, &NotificationSortFilterProxyModel::updateStats);
connect(this, &QSortFilterProxyModel::layoutChanged, this, &NotificationSortFilterProxyModel::updateStats);

updateStats();
}

NotificationSortFilterProxyModel::~NotificationSortFilterProxyModel()
{
delete d;
}

int NotificationSortFilterProxyModel::count(const QModelIndex &parent) const
int NotificationSortFilterProxyModel::count() const
{
return m_count;
}

Enums::Notification_Type NotificationSortFilterProxyModel::highestPriorityType() const
{
return rowCount(parent);
return m_highestPriorityType;
}

bool NotificationSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Expand Down Expand Up @@ -76,6 +84,37 @@ bool NotificationSortFilterProxyModel::lessThan(const QModelIndex &sourceLeft, c
return QSortFilterProxyModel::lessThan(sourceLeft, sourceRight);
}

void NotificationSortFilterProxyModel::updateStats()
{
// Note: the integer value of Enums::Notification_Type has no significance
Enums::Notification_Type highestPriorityType = Enums::Notification_Type::Notification_Info;
int count = rowCount();

for(int row = 0; row < rowCount(); row++) {
bool intOK = false;
Enums::Notification_Type type = static_cast<Enums::Notification_Type>(data(index(row, 0), NotificationsModel::NotificationRoles::Type).toInt(&intOK));
if(intOK) {
if(type == Enums::Notification_Type::Notification_Alarm) {
highestPriorityType = Enums::Notification_Type::Notification_Alarm;
break;
}
if(type == Enums::Notification_Type::Notification_Warning && highestPriorityType == Enums::Notification_Type::Notification_Info) {
highestPriorityType = Enums::Notification_Type::Notification_Warning;
}
}
}

if(highestPriorityType != m_highestPriorityType) {
m_highestPriorityType = highestPriorityType;
emit highestPriorityTypeChanged();
}

if(m_count != count) {
m_count = count;
emit countChanged();
}
}

QJSEngine * NotificationSortFilterProxyModel::getJSEngine() const
{
QQmlContext *context = QQmlEngine::contextForObject(this);
Expand Down
9 changes: 8 additions & 1 deletion src/notificationsortfilterproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QQmlEngine>
#include <QSortFilterProxyModel>
#include <QDateTime>
#include "enums.h"

namespace Victron {

Expand All @@ -23,14 +24,16 @@ class NotificationSortFilterProxyModel : public QSortFilterProxyModel
QML_ELEMENT

Q_PROPERTY(int count READ count NOTIFY countChanged FINAL)
Q_PROPERTY(Enums::Notification_Type highestPriorityType READ highestPriorityType NOTIFY highestPriorityTypeChanged FINAL)
Q_PROPERTY(QJSValue filterFunction READ filterFunction WRITE setFilterFunction NOTIFY filterFunctionChanged FINAL)
Q_PROPERTY(QJSValue sortFunction READ sortFunction WRITE setSortFunction NOTIFY sortFunctionChanged FINAL)

public:
explicit NotificationSortFilterProxyModel(QObject *parent = nullptr);
~NotificationSortFilterProxyModel();

int count(const QModelIndex& parent = QModelIndex()) const;
int count() const;
Enums::Notification_Type highestPriorityType() const;
QJSValue filterFunction() const;
void setFilterFunction(const QJSValue &callback);

Expand All @@ -39,6 +42,7 @@ class NotificationSortFilterProxyModel : public QSortFilterProxyModel

signals:
void countChanged();
void highestPriorityTypeChanged();
void filterFunctionChanged();
void sortFunctionChanged();

Expand All @@ -47,9 +51,12 @@ class NotificationSortFilterProxyModel : public QSortFilterProxyModel
bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override;

private:
void updateStats();
NotificationSortFilterProxyModelPrivate *d = nullptr;
QVariantMap get(int index) const;
QJSEngine *getJSEngine() const;
int m_count = 0;
Enums::Notification_Type m_highestPriorityType = Enums::Notification_Type::Notification_Info;
};

} /* VenusOS */
Expand Down

0 comments on commit bc325b0

Please sign in to comment.