File indexing completed on 2024-12-01 04:36:48

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "discussionlistview.h"
0008 #include "discussion/listdiscussiondelegate.h"
0009 #include "model/discussionsmodel.h"
0010 #include "rocketchataccount.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <QMenu>
0015 #include <QMouseEvent>
0016 
0017 #include "config-ruqola.h"
0018 
0019 DiscussionListView::DiscussionListView(RocketChatAccount *account, QWidget *parent)
0020     : MessageListViewBase(parent)
0021     , mListDiscussionDelegate(new ListDiscussionDelegate(this, account, this))
0022     , mRocketChatAccount(account)
0023 {
0024     setItemDelegate(mListDiscussionDelegate);
0025     connect(mListDiscussionDelegate, &ListDiscussionDelegate::openDiscussion, this, &DiscussionListView::slotOpenDiscussion);
0026     connect(mListDiscussionDelegate, &ListDiscussionDelegate::updateView, this, [this](const QModelIndex &index) {
0027         update(index);
0028     });
0029     setContextMenuPolicy(Qt::CustomContextMenu);
0030     connect(this, &QListView::customContextMenuRequested, this, &DiscussionListView::slotCustomContextMenuRequested);
0031     connect(this, &DiscussionListView::needToClearSizeHintCache, mListDiscussionDelegate, &ListDiscussionDelegate::clearSizeHintCache);
0032 }
0033 
0034 DiscussionListView::~DiscussionListView() = default;
0035 
0036 void DiscussionListView::setSearchText(const QString &str)
0037 {
0038     mListDiscussionDelegate->setSearchText(str);
0039 }
0040 
0041 bool DiscussionListView::maybeStartDrag(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0042 {
0043     return mListDiscussionDelegate->maybeStartDrag(event, option, index);
0044 }
0045 
0046 bool DiscussionListView::mouseEvent(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0047 {
0048     return mListDiscussionDelegate->mouseEvent(event, option, index);
0049 }
0050 
0051 void DiscussionListView::slotOpenDiscussion(const QString &roomDiscussionId)
0052 {
0053     if (mRocketChatAccount) {
0054         mRocketChatAccount->ddp()->openRoom(roomDiscussionId);
0055     }
0056 }
0057 
0058 void DiscussionListView::slotCustomContextMenuRequested(const QPoint &pos)
0059 {
0060     QMenu menu(this);
0061     const QModelIndex index = indexAt(pos);
0062     if (index.isValid()) {
0063         if (mListDiscussionDelegate->hasSelection()) {
0064             addTextPlugins(&menu, mListDiscussionDelegate->selectedText());
0065         }
0066 #if HAVE_TEXT_TO_SPEECH
0067         menu.addSeparator();
0068         auto speakAction = menu.addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")), i18n("Speak Text"));
0069         connect(speakAction, &QAction::triggered, this, [index, this]() {
0070             slotTextToSpeech(index);
0071         });
0072 #endif
0073         menu.addSeparator();
0074         menu.addAction(i18n("Select All"), this, [this, index]() {
0075             slotSelectAll(index);
0076         });
0077     }
0078     if (!menu.isEmpty()) {
0079         menu.exec(viewport()->mapToGlobal(pos));
0080     }
0081 }
0082 
0083 void DiscussionListView::slotTextToSpeech(const QModelIndex &index)
0084 {
0085     QString message = mListDiscussionDelegate->selectedText();
0086     if (message.isEmpty()) {
0087         message = index.data(DiscussionsModel::LastMessage).toString();
0088     }
0089     if (!message.isEmpty()) {
0090         Q_EMIT textToSpeech(message);
0091     }
0092 }
0093 
0094 void DiscussionListView::slotSelectAll(const QModelIndex &index)
0095 {
0096     mListDiscussionDelegate->selectAll(listViewOptions(), index);
0097 }
0098 
0099 #include "moc_discussionlistview.cpp"