]> git.sven.stormbind.net Git - sven/vym.git/blob - misc.cpp
Import Upstream version 2.6.11
[sven/vym.git] / 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 QString richTextToPlain (QString r, const QString &indent, const int &width)
13 {
14     Q_UNUSED( width );
15
16     // Avoid failing assert with mingw
17     if (r.isEmpty()) return r;
18
19     QRegExp rx;
20     rx.setMinimal(true);
21
22     // Remove all <style...> ...</style>
23     rx.setPattern("<style.*>.*</style>");
24     r.replace (rx,"");
25
26     // convert all "<br*>" to "\n"
27     rx.setPattern ("<br.*>");
28     r.replace (rx,"\n");
29
30     // convert all "</p>" to "\n"
31     rx.setPattern ("</p>");
32     r.replace (rx,"\n");
33
34     // remove all remaining tags
35     rx.setPattern ("<.*>");
36     r.replace (rx,"");
37
38     // If string starts with \n now, remove it.
39     // It would be wrong in an OOo export for example
40     while (r.length() > 0 && r.at(0)=='\n') r.remove (0,1);
41
42     // convert "&", "<" and ">"
43     rx.setPattern ("&gt;");
44     r.replace (rx,">");
45     rx.setPattern ("&lt;");
46     r.replace (rx,"<");
47     rx.setPattern ("&amp;");
48     r.replace (rx,"&");
49     rx.setPattern ("&quot;");
50     r.replace (rx,"\"");
51
52     // Indent everything
53     rx.setPattern ("^\n");
54     r.replace (rx,indent);
55     r = indent + r;   // Don't forget first line
56
57     return r;
58 }
59
60 QString qpointToString (const QPoint &p)
61 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
62
63 QString qpointFToString (const QPointF &p)
64 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
65
66 QString VectorToString (const Vector &p)
67 { return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")"; }
68
69 ostream &operator<< (ostream &stream, QPoint const &p)
70
71     stream << "("<<p.x()<<","<<p.y()<<")";
72     return stream;
73 }
74
75 ostream &operator<< (ostream &stream, QPointF const &p)
76
77     stream << "("<<p.x()<<","<<p.y()<<")";
78     return stream;
79 }
80
81 ostream &operator<< (ostream &stream, QRectF const &r)
82
83     stream << "tL="<<r.topLeft()<<" - (w,h)="<<r.width()<<","<<r.height()<<"  bR="<<r.bottomRight();
84     return stream;
85 }
86
87 ostream &operator<< (ostream &stream, Vector const &p)
88
89     stream << "("<<p.x()<<","<<p.y()<<")";
90     return stream;
91 }
92
93 qreal getAngle(const QPointF &p)
94 {   
95     // Calculate angle of vector to x-axis
96     if (p.x()==0)
97     {
98         if (p.y()>=0)
99             return M_PI_2;
100         else
101             return 3* M_PI_2;
102     } else
103     {
104         if (p.x()>0) 
105         {
106             if (p.y()<0)
107                 return (qreal)( - atan ( (qreal)(p.y()) / (qreal)(p.x()) ) );
108             else        
109                 return (qreal)( 2*M_PI - atan ( (qreal)(p.y()) / (qreal)(p.x()) ) );
110         }    
111         else    
112             return (qreal)(M_PI -atan ( (qreal)(p.y()) / (qreal)(p.x()) ) );
113     }   
114     /*
115     // Calculate angle of vector to y-axis
116     if (p.y()==0)
117     {
118         if (p.x()>=0)
119             return M_PI_2;
120         else
121             return 3* M_PI_2;
122     } else
123     {
124         if (p.y()>0) 
125             return (qreal)(M_PI  - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
126         else    
127             if (p.x()<0)
128                 return (qreal)( 2*M_PI - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
129             else    
130                 return (qreal)( - atan ( (qreal)(p.x()) / (qreal)(p.y()) ) );
131     }   
132     */
133 }
134
135 qreal min(qreal a, qreal b)
136 {
137     if (a<b)
138         return a;
139     return b;
140 }
141
142 qreal max(qreal a, qreal b)
143 {
144     if (a>b)
145         return a;
146     return b;
147 }
148
149 qreal roof (qreal x)
150 {
151     if (x<=0.5)
152         return  x;
153     else
154         return 1-x;
155 }
156
157 int round_int (qreal x)
158 {
159     return (x > 0.0) ? (x + 0.5) : (x - 0.5);
160 }
161
162 Qt::PenStyle penStyle (const QString &s, bool &ok)
163 {
164     ok=true;
165     Qt::PenStyle p(Qt::NoPen);
166     if (s=="Qt::NoPen")
167         p=Qt::SolidLine;
168     if (s=="Qt::SolidLine")
169         p=Qt::SolidLine;
170     else if (s=="Qt::DashLine")
171         p=Qt::DashLine;
172     else if (s=="Qt::DotLine")
173         p=Qt::DotLine;
174     else if (s=="Qt::DashDotLine")
175         p=Qt::DashDotLine;
176     else if (s=="Qt::DashDotDotLine")
177         p=Qt::DashDotDotLine;
178     else
179     {
180         qWarning()<<"misc.cpp penStyle - Unknown style s="<<s;
181         ok=false;
182     }
183     return p;
184 }
185
186 QString penStyleToString (Qt::PenStyle p) 
187 {
188     switch (p)
189     {
190         case Qt::NoPen:
191             return "Qt::NoPen";
192         case Qt::SolidLine:
193             return "Qt::SolidLine";
194         case Qt::DashLine:
195             return "Qt::DashLine";
196         case Qt::DotLine:
197             return "Qt::DotLine";
198         case Qt::DashDotLine:
199             return "Qt::DashDotLine";
200         case Qt::DashDotDotLine:
201             return "Qt::DashDotDotLine";
202         default:
203             return "";
204     }
205 }
206
207 QPointF point (const QString &s, bool &ok)
208 {
209     ok=true;
210     bool okx, oky;
211     qreal x=s.section (',',0,0).toFloat (&okx);
212     qreal y=s.section (',',1,1).toFloat (&oky);
213     if (okx && oky) 
214         return QPointF (x,y);
215     else        
216         qWarning()<<"misc.cpp Couldn't create QPointF from "<<s<<"  ok="<<okx<<","<<oky;
217         ok=false;
218         return QPointF();
219 }
220    
221 QString pointToString (const QPointF &p)
222 {
223     return QString("%1,%2").arg(p.x()).arg(p.y());
224 }
225
226 void centerDialog (QDialog *dia)
227 {
228     dia->move(QCursor::pos() - 0.5 * QPoint(dia->rect().width(),dia->rect().height() ) );
229 }
230