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