[r45]: trunk / selectField.cpp Maximize Restore History

Download this file

selectField.cpp    298 lines (252 with data), 10.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* ----------------------------------------------------------------
SmillaEnlarger - resize, especially magnify bitmaps in high quality
selectField.cpp: the select-widget within the EnlargerDialog
Copyright (C) 2009 Mischa Lusteck
This program is free software;
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------- */
#include "selectField.h"
#include <QtGui>
#include <iostream>
using namespace std;
const int selectBorderWidth = 14;
SelectField::SelectField( QWidget *parent ) {
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
myImage = QImage( 200, 200, QImage::Format_ARGB32 );
myImage.fill( qRgb(0,0,255) );
selectW = 50.0;
selectH = 50.0;
baseClipX0 = 0.0; baseClipY0 = 0.0;
baseClipX1 = float(myImage.width()); baseClipY1 = float(myImage.height());
UpdateSizeAndZoom();
sX = baseClipX0 + 0.5*( baseClipX1 - baseClipX0 ) - 0.5*selectW;
sY = baseClipY0 + 0.5*( baseClipY1 - baseClipY0 ) - 0.5*selectH;
CorrectSelect();
selectBox = QRect( ScreenX( sX ), ScreenY( sY ), int( selectW*zoom + 0.5 ), int( selectH*zoom + 0.5 ) );
emit selectionChanged( sX, sY );
clipX0 = baseClipX0; clipY0 = baseClipY0;
clipX1 = baseClipX1; clipY1 = baseClipY1;
selectMoveMode = false;
}
SelectField::~SelectField( void ) {
}
void SelectField::setTheImage( const QImage & newImage ) {
if (newImage != myImage) {
myImage = newImage;
baseClipX0 = 0.0; baseClipY0 = 0.0;
baseClipX1 = float(myImage.width()); baseClipY1 = float(myImage.height());
UpdateSizeAndZoom();
UpdateScreenImage();
float nX,nY;
nX = baseClipX0 + 0.5*( baseClipX1 - baseClipX0 ) - 0.5*selectW;
nY = baseClipY0 + 0.5*( baseClipY1 - baseClipY0 ) - 0.5*selectH;
moveSelection( nX, nY );
emit selectionChanged( sX, sY );
clipX0 = baseClipX0; clipY0 = baseClipY0;
clipX1 = baseClipX1; clipY1 = baseClipY1;
update();
updateGeometry();
}
}
QSize SelectField::sizeHint( void ) const {
return mySize;
}
QSize SelectField::minimumSizeHint( void ) const {
return mySize;
}
void SelectField::setSelectSize( float sW, float sH ) {
if( sW != selectW || sH != selectH ) {
// move box that center stays unchanged
float xCenter = sX + 0.5*selectW;
float yCenter = sY + 0.5*selectH;
sX = xCenter - 0.5*sW;
sY = yCenter - 0.5*sH;
selectW = sW;
selectH = sH;
CorrectSelect(); // have we left the bounds? move us back
UpdateSelectBox();
emit selectionChanged( sX, sY );
}
}
// try to move selectBox within image
void SelectField::moveSelection ( float nX, float nY ) {
if( nX != sX || nY != sY ) {
float xOld = sX, yOld = sY;
sX = nX;
sY = nY;
CorrectSelect(); // have we left the bounds? move us back
if( sX != xOld || sY != yOld ) { // real change ?
UpdateSelectBox();
emit selectionChanged( sX, sY );
}
}
}
// set the baseClipRect, which selects the used and displayed part of the source
void SelectField::setClipRect( float cx0, float cy0, float cx1, float cy1 ) {
baseClipX0 = cx0; baseClipY0 = cy0;
baseClipX1 = cx1; baseClipY1 = cy1;
clipX0 = cx0; clipY0 = cy0;
clipX1 = cx1; clipY1 = cy1;
UpdateSizeAndZoom();
UpdateScreenImage();
float xOld = sX, yOld = sY;
CorrectSelect(); // have we left the bounds? move us back
if( sX != xOld || sY != yOld ) { // real change ?
emit selectionChanged( sX, sY );
}
UpdateSelectBox();
updateGeometry();
update();
}
void SelectField::mousePressEvent( QMouseEvent *event ) {
if( event->button() == Qt::LeftButton ) {
this->setFocus(); // only a simple means to remove focus from textedits ( and thus allow image paste in the EnlargerDialog )
int px = event->pos().x(), py = event->pos().y();
int sx0 = selectBox.x(), sy0 = selectBox.y();
int sx1 = sx0 + selectBox.width(), sy1 = sy0 + selectBox.height();
const int bR = selectBorderWidth/2;
if( ( px >= sx0-bR && px <= sx1+bR && py >= sy0-bR && py <= sy1+bR ) &&
!( px > sx0+bR && px < sx1-bR && py > sy0+bR && py < sy1-bR ) ) {
// start moving select-box
selectMoveMode = true;
grabX = float( event->pos().x() - selectBox.x() );
grabY = float( event->pos().y() - selectBox.y() );
grabX /= zoom; grabY /= zoom;
}
else { // start changing clip-box
clipX0 = SrcX( event->pos().x() );
clipY0 = SrcY( event->pos().y() );
clipX1 = clipX0;
clipY1 = clipY0;
update();
}
}
}
void SelectField::mouseMoveEvent( QMouseEvent *event ) {
if( event->buttons() & Qt::LeftButton ) {
if( selectMoveMode ) { // move select-box
float newx,newy;
newx = SrcX( event->pos().x() ) - grabX;
newy = SrcY( event->pos().y() ) - grabY;
moveSelection( newx, newy );
}
else { // change clip-box
float newx,newy;
newx = SrcX( event->pos().x() );
newy = SrcY( event->pos().y() );
if( newx < baseClipX0 )
newx = baseClipX0;
else if( newx > baseClipX1 )
newx = baseClipX1;
if( newy < baseClipY0 )
newy = baseClipY0;
else if( newy > baseClipY1 )
newy = baseClipY1;
if( newx<clipX0 )
clipX0 = newx;
if( newy<clipY0 )
clipY0 = newy;
clipX1 = newx;
clipY1 = newy;
update();
}
}
}
void SelectField::mouseReleaseEvent( QMouseEvent *event ) {
if( event->button() == Qt::LeftButton ) { // finish moving select-box
if( selectMoveMode ) {
selectMoveMode = false;
}
else { // finish changing clip-box
if( clipX0<clipX1 && clipY0<clipY1 )
emit clippingChanged( clipX0, clipY0, clipX1, clipY1 );
else {
clipX0 = baseClipX0; clipY0 = baseClipY0;
clipX1 = baseClipX1; clipY1 = baseClipY1;
}
update();
}
}
}
void SelectField::paintEvent( QPaintEvent *event ) {
QPainter painter(this);
painter.setClipping( true );
painter.setClipRect( 0, 0, mySize.width(), mySize.height() );
painter.drawImage( 0, 0, screenImage );
// draw clip-rect
int cx0 = ScreenX( clipX0 ), cy0 = ScreenY( clipY0 );
int cx1 = ScreenX( clipX1 ), cy1 = ScreenY( clipY1 );
painter.fillRect( 0, 0, mySize.width(), cy0, QColor(0,0,0,100) );
painter.fillRect( 0, cy0, cx0, cy1-cy0, QColor(0,0,0,100) );
painter.fillRect( cx1, cy0, mySize.width()-cx1, cy1-cy0, QColor(0,0,0,100) );
painter.fillRect( 0, cy1, mySize.width(), mySize.height() - cy1, QColor(0,0,0,100) );
QPen rectPen( QColor( 0, 0, 0, 100 ) );
rectPen.setWidth( selectBorderWidth );
rectPen.setJoinStyle( Qt::MiterJoin );
painter.setPen( rectPen );
painter.drawRect( selectBox );
rectPen.setColor( QColor(255,255,255, 100 ) );
rectPen.setWidth( 0 );
painter.setPen( rectPen );
painter.drawRect( selectBox );
}
// ---- ------------- ----
void SelectField::UpdateSizeAndZoom( void ) {
float newW,newH;
zoom = MAXHEIGHT / ( baseClipY1 - baseClipY0 );
newH = MAXHEIGHT;
newW = ( baseClipX1 - baseClipX0 ) * zoom;
if( newW > MAXWIDTH ) {
zoom = zoom * MAXWIDTH / newW;
newW = MAXWIDTH;
newH = ( baseClipY1 - baseClipY0 ) * zoom;
}
mySize = QSize( int( newW ), int( newH ) );
}
void SelectField::CorrectSelect( void ) {
if( sX > baseClipX1 - selectW )
sX = baseClipX1 - selectW ;
if( sY > baseClipY1 - selectH )
sY = baseClipY1 - selectH ;
if( sX < baseClipX0 )
sX = baseClipX0;
if( sY < baseClipY0 )
sY = baseClipY0;
}
void SelectField::UpdateSelectBox( void ) {
const long bW = selectBorderWidth + 1;
QRect oldBox = selectBox;
selectBox.moveTo( ScreenX( sX ), ScreenY( sY ) );
selectBox.setWidth ( int( selectW*zoom ) );
selectBox.setHeight( int( selectH*zoom ) );
update( oldBox.x()-bW/2, oldBox.y()-bW/2, oldBox.width()+1+bW, oldBox.height()+1+bW );
update( selectBox.x()-bW/2, selectBox.y()-bW/2, selectBox.width()+1+bW, selectBox.height()+1+bW );
}
// instead of drawing the source image into the field, which may be slow for big sizes,
// use a screenImage, into which the needed clip of the source is drawn once at every clipping-change
void SelectField::UpdateScreenImage( void ) {
screenImage = QImage( mySize, QImage::Format_ARGB32 );
QPainter painter( &screenImage );
painter.setRenderHint(QPainter::Antialiasing, true );
int sx = int( SrcX(0) )-1, sy = int( SrcY(0) )-1;
int sw = int( baseClipX1 - baseClipX0 ) + 3;
int sh = int( baseClipY1 - baseClipY0 ) + 3;
int tx = int( ( float(sx) - SrcX(0) )*zoom );
int ty = int( ( float(sy) - SrcY(0) )*zoom );
int tw = int( float(sw)*zoom );
int th = int( float(sh)*zoom );
QRect sourceR, targetR;
sourceR = QRect( sx, sy, sw, sh );
targetR = QRect( tx, ty, tw, th );
painter.drawImage( targetR, myImage, sourceR );
}