moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kstars / kstars / thumbnaileditor.cpp
blobf4f01787f05380e7056d7bdad28edaae185232f1
1 /***************************************************************************
2 thumbnaileditor.cpp - description
3 -------------------
4 begin : Thu Mar 2 2005
5 copyright : (C) 2005 by Jason Harris
6 email : kstars@30doradus.org
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <qframe.h>
19 #include <qimage.h>
20 #include <qlayout.h>
21 #include <qpainter.h>
22 #include <qpoint.h>
24 #include <klocale.h>
25 #include <kdebug.h>
26 #include <kpushbutton.h>
28 #include "thumbnaileditor.h"
29 #include "thumbnaileditorui.h"
30 #include "thumbnailpicker.h"
32 ThumbnailEditor::ThumbnailEditor( QWidget *parent, const char *name )
33 : KDialogBase( KDialogBase::Plain, i18n( "Edit Thumbnail Image" ), Ok|Cancel, Ok, parent, name )
35 tp = (ThumbnailPicker*)parent;
37 QFrame *page = plainPage();
38 QHBoxLayout *hlay = new QHBoxLayout( page, 0, 0 );
39 ui = new ThumbnailEditorUI( page );
40 hlay->addWidget( ui );
42 ui->ImageCanvas->setCropRect( tp->imageRect()->x(), tp->imageRect()->y(),
43 tp->imageRect()->width(), tp->imageRect()->height() );
44 ui->ImageCanvas->setImage( tp->currentListImage() );
46 // ui->ImageCanvas->setFixedSize( Image.width(), Image.height() );
47 // setFixedSize( ui->ImageCanvas->width(), ui->ImageCanvas->height()
48 // + ui->CropButton->height()
49 // + 2*ui->CropLabel->height() + 5*hlay->spacing());
51 connect( ui->ImageCanvas, SIGNAL(cropRegionModified()), SLOT( slotUpdateCropLabel() ) );
52 slotUpdateCropLabel();
54 update();
57 ThumbnailEditor::~ThumbnailEditor()
60 QPixmap ThumbnailEditor::thumbnail() {
61 QImage im = ui->ImageCanvas->croppedImage().convertToImage();
62 im = im.smoothScale( 200, 200 );
63 QPixmap pm;
64 pm.convertFromImage( im );
65 return pm;
68 void ThumbnailEditor::slotUpdateCropLabel() {
69 QRect *r = ui->ImageCanvas->cropRect();
70 ui->CropLabel->setText( i18n( "Crop region: [%1,%2 %3x%4]" )
71 .arg( r->left() ).arg( r->top() ).arg( r->width() ).arg( r->height() ) );
74 QPixmap ThumbImage::croppedImage() {
75 QPixmap result( CropRect->width(), CropRect->height() );
76 bitBlt( &result, 0, 0, Image,
77 CropRect->left(), CropRect->top(),
78 CropRect->width(), CropRect->height() );
80 return result;
83 ThumbImage::ThumbImage( QWidget *parent, const char *name ) : QLabel( parent, name )
85 setBackgroundMode( QWidget::NoBackground );
86 bMouseButtonDown = false;
87 bTopLeftGrab = false;
88 bTopRightGrab = false;
89 bBottomLeftGrab = false;
90 bBottomRightGrab = false;
91 HandleSize = 10;
93 CropRect = new QRect();
94 Anchor = new QPoint();
95 Image = new QPixmap();
98 ThumbImage::~ThumbImage()
101 void ThumbImage::paintEvent( QPaintEvent* ) {
102 QPixmap c( *Image );
103 QPainter p;
104 p.begin( &c );
105 p.setPen( QPen( QColor( "Grey" ), 2 ) );
107 p.drawRect( *CropRect );
109 p.setPen( QPen( QColor( "Grey" ), 0 ) );
110 p.drawRect( QRect( CropRect->left(), CropRect->top(),
111 HandleSize, HandleSize ) );
112 p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->top(),
113 HandleSize, HandleSize ) );
114 p.drawRect( QRect( CropRect->left(), CropRect->bottom() - HandleSize,
115 HandleSize, HandleSize ) );
116 p.drawRect( QRect( CropRect->right() - HandleSize, CropRect->bottom() - HandleSize,
117 HandleSize, HandleSize ) );
119 if ( CropRect->x() > 0 )
120 p.fillRect( 0, 0, CropRect->x(), c.height(),
121 QBrush( QColor("white"), Dense3Pattern ) );
122 if ( CropRect->right() < c.width() )
123 p.fillRect( CropRect->right(), 0, (c.width() - CropRect->right()), c.height(),
124 QBrush( QColor("white"), Dense3Pattern ) );
125 if ( CropRect->y() > 0 )
126 p.fillRect( 0, 0, c.width(), CropRect->y(),
127 QBrush( QColor("white"), Dense3Pattern ) );
128 if ( CropRect->bottom() < c.height() )
129 p.fillRect( 0, CropRect->bottom(), c.width(), (c.height() - CropRect->bottom()),
130 QBrush( QColor("white"), Dense3Pattern ) );
132 p.end();
134 bitBlt( this, 0, 0, &c );
137 void ThumbImage::mousePressEvent( QMouseEvent *e ) {
138 if ( e->button() == LeftButton && CropRect->contains( e->pos() ) ) {
139 bMouseButtonDown = true;
141 //The Anchor tells how far from the CropRect corner we clicked
142 Anchor->setX( e->x() - CropRect->left() );
143 Anchor->setY( e->y() - CropRect->top() );
145 if ( e->x() <= CropRect->left() + HandleSize && e->y() <= CropRect->top() + HandleSize ) {
146 bTopLeftGrab = true;
148 if ( e->x() <= CropRect->left() + HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
149 bBottomLeftGrab = true;
150 Anchor->setY( e->y() - CropRect->bottom() );
152 if ( e->x() >= CropRect->right() - HandleSize && e->y() <= CropRect->top() + HandleSize ) {
153 bTopRightGrab = true;
154 Anchor->setX( e->x() - CropRect->right() );
156 if ( e->x() >= CropRect->right() - HandleSize && e->y() >= CropRect->bottom() - HandleSize ) {
157 bBottomRightGrab = true;
158 Anchor->setX( e->x() - CropRect->right() );
159 Anchor->setY( e->y() - CropRect->bottom() );
164 void ThumbImage::mouseReleaseEvent( QMouseEvent *e ) {
165 if ( bMouseButtonDown ) bMouseButtonDown = false;
166 if ( bTopLeftGrab ) bTopLeftGrab = false;
167 if ( bTopRightGrab ) bTopRightGrab = false;
168 if ( bBottomLeftGrab ) bBottomLeftGrab = false;
169 if ( bBottomRightGrab ) bBottomRightGrab = false;
172 void ThumbImage::mouseMoveEvent( QMouseEvent *e ) {
173 if ( bMouseButtonDown ) {
175 //If a corner was grabbed, we are resizing the box
176 if ( bTopLeftGrab ) {
177 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
178 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
179 if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
180 if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
181 if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
182 if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
183 } else if ( bTopRightGrab ) {
184 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
185 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setTop( e->y() - Anchor->y() );
186 if ( CropRect->right() > width() ) CropRect->setRight( width() );
187 if ( CropRect->top() < 0 ) CropRect->setTop( 0 );
188 if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
189 if ( CropRect->height() < 200 ) CropRect->setTop( CropRect->top() - 200 + CropRect->height() );
190 } else if ( bBottomLeftGrab ) {
191 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setLeft( e->x() - Anchor->x() );
192 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
193 if ( CropRect->left() < 0 ) CropRect->setLeft( 0 );
194 if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
195 if ( CropRect->width() < 200 ) CropRect->setLeft( CropRect->left() - 200 + CropRect->width() );
196 if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
197 } else if ( bBottomRightGrab ) {
198 if ( e->x() >= 0 && e->x() <= width() ) CropRect->setRight( e->x() - Anchor->x() );
199 if ( e->y() >= 0 && e->y() <= height() ) CropRect->setBottom( e->y() - Anchor->y() );
200 if ( CropRect->right() > width() ) CropRect->setRight( width() );
201 if ( CropRect->bottom() > height() ) CropRect->setBottom( height() );
202 if ( CropRect->width() < 200 ) CropRect->setRight( CropRect->right() + 200 - CropRect->width() );
203 if ( CropRect->height() < 200 ) CropRect->setBottom( CropRect->bottom() + 200 - CropRect->height() );
204 } else { //no corner grabbed; move croprect
205 CropRect->moveTopLeft( QPoint( e->x() - Anchor->x(), e->y() - Anchor->y() ) );
206 if ( CropRect->left() < 0 ) CropRect->moveLeft( 0 );
207 if ( CropRect->right() > width() ) CropRect->moveRight( width() );
208 if ( CropRect->top() < 0 ) CropRect->moveTop( 0 );
209 if ( CropRect->bottom() > height() ) CropRect->moveBottom( height() );
212 emit cropRegionModified();
213 update();