misc.cpp
author insilmaril
Tue, 03 Jan 2006 09:44:41 +0000
changeset 173 309609406650
parent 166 325958acb69b
child 176 f75489896084
permissions -rw-r--r--
1.7.6 New features for floatimages and fixes
insilmaril@139
     1
#include <math.h>
insilmaril@139
     2
insilmaril@0
     3
#include <qregexp.h>
insilmaril@0
     4
#include <qpoint.h>
insilmaril@164
     5
#include <stdlib.h>
insilmaril@0
     6
insilmaril@0
     7
#include "misc.h"
insilmaril@0
     8
insilmaril@129
     9
QString qpointToString (const QPoint &p)
insilmaril@129
    10
{
insilmaril@129
    11
	return "(" + QString("%1").arg(p.x()) +","+ QString ("%1").arg (p.y()) +")";
insilmaril@129
    12
}
insilmaril@0
    13
insilmaril@0
    14
ostream &operator<< (ostream &stream, QPoint const &p)
insilmaril@0
    15
{ 
insilmaril@129
    16
    return (stream << qpointToString(p) );
insilmaril@0
    17
}
insilmaril@0
    18
insilmaril@0
    19
float getAngle(const QPoint &p)
insilmaril@0
    20
{	
insilmaril@0
    21
	// Calculate angle of vector to y-axis
insilmaril@0
    22
	if (p.y()==0)
insilmaril@0
    23
	{
insilmaril@0
    24
		if (p.x()>=0)
insilmaril@0
    25
			return M_PI_2;
insilmaril@0
    26
		else
insilmaril@0
    27
			return 3* M_PI_2;
insilmaril@0
    28
	} else
insilmaril@0
    29
	{
insilmaril@0
    30
		if (p.y()>0) 
insilmaril@0
    31
			return (float)(M_PI  - atan ( (double)(p.x()) / (double)(p.y()) ) );
insilmaril@0
    32
		else	
insilmaril@0
    33
			if (p.x()<0)
insilmaril@0
    34
				return (float)( 2*M_PI - atan ( (double)(p.x()) / (double)(p.y()) ) );
insilmaril@0
    35
			else	
insilmaril@0
    36
				return (float)( - atan ( (double)(p.x()) / (double)(p.y()) ) );
insilmaril@0
    37
	}	
insilmaril@0
    38
}
insilmaril@0
    39
insilmaril@0
    40
QPoint normalise(const QPoint &p)
insilmaril@0
    41
{	
insilmaril@0
    42
	// Calculate normalised position (fixed length) 
insilmaril@0
    43
insilmaril@0
    44
	double px=p.x();
insilmaril@0
    45
	double py=p.y();
insilmaril@0
    46
	double x;
insilmaril@0
    47
	double y;
insilmaril@0
    48
	double r=150;
insilmaril@0
    49
insilmaril@0
    50
	if (px==0)
insilmaril@0
    51
	{
insilmaril@0
    52
		x=0;
insilmaril@0
    53
		if (py>=0)
insilmaril@0
    54
			y=r;
insilmaril@0
    55
		else
insilmaril@0
    56
			y=-r;
insilmaril@0
    57
	} else
insilmaril@0
    58
	{
insilmaril@0
    59
		double sign;
insilmaril@0
    60
		double a;
insilmaril@0
    61
		if (px>0) 
insilmaril@0
    62
			sign=1; 
insilmaril@0
    63
		else 
insilmaril@0
    64
			sign=-1;
insilmaril@0
    65
		
insilmaril@0
    66
		a=atan (py / px);
insilmaril@0
    67
		x=cos (a) * r *sign;
insilmaril@0
    68
		y=sin (a) * r *sign;
insilmaril@0
    69
	}	
insilmaril@0
    70
	return QPoint ((int) (x),(int) (y));
insilmaril@0
    71
}
insilmaril@0
    72
insilmaril@0
    73
