1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/misc.cpp Mon Mar 30 07:50:51 2009 +0000
1.3 @@ -0,0 +1,238 @@
1.4 +#include <qregexp.h>
1.5 +#include <qpoint.h>
1.6 +
1.7 +#include <math.h>
1.8 +
1.9 +#include "misc.h"
1.10 +
1.11 +
1.12 +ostream &operator<< (ostream &stream, QPoint const &p)
1.13 +{
1.14 + return (stream << "(" << p.x() << "," << p.y() << ")");
1.15 +}
1.16 +
1.17 +float getAngle(const QPoint &p)
1.18 +{
1.19 + // Calculate angle of vector to y-axis
1.20 + if (p.y()==0)
1.21 + {
1.22 + if (p.x()>=0)
1.23 + return M_PI_2;
1.24 + else
1.25 + return 3* M_PI_2;
1.26 + } else
1.27 + {
1.28 + if (p.y()>0)
1.29 + return (float)(M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
1.30 + else
1.31 + if (p.x()<0)
1.32 + return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
1.33 + else
1.34 + return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
1.35 + }
1.36 +}
1.37 +
1.38 +QPoint normalise(const QPoint &p)
1.39 +{
1.40 + // Calculate normalised position (fixed length)
1.41 +
1.42 + double px=p.x();
1.43 + double py=p.y();
1.44 + double x;
1.45 + double y;
1.46 + double r=150;
1.47 +
1.48 + if (px==0)
1.49 + {
1.50 + x=0;
1.51 + if (py>=0)
1.52 + y=r;
1.53 + else
1.54 + y=-r;
1.55 + } else
1.56 + {
1.57 + double sign;
1.58 + double a;
1.59 + if (px>0)
1.60 + sign=1;
1.61 + else
1.62 + sign=-1;
1.63 +
1.64 + a=atan (py / px);
1.65 + x=cos (a) * r *sign;
1.66 + y=sin (a) * r *sign;
1.67 + }
1.68 + return QPoint ((int) (x),(int) (y));
1.69 +}
1.70 +
1.71 +QString maskPath(QString p)
1.72 +{
1.73 + // Change " " to "\ " to enable blanks in filenames
1.74 + p=p.replace(QChar('&'),"\\&");
1.75 + return p.replace(QChar(' '),"\\ ");
1.76 +}
1.77 +
1.78 +QString convertToRel (const QString &src, const QString &dst)
1.79 +{
1.80 + QString s=src;
1.81 + QString d=dst;
1.82 + int i;
1.83 + // Find relative path from src to dst
1.84 +
1.85 + // Remove the first "/"
1.86 + if (s.section ("/",0,0).isEmpty())
1.87 + {
1.88 + s=s.right (s.length()-1);
1.89 + d=d.right (d.length()-1);
1.90 + }
1.91 +
1.92 + // remove identical left parts
1.93 + while (s.section("/",0,0) == d.section("/",0,0) )
1.94 + {
1.95 + i=s.find ("/");
1.96 + s=s.right (s.length()-i-1);
1.97 + d=d.right (d.length()-i-1);
1.98 + }
1.99 +
1.100 + int srcsep=s.contains("/");
1.101 + int dstsep=d.contains("/");
1.102 + if (srcsep >= dstsep )
1.103 + {
1.104 + // find path to go up first and then back to dst
1.105 + i=1;
1.106 + while (i<=srcsep)
1.107 + {
1.108 + d="../"+d;
1.109 + i++;
1.110 + }
1.111 + }
1.112 + return d;
1.113 +}
1.114 +
1.115 +// returns masked "<" ">" "&"
1.116 +QString quotemeta(const QString &s)
1.117 +{
1.118 + QString r=s;
1.119 + QRegExp rx("&(?!amp;)");
1.120 + r.replace ( rx,"&");
1.121 + rx.setPattern( ">");
1.122 + r.replace ( rx,">");
1.123 + rx.setPattern( "<");
1.124 + r.replace ( rx,"<");
1.125 + rx.setPattern( "\"");
1.126 + r.replace ( rx,""");
1.127 + return r;
1.128 +}
1.129 +
1.130 +int max(int a, int b)
1.131 +{
1.132 + if (a>b)
1.133 + return a;
1.134 + return b;
1.135 +}
1.136 +
1.137 +int xmlObj::actindent=0; // make instance of actindent
1.138 +
1.139 +xmlObj::xmlObj()
1.140 +{
1.141 + indentwidth=4;
1.142 +}
1.143 +
1.144 +
1.145 +// returns <s at />
1.146 +QString xmlObj::singleElement(QString s, QString at)
1.147 +{
1.148 + return indent() + "<" + s +" " + at +" " + "/>\n";
1.149 +}
1.150 +
1.151 +// returns <s>
1.152 +QString xmlObj::beginElement(QString s)
1.153 +{
1.154 + return indent() + "<" + s + ">\n";
1.155 +}
1.156 +
1.157 +// returns <s at>
1.158 +QString xmlObj::beginElement(QString s, QString at)
1.159 +{
1.160 + return indent() + "<" + s + " " + at + ">\n";
1.161 +}
1.162 +
1.163 +// returns </s>
1.164 +QString xmlObj::endElement(QString s)
1.165 +{
1.166 + return indent() + "</" + s + ">\n";
1.167 +}
1.168 +
1.169 +// returns at="val"
1.170 +QString xmlObj::attribut(QString at, QString val)
1.171 +{
1.172 + return " " + at + "=\"" + quotemeta (val) + "\"";
1.173 +}
1.174 +
1.175 +// returns <s> val </s>
1.176 +QString xmlObj::valueElement(QString el, QString val)
1.177 +{
1.178 + return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
1.179 +}
1.180 +
1.181 +// returns <s at> val </s>
1.182 +QString xmlObj::valueElement(QString el, QString val, QString at)
1.183 +{
1.184 + return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
1.185 +}
1.186 +
1.187 +void xmlObj::incIndent()
1.188 +{
1.189 + actindent++;
1.190 +}
1.191 +
1.192 +void xmlObj::decIndent()
1.193 +{
1.194 + actindent--;
1.195 + if (actindent<0) actindent=0;
1.196 +}
1.197 +
1.198 +QString xmlObj::indent()
1.199 +{
1.200 + QString s;
1.201 + int i;
1.202 + for (i=0; i<actindent*indentwidth; i++)
1.203 + {
1.204 + s= s + " ";
1.205 + }
1.206 + return s;
1.207 +}
1.208 +
1.209 +
1.210 +
1.211 +ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
1.212 +{
1.213 +}
1.214 +
1.215 +void ImagePreview::previewUrl( const QUrl &u )
1.216 +{
1.217 + QString path = u.path();
1.218 + QPixmap pix( path );
1.219 + if ( pix.isNull() )
1.220 + setText( QObject::tr("This is not an image.") );
1.221 + else
1.222 + {
1.223 + float max_w=300;
1.224 + float max_h=300;
1.225 + float r;
1.226 + if (pix.width()>max_w)
1.227 + {
1.228 + r=max_w / pix.width();
1.229 + pix.resize(lrint(pix.width()*r), lrint(pix.height()*r));
1.230 + // TODO not a resize, but a shrink/enlarge is needed here...
1.231 + }
1.232 + if (pix.height()>max_h)
1.233 + {
1.234 + r=max_h / pix.height();
1.235 + pix.resize(lrint(pix.width()*r), lrint(pix.height()*r));
1.236 + // TODO not a resize, but a shrink/enlarge is needed here...
1.237 + }
1.238 + setPixmap( pix );
1.239 + }
1.240 +}
1.241 +