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