File indexing completed on 2024-04-28 04:55:41
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "textbrowser.h" 0010 0011 #include <QAbstractTextDocumentLayout> 0012 #include <QAction> 0013 #include <QApplication> 0014 #include <QClipboard> 0015 #include <QDrag> 0016 #include <QMenu> 0017 #include <QMimeData> 0018 #include <QPointer> 0019 #include <QTextBrowser> 0020 0021 #include <KLocalizedString> 0022 0023 #include "postwidget.h" 0024 0025 using namespace Choqok::UI; 0026 0027 class TextBrowser::Private 0028 { 0029 public: 0030 Private() 0031 : isPressedForDrag(false) 0032 {} 0033 static QList< QPointer<QAction> > actions; 0034 PostWidget *parent; 0035 QPoint dragStartPosition; 0036 bool isPressedForDrag; 0037 }; 0038 0039 QList< QPointer<QAction> > TextBrowser::Private::actions; 0040 0041 TextBrowser::TextBrowser(QWidget *parent) 0042 : QTextBrowser(parent), d(new Private) 0043 { 0044 d->parent = qobject_cast<PostWidget *>(parent); 0045 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0046 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 0047 setOpenLinks(false); 0048 } 0049 0050 TextBrowser::~TextBrowser() 0051 { 0052 delete d; 0053 } 0054 0055 void TextBrowser::mousePressEvent(QMouseEvent *ev) 0056 { 0057 Q_EMIT clicked(ev); 0058 0059 if (ev->button() == Qt::LeftButton) { 0060 if (!cursorForPosition(ev->pos()).hasSelection() && !anchorAt(ev->pos()).isEmpty()) { 0061 d->dragStartPosition = ev->pos(); 0062 d->isPressedForDrag = true; 0063 } else { 0064 d->isPressedForDrag = false; 0065 } 0066 } 0067 ev->accept(); 0068 QTextBrowser::mousePressEvent(ev); 0069 } 0070 0071 void TextBrowser::mouseMoveEvent(QMouseEvent *ev) 0072 { 0073 if ((ev->buttons() & Qt::LeftButton) && d->isPressedForDrag) { 0074 QPoint diff = ev->pos() - d->dragStartPosition; 0075 if (diff.manhattanLength() > QApplication::startDragDistance()) { 0076 QString anchor = anchorAt(d->dragStartPosition); 0077 if (!anchor.isEmpty()) { 0078 QDrag *drag = new QDrag(this); 0079 QMimeData *mimeData; 0080 mimeData = new QMimeData; 0081 QList<QUrl> urls; 0082 urls.append(QUrl(anchor)); 0083 mimeData->setUrls(urls); 0084 mimeData->setText(anchor); 0085 drag->setMimeData(mimeData); 0086 drag->exec(Qt::CopyAction | Qt::MoveAction); 0087 } 0088 } else { 0089 QTextBrowser::mouseMoveEvent(ev); 0090 } 0091 } else { 0092 QTextBrowser::mouseMoveEvent(ev); 0093 } 0094 ev->accept(); 0095 } 0096 0097 void TextBrowser::resizeEvent(QResizeEvent *e) 0098 { 0099 QTextEdit::resizeEvent(e); 0100 } 0101 0102 void TextBrowser::contextMenuEvent(QContextMenuEvent *event) 0103 { 0104 QMenu *menu = new QMenu(this); 0105 QAction *copy = new QAction(i18nc("Copy text", "Copy"), this); 0106 // copy->setShortcut( KShortcut( Qt::ControlModifier | Qt::Key_C ) ); 0107 connect(copy, &QAction::triggered, this, &TextBrowser::slotCopyPostContent); 0108 menu->addAction(copy); 0109 QString anchor = document()->documentLayout()->anchorAt(event->pos()); 0110 if (!anchor.isEmpty()) { 0111 QAction *copyLink = new QAction(i18n("Copy Link Location"), this); 0112 copyLink->setData(anchor); 0113 connect(copyLink, &QAction::triggered, this, &TextBrowser::slotCopyLink); 0114 menu->addAction(copyLink); 0115 } 0116 QAction *selectAll = new QAction(i18nc("Select all text", "Select All"), this); 0117 // selectAll->setShortcut( KShortcut( Qt::ControlModifier | Qt::Key_A ) ); 0118 connect(selectAll, &QAction::triggered, this, &TextBrowser::selectAll); 0119 menu->addAction(selectAll); 0120 menu->addSeparator(); 0121 for (QAction *act: d->actions) { 0122 if (act) { 0123 act->setUserData(32, new PostWidgetUserData(d->parent)); 0124 menu->addAction(act); 0125 } 0126 } 0127 menu->popup(event->globalPos()); 0128 } 0129 0130 void TextBrowser::slotCopyPostContent() 0131 { 0132 QString txt = textCursor().selectedText(); 0133 if (txt.isEmpty()) { 0134 PostWidget *paPost = qobject_cast<PostWidget *>(parentWidget()); 0135 if (paPost) { 0136 QApplication::clipboard()->setText(paPost->currentPost()->content); 0137 } 0138 } else { 0139 QApplication::clipboard()->setText(txt); 0140 } 0141 } 0142 0143 void TextBrowser::slotCopyLink() 0144 { 0145 QAction *act = qobject_cast< QAction * >(sender()); 0146 if (act) { 0147 QString link = act->data().toString(); 0148 QApplication::clipboard()->setText(link); 0149 } 0150 } 0151 0152 void Choqok::UI::TextBrowser::wheelEvent(QWheelEvent *event) 0153 { 0154 event->ignore(); 0155 } 0156 0157 void Choqok::UI::TextBrowser::addAction(QAction *action) 0158 { 0159 if (action) { 0160 Private::actions.append(action); 0161 } 0162 } 0163 0164 #include "moc_textbrowser.cpp"