BibleTime
cbiblereadwindow.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-2021 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 "cbiblereadwindow.h"
14 
15 #include <QAction>
16 #include <QEvent>
17 #include <QMdiSubWindow>
18 #include <QMenu>
19 #include <QTimer>
20 #include <QWidget>
21 #include "../../backend/drivers/cswordbiblemoduleinfo.h"
22 #include "../../backend/keys/cswordversekey.h"
23 #include "../../backend/managers/cswordbackend.h"
24 #include "../../util/btassert.h"
25 #include "../../util/btconnect.h"
26 #include "../../util/cresmgr.h"
27 #include "../cexportmanager.h"
28 #include "../cmdiarea.h"
29 #include "../display/btmodelviewreaddisplay.h"
30 #include "../keychooser/ckeychooser.h"
31 #include "btactioncollection.h"
32 
33 
36 
39 
42 
43  // Apply settings to display:
44  lookup();
45 }
46 
50 
52 }
53 
54 
55 /** Reimplementation. */
57  QAction* qaction;
58 
59  qaction = new QAction(tr("Next book"), a);
61  a->addAction(QStringLiteral("nextBook"), qaction);
62 
63  qaction = new QAction(tr("Previous book"), a);
65  a->addAction(QStringLiteral("previousBook"), qaction);
66 
67  qaction = new QAction(tr("Next chapter"), a);
69  a->addAction(QStringLiteral("nextChapter"), qaction);
70 
71  qaction = new QAction(tr("Previous chapter"), a);
73  a->addAction(QStringLiteral("previousChapter"), qaction);
74 
75  qaction = new QAction(tr("Next verse"), a);
77  a->addAction(QStringLiteral("nextVerse"), qaction);
78 
79  qaction = new QAction(tr("Previous verse"), a);
81  a->addAction(QStringLiteral("previousVerse"), qaction);
82 
83  //popup menu items
84  qaction = new QAction(tr("Copy chapter"), a);
85  a->addAction(QStringLiteral("copyChapter"), qaction);
86 
87  qaction = new QAction(tr("Save chapter as plain text"), a);
88  a->addAction(QStringLiteral("saveChapterAsPlainText"), qaction);
89 
90  qaction = new QAction(tr("Save chapter as HTML"), a);
91  a->addAction(QStringLiteral("saveChapterAsHTML"), qaction);
92 
93  qaction = new QAction(tr("Print chapter"), a);
94  qaction->setShortcut(QKeySequence::Print);
95  a->addAction(QStringLiteral("printChapter"), qaction);
96 
97  // qaction = new QAction( /* QIcon(CResMgr::displaywindows::general::findStrongs::icon), */ tr("Strong's search"), a);
98  // qaction->setShortcut(CResMgr::displaywindows::general::findStrongs::accel);
99  // qaction->setToolTip(tr("Find all occurences of the Strong number currently under the mouse cursor"));
100  // a->addAction(CResMgr::displaywindows::general::findStrongs::actionName, qaction);
101 
102  // qaction = new QAction(tr("Reference only"), a );
103  // a->addAction("copyReferenceOnly", qaction);
104 
105  qaction = new QAction(tr("Text of reference"), a);
106  a->addAction(QStringLiteral("copyTextOfReference"), qaction);
107 
108  qaction = new QAction(tr("Reference with text"), a);
109  a->addAction(QStringLiteral("copyReferenceWithText"), qaction);
110 
111  qaction = new QAction(tr("Reference with text"), a);
112  a->addAction(QStringLiteral("printReferenceWithText"), qaction);
113 
114  qaction = new QAction(tr("Reference with text"), a);
115  a->addAction(QStringLiteral("saveReferenceWithText"), qaction);
116 }
117 
120 
121  CDisplayWindow::initActions(); //make sure the predefined actions are available
122 
124 
125  initAddAction(QStringLiteral("nextBook"),
126  this,
128  initAddAction(QStringLiteral("previousBook"),
129  this,
131  initAddAction(QStringLiteral("nextChapter"),
132  this,
134  initAddAction(QStringLiteral("previousChapter"),
135  this,
137  initAddAction(QStringLiteral("nextVerse"),
138  this,
140  initAddAction(QStringLiteral("previousVerse"),
141  this,
143 
144  m_actions.findText = &ac->action(QStringLiteral("findText"));
146  m_actions.copy.referenceOnly =
147  &ac->action(QStringLiteral("copyReferenceOnly"));
148 
149  m_actions.copy.referenceTextOnly =
150  &initAddAction(QStringLiteral("copyTextOfReference"),
151  displayWidget(),
153 
154  m_actions.copy.referenceAndText =
155  &initAddAction(QStringLiteral("copyReferenceWithText"),
156  displayWidget(),
158 
159  m_actions.copy.chapter =
160  &initAddAction(QStringLiteral("copyChapter"),
161  this,
163 
164  m_actions.copy.selectedText =
165  &ac->action(QStringLiteral("copySelectedText"));
166 
167  m_actions.copy.byReferences =
168  &ac->action(QStringLiteral("copyByReferences"));
169 
170  m_actions.save.referenceAndText =
171  &initAddAction(QStringLiteral("saveReferenceWithText"),
172  displayWidget(),
174 
175  m_actions.save.chapterAsPlain =
176  &initAddAction(QStringLiteral("saveChapterAsPlainText"),
177  this,
179 
180  m_actions.save.chapterAsHTML =
181  &initAddAction(QStringLiteral("saveChapterAsHTML"),
182  this,
184 
185  m_actions.print.reference =
186  &initAddAction(QStringLiteral("printReferenceWithText"),
187  this,
189 
190  m_actions.print.chapter =
191  &initAddAction(QStringLiteral("printChapter"),
192  this,
194 
195  ac->readShortcuts(QStringLiteral("Bible shortcuts"));
196 }
197 
200 
201  parentWidget()->installEventFilter(this);
202 }
203 
205  auto * const popupMenu = new QMenu(this);
206  BT_CONNECT(popupMenu, &QMenu::aboutToShow,
207  [this] {
208  auto const & display = *displayWidget();
209  m_actions.findStrongs->setEnabled(
210  !display.getCurrentNodeInfo().isNull());
211 
212  bool const hasActiveAnchor = display.hasActiveAnchor();
213  m_actions.copy.referenceOnly->setEnabled(hasActiveAnchor);
214  m_actions.copy.referenceTextOnly->setEnabled(
215  hasActiveAnchor);
216  m_actions.copy.referenceAndText->setEnabled(
217  hasActiveAnchor);
218 
219  m_actions.save.referenceAndText->setEnabled(
220  hasActiveAnchor);
221 
222  m_actions.print.reference->setEnabled(hasActiveAnchor);
223 
224  m_actions.copy.selectedText->setEnabled(hasSelectedText());
225  });
226  popupMenu->setTitle(tr("Bible window"));
227  popupMenu->setIcon(firstModule()->moduleIcon());
228  popupMenu->addAction(m_actions.findText);
229  QKeySequence ks = m_actions.findText->shortcut();
230  QString keys = ks.toString();
231  popupMenu->addAction(m_actions.findStrongs);
232 
233  popupMenu->addSeparator();
234 
235  m_actions.copyMenu = new QMenu(tr("Copy"), popupMenu);
236 
237  m_actions.copyMenu->addSeparator();
238 
239  m_actions.copyMenu->addAction(m_actions.copy.selectedText);
240  m_actions.copyMenu->addAction(m_actions.copy.byReferences);
241  m_actions.copyMenu->addAction(m_actions.copy.referenceOnly);
242  m_actions.copyMenu->addAction(m_actions.copy.referenceTextOnly);
243  m_actions.copyMenu->addAction(m_actions.copy.referenceAndText);
244  m_actions.copyMenu->addAction(m_actions.copy.chapter);
245 
246 
247  popupMenu->addMenu(m_actions.copyMenu);
248 
249  m_actions.saveMenu = new QMenu(tr("Save..."), popupMenu);
250  m_actions.saveMenu->addAction(m_actions.save.referenceAndText);
251  m_actions.saveMenu->addAction(m_actions.save.chapterAsPlain);
252  m_actions.saveMenu->addAction(m_actions.save.chapterAsHTML);
253  popupMenu->addMenu(m_actions.saveMenu);
254 
255  m_actions.printMenu = new QMenu(tr("Print..."), popupMenu);
256  m_actions.printMenu->addAction(m_actions.print.reference);
257  m_actions.printMenu->addAction(m_actions.print.chapter);
258  popupMenu->addMenu(m_actions.printMenu);
259  return popupMenu;
260 }
261 
262 /** Moves to the next book. */
264  if (verseKey()->next(CSwordVerseKey::UseBook)) {
265  keyChooser()->setKey(key());
266  }
267 }
268 
269 /** Moves one book behind. */
271  if (verseKey()->previous(CSwordVerseKey::UseBook)) {
272  keyChooser()->setKey(key());
273  }
274 }
275 
276 /** Moves to the next book. */
278  if (verseKey()->next(CSwordVerseKey::UseChapter)) {
279  keyChooser()->setKey(key());
280  }
281 }
282 
283 /** Moves one book behind. */
285  if (verseKey()->previous(CSwordVerseKey::UseChapter)) {
286  keyChooser()->setKey(key());
287  }
288 }
289 
290 /** Moves to the next book. */
292  if (verseKey()->next(CSwordVerseKey::UseVerse)) {
293  keyChooser()->setKey(key());
294  }
295 }
296 
297 /** Moves one book behind. */
299  if (verseKey()->previous(CSwordVerseKey::UseVerse)) {
300  keyChooser()->setKey(key());
301  }
302 }
303 
304 /** wrapper around key() to return the right type of key. */
306  CSwordVerseKey* k = dynamic_cast<CSwordVerseKey*>(CDisplayWindow::key());
307  BT_ASSERT(k);
308 
309  return k;
310 }
311 
312 /** Copies the current chapter into the clipboard. */
314  CSwordVerseKey dummy(*verseKey());
315  dummy.setVerse(1);
316 
317  CSwordVerseKey vk(*verseKey());
318  vk.setLowerBound(dummy);
319 
320  auto const bible =
321  dynamic_cast<CSwordBibleModuleInfo const *>(firstModule());
322  dummy.setVerse(bible->verseCount(dummy.bookName(), dummy.chapter()));
323  vk.setUpperBound(dummy);
324 
325  CExportManager mgr(false, tr("Copying"), filterOptions(), displayOptions());
326  mgr.copyKey(&vk, CExportManager::Text, true);
327 }
328 
329 /** Saves the chapter as valid HTML page. */
332 }
333 
334 /** Saves the chapter as valid HTML page. */
337 }
338 
341  if (! key)
342  return;
343  CSwordVerseKey* verseKey = dynamic_cast<CSwordVerseKey*>(key);
344  if (! verseKey)
345  return;
346 
348  CSwordVerseKey dummy(*verseKey);
349  dummy.setVerse(1);
350  vk.setLowerBound(dummy);
351 
352  auto const constMods = constModules();
353 
354  auto const bible =
355  dynamic_cast<CSwordBibleModuleInfo const *>(constMods.first());
356  /// \bug Check if bible is nullptr!
357  dummy.setVerse(bible->verseCount(dummy.book(), dummy.chapter()));
358  vk.setUpperBound(dummy);
359 
360  CExportManager mgr(true, tr("Saving"), filterOptions(), displayOptions());
361  mgr.saveKey(&vk, format, true, constMods);
362 }
363 
366 
367  if (m_modules.isEmpty()) {
368  close();
369  return;
370  }
371 
372  //refresh the book lists
373  verseKey()->setLocale(
374  CSwordBackend::instance().booknameLanguage().toLatin1());
376 
377  actionCollection()->readShortcuts(QStringLiteral("Bible shortcuts"));
378 }
379 
380 /** No descriptions */
382  const bool ret = CDisplayWindow::eventFilter(o, e);
383 
384  // BT_ASSERT(o->inherits("CDisplayWindow"));
385  // qWarning("class: %s", o->className());
386  if (e && (e->type() == QEvent::FocusIn)) { //sync other windows to this active
387 
388  /* This is a hack to work around a KHTML problem (similair to the Drag&Drop problem we had):
389  * If new HTML content is loaded from inside a kHTML event handler
390  * the widget's state will be confused, i.e. it's scrolling without having
391  * the mousebutton clicked.
392  *
393  * This is not really in a KHTML event handler but works anyway.
394  * Sometime KDE/Qt is hard to use ...
395  */
396  QTimer::singleShot(0, this, &CBibleReadWindow::syncWindows);
397  }
398 
399  return ret;
400 }
401 
404  syncWindows();
405 }
406 
408  for (auto * const subWindow : mdi()->subWindowList()) {
409  CDisplayWindow* w = dynamic_cast<CDisplayWindow*>(subWindow->widget());
410  if (w && w->syncAllowed()) {
411  w->lookupKey( key()->key() );
412  }
413  }
414 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
void readShortcuts(QString const &group)
Read shortcuts from config.
void addAction(QString const &name, QAction *const action)
QAction & action(QString const &name) const
static void storeFilterOptionsToGroup(FilterOptions const &options, BtConfigCore &group)
Saves the current filter options.
Definition: btconfig.cpp:301
static FilterOptions loadFilterOptionsFromGroup(BtConfigCore const &group)
Definition: btconfig.cpp:283
static DisplayOptions loadDisplayOptionsFromGroup(BtConfigCore const &group)
Definition: btconfig.cpp:320
static void storeDisplayOptionsToGroup(DisplayOptions const &options, BtConfigCore &group)
Saves the current display options.
Definition: btconfig.cpp:328
void saveChapter(CExportManager::Format const format)
void applyProfileSettings(BtConfigCore const &windowConf) override
Loads the settings of this window from configuration.
void lookupSwordKey(CSwordKey *newKey) override
static void insertKeyboardActions(BtActionCollection *const a)
void initActions() override
bool eventFilter(QObject *o, QEvent *e) override
void initView() override
void copyDisplayedText() override
void storeProfileSettings(BtConfigCore &windowConf) const override
Stores the settings of this window to configuration.
CSwordVerseKey * verseKey()
void reload() override
struct CBibleReadWindow::@1 m_actions
QMenu * newDisplayWidgetPopupMenu() override
The base class for all display windows of BibleTime.
DisplayOptions const & displayOptions() const
void sigDisplayOptionsChanged(const DisplayOptions &displayOptions)
void printAnchorWithText()
virtual void reload()
QAction & initAddAction(Args &&... args)
BtModuleList m_modules
CKeyChooser * keyChooser() const
virtual void storeProfileSettings(BtConfigCore &windowConf) const
Stores the settings of this window to configuration.
BtActionCollection * actionCollection() const
void setDisplayOptions(DisplayOptions const &v)
void sigFilterOptionsChanged(const FilterOptions &filterOptions)
virtual void initActions()
FilterOptions const & filterOptions() const
CMDIArea * mdi() const
virtual void lookupSwordKey(CSwordKey *)
void lookupKey(QString const &key)
void setFilterOptions(FilterOptions const &v)
BtConstModuleList constModules() const
virtual void initView()
virtual bool syncAllowed() const
BtModelViewReadDisplay * displayWidget() const
virtual void applyProfileSettings(BtConfigCore const &windowConf)
Loads the settings of this window from configuration.
CSwordModuleInfo const * firstModule() const noexcept
CSwordKey * key() const
CSwordKey * getMouseClickedKey() const
bool saveKey(CSwordKey const *const key, Format const format, bool const addText, const BtConstModuleList &modules)
bool copyKey(CSwordKey const *const key, Format const format, bool const addText)
virtual void setKey(CSwordKey *key)=0
virtual void refreshContent()=0
static CSwordBackend & instance() noexcept
Definition: cswordbackend.h:98
Implementation for Sword Bibles.
virtual QString key() const =0
QIcon moduleIcon() const
CSwordKey implementation for Sword's VerseKey.
void setLowerBound(CSwordVerseKey const &bound)
char book() const
QString bookName() const
void setLocale(char const *const locale)
void setUpperBound(CSwordVerseKey const &bound)
int chapter() const
void setVerse(int v)