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

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 "bannerinfolistview.h"
0008 #include "bannerinfolistviewdelegate.h"
0009 #include "model/bannerinfosmodel.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QMenu>
0014 #include <QMouseEvent>
0015 #include <QScrollBar>
0016 
0017 BannerInfoListView::BannerInfoListView(RocketChatAccount *account, QWidget *parent)
0018     : MessageListViewBase(parent)
0019     , mBannerInfoListViewDelegate(new BannerInfoListViewDelegate(this, account, this))
0020 {
0021     setItemDelegate(mBannerInfoListViewDelegate);
0022     setContextMenuPolicy(Qt::CustomContextMenu);
0023     connect(mBannerInfoListViewDelegate, &BannerInfoListViewDelegate::updateView, this, [this](const QModelIndex &index) {
0024         update(index);
0025     });
0026     const auto lineHeight = fontMetrics().height() + 10;
0027     verticalScrollBar()->setSingleStep(lineHeight);
0028     connect(this, &QListView::customContextMenuRequested, this, &BannerInfoListView::slotCustomContextMenuRequested);
0029     connect(this, &BannerInfoListView::needToClearSizeHintCache, mBannerInfoListViewDelegate, &BannerInfoListViewDelegate::clearSizeHintCache);
0030 }
0031 
0032 BannerInfoListView::~BannerInfoListView() = default;
0033 
0034 bool BannerInfoListView::maybeStartDrag(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0035 {
0036     return mBannerInfoListViewDelegate->maybeStartDrag(event, option, index);
0037 }
0038 
0039 bool BannerInfoListView::mouseEvent(QMouseEvent *event, const QStyleOptionViewItem &option, const QModelIndex &index)
0040 {
0041     return mBannerInfoListViewDelegate->mouseEvent(event, option, index);
0042 }
0043 
0044 void BannerInfoListView::slotCustomContextMenuRequested(const QPoint &pos)
0045 {
0046     if (model()->rowCount() > 0) {
0047         const QModelIndex index = indexAt(pos);
0048         if (index.isValid()) {
0049             QMenu menu(this);
0050             menu.addSeparator();
0051             menu.addAction(i18n("Select All"), this, [this, index]() {
0052                 slotSelectAll(index);
0053             });
0054             menu.addSeparator();
0055             auto copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Text"), &menu);
0056             copyAction->setShortcut(QKeySequence::Copy);
0057             connect(copyAction, &QAction::triggered, this, [this, index]() {
0058                 copyMessageToClipboard(index);
0059             });
0060             menu.addAction(copyAction);
0061             menu.exec(viewport()->mapToGlobal(pos));
0062         }
0063     }
0064 }
0065 
0066 void BannerInfoListView::slotSelectAll(const QModelIndex &index)
0067 {
0068     mBannerInfoListViewDelegate->selectAll(listViewOptions(), index);
0069 }
0070 
0071 QString BannerInfoListView::selectedText(const QModelIndex &index)
0072 {
0073     QString messageText = selectedText();
0074     if (messageText.isEmpty()) {
0075         if (!index.isValid()) {
0076             return {};
0077         }
0078         messageText = index.data(BannerInfosModel::Text).toString();
0079     }
0080     return messageText;
0081 }
0082 
0083 QString BannerInfoListView::selectedText() const
0084 {
0085     return mBannerInfoListViewDelegate->selectedText();
0086 }
0087 
0088 #include "moc_bannerinfolistview.cpp"