/
NeoChapay
/
telegraf
Обзор
Документация
Войти
/
NeoChapay
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/chatlistsortedmodel.cpp
216 строк
8 KB
Nikolay Sinyov
Добавил отображение прикрепленных чатов в папках (кроме Архива)
10 ноя 2024, 02:19
10 ноя 2024, 02:19
5c90f7d
Код
Авторство
О чём код?
#include "chatlistsortedmodel.h" #include "chatlistmodel.h" #define DEBUG_MODULE ChatListSortingModel #include "debuglog.h" #include <iostream> #include <QString> ChatListSortedModel::ChatListSortedModel(QObject *parent) : QSortFilterProxyModel(parent) { //connect(this, &QAbstractItemModel::rowsInserted, this, &ChatListSortedModel::countChanged); //connect(this, &QAbstractItemModel::rowsRemoved, this, &ChatListSortedModel::countChanged); //connect(this, &QAbstractItemModel::columnsRemoved, this, &ChatListSortedModel::countChanged); //connect(this, &QAbstractItemModel::columnsInserted, this, &ChatListSortedModel::countChanged); //connect(this, &QAbstractItemModel::modelReset, this, &ChatListSortedModel::countChanged); //connect(this, &QAbstractItemModel::layoutChanged, this, &ChatListSortedModel::countChanged); setSortRole(ChatListModel::RoleLastMessageDate); setDynamicSortFilter(true); } void ChatListSortedModel::setSource(QObject *model) { setSourceModel(qobject_cast<QAbstractItemModel*>(model)); } void ChatListSortedModel::setSourceModel(QAbstractItemModel *model) { if (sourceModel() != model) { QSortFilterProxyModel::setSourceModel(model); emit sourceChanged(); } } qlonglong ChatListSortedModel::getFolderId() const { return selectedFolderID; } QString ChatListSortedModel::getFolderType() const { auto chatModel = reinterpret_cast<ChatListModel*>(sourceModel()); if(selectedFolderID == chatModel->getMainChatListId()) { return "chatListMain"; } if (selectedFolderID == chatModel->getArchiveChatListId()) { return "chatListArchive"; } return "chatListFolder"; } bool ChatListSortedModel::isPinnedChat(qlonglong chatId) { auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); auto pinned_id_list = chatListSourceModel->getChatFoldersListConditionIDs(filterRegExp().pattern(), "pinned_chat_ids"); if (!pinned_id_list.empty() && pinned_id_list.contains(chatId)) { return true; } else if (chatListSourceModel->getById(chatId).value("is_pinned").toBool()){ return true; } return false; } //void ChatListSortedModel::countChanged(const QModelIndex &parent, int first, int last) //{ //} qlonglong ChatListSortedModel::unreadCount() { qlonglong unreadCount = 0; for(int i = 0; i < rowCount(); ++i) { if(index(i,0).data(ChatListModel::RoleUnreadCount).toInt() > 0) { ++unreadCount; } } return unreadCount; } bool ChatListSortedModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); auto folder = filterRegExp().pattern(); qlonglong selectedFolderID = chatListSourceModel->getSelectedFolderId(folder); bool isLeftPinned; bool isRightPinned; auto pinned_id_list = chatListSourceModel->getChatFoldersListConditionIDs(folder, "pinned_chat_ids"); auto dataLeft = chatListSourceModel->getChatData(source_left.row()); auto dataRight = chatListSourceModel->getChatData(source_right.row()); auto isLeftFromMain = source_left.data(ChatListModel::RoleMainChatPositionId).toLongLong() == selectedFolderID; auto isRightFromMain = source_right.data(ChatListModel::RoleMainChatPositionId).toLongLong() == selectedFolderID; if (isLeftFromMain && isRightFromMain) { isLeftPinned = source_left.data(ChatListModel::RoleIsPinned).toBool(); isRightPinned = source_right.data(ChatListModel::RoleIsPinned).toBool(); if (isLeftPinned && isRightPinned) { return dataLeft->order < dataRight->order; } else if (!isLeftPinned && !isRightPinned) { return dataLeft->compareTo(dataRight) == 1; } else if (isLeftPinned) { return false; } return true; } //todo: add handler for archive when we will save pin condition if (source_left.data(ChatListModel::RoleArchiveChatPositionId).toLongLong() == selectedFolderID) { return dataLeft->compareTo(dataRight) == 1; } isLeftPinned = pinned_id_list.contains(dataLeft->chatId); isRightPinned = pinned_id_list.contains(dataRight->chatId); if(isLeftPinned && isRightPinned) { int lRow = qMin(pinned_id_list.indexOf(dataLeft->chatId), pinned_id_list.size()); int rRow = qMin(pinned_id_list.indexOf(dataRight->chatId), pinned_id_list.size()); if (lRow < rRow) return false; if (lRow > rRow) return true; } else if (!isLeftPinned && !isRightPinned) { return dataLeft->compareTo(dataRight) == 1; } else if (isLeftPinned) { return false; } return true; } bool ChatListSortedModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { auto folder = filterRegExp().pattern(); QModelIndex index = reinterpret_cast<ChatListModel*>(sourceModel())->index(source_row, 0, source_parent); auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); const_cast<ChatListSortedModel*>(this)->selectedFolderID = chatListSourceModel->getSelectedFolderId(folder); if (index.data(ChatListModel::RoleMainChatPositionId).toLongLong() == selectedFolderID) return true; if (index.data(ChatListModel::RoleArchiveChatPositionId).toLongLong() == selectedFolderID) { if (index.data(ChatListModel::RoleIsArchived).toBool()) { return true; } return false; } if (chatListSourceModel->isChatFolderCondition(folder,"exclude_archived")) { if (index.data(ChatListModel::RoleIsArchived).toBool()) { return false; } } if (chatListSourceModel->isChatFolderCondition(folder, "exclude_muted")) { if (index.data(ChatListModel::RoleIsMuted).toBool()) { return false; } } if (chatListSourceModel->isChatFolderCondition(folder, "exclude_read")) { if (!(index.data(ChatListModel::RoleUnreadCount).toInt() > 0)) { return false; } } if (chatListSourceModel->isChatFolderCondition(folder, "include_bots")) { if (index.data(ChatListModel::RoleIsBot).toBool()) { return true; } } if (chatListSourceModel->isChatFolderCondition(folder, "include_channels")) { if (index.data(ChatListModel::RoleIsChannel).toBool()) { return true; } } if (chatListSourceModel->isChatFolderCondition(folder, "include_contacts")) { if (index.data(ChatListModel::RoleIsPrivateChat).toBool()) { if (index.data(ChatListModel::RoleIsContact).toBool()){ return true; } } } if (chatListSourceModel->isChatFolderCondition(folder, "include_groups")) { if (index.data(ChatListModel::RoleIsGroup).toBool()) { return true; } } if (chatListSourceModel->isChatFolderCondition(folder, "include_non_contacts")) { if (index.data(ChatListModel::RoleIsPrivateChat).toBool()) { if (!index.data(ChatListModel::RoleIsContact).toBool() && !index.data(ChatListModel::RoleIsBot).toBool()){ return true; } } } auto excluded_id_list = chatListSourceModel->getChatFoldersListConditionIDs(folder, "excluded_chat_ids"); auto included_id_list = chatListSourceModel->getChatFoldersListConditionIDs(folder, "included_chat_ids"); auto pinned_id_list = chatListSourceModel->getChatFoldersListConditionIDs(folder, "pinned_chat_ids"); qlonglong dataID = index.data(ChatListModel::RoleChatId).toLongLong(); if ((!included_id_list.contains(dataID) && !pinned_id_list.contains(dataID)) || excluded_id_list.contains(dataID)) return false; return true; }