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