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-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 "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 "btassert.h"
28
29
30namespace util {
31namespace tool {
32
33bool savePlainFile(const QString & filename,
34 void (&writer)(QTextStream &, void *),
35 void * userPtr)
36{
37 BT_ASSERT(!filename.isEmpty());
38
39 QFile saveFile(filename);
40
41 if (saveFile.exists())
42 saveFile.remove();
43
44 if (saveFile.open(QIODevice::ReadWrite)) {
45 QTextStream textstream(&saveFile);
46 writer(textstream, userPtr);
47 textstream.flush();
48 saveFile.close();
49 if (saveFile.error() == QFile::NoError)
50 return true;
51
52 QMessageBox::critical(nullptr, QObject::tr("Error"),
53 QStringLiteral("<qt>%1<br/><b>%2</b></qt>")
54 .arg(QObject::tr("Error while writing to file."))
55 .arg(QObject::tr("Please check that enough disk space is available.")));
56 }
57 else {
58 QMessageBox::critical(nullptr, QObject::tr("Error"),
59 QStringLiteral("<qt>%1<br/><b>%2</b></qt>")
60 .arg(QObject::tr("The file couldn't be opened for saving."))
61 .arg(QObject::tr("Please check permissions etc.")));
62 }
63
64 return false;
65}
66
67/** Creates the file filename and put text into the file.
68 */
69bool savePlainFile(QString const & filename, QString const & text)
70{
71 struct UserData { QString const & text; } userData{text};
72 static auto const writer =
73 +[](QTextStream & out, void * textPtr)
74 { out << static_cast<const UserData *>(textPtr)->text; };
75 return savePlainFile(filename, *writer, &userData);
76}
77
78void initExplanationLabel(QLabel * const label,
79 const QString & heading,
80 const QString & text)
81{
82 QString labelText;
83 if (!heading.isEmpty())
84 labelText += QStringLiteral("<b>%1</b>").arg(heading);
85
86 if (!heading.isEmpty() && !text.isEmpty())
87 labelText +=
88 QStringLiteral("<span style=\"white-space:pre\"> - </span>");
89
90 if (!text.isEmpty())
91 labelText += QStringLiteral("<small>%1</small>").arg(text);
92
93 label->setText(labelText);
94 label->setWordWrap(true);
95 label->setMargin(1);
96 label->setFrameStyle(QFrame::Box | QFrame::Sunken);
97}
98
99bool inHTMLTag(const int pos, const QString & text) {
100 int i1 = text.lastIndexOf('<', pos);
101 int i2 = text.lastIndexOf('>', pos);
102 int i3 = text.indexOf('>', pos);
103 int i4 = text.indexOf('<', pos);
104
105
106 // if ((i1>0) && (i2==-1)) //we're in th first html tag
107 // i2=i1; // not ncessary, just for explanation
108
109 if ((i3 > 0) && (i4 == -1)) //we're in the last html tag
110 i4 = i3 + 1;
111
112 // qWarning("%d > %d && %d < %d",i1,i2,i3,i4);
113
114 return (i1 > i2) && (i3 < i4);
115}
116
117int mWidth(QWidget const & widget, int const mCount)
118{ return widget.fontMetrics().horizontalAdvance(QString(mCount, 'M')); }
119
120QString fixSwordBcp47(QString input) {
121 input.replace('_', '-');
122 return input;
123}
124
125} // namespace tool {
126} // namespace util {
#define BT_ASSERT(...)
Definition btassert.h:17
QString fixSwordBcp47(QString input)
Definition tool.cpp:120
void initExplanationLabel(QLabel *const label, const QString &heading, const QString &text)
Initializes a QLabel to explain difficult things of dialogs.
Definition tool.cpp:78
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:117
bool savePlainFile(const QString &filename, void(&writer)(QTextStream &, void *), void *userPtr)
Definition tool.cpp:33
bool inHTMLTag(const int pos, const QString &text)
Definition tool.cpp:99