summaryrefslogtreecommitdiffstats
path: root/kalzium/src/kalziumutils.cpp
blob: d2b2baa8d5d588d02bfb55f670d623d6fdbe04f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/***************************************************************************
    copyright            : (C) 2005 by Carsten Niehaus
    email                : cniehaus@kde.org
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "kalziumutils.h"

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <tqfont.h>
#include <tqrect.h>
#include <tqpainter.h>
#include <tqglobal.h>

#include <math.h>
#if defined(Q_OS_SOLARIS)
#include <ieeefp.h>
#endif

int KalziumUtils::maxSize( const TQString& string, const TQRect& rect, TQFont font, TQPainter* p, int minFontSize, int maxFontSize )
{
	bool goodSizeFound = false;
	int size = maxFontSize;
	TQRect r;

	do
	{
		font.setPointSize( size );
		p->setFont( font );
		r = p->boundingRect( TQRect(), Qt::AlignAuto, string );
		r.moveBy( rect.left(), rect.top() );
		
		if ( rect.tqcontains( r ) )
			goodSizeFound = true;
		else
			size--;
	}
	while ( !goodSizeFound && ( size > minFontSize ) );

	return size;
}

int KalziumUtils::StringHeight( const TQString& string, const TQFont& font, TQPainter* p )
{
	return p->boundingRect( TQRect(), Qt::AlignAuto, string ).height();
}

int KalziumUtils::StringWidth( const TQString& string, const TQFont& font, TQPainter* p )
{
	return p->boundingRect( TQRect(), Qt::AlignAuto, string ).width();
}

double KalziumUtils::strippedValue( double num )
{
	if ( !finite( num ) )
		return num;

	double power;
	power = 1e-6;
	while ( power < num )
		power *= 10;

	num = num / power * 10000;
	num = tqRound( num );

	return num * power / 10000;
}
	
TQString KalziumUtils::localizedValue( double val, int precision, unsigned long options  )
{
	TQString str = KGlobal::locale()->formatNumber( val, precision ); 
	while( str.endsWith("0") )
		str.truncate( str.length()-1);
	if ( str.endsWith( KGlobal::locale()->decimalSymbol()  ) )
	{
		// we do not want trailing ',' values so readd trailing 0 
		str.append( '0' ); // 
	}
	return str;
}