QString maskPath(QString p)
insilmaril@0
    74
{
insilmaril@0
    75
	// Change " " to "\ " to enable blanks in filenames
insilmaril@0
    76
	p=p.replace(QChar('&'),"\\&");
insilmaril@0
    77
	return p.replace(QChar(' '),"\\ ");
insilmaril@0
    78
}
insilmaril@0
    79
insilmaril@0
    80
QString convertToRel (const QString &src, const QString &dst)
insilmaril@0
    81
{
insilmaril@0
    82
	QString s=src;
insilmaril@0
    83
	QString d=dst;
insilmaril@0
    84
	int i;
insilmaril@0
    85
insilmaril@158
    86
	if (s==d) 
insilmaril@0
    87
	{
insilmaril@158
    88
		// Special case, we just need the name of the file,
insilmaril@158
    89
		// not the complete path
insilmaril@158
    90
		i=d.findRev ("/");
insilmaril@158
    91
		d=d.right (d.length()-i-1);
insilmaril@158
    92
	} else
insilmaril@0
    93
	{
insilmaril@158
    94
		// Find relative path from src to dst
insilmaril@0
    95
insilmaril@158
    96
		// Remove the first "/"
insilmaril@158
    97
		if (s.section ("/",0,0).isEmpty()) 
insilmaril@0
    98
		{
insilmaril@158
    99
			s=s.right (s.length()-1);
insilmaril@158
   100
			d=d.right (d.length()-1);
insilmaril@158
   101
		}
insilmaril@158
   102
		
insilmaril@158
   103
		// remove identical left parts
insilmaril@158
   104
		while (s.section("/",0,0) == d.section("/",0,0) ) 
insilmaril@158
   105
		{
insilmaril@158
   106
			i=s.find ("/");
insilmaril@158
   107
			s=s.right (s.length()-i-1);
insilmaril@158
   108
			d=d.right (d.length()-i-1);
insilmaril@158
   109
		}
insilmaril@158
   110
insilmaril@158
   111
		int srcsep=s.contains("/");
insilmaril@158
   112
		int dstsep=d.contains("/");
insilmaril@158
   113
		if (srcsep >=  dstsep )
insilmaril@158
   114
		{
insilmaril@158
   115
			// find path to go up first and then back to dst
insilmaril@158
   116
			i=1;
insilmaril@158
   117
			while (i<=srcsep) 
insilmaril@158
   118
			{
insilmaril@158
   119
				d="../"+d;
insilmaril@158
   120
				i++;
insilmaril@158
   121
			}	
insilmaril@158
   122
		}
insilmaril@158
   123
	}	
insilmaril@0
   124
	return d;
insilmaril@0
   125
}
insilmaril@0
   126
insilmaril@164
   127
QString makeUniqueDir (QString s)
insilmaril@164
   128
{
insilmaril@164
   129
	char *p;
insilmaril@164
   130
	int bytes=s.length();
insilmaril@164
   131
	p=(char*) malloc (bytes+1);
insilmaril@164
   132
	int i;
insilmaril@164
   133
	for (i=0;i<bytes;i++)
insilmaril@164
   134
		p[i]=s.at(i).latin1();
insilmaril@164
   135
	p[bytes]=0;	
insilmaril@164
   136
	QString r=mkdtemp (p);
insilmaril@164
   137
	free (p);
insilmaril@164
   138
	return r;
insilmaril@164
   139
}
insilmaril@164
   140
insilmaril@163
   141
