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

Download this file

previewField.cpp    180 lines (151 with data), 5.8 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
/* ----------------------------------------------------------------
SmillaEnlarger - resize, especially magnify bitmaps in high quality
previewField.cpp: the preview-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 "previewField.h"
#include <QtGui>
PreviewField::PreviewField( QWidget *parent) {
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
selectX = 0.0;
selectY = 0.0;
previewCalculated = false;
zoomX = zoomY = 5.0;
selectW = preWidth/zoomX;
selectH = preHeight/zoomY;
srcImage = QImage( 200, 200, QImage::Format_ARGB32 );
srcImage.fill( qRgb(0,0,255) );
baseClipX0 = 0.0; baseClipY0 = 0.0;
baseClipX1 = float(srcImage.width()); baseClipY1 = float(srcImage.height());
}
PreviewField::~PreviewField( void ) {
}
void PreviewField::setZoom( float newZoomX, float newZoomY ) {
previewCalculated = false;
zoomX = newZoomX;
zoomY = newZoomY;
selectW = preWidth/zoomX;
selectH = preHeight/zoomY;
update();
}
void PreviewField::setTheImage( const QImage & newImage ) {
if( srcImage != newImage ) {
previewCalculated = false;
srcImage = newImage; //.convertToFormat(QImage::Format_ARGB32);
update();
}
}
void PreviewField::setPreview( const QImage & preImage ) {
previewCalculated = true;
previewImage = preImage;
update();
}
QSize PreviewField::sizeHint( void ) const {
return QSize( int( preWidth), int( preHeight ) );
}
QSize PreviewField::minimumSizeHint( void ) const {
return QSize( int( preWidth), int( preHeight ) );
}
// set the baseClipRect, which selects the used and displayed part of the source
void PreviewField::setClipRect( float cx0, float cy0, float cx1, float cy1 ) {
baseClipX0 = cx0; baseClipY0 = cy0;
baseClipX1 = cx1; baseClipY1 = cy1;
float xOld = selectX, yOld = selectY;
CorrectSelect(); // have we left the bounds? move us back
if( selectX != xOld || selectY != yOld ) { // real change ?
previewCalculated = false;
emit selectionChanged( selectX, selectY );
}
update();
}
void PreviewField::moveSelection( float nX, float nY ) {
if( nX != selectX || nY != selectY ) {
float xOld = selectX, yOld = selectY;
selectX = nX;
selectY = nY;
CorrectSelect(); // have we left the bounds? move us back
if( selectX != xOld || selectY != yOld ) { // real change ?
previewCalculated = false;
update();
emit selectionChanged( selectX, selectY );
}
}
}
QRect PreviewField::DstRect( void ) {
float x0,y0,x1,y1;
x0 = selectX;
y0 = selectY;
x1 = selectX + selectW;
y1 = selectY + selectH;
if( x1 > baseClipX1 )
x1 = baseClipX1;
if( y1 > baseClipY1 )
y1 = baseClipY1;
return QRect( int( x0*zoomX ), int( y0*zoomY ), int((x1-x0)*zoomX), int((y1-y0)*zoomY) );
}
void PreviewField::mousePressEvent( QMouseEvent *event ) {
if( event->button() == Qt::LeftButton ) {
mouseXGrabStart = event->pos().x();
mouseYGrabStart = event->pos().y();
sXGrabStart = selectX;
sYGrabStart = selectY;
}
}
void PreviewField::mouseMoveEvent( QMouseEvent *event ) {
if( event->buttons() & Qt::LeftButton ) {
float dx,dy,newx,newy;
newx = sXGrabStart - float( event->pos().x() - mouseXGrabStart)/zoomX;
newy = sYGrabStart - float( event->pos().y() - mouseYGrabStart)/zoomY;
moveSelection( newx, newy );
}
}
void PreviewField::paintEvent( QPaintEvent *event ) {
QPainter painter(this);
if( previewCalculated ) {
int tWidth = preWidth, tHeight=preHeight;
if( previewImage.width() < tWidth )
tWidth = previewImage.width();
if( previewImage.height() < tHeight )
tHeight = previewImage.height();
QRectF targetR( 0.0, 0.0, float(tWidth), float(tHeight) );
painter.drawImage( targetR, previewImage );
}
else {
int sx = int(selectX)-1, sy = int(selectY)-1;
int sw = int(selectW)+3, sh = int(selectH)+3;
if( sw > int( baseClipX1) - sx )
sw = int( baseClipX1) - sx;
if( sh > int( baseClipY1) - sy )
sh = int( baseClipY1) - sy;
int tx = int( ( float(sx) - selectX )*zoomX );
int ty = int( ( float(sy) - selectY )*zoomY );
int tw = int( float(sw)*zoomX );
int th = int( float(sh)*zoomY );
QRect sourceR, targetR;
sourceR = QRect( sx, sy, sw, sh );
targetR = QRect( tx, ty, tw, th );
painter.drawImage( targetR, srcImage, sourceR );
}
}
void PreviewField::CorrectSelect( void ) {
if( selectX > baseClipX1 - selectW )
selectX = baseClipX1 - selectW ;
if( selectY > baseClipY1 - selectH )
selectY = baseClipY1 - selectH ;
if( selectX < baseClipX0 )
selectX = baseClipX0;
if( selectY < baseClipY0 )
selectY = baseClipY0;
}