File indexing completed on 2024-12-08 04:34:35

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