Commit a1fc6a63 authored by Ronan's avatar Ronan
Browse files

fix(src/components/chat): use two filters to get filtered messages and the last

parent 55a4e008
......@@ -35,7 +35,6 @@ set(SOURCES
src/app/DefaultTranslator.cpp
src/app/Logger.cpp
src/components/chat/ChatModel.cpp
src/components/chat/ChatModelFilter.cpp
src/components/chat/ChatProxyModel.cpp
src/components/contacts/ContactModel.cpp
src/components/contacts/ContactsListModel.cpp
......@@ -56,7 +55,6 @@ set(HEADERS
src/app/DefaultTranslator.hpp
src/app/Logger.hpp
src/components/chat/ChatModel.hpp
src/components/chat/ChatModelFilter.hpp
src/components/chat/ChatProxyModel.hpp
src/components/contacts/ContactModel.hpp
src/components/contacts/ContactsListModel.hpp
......
......@@ -148,9 +148,7 @@ void ChatModel::removeEntry (ChatEntryData &pair) {
}
);
if (it == m_entries.end())
qWarning("Unable to find symmetric call.");
else
if (it != m_entries.end())
removeEntry(distance(m_entries.begin(), it));
});
}
......
......@@ -4,6 +4,8 @@
#include <QAbstractListModel>
#include <linphone++/linphone.hh>
// ===================================================================
// Fetch all N messages of a ChatRoom.
// ===================================================================
class ChatModel : public QAbstractListModel {
......
#include "ChatProxyModel.hpp"
// ===================================================================
ChatModelFilter::ChatModelFilter (QObject *parent) : QSortFilterProxyModel(parent) {
setSourceModel(&m_chat_model);
}
bool ChatModelFilter::filterAcceptsRow (int source_row, const QModelIndex &source_parent) const {
if (m_entry_type_filter == ChatModel::EntryType::GenericEntry)
return true;
QModelIndex index = sourceModel()->index(source_row, 0, QModelIndex());
const QVariantMap &data = qvariant_cast<QVariantMap>(
index.data()
);
return data["type"].toInt() == m_entry_type_filter;
}
#ifndef CHAT_MODEL_FILTER_H_
#define CHAT_MODEL_FILTER_H_
#include <QSortFilterProxyModel>
#include "ChatModel.hpp"
#include <QtDebug>
// ===================================================================
// Fetch K filtered chat entries.
// ===================================================================
class ChatModelFilter : public QSortFilterProxyModel {
friend class ChatProxyModel;
Q_OBJECT;
public:
ChatModelFilter (QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow (int source_row, const QModelIndex &source_parent) const;
private:
void setEntryTypeFilter (ChatModel::EntryType type) {
m_entry_type_filter = type;
invalidateFilter();
}
ChatModel m_chat_model;
ChatModel::EntryType m_entry_type_filter = ChatModel::EntryType::GenericEntry;
};
#endif // CHAT_MODEL_FILTER_H_
#include "ChatProxyModel.hpp"
// ===================================================================
ChatModelFilter::ChatModelFilter (QObject *parent) : QSortFilterProxyModel(parent) {
setSourceModel(&m_chat_model);
}
const unsigned int ChatProxyModel::ENTRIES_CHUNK_SIZE = 25;
bool ChatModelFilter::filterAcceptsRow (int source_row, const QModelIndex &) const {
if (m_entry_type_filter == ChatModel::EntryType::GenericEntry)
return true;
ChatProxyModel::ChatProxyModel (QObject *parent) : QSortFilterProxyModel(parent) {
setSourceModel(&m_chat_model_filter);
QModelIndex index = sourceModel()->index(source_row, 0, QModelIndex());
const QVariantMap &data = qvariant_cast<QVariantMap>(
index.data()
);
return data["type"].toInt() == m_entry_type_filter;
}
int ChatProxyModel::rowCount (const QModelIndex &parent) const {
int size = QSortFilterProxyModel::rowCount(parent);
return size < m_n_max_displayed_entries ? size : m_n_max_displayed_entries;
void ChatModelFilter::setEntryTypeFilter (ChatModel::EntryType type) {
m_entry_type_filter = type;
invalidateFilter();
}
QVariant ChatProxyModel::data (const QModelIndex &index, int role) const {
QAbstractItemModel *model = sourceModel();
// ===================================================================
const unsigned int ChatProxyModel::ENTRIES_CHUNK_SIZE = 25;
return model->data(
model->index(
mapToSource(index).row() + (model->rowCount() - rowCount()),
0
),
role
);
ChatProxyModel::ChatProxyModel (QObject *parent) : QSortFilterProxyModel(parent) {
setSourceModel(&m_chat_model_filter);
}
void ChatProxyModel::loadMoreEntries () {
......@@ -31,8 +35,11 @@ void ChatProxyModel::loadMoreEntries () {
void ChatProxyModel::removeEntry (int id) {
QModelIndex source_index = mapToSource(index(id, 0));
static_cast<ChatModel *>(m_chat_model_filter.sourceModel())->removeEntry(
mapToSource(source_index).row()
m_chat_model_filter.mapToSource(source_index).row()
);
}
bool ChatProxyModel::filterAcceptsRow (int source_row, const QModelIndex &) const {
return m_chat_model_filter.rowCount() - source_row <= m_n_max_displayed_entries;
}
#ifndef CHAT_PROXY_MODEL_H_
#define CHAT_PROXY_MODEL_H_
#include "ChatModelFilter.hpp"
#include <QSortFilterProxyModel>
#include "ChatModel.hpp"
// ===================================================================
// Fetch the L last filtered chat entries.
// ===================================================================
// Cannot be used as a nested class by Qt and `Q_OBJECTY` macro
// must be used in header c++ file.
class ChatModelFilter : public QSortFilterProxyModel {
friend class ChatProxyModel;
Q_OBJECT;
public:
ChatModelFilter (QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow (int source_row, const QModelIndex &parent) const;
private:
void setEntryTypeFilter (ChatModel::EntryType type);
ChatModel m_chat_model;
ChatModel::EntryType m_entry_type_filter = ChatModel::EntryType::GenericEntry;
};
// ===================================================================
class ChatProxyModel : public QSortFilterProxyModel {
Q_OBJECT;
......@@ -23,9 +46,6 @@ signals:
public:
ChatProxyModel (QObject *parent = Q_NULLPTR);
int rowCount (const QModelIndex &parent = QModelIndex()) const override;
QVariant data (const QModelIndex &index, int role) const override;
public slots:
void loadMoreEntries ();
......@@ -39,6 +59,9 @@ public slots:
static_cast<ChatModel *>(m_chat_model_filter.sourceModel())->removeAllEntries();
}
protected:
bool filterAcceptsRow (int source_row, const QModelIndex &source_parent) const;
private:
QString getSipAddress () const {
static_cast<ChatModel *>(m_chat_model_filter.sourceModel())->getSipAddress();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment