misc.cpp
author insilmaril
Tue, 03 Jan 2006 09:44:41 +0000
changeset 174 3674e9236634
parent 166 325958acb69b
child 176 f75489896084
permissions -rw-r--r--
1.7.6 New features for floatimages and fixes
     1 #include <math.h>
     2 
     3 #include <qregexp.h>
     4 #include <qpoint.h>
     5 #include <stdlib.h>
     6 
     7 #include "misc.h"
     8 
     9 QString qpointToString (const QPoint &p)
    10 {
    11 	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
    12 }
    13 
    14 ostream &operator<< (ostream &stream, QPoint const &p)
    15 { 
    16     return (stream << qpointToString(p) );
    17 }
    18 
    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 }
    39 
    40 QPoint normalise(const QPoint &p)
    41 {	
    42 	// Calculate normalised position (fixed length) 
    43 
    44 	double px=p.x();
    45 	double py=p.y();
    46 	double x;
    47 	double y;
    48 	double r=150;
    49 
    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;
    65 		
    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 }
    72 
    73 QString maskPath(QString p)
    74 {
    75 	// Change " " to "\ " to enable blanks in filenames
    76 	p=p.replace(QChar('&'),"\\&");
    77 	return p.replace(QChar(' '),"\\ ");
    78 }
    79 
    80 QString convertToRel (const QString &src, const QString &dst)
    81 {
    82 	QString s=src;
    83 	QString d=dst;
    84 	int i;
    85 
    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
    95 
    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 		}
   102 		
   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 		}
   110 
   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 }
   126 
   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 }
   140 
   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 	}
   149 
   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;
   155 
   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 );
   175 
   176 	while ( (fi = itfile.current()) != 0 ) 
   177 	{
   178 		QFile (fi->filePath()).remove();
   179 		
   180 		++itfile;
   181 	}	
   182 
   183 	if (!d.rmdir(d.path()))
   184 		qWarning ("removeDir("+d.path()+") failed!");
   185 }		
   186 
   187 void makeSubDirs (const QString &s)
   188 {
   189 	QDir d(s);
   190 	d.mkdir(s);
   191 	d.mkdir ("images");	
   192 	d.mkdir ("flags");	
   193 }
   194 
   195 // returns masked "<" ">" "&"
   196 QString quotemeta(const QString &s)
   197 {
   198 	QString r=s;
   199     QRegExp  rx("&(?!amp;)");
   200     r.replace ( rx,"&amp;");
   201     rx.setPattern( ">");
   202     r.replace ( rx,"&gt;");
   203     rx.setPattern( "<");
   204     r.replace ( rx,"&lt;");
   205     rx.setPattern( "\"");
   206     r.replace ( rx,"&quot;");
   207     return r;
   208 }
   209 
   210 int max(int a, int b)
   211 {
   212 	if (a>b)
   213 		return a;
   214 	return b;
   215 }
   216 
   217 int xmlObj::actindent=0;		// make instance of actindent
   218 
   219 xmlObj::xmlObj()
   220 {
   221     indentwidth=4;
   222 }
   223 
   224 
   225 // returns <s at />
   226 QString xmlObj::singleElement(QString s, QString at)
   227 {
   228     return indent() + "<" + s +" " + at +" " + "/>\n";
   229 }
   230 
   231 // returns <s>
   232 QString xmlObj::beginElement(QString s)
   233 {
   234     return indent() + "<" + s + ">\n";
   235 }
   236 
   237 // returns <s at>
   238 QString xmlObj::beginElement(QString s, QString at)
   239 {
   240     return indent() + "<" + s + " " + at + ">\n";
   241 }
   242 
   243 // returns </s>
   244 QString xmlObj::endElement(QString s)
   245 {
   246     return indent() + "</" + s + ">\n";
   247 }
   248 
   249 // returns  at="val"
   250 QString xmlObj::attribut(QString at, QString val)
   251 {
   252     return " " + at + "=\"" + quotemeta (val) + "\""; 
   253 }
   254 
   255 // returns <s> val </s>
   256 QString xmlObj::valueElement(QString el, QString val)
   257 {
   258     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
   259 }
   260 
   261 // returns <s at> val </s>
   262 QString xmlObj::valueElement(QString el, QString val, QString at)
   263 {
   264     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
   265 }
   266 
   267 void xmlObj::incIndent()
   268 {
   269     actindent++;
   270 }	
   271 
   272 void xmlObj::decIndent()
   273 {
   274     actindent--;
   275     if (actindent<0) actindent=0;
   276 }	
   277 
   278 QString xmlObj::indent()
   279 {
   280     QString s;
   281     int i;
   282     for (i=0; i<actindent*indentwidth; i++)
   283     {
   284 		s= s + " ";
   285     }
   286     return s;
   287 }	
   288 
   289 
   290 
   291 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
   292 {
   293 }
   294 
   295 void ImagePreview::previewUrl( const QUrl &u )
   296 {
   297     QString path = u.path();
   298     QPixmap pix( path );
   299     if ( pix.isNull() )
   300         setText( QObject::tr("This is not an image.") );
   301     else
   302 	{
   303 		float max_w=300;
   304 		float max_h=300;
   305 		float r;
   306 		if (pix.width()>max_w)
   307 		{
   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...
   311 		}
   312 		if (pix.height()>max_h)
   313 		{
   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...
   317 		}
   318         setPixmap( pix );
   319 	}	
   320 }
   321