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/btassert.h"
29#include "../util/btconnect.h"
30#include "bibletime.h"
31#include "bttextbrowser.h"
32
33using namespace Rendering;
34using namespace sword;
35
36namespace InfoDisplay {
37
39 : QWidget(parent)
40 , m_mainWindow(parent)
41{
42 QVBoxLayout * const layout = new QVBoxLayout(this);
43 layout->setContentsMargins(2, 2, 2, 2); // Leave small border
44 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
45 m_textBrowser = new BtTextBrowser(this);
46 m_textBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
47 BT_CONNECT(m_textBrowser, &QTextBrowser::customContextMenuRequested,
48 [this]{
49 QAction selectAllAction(tr("Select all"));
50 selectAllAction.setShortcut(QKeySequence::SelectAll);
51 BT_CONNECT(&selectAllAction, &QAction::triggered,
52 m_textBrowser, &QTextBrowser::selectAll);
53
54 QAction copyAction(tr("Copy"));
55 copyAction.setShortcut(QKeySequence(Qt::CTRL | Qt::Key_C));
56 BT_CONNECT(&copyAction, &QAction::triggered,
57 m_textBrowser, &QTextBrowser::copy);
58
59 QMenu menu;
60 menu.addAction(&selectAllAction);
61 menu.addAction(&copyAction);
62 menu.exec(QCursor::pos());
63 });
64 BT_CONNECT(m_textBrowser, &QTextBrowser::anchorClicked,
65 [this](QUrl const & url) {
66 auto const decodedLink(
68 url.toString()));
69 if (!decodedLink && !decodedLink->module)
70 return;
71
72 auto const * const m = decodedLink->module;
73 std::unique_ptr<CSwordKey> key(m->createKey());
74 key->setKey(decodedLink->key);
75
76 setInfo(key->renderedText(), m->language()->abbrev());
77 });
78 layout->addWidget(m_textBrowser);
79 unsetInfo();
80}
81
83 setInfo(tr("<small>This is the Mag viewer area. Hover the mouse over links "
84 "or other items which include some data and the contents appear "
85 "in the Mag after a short delay. Move the mouse into Mag "
86 "rapidly or lock the view by pressing and holding Shift while "
87 "moving the mouse.</small>"));
88}
89
91 QPalette p = m_textBrowser->palette();
92 p.setColor(QPalette::Base, ColorManager::getBackgroundColor());
93 p.setColor(QPalette::Text, ColorManager::getForegroundColor());
94 m_textBrowser->setPalette(p);
95}
96
97void CInfoDisplay::setInfo(const QString & renderedData, const QString & lang) {
98 QString text = Rendering::formatInfo(renderedData, lang);
99 text.replace(QStringLiteral("#CHAPTERTITLE#"), QString());
100 text.replace(QStringLiteral("#TEXT_ALIGN#"), QStringLiteral("left"));
101 text = ColorManager::replaceColors(text);
102 m_textBrowser->setText(text);
103}
104
105void CInfoDisplay::setInfo(const Rendering::InfoType type, const QString & data) {
106 setInfo(Rendering::ListInfoData() << qMakePair(type, data));
107}
108
110 // If the widget is hidden it would be inefficient to render and display the data
111 if (!isVisible())
112 return;
113
114 if (list.isEmpty()) {
115 m_textBrowser->setText(QString());
116 return;
117 }
118
121 if(m != nullptr)
122 l.append(m);
125}
126
128 if (module) {
129 setInfo(tr("<div class=\"moduleinfo\"><h3>%1</h3><p>%2</p><p>Version: %3</p></div>")
130 .arg(module->name().toHtmlEscaped())
131 .arg(module->config(CSwordModuleInfo::Description).toHtmlEscaped())
132 .arg(module->config(CSwordModuleInfo::ModuleVersion).toHtmlEscaped()));
133 } else {
134 unsetInfo();
135 }
136}
137
139 if (module) {
140 auto const lang = module->language();
141 m_textBrowser->setFont(btConfig().getFontForLanguage(*lang).second);
142 } else {
143 m_textBrowser->setFont(btConfig().getDefaultFont());
144 }
145}
146
148 return QSize(100, 150);
149}
150
151} //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)