File indexing completed on 2024-04-28 05:52:26

0001 /*
0002     SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "markdownview.hpp"
0008 
0009 // Qt
0010 #include <QScrollBar>
0011 #include <QContextMenuEvent>
0012 
0013 
0014 MarkdownView::MarkdownView(QTextDocument* document, QWidget* parent)
0015     : QTextBrowser(parent)
0016 {
0017     setOpenLinks(false);
0018 
0019     setDocument(document);
0020 }
0021 
0022 bool MarkdownView::hasSelection() const
0023 {
0024     return textCursor().hasSelection();
0025 }
0026 
0027 void MarkdownView::setScrollPosition(QPoint offset)
0028 {
0029     horizontalScrollBar()->setValue(offset.x());
0030     verticalScrollBar()->setValue(offset.y());
0031 }
0032 
0033 QPoint MarkdownView::scrollPosition() const
0034 {
0035     return {
0036         horizontalScrollBar()->value(),
0037         verticalScrollBar()->value()
0038     };
0039 }
0040 
0041 int MarkdownView::scrollPositionX() const
0042 {
0043     return horizontalScrollBar()->value();
0044 }
0045 
0046 int MarkdownView::scrollPositionY() const
0047 {
0048     return verticalScrollBar()->value();
0049 }
0050 
0051 void MarkdownView::contextMenuEvent(QContextMenuEvent* event)
0052 {
0053     // Compare KWebKitPart's WebView::contextMenuEvent & WebEnginePart's WebEngineView::contextMenuEvent
0054     // for the patterns used to fill the menu.
0055     // QTextBrowser at of Qt 5.15 provides less data though, so for now this is reduced variant.
0056 
0057     // trying to get linkText skipped, because not reliable to get:
0058     // anchorAt uses ExactHit test, but cursorAt FuzzyHit, so this might not match
0059 
0060     const QUrl linkUrl(anchorAt(event->pos()));
0061 
0062     // only report any selection if this is not a context menu for a link
0063     const bool hasSelection = !linkUrl.isValid() && this->hasSelection();
0064 
0065     Q_EMIT contextMenuRequested(event->globalPos(),
0066                                 linkUrl,
0067                                 hasSelection);
0068 
0069     event->accept();
0070 }
0071 
0072 #include "moc_markdownview.cpp"