misc.cpp
 author insilmaril Thu, 22 Sep 2005 12:14:23 +0000 changeset 164 d442a66e9121 parent 163 30b22f7bd009 child 166 325958acb69b permissions -rw-r--r--
new way to handle tmpdir, minor changes
1 #include <math.h>
3 #include <qregexp.h>
4 #include <qpoint.h>
5 #include <stdlib.h>
7 #include "misc.h"
9 QString qpointToString (const QPoint &p)
10 {
11 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
12 }
14 ostream &operator<< (ostream &stream, QPoint const &p)
15 {
16     return (stream << qpointToString(p) );
17 }
19 float getAngle(const QPoint &p)
20 {
21 	// Calculate angle of vector to y-axis
22 	if (p.y()==0)
23 	{
24 		if (p.x()>=0)
25 			return M_PI_2;
26 		else
27 			return 3* M_PI_2;
28 	} else
29 	{
30 		if (p.y()>0)
31 			return (float)(M_PI  - atan ( (double)(p.x()) / (double)(p.y()) ) );
32 		else
33 			if (p.x()<0)
34 				return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
35 			else
36 				return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
37 	}
38 }
40 QPoint normalise(const QPoint &p)
41 {
42 	// Calculate normalised position (fixed length)
44 	double px=p.x();
45 	double py=p.y();
46 	double x;
47 	double y;
48 	double r=150;
50 	if (px==0)
51 	{
52 		x=0;
53 		if (py>=0)
54 			y=r;
55 		else
56 			y=-r;
57 	} else
58 	{
59 		double sign;
60 		double a;
61 		if (px>0)
62 			sign=1;
63 		else
64 			sign=-1;
66 		a=atan (py / px);
67 		x=cos (a) * r *sign;
68 		y=sin (a) * r *sign;
69 	}
70 	return QPoint ((int) (x),(int) (y));
71 }
74 {
75 	// Change " " to "\ " to enable blanks in filenames
76 	p=p.replace(QChar('&'),"\\&");
77 	return p.replace(QChar(' '),"\\ ");
78 }
80 QString convertToRel (const QString &src, const QString &dst)
81 {
82 	QString s=src;
83 	QString d=dst;
84 	int i;
86 	if (s==d)
87 	{
88 		// Special case, we just need the name of the file,
89 		// not the complete path
90 		i=d.findRev ("/");
91 		d=d.right (d.length()-i-1);
92 	} else
93 	{
94 		// Find relative path from src to dst
96 		// Remove the first "/"
97 		if (s.section ("/",0,0).isEmpty())
98 		{
99 			s=s.right (s.length()-1);
100 			d=d.right (d.length()-1);
101 		}
103 		// remove identical left parts
104 		while (s.section("/",0,0) == d.section("/",0,0) )
105 		{
106 			i=s.find ("/");
107 			s=s.right (s.length()-i-1);
108 			d=d.right (d.length()-i-1);
109 		}
111 		int srcsep=s.contains("/");
112 		int dstsep=d.contains("/");
113 		if (srcsep >=  dstsep )
114 		{
115 			// find path to go up first and then back to dst
116 			i=1;
117 			while (i<=srcsep)
118 			{
119 				d="../"+d;
120 				i++;
121 			}
122 		}
123 	}
124 	return d;
125 }
127 QString makeUniqueDir (QString s)
128 {
129 	char *p;
130 	int bytes=s.length();
131 	p=(char*) malloc (bytes+1);
132 	int i;
133 	for (i=0;i<bytes;i++)
134 		p[i]=s.at(i).latin1();
135 	p[bytes]=0;
136 	QString r=mkdtemp (p);
137 	free (p);
138 	return r;
139 }
141 void removeDir(QDir d)
142 {
143 	if (d.path().left(4)!="/tmp")
144 	{
145 		// FIXME testing
146 		qWarning ("misc.cpp::removeDir should remove "+d.path()+" - aborted.");
147 		return;
148 	}
150 	// Traverse directories
151 	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
152 	const QFileInfoList *dirlist = d.entryInfoList();
153 	QFileInfoListIterator itdir( *dirlist );
154 	QFileInfo *fi;
156 	while ( (fi = itdir.current()) != 0 )
157 	{
158 		if (fi->fileName() != "." && fi->fileName() != ".." )
159 		{
160 			if ( !d.cd(fi->fileName()) )
161 				qWarning ("removeDir() cannot find the directory "+fi->fileName());
162 			else
163 			{
164 				// Recursively remove subdirs
165 				removeDir (d);
166 				d.cdUp();
167 			}
168 		}
169 		++itdir;
170 	}
171 	// Traverse files
172 	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
173 	const QFileInfoList *filelist = d.entryInfoList();
174 	QFileInfoListIterator itfile( *filelist );
176 	while ( (fi = itfile.current()) != 0 )
177 	{
178 		QFile (fi->filePath()).remove();
180 		++itfile;
181 	}
183 	if (!d.rmdir(d.path()))
184 		qWarning ("removeDir("+d.path()+") failed!");
185 }
187 // returns masked "<" ">" "&"
188 QString quotemeta(const QString &s)
189 {
190 	QString r=s;
191     QRegExp  rx("&(?!amp;)");
192     r.replace ( rx,"&amp;");
193     rx.setPattern( ">");
194     r.replace ( rx,"&gt;");
195     rx.setPattern( "<");
196     r.replace ( rx,"&lt;");
197     rx.setPattern( "\"");
198     r.replace ( rx,"&quot;");
199     return r;
200 }
202 int max(int a, int b)
203 {
204 	if (a>b)
205 		return a;
206 	return b;
207 }
209 int xmlObj::actindent=0;		// make instance of actindent
211 xmlObj::xmlObj()
212 {
213     indentwidth=4;
214 }
217 // returns <s at />
218 QString xmlObj::singleElement(QString s, QString at)
219 {
220     return indent() + "<" + s +" " + at +" " + "/>\n";
221 }
223 // returns <s>
224 QString xmlObj::beginElement(QString s)
225 {
226     return indent() + "<" + s + ">\n";
227 }
229 // returns <s at>
230 QString xmlObj::beginElement(QString s, QString at)
231 {
232     return indent() + "<" + s + " " + at + ">\n";
233 }
235 // returns </s>
236 QString xmlObj::endElement(QString s)
237 {
238     return indent() + "</" + s + ">\n";
239 }
241 // returns  at="val"
242 QString xmlObj::attribut(QString at, QString val)
243 {
244     return " " + at + "=\"" + quotemeta (val) + "\"";
245 }
247 // returns <s> val </s>
248 QString xmlObj::valueElement(QString el, QString val)
249 {
250     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
251 }
253 // returns <s at> val </s>
254 QString xmlObj::valueElement(QString el, QString val, QString at)
255 {
256     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
257 }
259 void xmlObj::incIndent()
260 {
261     actindent++;
262 }
264 void xmlObj::decIndent()
265 {
266     actindent--;
267     if (actindent<0) actindent=0;
268 }
270 QString xmlObj::indent()
271 {
272     QString s;
273     int i;
274     for (i=0; i<actindent*indentwidth; i++)
275     {
276 		s= s + " ";
277     }
278     return s;
279 }
283 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
284 {
285 }
287 void ImagePreview::previewUrl( const QUrl &u )
288 {
289     QString path = u.path();
290     QPixmap pix( path );
291     if ( pix.isNull() )
292         setText( QObject::tr("This is not an image.") );
293     else
294 	{
295 		float max_w=300;
296 		float max_h=300;
297 		float r;
298 		if (pix.width()>max_w)
299 		{
300 			r=max_w / pix.width();
301 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
302 			// TODO not a resize, but a shrink/enlarge is needed here...
303 		}
304 		if (pix.height()>max_h)
305 		{
306 			r=max_h / pix.height();
307 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
308 			// TODO not a resize, but a shrink/enlarge is needed here...
309 		}
310         setPixmap( pix );
311 	}
312 }