void removeDir(QDir d)
insilmaril@163
   142
{
insilmaril@164
   143
	if (d.path().left(4)!="/tmp")
insilmaril@164
   144
	{
insilmaril@164
   145
		// FIXME testing
insilmaril@164
   146
		qWarning ("misc.cpp::removeDir should remove "+d.path()+" - aborted.");
insilmaril@164
   147
		return;
insilmaril@164
   148
	}
insilmaril@164
   149
insilmaril@163
   150
	// Traverse directories
insilmaril@163
   151
	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@163
   152
	const QFileInfoList *dirlist = d.entryInfoList();
insilmaril@163
   153
	QFileInfoListIterator itdir( *dirlist );
insilmaril@163
   154
	QFileInfo *fi;
insilmaril@163
   155
insilmaril@163
   156
	while ( (fi = itdir.current()) != 0 ) 
insilmaril@163
   157
	{
insilmaril@163
   158
		if (fi->fileName() != "." && fi->fileName() != ".." )
insilmaril@163
   159
		{
insilmaril@163
   160
			if ( !d.cd(fi->fileName()) ) 
insilmaril@163
   161
				qWarning ("removeDir() cannot find the directory "+fi->fileName());
insilmaril@163
   162
			else 
insilmaril@163
   163
			{
insilmaril@163
   164
				// Recursively remove subdirs
insilmaril@163
   165
				removeDir (d);
insilmaril@163
   166
				d.cdUp();
insilmaril@163
   167
			}
insilmaril@163
   168
		}	
insilmaril@163
   169
		++itdir;
insilmaril@163
   170
	}		
insilmaril@163
   171
	// Traverse files
insilmaril@163
   172
	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@163
   173
	const QFileInfoList *filelist = d.entryInfoList();
insilmaril@163
   174
	QFileInfoListIterator itfile( *filelist );
insilmaril@163
   175
insilmaril@163
   176
	while ( (fi = itfile.current()) != 0 ) 
insilmaril@163
   177
	{
insilmaril@163
   178
		QFile (fi->filePath()).remove();
insilmaril@163
   179
		
insilmaril@163
   180
		++itfile;
insilmaril@163
   181
	}	
insilmaril@163
   182
insilmaril@163
   183
	if (!d.rmdir(d.path()))
insilmaril@163
   184
		qWarning ("removeDir("+d.path()+") failed!");
insilmaril@163
   185
}		
insilmaril@163
   186
insilmaril@166
   187
void makeSubDirs (const QString &s)
insilmaril@166
   188
{
insilmaril@166
   189
	QDir d(s);
insilmaril@166
   190
	d.mkdir(s);
insilmaril@166
   191
	d.mkdir ("images");	
insilmaril@166
   192
	d.mkdir ("flags");	
insilmaril@166
   193
}
insilmaril@166
   194
insilmaril@0
   195
// returns masked "<" ">" "&"
insilmaril@0
   196
QString quotemeta(const QString &s)
insilmaril@0
   197
{
insilmaril@0
   198
	QString r=s;
insilmaril@0
   199
    QRegExp  rx("&(?!amp;)");
insilmaril@0
   200
    r.replace ( rx,"&amp;");
insilmaril@0
   201
    rx.setPattern( ">");
insilmaril@0
   202
    r.replace ( rx,"&gt;");
insilmaril@0
   203
    rx.setPattern( "<");
insilmaril@0
   204
    r.replace ( rx,"&lt;");
insilmaril@0
   205
    rx.setPattern( "\"");
insilmaril@0
   206
    r.replace ( rx,"&quot;");
insilmaril@0
   207
    return r;
insilmaril@0
   208
}
insilmaril@0
   209
insilmaril@0
   210
int max(int a, int b)
insilmaril@0
   211
{
insilmaril@0
   212
	if (a>b)
insilmaril@0
   213
		return a;
insilmaril@0
   214
	return b;
insilmaril@0
   215
}
insilmaril@0
   216
insilmaril@0
   217
int xmlObj::actindent=0;		// make instance of actindent
insilmaril@0
   218
insilmaril@0
   219
xmlObj::xmlObj()
insilmaril@0
   220
{
insilmaril@0
   221
    indentwidth=4;
insilmaril@0
   222
}
insilmaril@0
   223
insilmaril@0
   224
insilmaril@0
   225
// returns <s at />
insilmaril@0
   226
QString xmlObj::singleElement(QString s, QString at)
insilmaril@0
   227
{
insilmaril@0
   228
    return indent() + "<" + s +" " + at +" " + "/>\n";
insilmaril@0
   229
}
insilmaril@0
   230
insilmaril@0
   231
// returns <s>
insilmaril@0
   232
