KoSize.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 Lukas Tinkl <lukas@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18
19 This file borrows from the TQSize class;
20 Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
21*/
22
23#ifndef koSize_h
24#define koSize_h
25
26#include <tqsize.h>
27#include <tqtl.h>
28
33class KoSize
34{
35public:
36 KoSize();
37 KoSize( double w, double h );
38
39 bool isNull() const;
40 bool isEmpty() const;
41 bool isValid() const;
42
43 double width() const;
44 double height() const;
45 void setWidth( double w );
46 void setHeight( double h );
47
48 KoSize expandedTo( const KoSize & ) const;
49 KoSize boundedTo( const KoSize & ) const;
50
51 double &rwidth();
52 double &rheight();
53
54 KoSize &operator+=( const KoSize & );
55 KoSize &operator-=( const KoSize & );
56 KoSize &operator*=( int c );
57 KoSize &operator*=( double c );
58 KoSize &operator/=( int c );
59 KoSize &operator/=( double c );
60
61 friend inline bool operator==( const KoSize &, const KoSize & );
62 friend inline bool operator!=( const KoSize &, const KoSize & );
63 friend inline const KoSize operator+( const KoSize &, const KoSize & );
64 friend inline const KoSize operator-( const KoSize &, const KoSize & );
65 friend inline const KoSize operator*( const KoSize &, int );
66 friend inline const KoSize operator*( int, const KoSize & );
67 friend inline const KoSize operator*( const KoSize &, double );
68 friend inline const KoSize operator*( double, const KoSize & );
69 friend inline const KoSize operator/( const KoSize &, int );
70 friend inline const KoSize operator/( const KoSize &, double );
71
72 inline TQSize toTQSize() const;
73 static KoSize fromTQSize( const TQSize &size )
74 {
75 return KoSize(size.width(), size.height());
76 }
77
78 void transpose()
79 {
80 tqSwap(wd, ht);
81 }
82
83private:
84 static void warningDivByZero()
85 {
86#if defined(TQT_CHECK_MATH)
87 tqWarning( "KoSize: Division by zero error" );
88#endif
89 }
90
91 double wd;
92 double ht;
93};
94
95
96/*****************************************************************************
97 KoSize inline functions
98 *****************************************************************************/
99
100inline KoSize::KoSize()
101{ wd = ht = -1.0; }
102
103inline KoSize::KoSize( double w, double h )
104{ wd=w; ht=h; }
105
106inline bool KoSize::isNull() const
107{ return wd==0.0 && ht==0.0; }
108
109inline bool KoSize::isEmpty() const
110{ return wd<=0.0 || ht<=0.0; }
111
112inline bool KoSize::isValid() const
113{ return wd>=0.0 && ht>=0.0; }
114
115inline double KoSize::width() const
116{ return wd; }
117
118inline double KoSize::height() const
119{ return ht; }
120
121inline void KoSize::setWidth( double w )
122{ wd=w; }
123
124inline void KoSize::setHeight( double h )
125{ ht=h; }
126
127inline double &KoSize::rwidth()
128{ return wd; }
129
130inline double &KoSize::rheight()
131{ return ht; }
132
133inline KoSize &KoSize::operator+=( const KoSize &s )
134{ wd+=s.wd; ht+=s.ht; return *this; }
135
136inline KoSize &KoSize::operator-=( const KoSize &s )
137{ wd-=s.wd; ht-=s.ht; return *this; }
138
139inline KoSize &KoSize::operator*=( int c )
140{ wd*=c; ht*=c; return *this; }
141
142inline KoSize &KoSize::operator*=( double c )
143{ wd=wd*c; ht=ht*c; return *this; }
144
145inline bool operator==( const KoSize &s1, const KoSize &s2 )
146{ return s1.wd == s2.wd && s1.ht == s2.ht; }
147
148inline bool operator!=( const KoSize &s1, const KoSize &s2 )
149{ return s1.wd != s2.wd || s1.ht != s2.ht; }
150
151inline const KoSize operator+( const KoSize & s1, const KoSize & s2 )
152{ return KoSize(s1.wd+s2.wd, s1.ht+s2.ht); }
153
154inline const KoSize operator-( const KoSize &s1, const KoSize &s2 )
155{ return KoSize(s1.wd-s2.wd, s1.ht-s2.ht); }
156
157inline const KoSize operator*( const KoSize &s, int c )
158{ return KoSize(s.wd*c, s.ht*c); }
159
160inline const KoSize operator*( int c, const KoSize &s )
161{ return KoSize(s.wd*c, s.ht*c); }
162
163inline const KoSize operator*( const KoSize &s, double c )
164{ return KoSize(s.wd*c, s.ht*c); }
165
166inline const KoSize operator*( double c, const KoSize &s )
167{ return KoSize(s.wd*c, s.ht*c); }
168
169inline KoSize &KoSize::operator/=( int c )
170{
171#if defined(TQT_CHECK_MATH)
172 if ( c == 0 )
173 warningDivByZero();
174#endif
175 wd/=c; ht/=c;
176 return *this;
177}
178
179inline KoSize &KoSize::operator/=( double c )
180{
181#if defined(TQT_CHECK_MATH)
182 if ( c == 0.0 )
183 warningDivByZero();
184#endif
185 wd=wd/c; ht=ht/c;
186 return *this;
187}
188
189inline const KoSize operator/( const KoSize &s, int c )
190{
191#if defined(TQT_CHECK_MATH)
192 if ( c == 0 )
193 KoSize::warningDivByZero();
194#endif
195 return KoSize(s.wd/c, s.ht/c);
196}
197
198inline const KoSize operator/( const KoSize &s, double c )
199{
200#if defined(TQT_CHECK_MATH)
201 if ( c == 0.0 )
202 KoSize::warningDivByZero();
203#endif
204 return KoSize(s.wd/c, s.ht/c);
205}
206
207inline KoSize KoSize::expandedTo( const KoSize & otherSize ) const
208{
209 return KoSize( TQMAX(wd,otherSize.wd), TQMAX(ht,otherSize.ht) );
210}
211
212inline KoSize KoSize::boundedTo( const KoSize & otherSize ) const
213{
214 return KoSize( TQMIN(wd,otherSize.wd), TQMIN(ht,otherSize.ht) );
215}
216
217inline TQSize KoSize::toTQSize() const
218{
219 return TQSize(tqRound(wd), tqRound(ht));
220}
221
222/******************************
223 kdDebug support
224*******************************/
225#include <kdebug.h>
226
227inline kdbgstream operator<<( kdbgstream str, const KoSize & sz ) { str << "[" << sz.width() << "x" << sz.height() << "]"; return str; }
228inline kndbgstream operator<<( kndbgstream str, const KoSize & ) { return str; }
229
230#endif
A size whose coordinates are floating-point values ( "double"s ).
Definition KoSize.h:34