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