BibleTime
cinfodisplay.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, https://bibletime.info/
6*
7* Copyright 1999-2025 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 "cinfodisplay.h"
14
15#include <memory>
16#include <QAction>
17#include <QLabel>
18#include <QLayout>
19#include <QSize>
20#include <QVBoxLayout>
21#include <QtAlgorithms>
22#include <QMenu>
23#include "../backend/config/btconfig.h"
24#include "../backend/drivers/cswordmoduleinfo.h"
25#include "../backend/keys/cswordkey.h"
26#include "../backend/managers/colormanager.h"
27#include "../backend/managers/referencemanager.h"
28#include "../util/btconnect.h"
29#include "bibletime.h"
30#include "bttextbrowser.h"
31
32using namespace Rendering;
33using namespace sword;
34
35namespace InfoDisplay {
36
38 : QWidget(parent)
39 , m_mainWindow(parent)
40{
41 QVBoxLayout * const layout = new QVBoxLayout(this);
42 layout->setContentsMargins(2, 2, 2, 2); // Leave small border
43 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
44 m_textBrowser = new BtTextBrowser(this);
45 m_textBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
46 BT_CONNECT(m_textBrowser, &QTextBrowser::customContextMenuRequested,
47 [this]{
48 QAction selectAllAction(tr("Select all"));
49 selectAllAction.setShortcut(QKeySequence::SelectAll);
50 BT_CONNECT(&selectAllAction, &QAction::triggered,
51 m_textBrowser, &QTextBrowser::selectAll);
52
53 QAction copyAction(tr("Copy"));
54 copyAction.setShortcut(QKeySequence(Qt::CTRL | Qt::Key_C));
55 BT_CONNECT(&copyAction, &QAction::triggered,
56 m_textBrowser, &QTextBrowser::copy);
57
58 QMenu menu;
59 menu.addAction(&selectAllAction);
60 menu.addAction(&copyAction);
61 menu.exec(QCursor::pos());
62 });
63 BT_CONNECT(m_textBrowser, &QTextBrowser::anchorClicked,
64 [this](QUrl const & url) {
65 auto const decodedLink(
67 url.toString()));
68 if (!decodedLink && !decodedLink->module)
69 return;
70
71 auto const * const m = decodedLink->module;
72 std::unique_ptr<CSwordKey> key(m->createKey());
73 key->setKey(decodedLink->key);
74
75 setInfo(key->renderedText(), m->language()->abbrev());
76 });
77 layout->addWidget(m_textBrowser);
78 unsetInfo();
79}
80
82 setInfo(tr("<small>This is the Mag viewer area. Hover the mouse over links "
83 "or other items which include some data and the contents appear "
84 "in the Mag after a short delay. Move the mouse into Mag "
85 "rapidly or lock the view by pressing and holding Shift while "
86 "moving the mouse.</small>"));
87}
88
90 QPalette p = m_textBrowser->palette();
91 p.setColor(QPalette::Base, ColorManager::getBackgroundColor());
92 p.setColor(QPalette::Text, ColorManager::getForegroundColor());
93 m_textBrowser->setPalette(p);
94}
95
96void CInfoDisplay::setInfo(const QString & renderedData, const QString & lang) {
97 QString text = Rendering::formatInfo(renderedData, lang);
98 text.replace(QStringLiteral("#CHAPTERTITLE#"), QString());
99 text.replace(QStringLiteral("#TEXT_ALIGN#"), QStringLiteral("left"));
100 text = ColorManager::replaceColors(text);
101 m_textBrowser->setText(text);
102}
103
104void CInfoDisplay::setInfo(const Rendering::InfoType type, const QString & data) {
105 setInfo(Rendering::ListInfoData() << qMakePair(type, data));
106}
107
109 // If the widget is hidden it would be inefficient to render and display the data
110 if (!isVisible())
111 return;
112
113 if (list.isEmpty()) {
114 m_textBrowser->setText(QString());
115 return;
116 }
117
120 if(m != nullptr)
121 l.append(m);
124}
125
127 if (module) {
128 setInfo(tr("<div class=\"moduleinfo\"><h3>%1</h3><p>%2</p><p>Version: %3</p></div>")
129 .arg(module->name().toHtmlEscaped())
130 .arg(module->config(CSwordModuleInfo::Description).toHtmlEscaped())
131 .arg(module->config(CSwordModuleInfo::ModuleVersion).toHtmlEscaped()));
132 } else {
133 unsetInfo();
134 }
135}
136
138 if (module) {
139 auto const lang = module->language();
140 m_textBrowser->setFont(btConfig().getFontForLanguage(*lang).second);
141 } else {
142 m_textBrowser->setFont(btConfig().getDefaultFont());
143 }
144}
145
147 return QSize(100, 150);
148}
149
150} //end of namespace InfoDisplay
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
#define BT_CONNECT(...)
Definition btconnect.h:20
QList< CSwordModuleInfo const * > BtConstModuleList
CSwordModuleInfo const * getCurrentModule()
A QTextBrowser subclass which adds the ability to start drags for references.
QString config(const CSwordModuleInfo::ConfigEntry entry) const
QString const & name() const
void setInfo(const QString &renderedData, const QString &lang=QString())
CInfoDisplay(BibleTime *parent=nullptr)
QSize sizeHint() const override
void setBrowserFont(const CSwordModuleInfo *const module)
BtTextBrowser * m_textBrowser
QString getForegroundColor()
QString getBackgroundColor()
QString replaceColors(QString content)
std::optional< DecodedHyperlink > decodeHyperlink(QString const &hyperlink)
QList< InfoData > ListInfoData
QString formatInfo(const ListInfoData &list, BtConstModuleList const &modules)