/
alexashkin
/
telegraph
Обзор
Документация
Войти
/
alexashkin
/
telegraph
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/data/chatlistsortedmodel.cpp
189 строк
6 KB
mbarashkov
Отрефакторил массив options у папки чатов
14 фев 2025, 19:10
14 фев 2025, 19:10
5761819
Код
Авторство
О чём код?
#include "chatlistsortedmodel.h" #include "chatlistmodel.h" #include "../debuglog.h" #include <iostream> #include <QString> #include "chatfoldersmodel.h" ChatListSortedModel::ChatListSortedModel(QObject *parent) : QSortFilterProxyModel(parent) { 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; } bool ChatListSortedModel::isPinnedChat(qlonglong chatId) { auto chatFoldersModel = ChatFoldersModel::instance; auto folder = chatFoldersModel->findFolder(filterRegExp().pattern()); if(folder != Q_NULLPTR) { auto pinned_id_list = folder->getPinnedChatIds(); if (pinned_id_list.contains(chatId)) { return true; } } auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); if (chatListSourceModel->getById(chatId).value("is_pinned").toBool()){ return true; } return false; } 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 chatFoldersModel = ChatFoldersModel::instance; auto folder = chatFoldersModel->findFolder(filterRegExp().pattern()); auto chatListSourceModel = reinterpret_cast<ChatListModel*>(sourceModel()); auto dataLeft = chatListSourceModel->getChatData(source_left.row()); auto dataRight = chatListSourceModel->getChatData(source_right.row()); if(folder != Q_NULLPTR) { if(folder->getType() == ChatFolderModel::Type::AllChats) { 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; } if(folder->getType() == ChatFolderModel::Archive) { return dataLeft->compareTo(dataRight) == 1; } 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 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 chatFoldersModel = ChatFoldersModel::instance; auto folder = chatFoldersModel->findFolder(filterRegExp().pattern()); if(folder == Q_NULLPTR) { return true; } QModelIndex index = reinterpret_cast<ChatListModel*>(sourceModel())->index(source_row, 0, source_parent); if (folder->getType() == ChatFolderModel::Type::AllChats) { // Поменять это на признак "папка всех чатов" if (index.data(ChatListModel::RoleIsArchived).toBool()) { return false; } return true; } if (folder->getType() == ChatFolderModel::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; }