Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

calibration.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the OPIE Project
00003                =.            Copyright (c)  2002 Maximilian Reiss <harlekin@handhelds.org>
00004              .=l.            Copyright (c)  2002 Robert Griebl <sandman@handhelds.org>
00005            .>+-=
00006  _;:,     .>    :=|.         This file is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011      ._= =}       :          or (at your option) any later version.
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This file is distributed in the hope that
00014      +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=       =       ;      Public License for more details.
00019 ++=   -.     .`     .:       
00020  :     =  ...= . :.=-        You should have received a copy of the GNU
00021  -.   .:....=;==+<;          General Public License along with this file;
00022   -_. . .   )=.  =           see the file COPYING. If not, write to the
00023     --        :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 #include "calibration.h"
00029 
00030 #include <qpainter.h>
00031 #include <qpixmap.h>
00032 
00033 #define BRD 3
00034 
00035 Calibration::Calibration ( QWidget *parent, const char *name, WFlags fl )
00036         : QWidget ( parent, name, fl | WRepaintNoErase )
00037 {
00038         setBackgroundMode ( NoBackground );
00039         
00040         m_scale = QSize ( 256, 256 );
00041         m_steps = 5;
00042         m_dragged = -1;
00043         m_interval = 5;
00044         
00045         m_p [0] = QPoint ( 0, 0 );
00046         m_p [1] = QPoint ( 255, 255 );
00047 }
00048 
00049 Calibration::~Calibration ( )
00050 {
00051 }
00052 
00053 void Calibration::setScale ( const QSize &s )
00054 {
00055         if ( s. width ( ) < 1 || s. height ( ) < 1 )
00056                 return;
00057 
00058         m_scale = s;    
00059         checkPoints ( );
00060         
00061         update ( );
00062 }
00063 
00064 QSize Calibration::scale ( ) const
00065 {
00066         return m_scale;
00067 }
00068 
00069 void Calibration::setLineSteps ( int steps )
00070 {
00071         if ( m_steps < 2 )
00072                 return;
00073 
00074         m_steps = steps;
00075         update ( );
00076 }
00077 
00078 int Calibration::lineSteps ( ) const
00079 {
00080         return m_steps;
00081 }
00082 
00083 void Calibration::setInterval ( int iv )
00084 {
00085         if ( iv < 1 )
00086         return;
00087 
00088         m_interval = iv;
00089 //      update ( );
00090 }
00091 
00092 int Calibration::interval ( ) const
00093 {
00094         return m_interval;
00095 }
00096 
00097 void Calibration::setStartPoint ( const QPoint &p )
00098 {
00099         m_p [0] = QPoint ( p. x ( ), m_scale. height ( ) - p. y ( ) - 1 );
00100         checkPoints ( );        
00101         update ( );
00102 }
00103 
00104 QPoint Calibration::startPoint ( ) const
00105 {
00106         return QPoint ( m_p [0]. x ( ), m_scale. height ( ) - m_p [0]. y ( ) - 1 );
00107 }
00108 
00109 void Calibration::setEndPoint ( const QPoint &p )
00110 {
00111         m_p [1] = QPoint ( p. x ( ), m_scale. height ( ) - p. y ( ) - 1 );
00112         checkPoints ( );
00113         update ( );
00114 }
00115 
00116 QPoint Calibration::endPoint ( ) const
00117 {
00118         return QPoint ( m_p [1]. x ( ), m_scale. height ( ) - m_p [1]. y ( ) - 1 );
00119 }
00120 
00121 void Calibration::checkPoints ( )
00122 {
00123         int dx = m_scale. width ( );
00124         int dy = m_scale. height ( );
00125 
00126         if ( m_p [1]. x ( ) < 0 )
00127                 m_p [1]. setX ( 0 );
00128         if ( m_p [1]. x ( ) >= dx )
00129                 m_p [1]. setX ( dx - 1 );
00130         if ( m_p [0]. x ( ) < 0 )
00131                 m_p [0]. setX ( 0 );
00132         if ( m_p [0]. x ( ) > m_p [1]. x ( ))
00133                 m_p [0]. setX ( m_p [1]. x ( ));
00134 
00135         if ( m_p [1]. y ( ) < 0 )
00136                 m_p [1]. setY ( 0 );
00137         if ( m_p [1]. y ( ) >= dy )
00138                 m_p [1]. setY ( dy - 1 );
00139         if ( m_p [0]. y ( ) < 0 )
00140                 m_p [0]. setY ( 0 );
00141         if ( m_p [0]. y ( ) >= dy )
00142                 m_p [0]. setY ( dy - 1 );
00143 }
00144 
00145 
00146 #define SCALEX(x)   (BRD+(x)*(width()- 2*BRD)/m_scale.width())
00147 #define SCALEY(y)   (BRD+(y)*(height()-2*BRD)/m_scale.height())
00148 
00149 
00150 static QRect around ( int x, int y )
00151 {
00152         return QRect ( x - BRD, y - BRD, 2 * BRD + 1, 2 * BRD + 1 );    
00153 }
00154 
00155 void Calibration::mousePressEvent ( QMouseEvent *e )
00156 {
00157         if ( e-> button ( ) != LeftButton )
00158                 return QWidget::mousePressEvent ( e );
00159 
00160         int olddragged = m_dragged;
00161         int x [2], y [2];
00162 
00163         m_dragged = -1;
00164         for ( int i = 0; i < 2; i++ ) {
00165                 x [i] = SCALEX( m_p [i]. x ( ));
00166                 y [i] = SCALEY( m_p [i]. y ( ));
00167 
00168                 if (( QABS( e-> x ( ) - x [i] ) <= 2 * BRD ) &&
00169                     ( QABS( e-> y ( ) - y [i] ) <= 2 * BRD )) {
00170                         m_dragged = i;
00171                         break;
00172                 }
00173         }
00174 
00175         if ( m_dragged != olddragged ) {
00176                 QRect r;
00177         
00178                 if ( olddragged >= 0 )
00179                         r |= around ( x [olddragged], y [olddragged] ); 
00180                 if ( m_dragged >= 0 ) 
00181                         r |= around ( x [m_dragged], y [m_dragged] );   
00182                 repaint ( r, false );
00183         }       
00184 }
00185 
00186 void Calibration::mouseMoveEvent ( QMouseEvent *e )
00187 {
00188         if ( m_dragged < 0 )
00189                 return;
00190                 
00191         QPoint n [2];
00192         
00193         n [m_dragged]. setX (( e-> x ( ) - BRD ) * m_scale. width ( ) / ( width ( ) - 2 * BRD ));
00194         n [m_dragged]. setY (( e-> y ( ) - BRD ) * m_scale. height ( ) / ( height ( ) - 2 * BRD ));     
00195         n [1 - m_dragged] = m_p [1 - m_dragged];
00196         
00197         if ( n [m_dragged]. x ( ) < 0 ) 
00198                 n [m_dragged]. setX ( 0 );
00199         if ( n [m_dragged]. x ( ) >= m_scale. width ( ))
00200                 n [m_dragged]. setX ( m_scale. width ( ) - 1 );
00201         if ( n [0]. x ( ) > n [1]. x ( ))
00202                 n [m_dragged]. setX ( n [1 - m_dragged]. x ( ));
00203         if ( n [m_dragged]. y ( ) < 0 ) 
00204                 n [m_dragged]. setY ( 0 );
00205         if ( n [m_dragged]. y ( ) >= m_scale. height ( ))
00206                 n [m_dragged]. setY ( m_scale. height ( ) - 1 );
00207 //      if ( n [0]. y ( ) > n [1]. y ( ))
00208 //              n [m_dragged]. setY ( n [1 - m_dragged]. y ( ));
00209                 
00210         QRect r;        
00211         int ox [2], oy [2], nx [2], ny [2];     
00212         
00213         for ( int i = 0; i < 2; i++ ) {
00214                 nx [i] = SCALEX( n [i]. x ( ));
00215                 ny [i] = SCALEY( n [i]. y ( ));
00216                 ox [i] = SCALEX( m_p [i]. x ( ));
00217                 oy [i] = SCALEY( m_p [i]. y ( ));
00218         
00219                 if ( n [i] != m_p [i] ) {
00220                         r |= around ( nx [i], ny [i] ); 
00221                         r |= around ( ox [i], oy [i] ); 
00222                         m_p [i] = n [i];
00223                         
00224                         if ( i == 0 ) {
00225                                 r |= QRect ( 0, ny [0], nx [0] - 0 + 1, 1 );
00226                                 r |= QRect ( 0, oy [0], ox [0] - 0 + 1, 1 );
00227                         
00228                                 emit startPointChanged ( startPoint ( ));
00229                         }
00230                         else if ( i == 1 ) {
00231                                 r |= QRect ( nx [1], ny [1], width ( ) - nx [1], 1 ); 
00232                                 r |= QRect ( ox [1], oy [1], width ( ) - ox [1], 1 ); 
00233 
00234                                 emit endPointChanged ( endPoint ( ));
00235                         }
00236                 }
00237         }
00238         if ( r. isValid ( )) {
00239                 r |= QRect ( nx [0], ny [0], nx [1] - nx [0] + 1, ny [1] - ny [0] + 1 ). normalize ( );
00240                 r |= QRect ( ox [0], oy [0], ox [1] - ox [0] + 1, oy [1] - oy [0] + 1 ). normalize ( );
00241                 
00242                 repaint ( r, false );
00243         }
00244 }
00245 
00246 void Calibration::mouseReleaseEvent ( QMouseEvent *e )
00247 {
00248         if ( e-> button ( ) != LeftButton )
00249                 return QWidget::mouseReleaseEvent ( e );
00250         
00251         if ( m_dragged < 0 )
00252                 return;
00253         
00254         int x = SCALEX( m_p [m_dragged]. x ( ));
00255         int y = SCALEY( m_p [m_dragged]. y ( ));
00256         m_dragged = -1;
00257         
00258         repaint ( around ( x, y ), false );
00259 }
00260 
00261 void Calibration::paintEvent ( QPaintEvent *pe )
00262 {
00263         QPixmap pix ( size ( ));
00264         QPainter p ( &pix, this );
00265         QRect cr = pe-> rect ( );
00266         
00267         int x0 = SCALEX( m_p [0]. x ( ));
00268         int y0 = SCALEY( m_p [0]. y ( ));
00269         int x1 = SCALEX( m_p [1]. x ( ));
00270         int y1 = SCALEY( m_p [1]. y ( ));
00271                         
00272         int dx = x1 - x0;
00273         int dy = y1 - y0;
00274         
00275         // restrict steps to real x and y resolution
00276         int st = QMIN( QMIN( m_steps, ( dx + 1 )), ( QABS( dy ) + 1 ));
00277         
00278         QString stepstr = tr( "%1 Steps" ). arg ( st );
00279         QRect tr = p. boundingRect ( BRD, BRD, width ( ) - 2*BRD, height() - 2*BRD, AlignTop | AlignRight, stepstr );
00280         tr. setLeft ( tr. left ( ) - 20 );
00281         if ( p. hasClipping ( ))
00282                 p. setClipRegion ( p. clipRegion ( ) | QRegion ( tr ));
00283         
00284         QColorGroup g = colorGroup ( );
00285 
00286         p. fillRect ( cr, g. base ( ));
00287         p. fillRect ( tr, g. base ( ));
00288 
00289         int ex = x0, ey = y0;
00290         
00291         p. setPen ( g. mid ( ));
00292         
00293         int gx0 = SCALEX( 0 );
00294         int gy0 = SCALEY( 0 );
00295         int gx1 = SCALEX( m_scale. width ( ) - 1 );
00296         int gy1 = SCALEY( m_scale. height ( ) - 1 );
00297 
00298         int xdiv = QMIN( 4, m_scale. width ( ));
00299         int ydiv = QMIN( 4, m_scale. height ( ));
00300         
00301         xdiv = ( gx1 - gx0 + 1 ) / xdiv;
00302         ydiv = ( gy1 - gy0 + 1 ) / ydiv;
00303         
00304         for ( int i = gx0 + xdiv; i <= ( gx1 - xdiv ); i += xdiv )
00305                 p. drawLine ( i, gy0, i, gy1 );
00306         
00307         for ( int i = gy0 + ydiv; i <= ( gy1 - ydiv ); i += ydiv )
00308                 p. drawLine ( gx0, i, gx1, i );
00309         
00310         p. setPen ( g. highlight ( ));
00311         
00312         p. drawLine ( BRD, ey, ex, ey );
00313         
00314         for ( int i = 1; i < st; i++ ) {
00315                 int fx = x0 + dx * i / st;
00316                 int fy = y0 + dy * i / ( st - 1 );
00317                 
00318                 p. drawLine ( ex, ey, fx, ey );
00319                 p. drawLine ( fx, ey, fx, fy );
00320                                 
00321                 ex = fx;
00322                 ey = fy;
00323         }       
00324         if ( st == 1 ) {
00325                 p. drawLine ( ex, ey, ex, y1 );
00326                 ey = y1;
00327         }
00328         
00329         p. drawLine ( ex, ey, width ( ) - 1 - BRD, ey );
00330         
00331         
00332         p. fillRect ( around ( x0, y0 ), m_dragged == 0 ? g. highlightedText ( ) : g. text ( ));
00333         p. drawRect ( around ( x0, y0 ));
00334         p. fillRect ( around ( x1, y1 ), m_dragged == 1 ? g. highlightedText ( ) : g. text ( ));
00335         p. drawRect ( around ( x1, y1 ));
00336         
00337         p. setPen ( g. text ( ));
00338         p. drawText ( tr, AlignTop | AlignRight, stepstr );
00339         
00340         p. end ( );
00341         bitBlt ( this, cr. topLeft ( ), &pix, cr ); 
00342         if ( !cr. contains ( tr ))
00343                 bitBlt ( this, tr. topLeft ( ), &pix, tr );             
00344 }
00345 

Generated on Sat Nov 5 16:15:57 2005 for OPIE by  doxygen 1.4.2