00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "imageedit.h"
00021
00022 ImageEdit::ImageEdit( QWidget *parent, const char *name)
00023 : QScrollView( parent, name, WNorthWestGravity | WResizeNoErase ), buffer()
00024 {
00025 buffer.resize( size() );
00026 buffer.fill( colorGroup().color( QColorGroup::Base ) );
00027 }
00028
00029 ImageEdit::~ImageEdit()
00030 {
00031
00032 }
00033
00034 void ImageEdit::contentsMousePressEvent( QMouseEvent *e )
00035 {
00036 lastPos = e->pos();
00037 }
00038
00039 void ImageEdit::contentsMouseMoveEvent( QMouseEvent *e )
00040 {
00041 QPainter pw( viewport() );
00042 QPainter pb( &buffer );
00043 pb.drawLine( lastPos, e->pos() );
00044 pw.drawLine( contentsToViewport( lastPos ),
00045 contentsToViewport( e->pos() ) );
00046 lastPos = e->pos();
00047 }
00048
00049 void ImageEdit::contentsMouseReleaseEvent( QMouseEvent * )
00050 {
00051 }
00052
00053 void ImageEdit::viewportResizeEvent( QResizeEvent *e )
00054 {
00055 enlargeBuffer(e->size());
00056 }
00057
00058 void ImageEdit::enlargeBuffer( const QSize& sz )
00059 {
00060 QSize osz = buffer.size();
00061 QSize nsz( QMAX( osz.width(), sz.width() ), QMAX( osz.height(), sz.height() ) );
00062 buffer.resize( nsz.width(), nsz.height() );
00063
00064 QPainter p( &buffer );
00065 if ( sz.width() > osz.width() )
00066 p.fillRect( osz.width(), 0, sz.width() - osz.width(), nsz.height(), colorGroup().color( QColorGroup::Base ) );
00067 if ( sz.height() > osz.height() )
00068 p.fillRect( 0, osz.height(), nsz.width(), sz.height() - osz.height(), colorGroup().color( QColorGroup::Base ) );
00069 p.end();
00070 }
00071
00072 void ImageEdit::drawContents( QPainter *p, int cx, int cy, int cw, int ch )
00073 {
00074 p->drawPixmap( cx, cy, buffer, cx, cy, cw, ch );
00075 }
00076
00077 void ImageEdit::setPixmap( const QPixmap &pm )
00078 {
00079 QSize osz = buffer.size();
00080 if ( pm.width() < osz.width() || pm.height() < osz.height() ) {
00081 buffer.fill(white);
00082 enlargeBuffer( pm.size() );
00083 QPainter p(&buffer);
00084 p.drawPixmap(0,0,pm);
00085 } else {
00086 buffer = pm;
00087 }
00088 resizeContents( buffer.width(), buffer.height() );
00089 viewport()->repaint( FALSE );
00090 }
00091
00092 QPixmap ImageEdit::pixmap() const
00093 {
00094 return buffer;
00095 }
00096