BibleTime
tool.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 "tool.h"
14 
15 #include <QApplication>
16 #include <QChar>
17 #include <QFile>
18 #include <QFileDevice>
19 #include <QFontMetrics>
20 #include <QFrame>
21 #include <QIODevice>
22 #include <QLabel>
23 #include <QMessageBox>
24 #include <QObject>
25 #include <QTextStream>
26 #include <QWidget>
27 #include "../backend/drivers/cswordmoduleinfo.h"
28 #include "btassert.h"
29 #include "cresmgr.h"
30 
31 
32 namespace util {
33 namespace tool {
34 
35 bool savePlainFile(const QString & filename,
36  void (&writer)(QTextStream &, void *),
37  void * userPtr)
38 {
39  BT_ASSERT(!filename.isEmpty());
40 
41  QFile saveFile(filename);
42 
43  if (saveFile.exists())
44  saveFile.remove();
45 
46  if (saveFile.open(QIODevice::ReadWrite)) {
47  QTextStream textstream(&saveFile);
48  writer(textstream, userPtr);
49  textstream.flush();
50  saveFile.close();
51  if (saveFile.error() == QFile::NoError)
52  return true;
53 
54  QMessageBox::critical(nullptr, QObject::tr("Error"),
55  QStringLiteral("<qt>%1<br/><b>%2</b></qt>")
56  .arg(QObject::tr("Error while writing to file."))
57  .arg(QObject::tr("Please check that enough disk space is available.")));
58  }
59  else {
60  QMessageBox::critical(nullptr, QObject::tr("Error"),
61  QStringLiteral("<qt>%1<br/><b>%2</b></qt>")
62  .arg(QObject::tr("The file couldn't be opened for saving."))
63  .arg(QObject::tr("Please check permissions etc.")));
64  }
65 
66  return false;
67 }
68 
69 /** Creates the file filename and put text into the file.
70  */
71 bool savePlainFile(QString const & filename, QString const & text)
72 {
73  struct UserData { QString const & text; } userData{text};
74  static auto const writer =
75  +[](QTextStream & out, void * textPtr)
76  { out << static_cast<const UserData *>(textPtr)->text; };
77  return savePlainFile(filename, *writer, &userData);
78 }
79 
80 QIcon const & getIconForModule(const CSwordModuleInfo * const module) {
81  if (!module)
82  return CResMgr::modules::book::icon_locked();
83 
84  if (module->category() == CSwordModuleInfo::Cult)
85  return CResMgr::modules::icon_cult();
86 
87  switch (module->type()) {
89  if (module->isLocked())
90  return CResMgr::modules::bible::icon_locked();
91  return CResMgr::modules::bible::icon_unlocked();
92 
94  if (module->isLocked())
95  return CResMgr::modules::lexicon::icon_locked();
96  return CResMgr::modules::lexicon::icon_unlocked();
97 
99  if (module->isLocked())
100  return CResMgr::modules::commentary::icon_locked();
101  return CResMgr::modules::commentary::icon_unlocked();
102 
104  if (module->isLocked())
105  return CResMgr::modules::book::icon_locked();
106  return CResMgr::modules::book::icon_unlocked();
107 
108  case CSwordModuleInfo::Unknown: //fallback
109  default:
110  if (module->isLocked())
111  return CResMgr::modules::book::icon_locked();
112  return CResMgr::modules::book::icon_unlocked();
113  }
114 }
115 
116 void initExplanationLabel(QLabel * const label,
117  const QString & heading,
118  const QString & text)
119 {
120  QString labelText;
121  if (!heading.isEmpty())
122  labelText += QStringLiteral("<b>%1</b>").arg(heading);
123 
124  if (!heading.isEmpty() && !text.isEmpty())
125  labelText +=
126  QStringLiteral("<span style=\"white-space:pre\"> - </span>");
127 
128  if (!text.isEmpty())
129  labelText += QStringLiteral("<small>%1</small>").arg(text);
130 
131  label->setText(labelText);
132  label->setWordWrap(true);
133  label->setMargin(1);
134  label->setFrameStyle(QFrame::Box | QFrame::Sunken);
135 }
136 
137 bool inHTMLTag(const int pos, const QString & text) {
138  int i1 = text.lastIndexOf('<', pos);
139  int i2 = text.lastIndexOf('>', pos);
140  int i3 = text.indexOf('>', pos);
141  int i4 = text.indexOf('<', pos);
142 
143 
144  // if ((i1>0) && (i2==-1)) //we're in th first html tag
145  // i2=i1; // not ncessary, just for explanation
146 
147  if ((i3 > 0) && (i4 == -1)) //we're in the last html tag
148  i4 = i3 + 1;
149 
150  // qWarning("%d > %d && %d < %d",i1,i2,i3,i4);
151 
152  return (i1 > i2) && (i3 < i4);
153 }
154 
155 int mWidth(QWidget const & widget, int const mCount)
156 { return widget.fontMetrics().horizontalAdvance(QString(mCount, 'M')); }
157 
158 QString fixSwordBcp47(QString input) {
159  input.replace('_', '-');
160  return input;
161 }
162 
163 } // namespace tool {
164 } // namespace util {
#define BT_ASSERT(...)
Definition: btassert.h:17
ModuleType type() const
CSwordModuleInfo::Category category() const
QString fixSwordBcp47(QString input)
Definition: tool.cpp:158
void initExplanationLabel(QLabel *const label, const QString &heading, const QString &text)
Initializes a QLabel to explain difficult things of dialogs.
Definition: tool.cpp:116
int mWidth(QWidget const &widget, int const mCount)
Calculates a maximum rendered text width for a widget and a string with the a given length.
Definition: tool.cpp:155
bool savePlainFile(const QString &filename, void(&writer)(QTextStream &, void *), void *userPtr)
Definition: tool.cpp:35
bool inHTMLTag(const int pos, const QString &text)
Definition: tool.cpp:137
QIcon const & getIconForModule(const CSwordModuleInfo *const module)
Definition: tool.cpp:80
Definition: cp1252.cpp:24