/
s5z6
/
telegraf
Обзор
Документация
Войти
/
s5z6
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/data/chatlistsortedmodel.cpp
218 строк
7 KB
mbarashkov
Косметический рефакторинг
06 июл 2025, 16:19
06 июл 2025, 16:19
9e71852
Код
Авторство
О чём код?
#include "chatlistsortedmodel.h" #include "chatlistmodel.h" #include "../debuglog.h" #include <iostream> #include <QString> #include "chatfoldersmodel.h" ChatListSortedModel::ChatListSortedModel(QObject *parent) : QSortFilterProxyModel(parent), chatFolderId(new ChatFolderId(this)) { setSortRole(ChatListModel::RoleLastMessageDate); setDynamicSortFilter(true); } ChatFolderModel* ChatListSortedModel::getChatFolder() const { return ChatFoldersModel::getInstance()->findFolder(chatFolderId); } void ChatListSortedModel::setSource(QObject *model) { setSourceModel(qobject_cast<QAbstractItemModel*>(model)); } void ChatListSortedModel::setSourceModel(QAbstractItemModel *model) { if (sourceModel() != model) { QSortFilterProxyModel::setSourceModel(model); emit sourceChanged(); } } bool ChatListSortedModel::isPinnedChat(const ChatFolderId::Type folderType, const qlonglong folderId, qlonglong chatId) const { if(folderType == ChatFolderId::Type::AllChats || folderType == ChatFolderId::Type::Archive) { auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); return chatListSourceModel->isChatPinnedInMainList(chatId); } else { auto chatFolderId = ChatFolderId(folderType, folderId); auto folder = ChatFoldersModel::getInstance()->findFolder(&chatFolderId); if(folder != Q_NULLPTR) { auto pinned_id_list = folder->getPinnedChatIds(); if (pinned_id_list.contains(chatId)) { return true; } } return false; } } qlonglong ChatListSortedModel::unreadCount(const ChatFolderId::Type type, const qlonglong id) const { auto chatFolderId = ChatFolderId(type, id); auto folder = ChatFoldersModel::getInstance()->findFolder(&chatFolderId); if(folder == Q_NULLPTR) { return 0; } auto chatListModel = reinterpret_cast<ChatListModel*>(sourceModel()); qlonglong unreadCount = 0; for(int i = 0; i < chatListModel->rowCount(); i++) { auto rowIndex = chatListModel->index(i, 0); if(internalFilterRow(folder, rowIndex) && sourceModel()->data(rowIndex, ChatListModel::RoleUnreadCount).toInt() > 0) { unreadCount++; } } return unreadCount; } bool ChatListSortedModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { auto folder = getChatFolder(); auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); auto dataLeft = chatListSourceModel->getChatData(source_left.row()); auto dataRight = chatListSourceModel->getChatData(source_right.row()); if(folder != Q_NULLPTR) { auto folderType = folder->getType(); if(folderType == ChatFolderId::Type::AllChats || folderType == ChatFolderId::Type::Archive) { auto isLeftPinned = source_left.data(ChatListModel::RoleIsPinned).toBool(); auto 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; } else if(folderType == ChatFolderId::Type::Archive) { return dataLeft->compareTo(dataRight) == 1; } else { auto pinned_id_list = folder->getPinnedChatIds(); auto isLeftPinned = pinned_id_list.contains(dataLeft->chatId); auto isRightPinned = pinned_id_list.contains(dataRight->chatId); if(isLeftPinned && isRightPinned) { int lOrder = pinned_id_list.indexOf(dataLeft->chatId); int rOrder = pinned_id_list.indexOf(dataRight->chatId); return lOrder > rOrder; } else if (!isLeftPinned && !isRightPinned) { return dataLeft->compareTo(dataRight) == 1; } else if (isLeftPinned) { return false; } } } return true; } bool ChatListSortedModel::internalFilterRow(const ChatFolderModel* folder, const QModelIndex index) const { if (folder->getType() == ChatFolderId::Type::AllChats) { if (index.data(ChatListModel::RoleIsArchived).toBool()) { return false; } return true; } if (folder->getType() == ChatFolderId::Type::Archive) { if (index.data(ChatListModel::RoleIsArchived).toBool()) { return true; } return false; } if (folder->getOptionValue(ChatFolderModel::Option::ExcludeArchived)) { if (index.data(ChatListModel::RoleIsArchived).toBool()) { return false; } } if (folder->getOptionValue(ChatFolderModel::Option::ExcludeMuted)) { if (index.data(ChatListModel::RoleIsMuted).toBool()) { return false; } } if (folder->getOptionValue(ChatFolderModel::Option::ExcludeRead)) { if (!(index.data(ChatListModel::RoleUnreadCount).toInt() > 0)) { return false; } } if (folder->getOptionValue(ChatFolderModel::Option::IncludeBots)) { if (index.data(ChatListModel::RoleIsBot).toBool()) { return true; } } if (folder->getOptionValue(ChatFolderModel::Option::IncludeChannels)) { if (index.data(ChatListModel::RoleIsChannel).toBool()) { return true; } } if (folder->getOptionValue(ChatFolderModel::Option::IncludeContacts)) { if (index.data(ChatListModel::RoleIsPrivateChat).toBool() && index.data(ChatListModel::RoleIsContact).toBool()) { return true; } } if (folder->getOptionValue(ChatFolderModel::Option::IncludeGroups)) { if (index.data(ChatListModel::RoleIsGroup).toBool() && !index.data(ChatListModel::RoleIsChannel).toBool()) { return true; } } if (folder->getOptionValue(ChatFolderModel::Option::IncludeNonContacts)) { if (index.data(ChatListModel::RoleIsPrivateChat).toBool() && !index.data(ChatListModel::RoleIsContact).toBool() && !index.data(ChatListModel::RoleIsBot).toBool()) { return true; } } auto chatId = index.data(ChatListModel::RoleChatId).toLongLong(); auto isIncluded = folder->isIncluded(chatId); auto isPinned = folder->isPinned(chatId); auto isExcluded = folder->isExcluded(chatId); if((!isIncluded && !isPinned) || isExcluded) { return false; } return true; } bool ChatListSortedModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { auto folder = getChatFolder(); if(folder == Q_NULLPTR) { return true; } QModelIndex index = reinterpret_cast<ChatListModel*>(sourceModel())->index(source_row, 0, source_parent); return internalFilterRow(folder, index); }