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