ChatProxyModel.cpp 2.04 KB
Newer Older
1 2
#include "ChatProxyModel.hpp"

Ronan Abhamon's avatar
Ronan Abhamon committed
3
// =============================================================================
4

5 6 7
ChatModelFilter::ChatModelFilter (QObject *parent) : QSortFilterProxyModel(parent) {
  setSourceModel(&m_chat_model);
}
8

9 10 11
bool ChatModelFilter::filterAcceptsRow (int source_row, const QModelIndex &) const {
  if (m_entry_type_filter == ChatModel::EntryType::GenericEntry)
    return true;
Ronan Abhamon's avatar
Ronan Abhamon committed
12

13
  QModelIndex index = sourceModel()->index(source_row, 0, QModelIndex());
Ronan Abhamon's avatar
Ronan Abhamon committed
14
  const QVariantMap &data = index.data().toMap();
15 16

  return data["type"].toInt() == m_entry_type_filter;
17 18
}

19 20 21
void ChatModelFilter::setEntryTypeFilter (ChatModel::EntryType type) {
  m_entry_type_filter = type;
  invalidateFilter();
22 23
}

Ronan Abhamon's avatar
Ronan Abhamon committed
24
// =============================================================================
25

26
const unsigned int ChatProxyModel::ENTRIES_CHUNK_SIZE = 50;
27

28 29
ChatProxyModel::ChatProxyModel (QObject *parent) : QSortFilterProxyModel(parent) {
  setSourceModel(&m_chat_model_filter);
Ronan Abhamon's avatar
Ronan Abhamon committed
30
}
Ronan Abhamon's avatar
Ronan Abhamon committed
31

Ronan Abhamon's avatar
Ronan Abhamon committed
32
void ChatProxyModel::loadMoreEntries () {
33
  int count = rowCount();
34
  int parent_count = m_chat_model_filter.rowCount();
35

36 37 38 39 40
  if (count < parent_count) {
    // Do not increase `m_n_max_displayed_entries` if it's not necessary...
    // Limit qml calls.
    if (count == m_n_max_displayed_entries)
      m_n_max_displayed_entries += ENTRIES_CHUNK_SIZE;
41

42 43
    invalidateFilter();

44 45 46 47
    count = rowCount() - count;

    if (count > 0)
      emit moreEntriesLoaded(count);
48
  }
Ronan Abhamon's avatar
Ronan Abhamon committed
49 50
}

51 52 53 54 55 56 57
void ChatProxyModel::setEntryTypeFilter (ChatModel::EntryType type) {
  if (m_chat_model_filter.m_entry_type_filter != type) {
    m_chat_model_filter.setEntryTypeFilter(type);
    emit entryTypeFilterChanged(type);
  }
}

Ronan Abhamon's avatar
Ronan Abhamon committed
58 59 60
void ChatProxyModel::removeEntry (int id) {
  QModelIndex source_index = mapToSource(index(id, 0));
  static_cast<ChatModel *>(m_chat_model_filter.sourceModel())->removeEntry(
61
    m_chat_model_filter.mapToSource(source_index).row()
Ronan Abhamon's avatar
Ronan Abhamon committed
62
  );
63
}
64 65 66 67

bool ChatProxyModel::filterAcceptsRow (int source_row, const QModelIndex &) const {
  return m_chat_model_filter.rowCount() - source_row <= m_n_max_displayed_entries;
}