misc.cpp
author insilmaril
Tue, 30 Aug 2005 14:09:19 +0000
changeset 158 a33a2bdd8299
parent 139 087e60400acc
child 163 30b22f7bd009
permissions -rw-r--r--
bugfixes for copy/paste of vymlink and hidden floatimages
     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 
    85 	if (s==d) 
    86 	{
    87 		// Special case, we just need the name of the file,
    88 		// not the complete path
    89 		i=d.findRev ("/");
    90 		d=d.right (d.length()-i-1);
    91 	} else
    92 	{
    93 		// Find relative path from src to dst
    94 
    95 		// Remove the first "/"
    96 		if (s.section ("/",0,0).isEmpty()) 
    97 		{
    98 			s=s.right (s.length()-1);
    99 			d=d.right (d.length()-1);
   100 		}
   101 		
   102 		// remove identical left parts
   103 		while (s.section("/",0,0) == d.section("/",0,0) ) 
   104 		{
   105 			i=s.find ("/");
   106 			s=s.right (s.length()-i-1);
   107 			d=d.right (d.length()-i-1);
   108 		}
   109 
   110 		int srcsep=s.contains("/");
   111 		int dstsep=d.contains("/");
   112 		if (srcsep >=  dstsep )
   113 		{
   114 			// find path to go up first and then back to dst
   115 			i=1;
   116 			while (i<=srcsep) 
   117 			{
   118 				d="../"+d;
   119 				i++;
   120 			}	
   121 		}
   122 	}	
   123 	return d;
   124 }
   125 
   126 // returns masked "<" ">" "&"
   127 QString quotemeta(const QString &s)
   128 {
   129 	QString r=s;
   130     QRegExp  rx("&(?!amp;)");
   131     r.replace ( rx,"&amp;");
   132     rx.setPattern( ">");
   133     r.replace ( rx,"&gt;");
   134     rx.setPattern( "<");
   135     r.replace ( rx,"&lt;");
   136     rx.setPattern( "\"");
   137     r.replace ( rx,"&quot;");
   138     return r;
   139 }
   140 
   141 int max(int a, int b)
   142 {
   143 	if (a>b)
   144 		return a;
   145 	return b;
   146 }
   147 
   148 int xmlObj::actindent=0;		// make instance of actindent
   149 
   150 xmlObj::xmlObj()
   151 {
   152     indentwidth=4;
   153 }
   154 
   155 
   156 // returns <s at />
   157 QString xmlObj::singleElement(QString s, QString at)
   158 {
   159     return indent() + "<" + s +" " + at +" " + "/>\n";
   160 }
   161 
   162 // returns <s>
   163 QString xmlObj::beginElement(QString s)
   164 {
   165     return indent() + "<" + s + ">\n";
   166 }
   167 
   168 // returns <s at>
   169 QString xmlObj::beginElement(QString s, QString at)
   170 {
   171     return indent() + "<" + s + " " + at + ">\n";
   172 }
   173 
   174 // returns </s>
   175 QString xmlObj::endElement(QString s)
   176 {
   177     return indent() + "</" + s + ">\n";
   178 }
   179 
   180 // returns  at="val"
   181 QString xmlObj::attribut(QString at, QString val)
   182 {
   183     return " " + at + "=\"" + quotemeta (val) + "\""; 
   184 }
   185 
   186 // returns <s> val </s>
   187 QString xmlObj::valueElement(QString el, QString val)
   188 {
   189     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
   190 }
   191 
   192 // returns <s at> val </s>
   193 QString xmlObj::valueElement(QString el, QString val, QString at)
   194 {
   195     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
   196 }
   197 
   198 void xmlObj::incIndent()
   199 {
   200     actindent++;
   201 }	
   202 
   203 void xmlObj::decIndent()
   204 {
   205     actindent--;
   206     if (actindent<0) actindent=0;
   207 }	
   208 
   209 QString xmlObj::indent()
   210 {
   211     QString s;
   212     int i;
   213     for (i=0; i<actindent*indentwidth; i++)
   214     {
   215 		s= s + " ";
   216     }
   217     return s;
   218 }	
   219 
   220 
   221 
   222 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
   223 {
   224 }
   225 
   226 void ImagePreview::previewUrl( const QUrl &u )
   227 {
   228     QString path = u.path();
   229     QPixmap pix( path );
   230     if ( pix.isNull() )
   231         setText( QObject::tr("This is not an image.") );
   232     else
   233 	{
   234 		float max_w=300;
   235 		float max_h=300;
   236 		float r;
   237 		if (pix.width()>max_w)
   238 		{
   239 			r=max_w / pix.width();
   240 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   241 			// TODO not a resize, but a shrink/enlarge is needed here...
   242 		}
   243 		if (pix.height()>max_h)
   244 		{
   245 			r=max_h / pix.height();
   246 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   247 			// TODO not a resize, but a shrink/enlarge is needed here...
   248 		}
   249         setPixmap( pix );
   250 	}	
   251 }
   252