00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef koSize_h
00024 #define koSize_h
00025
00026 #include <QSize>
00027 #include <q3tl.h>
00028
00033 class KoSize
00034 {
00035 public:
00036 KoSize();
00037 KoSize( double w, double h );
00038
00039 bool isNull() const;
00040 bool isEmpty() const;
00041 bool isValid() const;
00042
00043 double width() const;
00044 double height() const;
00045 void setWidth( double w );
00046 void setHeight( double h );
00047
00048 KoSize expandedTo( const KoSize & ) const;
00049 KoSize boundedTo( const KoSize & ) const;
00050
00051 double &rwidth();
00052 double &rheight();
00053
00054 KoSize &operator+=( const KoSize & );
00055 KoSize &operator-=( const KoSize & );
00056 KoSize &operator*=( int c );
00057 KoSize &operator*=( double c );
00058 KoSize &operator/=( int c );
00059 KoSize &operator/=( double c );
00060
00061 friend inline bool operator==( const KoSize &, const KoSize & );
00062 friend inline bool operator!=( const KoSize &, const KoSize & );
00063 friend inline const KoSize operator+( const KoSize &, const KoSize & );
00064 friend inline const KoSize operator-( const KoSize &, const KoSize & );
00065 friend inline const KoSize operator*( const KoSize &, int );
00066 friend inline const KoSize operator*( int, const KoSize & );
00067 friend inline const KoSize operator*( const KoSize &, double );
00068 friend inline const KoSize operator*( double, const KoSize & );
00069 friend inline const KoSize operator/( const KoSize &, int );
00070 friend inline const KoSize operator/( const KoSize &, double );
00071
00072 inline QSize toQSize() const;
00073 static KoSize fromQSize( const QSize &size )
00074 {
00075 return KoSize(size.width(), size.height());
00076 }
00077
00078 void transpose()
00079 {
00080 qSwap(wd, ht);
00081 }
00082
00083 private:
00084 static void warningDivByZero()
00085 {
00086 #if defined(QT_CHECK_MATH)
00087 qWarning( "KoSize: Division by zero error" );
00088 #endif
00089 }
00090
00091 double wd;
00092 double ht;
00093 };
00094
00095
00096
00097
00098
00099
00100 inline KoSize::KoSize()
00101 { wd = ht = -1.0; }
00102
00103 inline KoSize::KoSize( double w, double h )
00104 { wd=w; ht=h; }
00105
00106 inline bool KoSize::isNull() const
00107 { return wd==0.0 && ht==0.0; }
00108
00109 inline bool KoSize::isEmpty() const
00110 { return wd<=0.0 || ht<=0.0; }
00111
00112 inline bool KoSize::isValid() const
00113 { return wd>=0.0 && ht>=0.0; }
00114
00115 inline double KoSize::width() const
00116 { return wd; }
00117
00118 inline double KoSize::height() const
00119 { return ht; }
00120
00121 inline void KoSize::setWidth( double w )
00122 { wd=w; }
00123
00124 inline void KoSize::setHeight( double h )
00125 { ht=h; }
00126
00127 inline double &KoSize::rwidth()
00128 { return wd; }
00129
00130 inline double &KoSize::rheight()
00131 { return ht; }
00132
00133 inline KoSize &KoSize::operator+=( const KoSize &s )
00134 { wd+=s.wd; ht+=s.ht; return *this; }
00135
00136 inline KoSize &KoSize::operator-=( const KoSize &s )
00137 { wd-=s.wd; ht-=s.ht; return *this; }
00138
00139 inline KoSize &KoSize::operator*=( int c )
00140 { wd*=c; ht*=c; return *this; }
00141
00142 inline KoSize &KoSize::operator*=( double c )
00143 { wd=wd*c; ht=ht*c; return *this; }
00144
00145 inline bool operator==( const KoSize &s1, const KoSize &s2 )
00146 { return s1.wd == s2.wd && s1.ht == s2.ht; }
00147
00148 inline bool operator!=( const KoSize &s1, const KoSize &s2 )
00149 { return s1.wd != s2.wd || s1.ht != s2.ht; }
00150
00151 inline const KoSize operator+( const KoSize & s1, const KoSize & s2 )
00152 { return KoSize(s1.wd+s2.wd, s1.ht+s2.ht); }
00153
00154 inline const KoSize operator-( const KoSize &s1, const KoSize &s2 )
00155 { return KoSize(s1.wd-s2.wd, s1.ht-s2.ht); }
00156
00157 inline const KoSize operator*( const KoSize &s, int c )
00158 { return KoSize(s.wd*c, s.ht*c); }
00159
00160 inline const KoSize operator*( int c, const KoSize &s )
00161 { return KoSize(s.wd*c, s.ht*c); }
00162
00163 inline const KoSize operator*( const KoSize &s, double c )
00164 { return KoSize(s.wd*c, s.ht*c); }
00165
00166 inline const KoSize operator*( double c, const KoSize &s )
00167 { return KoSize(s.wd*c, s.ht*c); }
00168
00169 inline KoSize &KoSize::operator/=( int c )
00170 {
00171 #if defined(QT_CHECK_MATH)
00172 if ( c == 0 )
00173 warningDivByZero();
00174 #endif
00175 wd/=c; ht/=c;
00176 return *this;
00177 }
00178
00179 inline KoSize &KoSize::operator/=( double c )
00180 {
00181 #if defined(QT_CHECK_MATH)
00182 if ( c == 0.0 )
00183 warningDivByZero();
00184 #endif
00185 wd=wd/c; ht=ht/c;
00186 return *this;
00187 }
00188
00189 inline const KoSize operator/( const KoSize &s, int c )
00190 {
00191 #if defined(QT_CHECK_MATH)
00192 if ( c == 0 )
00193 KoSize::warningDivByZero();
00194 #endif
00195 return KoSize(s.wd/c, s.ht/c);
00196 }
00197
00198 inline const KoSize operator/( const KoSize &s, double c )
00199 {
00200 #if defined(QT_CHECK_MATH)
00201 if ( c == 0.0 )
00202 KoSize::warningDivByZero();
00203 #endif
00204 return KoSize(s.wd/c, s.ht/c);
00205 }
00206
00207 inline KoSize KoSize::expandedTo( const KoSize & otherSize ) const
00208 {
00209 return KoSize( qMax(wd,otherSize.wd), qMax(ht,otherSize.ht) );
00210 }
00211
00212 inline KoSize KoSize::boundedTo( const KoSize & otherSize ) const
00213 {
00214 return KoSize( qMin(wd,otherSize.wd), qMin(ht,otherSize.ht) );
00215 }
00216
00217 inline QSize KoSize::toQSize() const
00218 {
00219 return QSize(qRound(wd), qRound(ht));
00220 }
00221
00222
00223
00224
00225 #include <kdebug.h>
00226
00227 inline kdbgstream operator<<( kdbgstream str, const KoSize & sz ) { str << "[" << sz.width() << "x" << sz.height() << "]"; return str; }
00228 inline kndbgstream operator<<( kndbgstream str, const KoSize & ) { return str; }
00229
00230 #endif