File indexing completed on 2024-12-22 04:45:37

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "moderationreportinfolistview.h"
0008 #include "model/moderationreportinfomodel.h"
0009 #include "moderationreportinfodelegate.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QMenu>
0014 #include <QMouseEvent>
0015 
0016 #include "config-ruqola.h"
0017 
0018 ModerationReportInfoListView::ModerationReportInfoListView(RocketChatAccount *account, QWidget *parent)
0019     : MessageListViewBase(parent)
0020     , mModerationReportInfoDelegate(new ModerationReportInfoDelegate(account, this, this))
0021 {
0022     mModerationReportInfoDelegate->setObjectName(QStringLiteral("mModerationReportInfoDelegate"));
0023     setItemDelegate(mModerationReportInfoDelegate);
0024     setContextMenuPolicy(Qt::CustomContextMenu);
0025 
0026     connect(mModerationReportInfoDelegate, &ModerationReportInfoDelegate::updateView, this, [this](const QModelIndex &index) {
0027         update(index);
0028     });
0029     connect(this, &QListView::customContextMenuRequested, this, &ModerationReportInfoListView::slotCustomContextMenuRequested);
0030     connect(this, &ModerationReportInfoListView::needToClearSizeHintCache, mModerationReportInfoDelegate, &ModerationReportInfoDelegate::clearSizeHintCache);
0031 }
0032 
0033 ModerationReportInfoListView::~ModerationReportInfoListView() = default;
0034 
0035 bool ModerationReportInfoListView::maybeStartDrag(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0036 {
0037     return mModerationReportInfoDelegate->maybeStartDrag(event, option, index);
0038 }
0039 
0040 bool ModerationReportInfoListView::mouseEvent(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0041 {
0042     return mModerationReportInfoDelegate->mouseEvent(event, option, index);
0043 }
0044 
0045 void ModerationReportInfoListView::clearCache()
0046 {
0047     mModerationReportInfoDelegate->clearCache();
0048 }
0049 
0050 void ModerationReportInfoListView::slotSelectAll(const QModelIndex &index)
0051 {
0052     mModerationReportInfoDelegate->selectAll(listViewOptions(), index);
0053 }
0054 
0055 const QString &ModerationReportInfoListView::searchText() const
0056 {
0057     return mModerationReportInfoDelegate->searchText();
0058 }
0059 
0060 void ModerationReportInfoListView::setSearchText(const QString &newSearchText)
0061 {
0062     mModerationReportInfoDelegate->setSearchText(newSearchText);
0063 }
0064 
0065 QString ModerationReportInfoListView::selectedText() const
0066 {
0067     return mModerationReportInfoDelegate->selectedText();
0068 }
0069 
0070 void ModerationReportInfoListView::slotCustomContextMenuRequested(const QPoint &pos)
0071 {
0072     if (model()->rowCount() > 0) {
0073         QMenu menu(this);
0074         const QModelIndex index = indexAt(pos);
0075         if (index.isValid()) {
0076             auto copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Message"), &menu);
0077             copyAction->setShortcut(QKeySequence::Copy);
0078             connect(copyAction, &QAction::triggered, this, [this, index]() {
0079                 copyMessageToClipboard(index);
0080             });
0081             menu.addAction(copyAction);
0082             if (mModerationReportInfoDelegate->hasSelection()) {
0083                 addTextPlugins(&menu, mModerationReportInfoDelegate->selectedText());
0084             }
0085 #if HAVE_TEXT_TO_SPEECH
0086             menu.addSeparator();
0087             auto speakAction = menu.addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")), i18n("Speak Text"));
0088             connect(speakAction, &QAction::triggered, this, [this, index]() {
0089                 slotTextToSpeech(index);
0090             });
0091 #endif
0092             menu.addSeparator();
0093             menu.addAction(i18n("Select All"), this, [this, index]() {
0094                 slotSelectAll(index);
0095             });
0096         }
0097         menu.exec(viewport()->mapToGlobal(pos));
0098     }
0099 }
0100 
0101 void ModerationReportInfoListView::slotTextToSpeech(const QModelIndex &index)
0102 {
0103     const QString messageText = selectedText(index);
0104     if (!messageText.isEmpty()) {
0105         Q_EMIT textToSpeech(messageText);
0106     }
0107 }
0108 
0109 QString ModerationReportInfoListView::selectedText(const QModelIndex &index)
0110 {
0111     QString messageText = selectedText();
0112     if (messageText.isEmpty()) {
0113         if (!index.isValid()) {
0114             return {};
0115         }
0116         messageText = index.data(ModerationReportInfoModel::Message).toString();
0117     }
0118     return messageText;
0119 }
0120 
0121 #include "moc_moderationreportinfolistview.cpp"