QString xmlObj::beginElement(QString s)
insilmaril@0
   233
{
insilmaril@0
   234
    return indent() + "<" + s + ">\n";
insilmaril@0
   235
}
insilmaril@0
   236
insilmaril@0
   237
// returns <s at>
insilmaril@0
   238
QString xmlObj::beginElement(QString s, QString at)
insilmaril@0
   239
{
insilmaril@0
   240
    return indent() + "<" + s + " " + at + ">\n";
insilmaril@0
   241
}
insilmaril@0
   242
insilmaril@0
   243
// returns </s>
insilmaril@0
   244
QString xmlObj::endElement(QString s)
insilmaril@0
   245
{
insilmaril@0
   246
    return indent() + "</" + s + ">\n";
insilmaril@0
   247
}
insilmaril@0
   248
insilmaril@0
   249
// returns  at="val"
insilmaril@0
   250
QString xmlObj::attribut(QString at, QString val)
insilmaril@0
   251
{
insilmaril@0
   252
    return " " + at + "=\"" + quotemeta (val) + "\""; 
insilmaril@0
   253
}
insilmaril@0
   254
insilmaril@0
   255
// returns <s> val </s>
insilmaril@0
   256
QString xmlObj::valueElement(QString el, QString val)
insilmaril@0
   257
{
insilmaril@0
   258
    return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
insilmaril@0
   259
}
insilmaril@0
   260
insilmaril@0
   261
// returns <s at> val </s>
insilmaril@0
   262
QString xmlObj::valueElement(QString el, QString val, QString at)
insilmaril@0
   263
{
insilmaril@0
   264
    return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
insilmaril@0
   265
}
insilmaril@0
   266
insilmaril@0
   267
void xmlObj::incIndent()
insilmaril@0
   268
{
insilmaril@0
   269
    actindent++;
insilmaril@0
   270
}	
insilmaril@0
   271
insilmaril@0
   272
void xmlObj::decIndent()
insilmaril@0
   273
{
insilmaril@0
   274
    actindent--;
insilmaril@0
   275
    if (actindent<0) actindent=0;
insilmaril@0
   276
}	
insilmaril@0
   277
insilmaril@0
   278
QString xmlObj::indent()
insilmaril@0
   279
{
insilmaril@0
   280
    QString s;
insilmaril@0
   281
    int i;
insilmaril@0
   282
    for (i=0; i<actindent*indentwidth; i++)
insilmaril@0
   283
    {
insilmaril@0
   284
		s= s + " ";
insilmaril@0
   285
    }
insilmaril@0
   286
    return s;
insilmaril@0
   287
}	
insilmaril@0
   288
insilmaril@0
   289
insilmaril@0
   290
insilmaril@0
   291
ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
insilmaril@0
   292
{
insilmaril@0
   293
}
insilmaril@0
   294
insilmaril@0
   295
void ImagePreview::previewUrl( const QUrl &u )
insilmaril@0
   296
{
insilmaril@0
   297
    QString path = u.path();
insilmaril@0
   298
    QPixmap pix( path );
insilmaril@0
   299
    if ( pix.isNull() )
insilmaril@0
   300
        setText( QObject::tr("This is not an image.") );
insilmaril@0
   301
    else
insilmaril@0
   302
	{
insilmaril@0
   303
		float max_w=300;
insilmaril@0
   304
		float max_h=300;
insilmaril@0
   305
		float r;
insilmaril@0
   306
		if (pix.width()>max_w)
insilmaril@0
   307
		{
insilmaril@0
   308
			r=max_w / pix.width();
insilmaril@104
   309
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@0
   310
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@0
   311
		}
insilmaril@0
   312
		if (pix.height()>max_h)
insilmaril@0
   313
		{
insilmaril@0
   314
			r=max_h / pix.height();
insilmaril@104
   315
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@0
   316
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@0
   317
		}
insilmaril@0
   318
        setPixmap( pix );
insilmaril@0
   319
	}	
insilmaril@0
   320
}
insilmaril@0
   321