misc.cpp
author insilmaril
Sun, 30 Jan 2005 12:58:52 +0000
branchvendor
changeset 23 1773dcd0a0a4
parent 0 7a96bd401351
child 2 608f976aa7bb
child 95 f688a9913724
permissions -rw-r--r--
Initial import.
     1 #include <qregexp.h>
     2 #include <qpoint.h>
     3 
     4 #include <math.h>
     5 
     6 #include "misc.h"
     7 
     8 
     9 ostream &operator<< (ostream &stream, QPoint const &p)
    10 { 
    11     return (stream << "(" << p.x() << "," << p.y() << ")");
    12 }
    13 
    14 float getAngle(const QPoint &p)
    15 {	
    16 	// Calculate angle of vector to y-axis
    17 	if (p.y()==0)
    18 	{
    19 		if (p.x()>=0)
    20 			return M_PI_2;
    21 		else
    22 			return 3* M_PI_2;
    23 	} else
    24 	{
    25 		if (p.y()>0) 
    26 			return (float)(M_PI  - atan ( (double)(p.x()) / (double)(p.y()) ) );
    27 		else	
    28 			if (p.x()<0)
    29 				return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
    30 			else	
    31 				return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
    32 	}	
    33 }
    34 
    35 QPoint normalise(const QPoint &p)
    36 {	
    37 	// Calculate normalised position (fixed length) 
    38 
    39 	double px=p.x();
    40 	double py=p.y();
    41 	double x;
    42 	double y;
    43 	double r=150;
    44 
    45 	if (px==0)
    46 	{
    47 		x=0;
    48 		if (py>=0)
    49 			y=r;
    50 		else
    51 			y=-r;
    52 	} else
    53 	{
    54 		double sign;
    55 		double a;
    56 		if (px>0) 
    57 			sign=1; 
    58 		else 
    59 			sign=-1;
    60 		
    61 		a=atan (py / px);
    62 		x=cos (a) * r *sign;
    63 		y=sin (a) * r *sign;
    64 	}	
    65 	return QPoint ((int) (x),(int) (y));
    66 }
    67 
    68 QString maskPath(QString p)
    69 {
    70 	// Change " " to "\ " to enable blanks in filenames
    71 	p=p.replace(QChar('&'),"\\&");
    72 	return p.replace(QChar(' '),"\\ ");
    73 }
    74 
    75 QString convertToRel (const QString &src, const QString &dst)
    76 {
    77 	QString s=src;
    78 	QString d=dst;
    79 	int i;
    80 	// Find relative path from src to dst
    81 
    82 	// Remove the first "/"
    83 	if (s.section ("/",0,0).isEmpty()) 
    84 	{
    85 		s=s.right (s.length()-1);
    86 		d=d.right (d.length()-1);
    87 	}
    88 	
    89 	// remove identical left parts
    90 	while (s.section("/",0,0) == d.section("/",0,0) ) 
    91 	{
    92 		i=s.find ("/");
    93 		s=s.right (s.length()-i-1);
    94 		d=d.right (d.length()-i-1);
    95 	}
    96 
    97 	int srcsep=s.contains("/");
    98 	int dstsep=d.contains("/");
    99 	if (srcsep >=  dstsep )
   100 	{
   101 		// find path to go up first and then back to dst
   102 		i=1;
   103 		while (i<=srcsep) 
   104 		{
   105 			d="../"+d;
   106 			i++;
   107 		}	
   108 	}
   109 	return d;
   110 }
   111 
   112 // returns masked "<" ">" "&"
   113 QString quotemeta(const QString &s)
   114 {
   115 	QString r=s;
   116     QRegExp  rx("&(?!amp;)");
   117     r.replace ( rx,"&amp;");
   118     rx.setPattern( ">");
   119     r.replace ( rx,"&gt;");
   120     rx.setPattern( "<");
   121     r.replace ( rx,"&lt;");
   122     rx.setPattern( "\"");
   123     r.replace ( rx,"&quot;");
   124     return r;
   125 }
   126 
   127 int max(int a, int b)
   128 {
   129 	if (a>b)
   130 		return a;
   131 	return b;
   132 }
   133 
   134 int xmlObj::actindent=0;		// make instance of actindent
   135 
   136 xmlObj::xmlObj()
   137 {
   138     indentwidth=4;
   139 }
   140 
   141 
   142 // returns <s at />
   143 QString xmlObj::singleElement(QString s, QString at)
   144 {
   145     return indent() + "<" + s +" " + at +" " + "/>\n";
   146 }
   147 
   148 // returns <s>
   149 QString xmlObj::beginElement(QString s)
   150 {
   151     return indent() + "<" + s + ">\n";
   152 }
   153 
   154 // returns <s at>
   155 QString xmlObj::beginElement(QString s, QString at)
   156 {
   157     return indent() + "<" + s + " " + at + ">\n";
   158 }
   159 
   160 // returns </s>
   161 QString xmlObj::endElement(QString s)
   162 {
   163     return indent() + "</" + s + ">\n";
   164 }
   165 
   166 // returns  at="val"
   167 QString xmlObj::attribut(QString at, QString val)
   168 {
   169     return " " + at + "=\"" + quotemeta (val) + "\""; 
   170 }
   171 
   172 // returns <s> val </s>
   173 QString xmlObj::valueElement(QString el, QString val)
   174 {
   175     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
   176 }
   177 
   178 // returns <s at> val </s>
   179 QString xmlObj::valueElement(QString el, QString val, QString at)
   180 {
   181     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
   182 }
   183 
   184 void xmlObj::incIndent()
   185 {
   186     actindent++;
   187 }	
   188 
   189 void xmlObj::decIndent()
   190 {
   191     actindent--;
   192     if (actindent<0) actindent=0;
   193 }	
   194 
   195 QString xmlObj::indent()
   196 {
   197     QString s;
   198     int i;
   199     for (i=0; i<actindent*indentwidth; i++)
   200     {
   201 		s= s + " ";
   202     }
   203     return s;
   204 }	
   205 
   206 
   207 
   208 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
   209 {
   210 }
   211 
   212 void ImagePreview::previewUrl( const QUrl &u )
   213 {
   214     QString path = u.path();
   215     QPixmap pix( path );
   216     if ( pix.isNull() )
   217         setText( QObject::tr("This is not an image.") );
   218     else
   219 	{
   220 		float max_w=300;
   221 		float max_h=300;
   222 		float r;
   223 		if (pix.width()>max_w)
   224 		{
   225 			r=max_w / pix.width();
   226 			pix.resize(lrint(pix.width()*r), lrint(pix.height()*r));
   227 			// TODO not a resize, but a shrink/enlarge is needed here...
   228 		}
   229 		if (pix.height()>max_h)
   230 		{
   231 			r=max_h / pix.height();
   232 			pix.resize(lrint(pix.width()*r), lrint(pix.height()*r));
   233 			// TODO not a resize, but a shrink/enlarge is needed here...
   234 		}
   235         setPixmap( pix );
   236 	}	
   237 }
   238