]> git.sven.stormbind.net Git - sven/vym.git/blob - src/misc.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / misc.cpp
1 #include "misc.h"
2
3 #include "geometry.h"
4
5 #include <math.h>
6 #include <stdlib.h>
7
8 #include <QDebug>
9 #include <QDialog>
10 #include <QString>
11
12 extern QString vymVersion;
13
14 QString richTextToPlain(QString r, const QString &indent, const int &width)
15 {
16     Q_UNUSED(width);
17
18     // Avoid failing assert with mingw
19     if (r.isEmpty())
20         return r;
21
22     QRegExp rx;
23     rx.setMinimal(true);
24
25     // Remove all <style...> ...</style>
26     rx.setPattern("<style.*>.*</style>");
27     r.replace(rx, "");
28
29     // convert all "<br*>" to "\n"
30     rx.setPattern("<br.*>");
31     r.replace(rx, "\n");
32
33     // convert all "</p>" to "\n"
34     rx.setPattern("</p>");
35     r.replace(rx, "\n");
36
37     // remove all remaining tags
38     rx.setPattern("<.*>");
39     r.replace(rx, "");
40
41     // If string starts with \n now, remove it.
42     // It would be wrong in an OOo export for example
43     while (r.length() > 0 && r.at(0) == '\n')
44         r.remove(0, 1);
45
46     // convert "&", "<" and ">"
47     rx.setPattern("&gt;");
48     r.replace(rx, ">");
49     rx.setPattern("&lt;");
50     r.replace(rx, "<");
51     rx.setPattern("&amp;");
52     r.replace(rx, "&");
53     rx.setPattern("&quot;");
54     r.replace(rx, "\"");
55
56     // Indent everything
57     rx.setPattern("^\n");
58     r.replace(rx, indent);
59     r = indent + r; // Don't forget first line
60
61     return r;
62 }
63
64 QString qpointToString(const QPoint &p)
65 {
66     return "(" + QString("%1").arg(p.x()) + "," + QString("%1").arg(p.y()) +
67            ")";
68 }
69
70 QString qpointFToString(const QPointF &p)
71 {
72     return "(" + QString("%1").arg(p.x()) + "," + QString("%1").arg(p.y()) +
73            ")";
74 }
75
76 QString qrectFToString(const QRectF &r, int d)
77 {
78     return QString("(%1, %2  %3x%4)")
79         .arg(QString::number(r.x(),'f', d))
80         .arg(QString::number(r.y(),'f', d))
81         .arg(QString::number(r.width(),'f', d))
82         .arg(QString::number(r.height(),'f', d));
83 }
84
85 QString VectorToString(const Vector &p)
86 {
87     return "(" + QString("%1").arg(p.x()) + "," + QString("%1").arg(p.y()) +
88            ")";
89 }
90
91 ostream &operator<<(ostream &stream, QPoint const &p)
92 {
93     stream << "(" << p.x() << "," << p.y() << ")";
94     return stream;
95 }
96
97 ostream &operator<<(ostream &stream, QPointF const &p)
98 {
99     stream << "(" << p.x() << "," << p.y() << ")";
100     return stream;
101 }
102
103 ostream &operator<<(ostream &stream, QRectF const &r)
104 {
105     stream << "tL=" << r.topLeft() << " - (w,h)=" << r.width() << ","
106            << r.height() << "  bR=" << r.bottomRight();
107     return stream;
108 }
109
110 ostream &operator<<(ostream &stream, Vector const &p)
111 {
112     stream << "(" << p.x() << "," << p.y() << ")";
113     return stream;
114 }
115
116 qreal getAngle(const QPointF &p)
117 {
118     // Calculate angle of vector to x-axis
119     if (p.x() == 0) {
120         if (p.y() >= 0)
121             return M_PI_2;
122         else
123             return 3 * M_PI_2;
124     }
125     else {
126         if (p.x() > 0) {
127             if (p.y() < 0)
128                 return (qreal)(-atan((qreal)(p.y()) / (qreal)(p.x())));
129             else
130                 return (qreal)(2 * M_PI -
131                                atan((qreal)(p.y()) / (qreal)(p.x())));
132         }
133         else
134             return (qreal)(M_PI - atan((qreal)(p.y()) / (qreal)(p.x())));
135     }
136     /*
137     // Calculate angle of vector to y-axis
138     if (p.y()==0)
139     {
140     if (p.x()>=0)
141         return M_PI_2;
142     else
143         return 3* M_PI_2;
144     } else
145     {
146     if (p.y()>0)
147         return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
148     else
149         if (p.x()<0)
150         return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
151         else
152         return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
153     }
154     */
155 }
156
157 qreal min(qreal a, qreal b)
158 {
159     if (a < b)
160         return a;
161     return b;
162 }
163
164 qreal max(qreal a, qreal b)
165 {
166     if (a > b)
167         return a;
168     return b;
169 }
170
171 qreal roof(qreal x)
172 {
173     if (x <= 0.5)
174         return x;
175     else
176         return 1 - x;
177 }
178
179 int round_int(qreal x) { return (x > 0.0) ? (x + 0.5) : (x - 0.5); }
180
181 Qt::PenStyle penStyle(const QString &s, bool &ok)
182 {
183     ok = true;
184     Qt::PenStyle p(Qt::NoPen);
185     if (s == "Qt::NoPen")
186         p = Qt::SolidLine;
187     if (s == "Qt::SolidLine")
188         p = Qt::SolidLine;
189     else if (s == "Qt::DashLine")
190         p = Qt::DashLine;
191     else if (s == "Qt::DotLine")
192         p = Qt::DotLine;
193     else if (s == "Qt::DashDotLine")
194         p = Qt::DashDotLine;
195     else if (s == "Qt::DashDotDotLine")
196         p = Qt::DashDotDotLine;
197     else {
198         qWarning() << "misc.cpp penStyle - Unknown style s=" << s;
199         ok = false;
200     }
201     return p;
202 }
203
204 QString penStyleToString(Qt::PenStyle p)
205 {
206     switch (p) {
207     case Qt::NoPen:
208         return "Qt::NoPen";
209     case Qt::SolidLine:
210         return "Qt::SolidLine";
211     case Qt::DashLine:
212         return "Qt::DashLine";
213     case Qt::DotLine:
214         return "Qt::DotLine";
215     case Qt::DashDotLine:
216         return "Qt::DashDotLine";
217     case Qt::DashDotDotLine:
218         return "Qt::DashDotDotLine";
219     default:
220         return "";
221     }
222 }
223
224 QPointF point(const QString &s, bool &ok)
225 {
226     ok = true;
227     bool okx, oky;
228     qreal x = s.section(',', 0, 0).toFloat(&okx);
229     qreal y = s.section(',', 1, 1).toFloat(&oky);
230     if (okx && oky)
231         return QPointF(x, y);
232     else
233         qWarning() << "misc.cpp Couldn't create QPointF from " << s
234                    << "  ok=" << okx << "," << oky;
235     ok = false;
236     return QPointF();
237 }
238
239 QString pointToString(const QPointF &p)
240 {
241     return QString("%1,%2").arg(p.x()).arg(p.y());
242 }
243
244 void centerDialog(QDialog *dia)
245 {
246     dia->move(QCursor::pos() -
247               0.5 * QPoint(dia->rect().width(), dia->rect().height()));
248 }
249
250 // #include "version.h"
251
252 // #include <QDebug>
253 // #include <QRegExp>
254
255 bool versionLowerThanVym(const QString &v)
256 {
257     // returns true, if Version v <  VYM_VERSION
258     if (v == vymVersion)
259         return false;
260     else
261         return versionLowerOrEqualThanVym(v);
262 }
263
264 bool versionLowerOrEqualThanVym(const QString &v)
265 {
266     // returns true, if Version v <=  VYM_VERSION
267     return versionLowerOrEqual(v, vymVersion);
268 }
269
270 bool versionLowerOrEqual(const QString &v, const QString &vstatic)
271 {
272     // returns true, if version v <= vstatic
273     bool ok = false;
274     int v1 = 0;
275     int v2 = 0;
276     int v3 = 0;
277     int vs1 = 0;
278     int vs2 = 0;
279     int vs3 = 0;
280
281     QRegExp rx("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
282     int pos = rx.indexIn(v);
283     if (pos > -1) {
284         v1 = rx.cap(1).toInt(&ok);
285         if (ok)
286             v2 = rx.cap(2).toInt(&ok);
287         if (ok)
288             v3 = rx.cap(3).toInt(&ok);
289     }
290
291     pos = rx.indexIn(vstatic);
292     if (ok && pos > -1) {
293         vs1 = rx.cap(1).toInt(&ok);
294         if (ok)
295             vs2 = rx.cap(2).toInt(&ok);
296         if (ok)
297             vs3 = rx.cap(3).toInt(&ok);
298     }
299
300     if (!ok) {
301         qWarning() << QString(
302                           "Warning: Checking version failed: v=%1  vstatic=%2")
303                           .arg(v)
304                           .arg(vstatic);
305         return false;
306     }
307
308     if (vs1 > v1)
309         return true;
310     if (vs1 < v1)
311         return false;
312     if (vs2 > v2)
313         return true;
314     if (vs2 < v2)
315         return false;
316     if (vs3 > v3)
317         return true;
318     if (vs3 < v3)
319         return false;
320     return true;
321 }