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

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 "notificationhistorylistview.h"
0008 #include "model/notificationhistorymodel.h"
0009 #include "notificationhistorydelegate.h"
0010 #include "notificationhistorymanager.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <QMenu>
0015 #include <QMouseEvent>
0016 
0017 #include "config-ruqola.h"
0018 
0019 NotificationHistoryListView::NotificationHistoryListView(QWidget *parent)
0020     : MessageListViewBase(parent)
0021     , mListNotificationsDelegate(new NotificationHistoryDelegate(this, this))
0022 {
0023     mListNotificationsDelegate->setObjectName(QStringLiteral("listNotificationsDelegate"));
0024     setItemDelegate(mListNotificationsDelegate);
0025     setContextMenuPolicy(Qt::CustomContextMenu);
0026 
0027     connect(mListNotificationsDelegate, &NotificationHistoryDelegate::updateView, this, [this](const QModelIndex &index) {
0028         update(index);
0029     });
0030     connect(this, &QListView::customContextMenuRequested, this, &NotificationHistoryListView::slotCustomContextMenuRequested);
0031     connect(this, &NotificationHistoryListView::needToClearSizeHintCache, mListNotificationsDelegate, &NotificationHistoryDelegate::clearSizeHintCache);
0032 }
0033 
0034 NotificationHistoryListView::~NotificationHistoryListView() = default;
0035 
0036 bool NotificationHistoryListView::maybeStartDrag(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0037 {
0038     return mListNotificationsDelegate->maybeStartDrag(event, option, index);
0039 }
0040 
0041 bool NotificationHistoryListView::mouseEvent(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0042 {
0043     return mListNotificationsDelegate->mouseEvent(event, option, index);
0044 }
0045 
0046 void NotificationHistoryListView::clearCache()
0047 {
0048     mListNotificationsDelegate->clearCache();
0049 }
0050 
0051 void NotificationHistoryListView::slotSelectAll(const QModelIndex &index)
0052 {
0053     mListNotificationsDelegate->selectAll(listViewOptions(), index);
0054 }
0055 
0056 const QString &NotificationHistoryListView::searchText() const
0057 {
0058     return mListNotificationsDelegate->searchText();
0059 }
0060 
0061 void NotificationHistoryListView::setSearchText(const QString &newSearchText)
0062 {
0063     mListNotificationsDelegate->setSearchText(newSearchText);
0064 }
0065 
0066 QString NotificationHistoryListView::selectedText() const
0067 {
0068     return mListNotificationsDelegate->selectedText();
0069 }
0070 
0071 void NotificationHistoryListView::slotCustomContextMenuRequested(const QPoint &pos)
0072 {
0073     if (model()->rowCount() > 0) {
0074         QMenu menu(this);
0075         menu.addAction(QIcon::fromTheme(QStringLiteral("edit-clear-history")), i18n("Clear"), this, &NotificationHistoryListView::slotClearList);
0076         const QModelIndex index = indexAt(pos);
0077         if (index.isValid()) {
0078             menu.addSeparator();
0079             menu.addAction(i18n("Go to Message"), this, [this, index]() {
0080                 Q_EMIT showMessage(index);
0081             });
0082             menu.addSeparator();
0083             auto copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Message"), &menu);
0084             copyAction->setShortcut(QKeySequence::Copy);
0085             connect(copyAction, &QAction::triggered, this, [this, index]() {
0086                 copyMessageToClipboard(index);
0087             });
0088             menu.addAction(copyAction);
0089             if (mListNotificationsDelegate->hasSelection()) {
0090                 addTextPlugins(&menu, mListNotificationsDelegate->selectedText());
0091             }
0092 #if HAVE_TEXT_TO_SPEECH
0093             menu.addSeparator();
0094             auto speakAction = menu.addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")), i18n("Speak Text"));
0095             connect(speakAction, &QAction::triggered, this, [this, index]() {
0096                 slotTextToSpeech(index);
0097             });
0098 #endif
0099             menu.addSeparator();
0100             menu.addAction(i18n("Select All"), this, [this, index]() {
0101                 slotSelectAll(index);
0102             });
0103         }
0104         menu.exec(viewport()->mapToGlobal(pos));
0105     }
0106 }
0107 
0108 void NotificationHistoryListView::slotTextToSpeech(const QModelIndex &index)
0109 {
0110     const QString messageText = selectedText(index);
0111     if (!messageText.isEmpty()) {
0112         Q_EMIT textToSpeech(messageText);
0113     }
0114 }
0115 
0116 QString NotificationHistoryListView::selectedText(const QModelIndex &index)
0117 {
0118     QString messageText = selectedText();
0119     if (messageText.isEmpty()) {
0120         if (!index.isValid()) {
0121             return {};
0122         }
0123         messageText = index.data(NotificationHistoryModel::MessageStr).toString();
0124     }
0125     return messageText;
0126 }
0127 
0128 void NotificationHistoryListView::slotClearList()
0129 {
0130     clearCache();
0131     NotificationHistoryManager::self()->notificationHistoryModel()->clear();
0132 }
0133 
0134 #include "moc_notificationhistorylistview.cpp"