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