Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / helpers_html.cpp
blobf417e0dfff00bce9c924bbca9cd0d9ea4b04c557
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "helpers.h"
22 #include "helpers_html.h"
23 #include <QStringList>
24 #include <QDir>
25 #include <QFile>
26 #include <QTextStream>
28 QString tdAlign(const QString & s, const QString & align, const QString & color, bool bold)
30 QString str = s;
31 if (bold) str = "<b>" + str + "</b>";
32 if (!color.isEmpty()) str = "<font color='" + color + "'>" + str + "</font>";
33 return "<td align='" + align + "'>" + str + "</td>";
36 QString doTC(const QString & s, const QString & color, bool bold)
38 return tdAlign(s, "center", color, bold);
41 QString doTR(const QString & s, const QString & color, bool bold)
43 return tdAlign(s, "right", color, bold);
46 QString doTL(const QString & s, const QString & color, bool bold)
48 return tdAlign(s, "left", color, bold);
51 QString fv(const QString & name, const QString & value, const QString & color)
53 return "<b>" + name + ": </b><font color='" + color + "'>" + value + "</font><br>";
56 QString doTableCell(const QString & s, const unsigned int width, const QString & align, const QString & color, bool bold)
58 QString prfx = "<td";
59 if (width)
60 prfx.append(QString(" width='%1%'").arg(QString::number(width)));
61 if (!align.isEmpty())
62 prfx.append(QString(" align='%1'").arg(align));
63 prfx.append(">");
65 QString str = s;
66 if (bold)
67 str = "<b>" + str + "</b>";
68 if (!color.isEmpty())
69 str = QString("<font color='%1'>%2</font>").arg(color).arg(str);
71 str = prfx + str + "</td>";
72 return str;
75 QString doTableRow(const QStringList & strl, const unsigned int width, const QString & align, const QString & color, bool bold)
77 QString str;
78 for (int i=0; i<strl.count(); i++) {
79 str.append(doTableCell(strl.at(i), width, align, color, bold));
81 return "<tr>" + str + "</tr>";
84 QString doTableBlankRow()
86 return "<tr></tr>";
89 Stylesheet::Stylesheet(const QString & name):
90 mName(name)
92 init();
95 Stylesheet::Stylesheet(const QString & name, const StyleType styleType):
96 mName(name)
98 init();
99 mResult = load(styleType);
102 Stylesheet::~Stylesheet()
106 void Stylesheet::init()
108 mResult = false;
109 mErrormsg = "";
110 mText = "";
111 QStringList p = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
112 mCustomPath = p[0];
113 mCustomFile = p[0] + "/" + mName;
114 mDefaultFile = THEMES_DEFAULT_PATH + mName;
117 bool Stylesheet::load(const StyleType styleType)
119 mResult = false;
120 switch (styleType) {
121 case STYLE_TYPE_NONE:
122 case STYLE_TYPE_DEFAULT:
123 mResult = read(mDefaultFile);
124 case STYLE_TYPE_CUSTOM:
125 mResult = read(mCustomFile);
126 case STYLE_TYPE_EFFECTIVE:
127 mResult = read(mCustomFile);
128 if(!mResult)
129 mResult = read(mDefaultFile);
131 return mResult;
134 bool Stylesheet::read(const QString & path)
136 mResult = false;
137 mText = "";
139 QFile file(path);
140 if (file.exists()) {
141 if (file.open(QFile::ReadOnly | QFile::Text)) {
142 QTextStream in(&file);
143 if (in.status()==QTextStream::Ok) {
144 mText = in.readAll();
145 if (in.status()==QTextStream::Ok)
146 mResult = true;
147 else
148 mText = "";
150 file.close();
153 if (mResult)
154 mErrormsg = tr("Style sheet data read from '%1'").arg(QDir::toNativeSeparators(path));
155 else
156 mErrormsg = tr("Style sheet data unable to be read from '%1'").arg(QDir::toNativeSeparators(path));
157 qDebug() << mErrormsg;
158 return mResult;
161 bool Stylesheet::update()
163 mResult = false;
164 mErrormsg = "";
165 QDir path(mCustomPath);
166 if (!path.exists()) {
167 QDir dir;
168 if (!dir.mkpath(mCustomPath))
169 mErrormsg = tr("Cannot create folder '%1'").arg(QDir::toNativeSeparators(mCustomPath));
171 if (path.exists()) {
172 QFile file(mCustomFile);
173 if (!file.open(QFile::WriteOnly | QFile::Text)) {
174 mErrormsg = tr("Cannot open file for writing '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile), file.errorString());
176 else {
177 QTextStream out(&file);
178 if (out.status()==QTextStream::Ok) {
179 out << mText;
180 if (!(out.status()==QTextStream::Ok)) {
181 mErrormsg = tr("Cannot write to file '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile), file.errorString());
182 if (!file.flush()) {
183 mErrormsg = tr("Cannot flush buffer for file '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile), file.errorString());
186 else {
187 mResult = true;
188 mErrormsg = tr("Style sheet written to '%1'").arg(QDir::toNativeSeparators(mCustomFile));
192 file.close();
194 qDebug() << mErrormsg;
195 return mResult;
198 bool Stylesheet::deleteCustom()
200 QFile file(mCustomFile);
201 mResult = file.remove();
202 if (mResult)
203 mErrormsg = tr("Custom style sheet deleted: '%1'").arg(QDir::toNativeSeparators(mCustomFile));
204 else
205 mErrormsg = tr("Unable to delete custom style sheet: '%1'").arg(QDir::toNativeSeparators(mCustomFile));
206 qDebug() << mErrormsg;
207 return mResult;
210 QString Stylesheet::name()
212 return mName;
215 QString Stylesheet::text()
217 return mText;
220 void Stylesheet::setText(const QString & text)
222 mText = text;
225 QString Stylesheet::errormsg()
227 return mErrormsg;