BibleTime
bttextbrowser.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, http://www.bibletime.info/
6*
7* Copyright 1999-2020 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include "bttextbrowser.h"
14
15#include <memory>
16#include <QApplication>
17#include <QDrag>
18#include <QMouseEvent>
19#include "../backend/keys/cswordkey.h"
20#include "../backend/managers/referencemanager.h"
21#include "../backend/managers/cswordbackend.h"
22#include "BtMimeData.h"
23
24
26 : QTextBrowser(parent)
27{ setTextInteractionFlags(Qt::TextSelectableByMouse); }
28
29void BtTextBrowser::keyPressEvent(QKeyEvent * event) {
30 QTextBrowser::keyPressEvent(event);
31 if (event->isAccepted())
32 m_readyToStartDrag = false;
33}
34
35void BtTextBrowser::mousePressEvent(QMouseEvent * event) {
36 if (event->buttons() == Qt::LeftButton) {
37 m_startPos = event->pos();
38 m_readyToStartDrag = true;
39 } else {
40 m_readyToStartDrag = false;
41 }
42 QTextBrowser::mousePressEvent(event);
43}
44
45void BtTextBrowser::mouseReleaseEvent(QMouseEvent * event) {
46 m_readyToStartDrag = false;
47 QTextBrowser::mouseReleaseEvent(event);
48}
49
50void BtTextBrowser::mouseMoveEvent(QMouseEvent * event) {
52 auto const dragManhattanLength =
53 (event->pos() - m_startPos).manhattanLength();
54 if (dragManhattanLength >= qApp->startDragDistance()) {
55 m_readyToStartDrag = false;
56
57 if (auto const decodedLink =
59 {
60 auto mimedata =
61 std::make_unique<BTMimeData>(
63 decodedLink->module->name(),
64 decodedLink->key,
65 {}}});
66
67 //add real Bible text from module/key
68 if (auto const * const module =
69 CSwordBackend::instance().findModuleByName(
70 decodedLink->module->name()))
71 {
72 std::unique_ptr<CSwordKey> key(module->createKey());
73 key->setKey(decodedLink->key);
74 mimedata->setText(key->strippedText());
75 }
76
77 auto * const drag = new QDrag(this); // Deleted by Qt
78 drag->setMimeData(mimedata.release());
79 drag->exec(Qt::CopyAction, Qt::CopyAction);
80 return;
81 }
82 }
83 }
84 QTextBrowser::mouseMoveEvent(event);
85}
QList< BookmarkItem > ItemList
Definition BtMimeData.h:38
bool m_readyToStartDrag
BtTextBrowser(QWidget *parent=nullptr)
void mouseReleaseEvent(QMouseEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void keyPressEvent(QKeyEvent *event) override
static CSwordBackend & instance() noexcept
std::optional< DecodedHyperlink > decodeHyperlink(QString const &hyperlink)