00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <qframe.h>
00019 #include <qimage.h>
00020 #include <qlayout.h>
00021 #include <qpainter.h>
00022 #include <qpoint.h>
00023
00024 #include <klocale.h>
00025 #include <kdebug.h>
00026 #include <kpushbutton.h>
00027
00028 #include "thumbnaileditor.h"
00029 #include "thumbnaileditorui.h"
00030 #include "thumbnailpicker.h"
00031
00032 ThumbnailEditor::ThumbnailEditor( QWidget *parent, const char *name )
00033 : KDialogBase( KDialogBase::Plain, i18n( "Edit Thumbnail Image" ), Ok|Cancel, Ok, parent, name )
00034 {
00035 tp = (ThumbnailPicker*)parent;
00036
00037 QFrame *page = plainPage();
00038 QHBoxLayout *hlay = new QHBoxLayout( page, 0, 0 );
00039 ui = new ThumbnailEditorUI( page );
00040 hlay->addWidget( ui );
00041
00042 ui->ImageCanvas->setCropRect( tp->imageRect()->x(), tp->imageRect()->y(),
00043 tp->imageRect()->width(), tp->imageRect()->height() );
00044 ui->ImageCanvas->setImage( tp->currentListImage() );
00045
00046 connect( ui->ImageCanvas, SIGNAL(cropRegionModified()), SLOT( slotUpdateCropLabel() ) );
00047 slotUpdateCropLabel();
00048
00049 update();
00050 }
00051
00052 ThumbnailEditor::~ThumbnailEditor()
00053 {}
00054
00055 QPixmap ThumbnailEditor::thumbnail() {
00056 QImage im = ui->ImageCanvas->croppedImage().convertToImage();
00057 im = im.smoothScale( 200, 200 );
00058 QPixmap pm;
00059 pm.convertFromImage( im );
00060 return pm;
00061 }
00062
00063 void ThumbnailEditor::slotUpdateCropLabel() {
00064 QRect *r = ui->ImageCanvas->cropRect();
00065 ui->CropLabel->setText( i18n( "Crop region: [%1,%2 %3x%4]" )
00066 .arg( r->left() ).arg( r->top() ).arg( r->width() ).arg( r->height() ) );
00067 }
00068
00069 QPixmap ThumbImage::croppedImage() {
00070 QPixmap result( CropRect->width(), CropRect->height() );
00071 bitBlt( &result, 0, 0, Image,
00072 CropRect->left(), CropRect->top(),
00073 CropRect->width(), CropRect->height() );
00074
00075 return result;
00076 }
00077
00078 ThumbImage::ThumbImage( QWidget *parent, const char *name ) : QLabel( parent, name )
00079 {
00080 setBackgroundMode( QWidget::NoBackground );
00081 bMouseButtonDown = false;
00082 bTopLeftGrab = false;
00083 bTopRightGrab = false;
00084 bBottomLeftGrab = false;
00085 bBottomRightGrab = false;
00086 HandleSize = 10;
00087
00088 CropRect = new QRect();
00089 Anchor = new QPoint();
00090 Image = new QPixmap();
00091 }
00092
00093 ThumbImage::~ThumbImage()
00094 {}
00095
00096 void ThumbImage::paintEvent( QPaintEvent* ) {
00097 QPixmap c( *Image );
00098 QPainter p;
00099 p.begin( &c );
00100 p.setPen( QPen( QColor( "Grey" ), 2 ) );
00101
00102 p.drawRect( *CropRect );
00103
00104 p.setPen( QPen( QColor( "Grey" ), 0 ) );
00105 p.drawRect( QRect( CropRect->left(), CropRect->top(),
00106 HandleSize, HandleSize ) );
00107 p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->top(),
00108 HandleSize, HandleSize ) );
00109 p.drawRect( QRect( CropRect->left(), CropRect->bottom() - HandleSize,
00110 HandleSize, HandleSize ) );
00111 p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->bottom() - HandleSize,
00112 HandleSize, HandleSize ) );
00113
00114 if ( CropRect->x() > 0 )
00115 p.fillRect( 0, 0, CropRect->x(), c.height(),
00116 QBrush( QColor("white"), Dense3Pattern ) );
00117 if ( CropRect->right() < c.width() )
00118 p.fillRect( CropRect->right(), 0, (c.width() - CropRect->right()), c.height(),
00119 QBrush( QColor("white"), Dense3Pattern ) );
00120 if ( CropRect->y() > 0 )
00121 p.fillRect( 0, 0, c.width(), CropRect->y(),
00122 QBrush( QColor("white"), Dense3Pattern ) );
00123 if ( CropRect->bottom() < c.height() )
00124 p.fillRect( 0, CropRect->bottom(), c.width(), (c.height() - CropRect->bottom()),
00125 QBrush( QColor("white"), Dense3Pattern ) );
00126
00127 p.end();
00128
00129 bitBlt( this, 0, 0, &c );
00130 }
00131
00132 void ThumbImage::mousePressEvent( QMouseEvent *e ) {
00133 if ( e->button() == LeftButton && CropRect->contains( e->pos() ) ) {
00134 bMouseButtonDown = true;
00135
00136
00137 Anchor->setX( e->x() - CropRect->left() );
00138 Anchor->setY( e->y() - CropRect->top() );
00139
00140 if ( e->x() <= CropRect->left() + HandleSize && e->y() <= CropRect->top() + HandleSize ) {
00141 bTopLeftGrab = true;
00142 }
00143 if ( e->x() <= CropRect->left() + HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
00144 bBottomLeftGrab = true;
00145 Anchor->setY( e->y() - CropRect->bottom() );
00146 }
00147 if ( e->x() >= CropRect->right() - HandleSize && e->y() <= CropRect->top() + HandleSize ) {
00148 bTopRightGrab = true;
00149 Anchor->setX( e->x() - CropRect->right() );
00150 }
00151 if ( e->x() >= CropRect->right() - HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
00152 bBottomRightGrab = true;
00153 Anchor->setX( e->x() - CropRect->right() );
00154 Anchor->setY( e->y() - CropRect->bottom() );
00155 }
00156 }
00157 }
00158
00159 void ThumbImage::mouseReleaseEvent( QMouseEvent *e ) {
00160 if ( bMouseButtonDown ) bMouseButtonDown = false;
00161 if ( bTopLeftGrab ) bTopLeftGrab = false;
00162 if ( bTopRightGrab ) bTopRightGrab = false;
00163 if ( bBottomLeftGrab ) bBottomLeftGrab = false;
00164 if ( bBottomRightGrab ) bBottomRightGrab = false;
00165 }
00166
00167 void ThumbImage::mouseMoveEvent( QMouseEvent *e ) {
00168 if ( bMouseButtonDown ) {
00169
00170
00171 if ( bTopLeftGrab ) {
00172 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
00173 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
00174 if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
00175 if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
00176 if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
00177 if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
00178 } else if ( bTopRightGrab ) {
00179 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
00180 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
00181 if ( CropRect->right() > width() ) CropRect->setRight( width() );
00182 if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
00183 if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
00184 if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
00185 } else if ( bBottomLeftGrab ) {
00186 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
00187 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
00188 if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
00189 if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
00190 if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
00191 if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
00192 } else if ( bBottomRightGrab ) {
00193 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
00194 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
00195 if ( CropRect->right() > width() ) CropRect->setRight( width() );
00196 if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
00197 if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
00198 if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
00199 } else {
00200 CropRect->moveTopLeft( QPoint( e->x() - Anchor->x(), e->y() - Anchor->y() ) );
00201 if ( CropRect->left() < 0 ) CropRect->moveLeft( 0 );
00202 if ( CropRect->right() > width() ) CropRect->moveRight( width() );
00203 if ( CropRect->top() < 0 ) CropRect->moveTop( 0 );
00204 if ( CropRect->bottom() > height() ) CropRect->moveBottom( height() );
00205 }
00206
00207 emit cropRegionModified();
00208 update();
00209 }
00210 }
00211
00212 #include "thumbnaileditor.moc"