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