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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* This file is part of the KDE libraries
Copyright (C) 2004 Antonio Larrosa <larrosa@kde.org
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/* NOTE: There are two copies of this .h and the .cpp file, with subtle differences.
* One copy is in tdelibs/tdeui, and the other copy is in tdepim/libtdepim
* This is because tdepim has to remain backwards compatible. Any changes
* to either file should be made to the other.
*/
#include "kpixmapregionselectorwidget.h"
#include <tqpainter.h>
#include <tqcolor.h>
#include <tqimage.h>
#include <tqlayout.h>
#include <kimageeffect.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdepopupmenu.h>
#include <tdeaction.h>
#include <stdlib.h>
#include <tqcursor.h>
#include <tqapplication.h>
KPixmapRegionSelectorWidget::KPixmapRegionSelectorWidget( TQWidget *parent,
const char *name) : TQWidget( parent, name)
{
TQHBoxLayout * hboxLayout=new TQHBoxLayout( this );
hboxLayout->addStretch();
TQVBoxLayout * vboxLayout=new TQVBoxLayout( hboxLayout );
vboxLayout->addStretch();
m_label = new TQLabel(this, "pixmapHolder");
m_label->setBackgroundMode( TQt::NoBackground );
m_label->installEventFilter( this );
vboxLayout->addWidget(m_label);
vboxLayout->addStretch();
hboxLayout->addStretch();
m_forcedAspectRatio=0;
m_zoomFactor=1.0;
}
KPixmapRegionSelectorWidget::~KPixmapRegionSelectorWidget()
{
}
void KPixmapRegionSelectorWidget::setPixmap( const TQPixmap &pixmap )
{
Q_ASSERT(!pixmap.isNull()); //This class isn't designed to deal with null pixmaps.
m_originalPixmap = pixmap;
m_unzoomedPixmap = pixmap;
m_label->setPixmap( pixmap );
resetSelection();
}
void KPixmapRegionSelectorWidget::resetSelection()
{
m_selectedRegion = m_originalPixmap.rect();
updatePixmap();
}
TQRect KPixmapRegionSelectorWidget::selectedRegion() const
{
return m_selectedRegion;
}
void KPixmapRegionSelectorWidget::setSelectedRegion(const TQRect &rect)
{
if (!rect.isValid()) resetSelection();
else
{
m_selectedRegion=rect;
updatePixmap();
TQRect r=unzoomedSelectedRegion();
}
}
void KPixmapRegionSelectorWidget::updatePixmap()
{
Q_ASSERT(!m_originalPixmap.isNull()); if(m_originalPixmap.isNull()) { m_label->setPixmap(m_originalPixmap); return; }
if (m_selectedRegion.width()>m_originalPixmap.width()) m_selectedRegion.setWidth( m_originalPixmap.width() );
if (m_selectedRegion.height()>m_originalPixmap.height()) m_selectedRegion.setHeight( m_originalPixmap.height() );
TQPainter painter;
if (m_linedPixmap.isNull())
{
m_linedPixmap = m_originalPixmap;
painter.begin(&m_linedPixmap);
painter.setRasterOp( TQt::XorROP );
painter.fillRect(0,0,m_linedPixmap.width(), m_linedPixmap.height(),
TQBrush( TQColor(255,255,255), Qt::BDiagPattern) );
painter.end();
TQImage image=m_linedPixmap.convertToImage();
image=KImageEffect::fade(image, (float)0.4, TQColor(0,0,0));
m_linedPixmap.convertFromImage(image);
}
TQPixmap pixmap = m_linedPixmap;
painter.begin(&pixmap);
painter.drawPixmap( m_selectedRegion.topLeft(),
m_originalPixmap, m_selectedRegion );
painter.setPen( TQColor(255,255,255) );
painter.setRasterOp( TQt::XorROP );
painter.drawRect( m_selectedRegion );
painter.end();
m_label->setPixmap(pixmap);
}
TDEPopupMenu *KPixmapRegionSelectorWidget::createPopupMenu()
{
TDEPopupMenu *popup=new TDEPopupMenu(this, "PixmapRegionSelectorPopup");
popup->insertTitle(i18n("Image Operations"));
TDEAction *action = new TDEAction(i18n("&Rotate Clockwise"), "object-rotate-right",
0, TQT_TQOBJECT(this), TQT_SLOT(rotateClockwise()),
TQT_TQOBJECT(popup), "rotateclockwise");
action->plug(popup);
action = new TDEAction(i18n("Rotate &Counterclockwise"), "object-rotate-left",
0, TQT_TQOBJECT(this), TQT_SLOT(rotateCounterclockwise()),
TQT_TQOBJECT(popup), "rotatecounterclockwise");
action->plug(popup);
/*
I wonder if it would be appropiate to have here an "Open with..." option to
edit the image (antlarr)
*/
return popup;
}
void KPixmapRegionSelectorWidget::rotate(KImageEffect::RotateDirection direction)
{
int w=m_originalPixmap.width();
int h=m_originalPixmap.height();
TQImage img=m_unzoomedPixmap.convertToImage();
img= KImageEffect::rotate(img, direction);
m_unzoomedPixmap.convertFromImage(img);
img=m_originalPixmap.convertToImage();
img= KImageEffect::rotate(img, direction);
m_originalPixmap.convertFromImage(img);
m_linedPixmap=TQPixmap();
if (m_forcedAspectRatio>0 && m_forcedAspectRatio!=1)
resetSelection();
else
{
switch (direction)
{
case ( KImageEffect::Rotate90 ):
{
int x=h-m_selectedRegion.y()-m_selectedRegion.height();
int y=m_selectedRegion.x();
m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
updatePixmap();
} break;
case ( KImageEffect::Rotate270 ):
{
int x=m_selectedRegion.y();
int y=w-m_selectedRegion.x()-m_selectedRegion.width();
m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
updatePixmap();
} break;
default: resetSelection();
}
}
}
void KPixmapRegionSelectorWidget::rotateClockwise()
{
rotate(KImageEffect::Rotate90);
}
void KPixmapRegionSelectorWidget::rotateCounterclockwise()
{
rotate(KImageEffect::Rotate270);
}
bool KPixmapRegionSelectorWidget::eventFilter(TQObject *obj, TQEvent *ev)
{
if ( ev->type() == TQEvent::MouseButtonPress )
{
TQMouseEvent *mev= (TQMouseEvent *)(ev);
//kdDebug() << TQString("click at %1,%2").arg( mev->x() ).arg( mev->y() ) << endl;
if ( mev->button() == Qt::RightButton )
{
TDEPopupMenu *popup = createPopupMenu( );
popup->exec( mev->globalPos() );
delete popup;
return TRUE;
};
TQCursor cursor;
if ( m_selectedRegion.contains( mev->pos() )
&& m_selectedRegion!=m_originalPixmap.rect() )
{
m_state=Moving;
cursor.setShape( Qt::SizeAllCursor );
}
else
{
m_state=Resizing;
cursor.setShape( Qt::CrossCursor );
}
TQApplication::setOverrideCursor(cursor);
m_tempFirstClick=mev->pos();
return TRUE;
}
if ( ev->type() == TQEvent::MouseMove )
{
TQMouseEvent *mev= (TQMouseEvent *)(ev);
//kdDebug() << TQString("move to %1,%2").arg( mev->x() ).arg( mev->y() ) << endl;
if ( m_state == Resizing )
{
setSelectedRegion (
calcSelectionRectangle( m_tempFirstClick, mev->pos() ) );
}
else if (m_state == Moving )
{
int mevx = mev->x();
int mevy = mev->y();
bool mouseOutside=false;
if ( mevx < 0 )
{
m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
mouseOutside=true;
}
else if ( mevx > m_originalPixmap.width() )
{
m_selectedRegion.moveBy(m_originalPixmap.width()-m_selectedRegion.width()-m_selectedRegion.x(),0);
mouseOutside=true;
}
if ( mevy < 0 )
{
m_selectedRegion.moveBy(0,-m_selectedRegion.y());
mouseOutside=true;
}
else if ( mevy > m_originalPixmap.height() )
{
m_selectedRegion.moveBy(0,m_originalPixmap.height()-m_selectedRegion.height()-m_selectedRegion.y());
mouseOutside=true;
}
if (mouseOutside) { updatePixmap(); return TRUE; };
m_selectedRegion.moveBy( mev->x()-m_tempFirstClick.x(),
mev->y()-m_tempFirstClick.y() );
// Check that the region has not fallen outside the image
if (m_selectedRegion.x() < 0)
m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
else if (m_selectedRegion.right() > m_originalPixmap.width())
m_selectedRegion.moveBy(-(m_selectedRegion.right()-m_originalPixmap.width()),0);
if (m_selectedRegion.y() < 0)
m_selectedRegion.moveBy(0,-m_selectedRegion.y());
else if (m_selectedRegion.bottom() > m_originalPixmap.height())
m_selectedRegion.moveBy(0,-(m_selectedRegion.bottom()-m_originalPixmap.height()));
m_tempFirstClick=mev->pos();
updatePixmap();
}
return TRUE;
}
if ( ev->type() == TQEvent::MouseButtonRelease )
{
TQMouseEvent *mev= (TQMouseEvent *)(ev);
if ( m_state == Resizing && mev->pos() == m_tempFirstClick)
resetSelection();
m_state=None;
TQApplication::restoreOverrideCursor();
return TRUE;
}
TQWidget::eventFilter(obj, ev);
return FALSE;
}
TQRect KPixmapRegionSelectorWidget::calcSelectionRectangle( const TQPoint & startPoint, const TQPoint & _endPoint )
{
TQPoint endPoint = _endPoint;
if ( endPoint.x() < 0 ) endPoint.setX(0);
else if ( endPoint.x() > m_originalPixmap.width() ) endPoint.setX(m_originalPixmap.width());
if ( endPoint.y() < 0 ) endPoint.setY(0);
else if ( endPoint.y() > m_originalPixmap.height() ) endPoint.setY(m_originalPixmap.height());
int w=abs(startPoint.x()-endPoint.x());
int h=abs(startPoint.y()-endPoint.y());
if (m_forcedAspectRatio>0)
{
double aspectRatio=w/double(h);
if (aspectRatio>m_forcedAspectRatio)
h=(int)(w/m_forcedAspectRatio);
else
w=(int)(h*m_forcedAspectRatio);
}
int x,y;
if ( startPoint.x() < endPoint.x() )
x=startPoint.x();
else
x=startPoint.x()-w;
if ( startPoint.y() < endPoint.y() )
y=startPoint.y();
else
y=startPoint.y()-h;
if (x<0)
{
w+=x;
x=0;
h=(int)(w/m_forcedAspectRatio);
if ( startPoint.y() > endPoint.y() )
y=startPoint.y()-h;
}
else if (x+w>m_originalPixmap.width())
{
w=m_originalPixmap.width()-x;
h=(int)(w/m_forcedAspectRatio);
if ( startPoint.y() > endPoint.y() )
y=startPoint.y()-h;
}
if (y<0)
{
h+=y;
y=0;
w=(int)(h*m_forcedAspectRatio);
if ( startPoint.x() > endPoint.x() )
x=startPoint.x()-w;
}
else if (y+h>m_originalPixmap.height())
{
h=m_originalPixmap.height()-y;
w=(int)(h*m_forcedAspectRatio);
if ( startPoint.x() > endPoint.x() )
x=startPoint.x()-w;
}
return TQRect(x,y,w,h);
}
TQRect KPixmapRegionSelectorWidget::unzoomedSelectedRegion() const
{
return TQRect((int)(m_selectedRegion.x()/m_zoomFactor),
(int)(m_selectedRegion.y()/m_zoomFactor),
(int)(m_selectedRegion.width()/m_zoomFactor),
(int)(m_selectedRegion.height()/m_zoomFactor));
}
TQImage KPixmapRegionSelectorWidget::selectedImage() const
{
TQImage origImage=m_unzoomedPixmap.convertToImage();
return origImage.copy(unzoomedSelectedRegion());
}
void KPixmapRegionSelectorWidget::setSelectionAspectRatio(int width, int height)
{
m_forcedAspectRatio=width/double(height);
}
void KPixmapRegionSelectorWidget::setFreeSelectionAspectRatio()
{
m_forcedAspectRatio=0;
}
void KPixmapRegionSelectorWidget::setMaximumWidgetSize(int width, int height)
{
m_maxWidth=width;
m_maxHeight=height;
m_originalPixmap=m_unzoomedPixmap;
if (m_selectedRegion == m_originalPixmap.rect()) m_selectedRegion=TQRect();
// kdDebug() << TQString(" original Pixmap :") << m_originalPixmap.rect() << endl;
// kdDebug() << TQString(" unzoomed Pixmap : %1 x %2 ").arg(m_unzoomedPixmap.width()).arg(m_unzoomedPixmap.height()) << endl;
if ( !m_originalPixmap.isNull() &&
( m_originalPixmap.width() > m_maxWidth ||
m_originalPixmap.height() > m_maxHeight ) )
{
/* We have to resize the pixmap to get it complete on the screen */
TQImage image=m_originalPixmap.convertToImage();
m_originalPixmap.convertFromImage( image.smoothScale( width, height, TQImage::ScaleMin ) );
double oldZoomFactor = m_zoomFactor;
m_zoomFactor=m_originalPixmap.width()/(double)m_unzoomedPixmap.width();
if (m_selectedRegion.isValid())
{
m_selectedRegion=
TQRect((int)(m_selectedRegion.x()*m_zoomFactor/oldZoomFactor),
(int)(m_selectedRegion.y()*m_zoomFactor/oldZoomFactor),
(int)(m_selectedRegion.width()*m_zoomFactor/oldZoomFactor),
(int)(m_selectedRegion.height()*m_zoomFactor/oldZoomFactor) );
}
}
if (!m_selectedRegion.isValid()) m_selectedRegion = m_originalPixmap.rect();
m_linedPixmap=TQPixmap();
updatePixmap();
resize(m_label->width(), m_label->height());
}
#include "kpixmapregionselectorwidget.moc"
|