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