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