9 QString qpointToString (const QPoint &p)
11 return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
14 ostream &operator<< (ostream &stream, QPoint const &p)
16 return (stream << qpointToString(p) );
19 float getAngle(const QPoint &p)
21 // Calculate angle of vector to y-axis
31 return (float)(M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
34 return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
36 return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
40 QPoint normalise(const QPoint &p)
42 // Calculate normalised position (fixed length)
70 return QPoint ((int) (x),(int) (y));
73 QString maskPath(QString p)
75 // Change " " to "\ " to enable blanks in filenames
76 p=p.replace(QChar('&'),"\\&");
77 return p.replace(QChar(' '),"\\ ");
80 QString convertToRel (const QString &src, const QString &dst)
88 // Special case, we just need the name of the file,
89 // not the complete path
91 d=d.right (d.length()-i-1);
94 // Find relative path from src to dst
96 // Remove the first "/"
97 if (s.section ("/",0,0).isEmpty())
99 s=s.right (s.length()-1);
100 d=d.right (d.length()-1);
103 // remove identical left parts
104 while (s.section("/",0,0) == d.section("/",0,0) )
107 s=s.right (s.length()-i-1);
108 d=d.right (d.length()-i-1);
111 int srcsep=s.contains("/");
112 int dstsep=d.contains("/");
113 if (srcsep >= dstsep )
115 // find path to go up first and then back to dst
127 QString makeUniqueDir (QString s)
130 int bytes=s.length();
131 p=(char*) malloc (bytes+1);
133 for (i=0;i<bytes;i++)
134 p[i]=s.at(i).latin1();
136 QString r=mkdtemp (p);
141 void removeDir(QDir d)
143 if (d.path().left(4)!="/tmp")
146 qWarning ("misc.cpp::removeDir should remove "+d.path()+" - aborted.");
150 // Traverse directories
151 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
152 const QFileInfoList *dirlist = d.entryInfoList();
153 QFileInfoListIterator itdir( *dirlist );
156 while ( (fi = itdir.current()) != 0 )
158 if (fi->fileName() != "." && fi->fileName() != ".." )
160 if ( !d.cd(fi->fileName()) )
161 qWarning ("removeDir() cannot find the directory "+fi->fileName());
164 // Recursively remove subdirs
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 )
178 QFile (fi->filePath()).remove();
183 if (!d.rmdir(d.path()))
184 qWarning ("removeDir("+d.path()+") failed!");
187 void makeSubDirs (const QString &s)
195 // returns masked "<" ">" "&"
196 QString quotemeta(const QString &s)
199 QRegExp rx("&(?!amp;)");
200 r.replace ( rx,"&");
202 r.replace ( rx,">");
204 r.replace ( rx,"<");
205 rx.setPattern( "\"");
206 r.replace ( rx,""");
210 int max(int a, int b)
217 int xmlObj::actindent=0; // make instance of actindent
226 QString xmlObj::singleElement(QString s, QString at)
228 return indent() + "<" + s +" " + at +" " + "/>\n";
232 QString xmlObj::beginElement(QString s)
234 return indent() + "<" + s + ">\n";
238 QString xmlObj::beginElement(QString s, QString at)
240 return indent() + "<" + s + " " + at + ">\n";
244 QString xmlObj::endElement(QString s)
246 return indent() + "</" + s + ">\n";
250 QString xmlObj::attribut(QString at, QString val)
252 return " " + at + "=\"" + quotemeta (val) + "\"";
255 // returns <s> val </s>
256 QString xmlObj::valueElement(QString el, QString val)
258 return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
261 // returns <s at> val </s>
262 QString xmlObj::valueElement(QString el, QString val, QString at)
264 return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
267 void xmlObj::incIndent()
272 void xmlObj::decIndent()
275 if (actindent<0) actindent=0;
278 QString xmlObj::indent()
282 for (i=0; i<actindent*indentwidth; i++)
291 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
295 void ImagePreview::previewUrl( const QUrl &u )
297 QString path = u.path();
300 setText( QObject::tr("This is not an image.") );
306 if (pix.width()>max_w)
308 r=max_w / pix.width();
309 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
310 // TODO not a resize, but a shrink/enlarge is needed here...
312 if (pix.height()>max_h)
314 r=max_h / pix.height();
315 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
316 // TODO not a resize, but a shrink/enlarge is needed here...