00001 #include "oimagezoomer.h"
00002
00003 #include <opie2/odebug.h>
00004
00005 #include <qimage.h>
00006 #include <qpixmap.h>
00007 #include <qpainter.h>
00008 #include <qrect.h>
00009 #include <qpoint.h>
00010 #include <qsize.h>
00011
00012 namespace Opie {
00013 namespace MM {
00014
00025 OImageZoomer::OImageZoomer( QWidget* parent, const char* name, WFlags fl )
00026 : QFrame( parent, name, fl ) {
00027 init();
00028 }
00029
00030
00042 OImageZoomer::OImageZoomer( const QPixmap& pix, QWidget* par, const char* name, WFlags fl )
00043 : QFrame( par, name, fl ) {
00044 init();
00045 setImage( pix );
00046 }
00047
00048
00058 OImageZoomer::OImageZoomer( const QImage& img, QWidget* par, const char* name, WFlags fl)
00059 : QFrame( par, name, fl ) {
00060 init();
00061 setImage( img );
00062 }
00063
00064
00077 OImageZoomer::OImageZoomer( const QSize& pSize, const QSize& vSize, QWidget* par,
00078 const char* name, WFlags fl )
00079 : QFrame( par, name, fl ), m_imgSize( pSize ),m_visSize( vSize ) {
00080 init();
00081 }
00082
00086 OImageZoomer::~OImageZoomer() {
00087
00088 }
00089
00090 void OImageZoomer::init() {
00091 m_mevent = false;
00092 setFrameStyle( Panel | Sunken );
00093 }
00094
00095
00103 void OImageZoomer::setImageSize( const QSize& size ) {
00104 m_imgSize = size;
00105 repaint();
00106 }
00107
00117 void OImageZoomer::setViewPortSize( const QSize& size ) {
00118 m_visSize = size;
00119 repaint();
00120 }
00121
00129 void OImageZoomer::setVisiblePoint( const QPoint& pt ) {
00130 m_visPt = pt;
00131 repaint();
00132 }
00133
00134
00141 void OImageZoomer::setImage( const QImage& img) {
00142 m_img = img;
00143 resizeEvent( 0 );
00144 repaint();
00145 }
00146
00150 void OImageZoomer::setImage( const QPixmap& pix) {
00151 setImage( pix.convertToImage() );
00152 }
00153
00154 void OImageZoomer::resizeEvent( QResizeEvent* ev ) {
00155 QFrame::resizeEvent( ev );
00156 setBackgroundOrigin( QWidget::WidgetOrigin );
00157
00158 QPixmap pix; pix.convertFromImage( m_img.smoothScale( size().width(), size().height() ) );
00159 setBackgroundPixmap( pix);
00160 }
00161
00162 void OImageZoomer::drawContents( QPainter* p ) {
00163
00164
00165
00166 if ( m_imgSize.isEmpty() )
00167 return;
00168
00169
00170
00171
00172
00173
00174
00175
00176 QRect c( contentsRect() );
00177 p->setPen( Qt::red );
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 int len = m_imgSize.width();
00189 int x = (c.width()*m_visPt.x())/len + c.x();
00190 int w = (c.width()*m_visSize.width() )/len + c.x();
00191 if ( w > c.width() ) w = c.width();
00192
00193 len = m_imgSize.height();
00194 int y = (c.height()*m_visPt.y() )/len + c.y();
00195 int h = (c.height()*m_visSize.height() )/len + c.y();
00196 if ( h > c.height() ) h = c.height();
00197
00198 p->drawRect( x, y, w, h );
00199 }
00200
00201 void OImageZoomer::mousePressEvent( QMouseEvent*) {
00202 m_mouseX = m_mouseY = -1;
00203 m_mevent = true;
00204 }
00205
00206 void OImageZoomer::mouseReleaseEvent( QMouseEvent*ev) {
00207 if (!m_mevent) return;
00208 int mx, my;
00209 mx = ev->x();
00210 my = ev->y();
00211 int diffx = (mx) * m_imgSize.width() / width();
00212 int diffy = (my) * m_imgSize.height() / height();
00213 emit zoomArea(diffx,diffy);
00214 }
00215
00216 void OImageZoomer::mouseMoveEvent( QMouseEvent* ev ) {
00217 int mx, my;
00218 mx = ev->x();
00219 my = ev->y();
00220
00221 if ( m_mouseX != -1 && m_mouseY != -1 ) {
00222 m_mevent = false;
00223 int diffx = ( mx - m_mouseX ) * m_imgSize.width() / width();
00224 int diffy = ( my - m_mouseY ) * m_imgSize.height() / height();
00225 emit zoomAreaRel( diffx, diffy );
00226 }
00227 m_mouseX = mx;
00228 m_mouseY = my;
00229 }
00230
00231
00232 }
00233 }