• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kpixmapregionselectorwidget.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2004 Antonio Larrosa <larrosa@kde.org
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 /* NOTE: There are two copies of this .h and the .cpp file, with subtle differences.
21  * One copy is in tdelibs/tdeui, and the other copy is in tdepim/libtdepim
22  * This is because tdepim has to remain backwards compatible. Any changes
23  * to either file should be made to the other.
24  */
25 
26 #include "kpixmapregionselectorwidget.h"
27 #include <tqpainter.h>
28 #include <tqcolor.h>
29 #include <tqimage.h>
30 #include <tqlayout.h>
31 #include <kimageeffect.h>
32 #include <kdebug.h>
33 #include <tdelocale.h>
34 #include <tdepopupmenu.h>
35 #include <tdeaction.h>
36 #include <stdlib.h>
37 #include <tqcursor.h>
38 #include <tqapplication.h>
39 
40 KPixmapRegionSelectorWidget::KPixmapRegionSelectorWidget( TQWidget *parent,
41  const char *name) : TQWidget( parent, name)
42 {
43  TQHBoxLayout * hboxLayout=new TQHBoxLayout( this );
44 
45  hboxLayout->addStretch();
46  TQVBoxLayout * vboxLayout=new TQVBoxLayout( hboxLayout );
47 
48  vboxLayout->addStretch();
49  m_label = new TQLabel(this, "pixmapHolder");
50  m_label->setBackgroundMode( TQt::NoBackground );
51  m_label->installEventFilter( this );
52 
53  vboxLayout->addWidget(m_label);
54  vboxLayout->addStretch();
55 
56  hboxLayout->addStretch();
57 
58  m_forcedAspectRatio=0;
59 
60  m_zoomFactor=1.0;
61 }
62 
63 KPixmapRegionSelectorWidget::~KPixmapRegionSelectorWidget()
64 {
65 }
66 
67 void KPixmapRegionSelectorWidget::setPixmap( const TQPixmap &pixmap )
68 {
69  Q_ASSERT(!pixmap.isNull()); //This class isn't designed to deal with null pixmaps.
70  m_originalPixmap = pixmap;
71  m_unzoomedPixmap = pixmap;
72  m_label->setPixmap( pixmap );
73  resetSelection();
74 }
75 
76 void KPixmapRegionSelectorWidget::resetSelection()
77 {
78  m_selectedRegion = m_originalPixmap.rect();
79  updatePixmap();
80 }
81 
82 TQRect KPixmapRegionSelectorWidget::selectedRegion() const
83 {
84  return m_selectedRegion;
85 }
86 
87 void KPixmapRegionSelectorWidget::setSelectedRegion(const TQRect &rect)
88 {
89  if (!rect.isValid()) resetSelection();
90  else
91  {
92  m_selectedRegion=rect;
93  updatePixmap();
94 
95  TQRect r=unzoomedSelectedRegion();
96  }
97 }
98 
99 void KPixmapRegionSelectorWidget::updatePixmap()
100 {
101  Q_ASSERT(!m_originalPixmap.isNull()); if(m_originalPixmap.isNull()) { m_label->setPixmap(m_originalPixmap); return; }
102  if (m_selectedRegion.width()>m_originalPixmap.width()) m_selectedRegion.setWidth( m_originalPixmap.width() );
103  if (m_selectedRegion.height()>m_originalPixmap.height()) m_selectedRegion.setHeight( m_originalPixmap.height() );
104 
105  TQPainter painter;
106  if (m_linedPixmap.isNull())
107  {
108  m_linedPixmap = m_originalPixmap;
109 
110  painter.begin(&m_linedPixmap);
111  painter.setRasterOp( TQt::XorROP );
112  painter.fillRect(0,0,m_linedPixmap.width(), m_linedPixmap.height(),
113  TQBrush( TQColor(255,255,255), TQt::BDiagPattern) );
114  painter.end();
115 
116  TQImage image=m_linedPixmap.convertToImage();
117  image=KImageEffect::fade(image, (float)0.4, TQColor(0,0,0));
118  m_linedPixmap.convertFromImage(image);
119  }
120 
121  TQPixmap pixmap = m_linedPixmap;
122 
123  painter.begin(&pixmap);
124  painter.drawPixmap( m_selectedRegion.topLeft(),
125  m_originalPixmap, m_selectedRegion );
126 
127  painter.setPen( TQColor(255,255,255) );
128  painter.setRasterOp( TQt::XorROP );
129 
130  painter.drawRect( m_selectedRegion );
131 
132  painter.end();
133 
134  m_label->setPixmap(pixmap);
135 }
136 
137 
138 TDEPopupMenu *KPixmapRegionSelectorWidget::createPopupMenu()
139 {
140  TDEPopupMenu *popup=new TDEPopupMenu(this, "PixmapRegionSelectorPopup");
141  popup->insertTitle(i18n("Image Operations"));
142 
143  TDEAction *action = new TDEAction(i18n("&Rotate Clockwise"), "object-rotate-right",
144  0, this, TQ_SLOT(rotateClockwise()),
145  popup, "rotateclockwise");
146  action->plug(popup);
147 
148  action = new TDEAction(i18n("Rotate &Counterclockwise"), "object-rotate-left",
149  0, this, TQ_SLOT(rotateCounterclockwise()),
150  popup, "rotatecounterclockwise");
151  action->plug(popup);
152 
153 /*
154  I wonder if it would be appropiate to have here an "Open with..." option to
155  edit the image (antlarr)
156 */
157  return popup;
158 }
159 
160 void KPixmapRegionSelectorWidget::rotate(KImageEffect::RotateDirection direction)
161 {
162  int w=m_originalPixmap.width();
163  int h=m_originalPixmap.height();
164  TQImage img=m_unzoomedPixmap.convertToImage();
165  img= KImageEffect::rotate(img, direction);
166  m_unzoomedPixmap.convertFromImage(img);
167 
168  img=m_originalPixmap.convertToImage();
169  img= KImageEffect::rotate(img, direction);
170  m_originalPixmap.convertFromImage(img);
171 
172  m_linedPixmap=TQPixmap();
173 
174  if (m_forcedAspectRatio>0 && m_forcedAspectRatio!=1)
175  resetSelection();
176  else
177  {
178  switch (direction)
179  {
180  case ( KImageEffect::Rotate90 ):
181  {
182  int x=h-m_selectedRegion.y()-m_selectedRegion.height();
183  int y=m_selectedRegion.x();
184  m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
185  updatePixmap();
186  } break;
187  case ( KImageEffect::Rotate270 ):
188  {
189  int x=m_selectedRegion.y();
190  int y=w-m_selectedRegion.x()-m_selectedRegion.width();
191  m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
192  updatePixmap();
193  } break;
194  default: resetSelection();
195  }
196  }
197 }
198 
199 void KPixmapRegionSelectorWidget::rotateClockwise()
200 {
201  rotate(KImageEffect::Rotate90);
202 }
203 
204 void KPixmapRegionSelectorWidget::rotateCounterclockwise()
205 {
206  rotate(KImageEffect::Rotate270);
207 }
208 
209 bool KPixmapRegionSelectorWidget::eventFilter(TQObject *obj, TQEvent *ev)
210 {
211  if ( ev->type() == TQEvent::MouseButtonPress )
212  {
213  TQMouseEvent *mev= (TQMouseEvent *)(ev);
214  //kdDebug() << TQString("click at %1,%2").arg( mev->x() ).arg( mev->y() ) << endl;
215 
216  if ( mev->button() == TQt::RightButton )
217  {
218  TDEPopupMenu *popup = createPopupMenu( );
219  popup->exec( mev->globalPos() );
220  delete popup;
221  return TRUE;
222  };
223 
224  TQCursor cursor;
225 
226  if ( m_selectedRegion.contains( mev->pos() )
227  && m_selectedRegion!=m_originalPixmap.rect() )
228  {
229  m_state=Moving;
230  cursor.setShape( TQt::SizeAllCursor );
231  }
232  else
233  {
234  m_state=Resizing;
235  cursor.setShape( TQt::CrossCursor );
236  }
237  TQApplication::setOverrideCursor(cursor);
238 
239  m_tempFirstClick=mev->pos();
240 
241 
242  return TRUE;
243  }
244 
245  if ( ev->type() == TQEvent::MouseMove )
246  {
247  TQMouseEvent *mev= (TQMouseEvent *)(ev);
248 
249  //kdDebug() << TQString("move to %1,%2").arg( mev->x() ).arg( mev->y() ) << endl;
250 
251  if ( m_state == Resizing )
252  {
253  setSelectedRegion (
254  calcSelectionRectangle( m_tempFirstClick, mev->pos() ) );
255  }
256  else if (m_state == Moving )
257  {
258  int mevx = mev->x();
259  int mevy = mev->y();
260  bool mouseOutside=false;
261  if ( mevx < 0 )
262  {
263  m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
264  mouseOutside=true;
265  }
266  else if ( mevx > m_originalPixmap.width() )
267  {
268  m_selectedRegion.moveBy(m_originalPixmap.width()-m_selectedRegion.width()-m_selectedRegion.x(),0);
269  mouseOutside=true;
270  }
271  if ( mevy < 0 )
272  {
273  m_selectedRegion.moveBy(0,-m_selectedRegion.y());
274  mouseOutside=true;
275  }
276  else if ( mevy > m_originalPixmap.height() )
277  {
278  m_selectedRegion.moveBy(0,m_originalPixmap.height()-m_selectedRegion.height()-m_selectedRegion.y());
279  mouseOutside=true;
280  }
281  if (mouseOutside) { updatePixmap(); return TRUE; };
282 
283  m_selectedRegion.moveBy( mev->x()-m_tempFirstClick.x(),
284  mev->y()-m_tempFirstClick.y() );
285 
286  // Check that the region has not fallen outside the image
287  if (m_selectedRegion.x() < 0)
288  m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
289  else if (m_selectedRegion.right() > m_originalPixmap.width())
290  m_selectedRegion.moveBy(-(m_selectedRegion.right()-m_originalPixmap.width()),0);
291 
292  if (m_selectedRegion.y() < 0)
293  m_selectedRegion.moveBy(0,-m_selectedRegion.y());
294  else if (m_selectedRegion.bottom() > m_originalPixmap.height())
295  m_selectedRegion.moveBy(0,-(m_selectedRegion.bottom()-m_originalPixmap.height()));
296 
297  m_tempFirstClick=mev->pos();
298  updatePixmap();
299  }
300  return TRUE;
301  }
302 
303  if ( ev->type() == TQEvent::MouseButtonRelease )
304  {
305  TQMouseEvent *mev= (TQMouseEvent *)(ev);
306 
307  if ( m_state == Resizing && mev->pos() == m_tempFirstClick)
308  resetSelection();
309 
310  m_state=None;
311  TQApplication::restoreOverrideCursor();
312 
313  return TRUE;
314  }
315 
316  TQWidget::eventFilter(obj, ev);
317  return FALSE;
318 }
319 
320 TQRect KPixmapRegionSelectorWidget::calcSelectionRectangle( const TQPoint & startPoint, const TQPoint & _endPoint )
321 {
322  TQPoint endPoint = _endPoint;
323  if ( endPoint.x() < 0 ) endPoint.setX(0);
324  else if ( endPoint.x() > m_originalPixmap.width() ) endPoint.setX(m_originalPixmap.width());
325  if ( endPoint.y() < 0 ) endPoint.setY(0);
326  else if ( endPoint.y() > m_originalPixmap.height() ) endPoint.setY(m_originalPixmap.height());
327  int w=abs(startPoint.x()-endPoint.x());
328  int h=abs(startPoint.y()-endPoint.y());
329 
330  if (m_forcedAspectRatio>0)
331  {
332  double aspectRatio=w/double(h);
333 
334  if (aspectRatio>m_forcedAspectRatio)
335  h=(int)(w/m_forcedAspectRatio);
336  else
337  w=(int)(h*m_forcedAspectRatio);
338  }
339 
340  int x,y;
341  if ( startPoint.x() < endPoint.x() )
342  x=startPoint.x();
343  else
344  x=startPoint.x()-w;
345  if ( startPoint.y() < endPoint.y() )
346  y=startPoint.y();
347  else
348  y=startPoint.y()-h;
349 
350  if (x<0)
351  {
352  w+=x;
353  x=0;
354  h=(int)(w/m_forcedAspectRatio);
355 
356  if ( startPoint.y() > endPoint.y() )
357  y=startPoint.y()-h;
358  }
359  else if (x+w>m_originalPixmap.width())
360  {
361  w=m_originalPixmap.width()-x;
362  h=(int)(w/m_forcedAspectRatio);
363 
364  if ( startPoint.y() > endPoint.y() )
365  y=startPoint.y()-h;
366  }
367  if (y<0)
368  {
369  h+=y;
370  y=0;
371  w=(int)(h*m_forcedAspectRatio);
372 
373  if ( startPoint.x() > endPoint.x() )
374  x=startPoint.x()-w;
375  }
376  else if (y+h>m_originalPixmap.height())
377  {
378  h=m_originalPixmap.height()-y;
379  w=(int)(h*m_forcedAspectRatio);
380 
381  if ( startPoint.x() > endPoint.x() )
382  x=startPoint.x()-w;
383  }
384 
385  return TQRect(x,y,w,h);
386 }
387 
388 TQRect KPixmapRegionSelectorWidget::unzoomedSelectedRegion() const
389 {
390  return TQRect((int)(m_selectedRegion.x()/m_zoomFactor),
391  (int)(m_selectedRegion.y()/m_zoomFactor),
392  (int)(m_selectedRegion.width()/m_zoomFactor),
393  (int)(m_selectedRegion.height()/m_zoomFactor));
394 }
395 
396 TQImage KPixmapRegionSelectorWidget::selectedImage() const
397 {
398  TQImage origImage=m_unzoomedPixmap.convertToImage();
399  return origImage.copy(unzoomedSelectedRegion());
400 }
401 
402 void KPixmapRegionSelectorWidget::setSelectionAspectRatio(int width, int height)
403 {
404  m_forcedAspectRatio=width/double(height);
405 }
406 
407 void KPixmapRegionSelectorWidget::setFreeSelectionAspectRatio()
408 {
409  m_forcedAspectRatio=0;
410 }
411 
412 void KPixmapRegionSelectorWidget::setMaximumWidgetSize(int width, int height)
413 {
414  m_maxWidth=width;
415  m_maxHeight=height;
416 
417  m_originalPixmap=m_unzoomedPixmap;
418  if (m_selectedRegion == m_originalPixmap.rect()) m_selectedRegion=TQRect();
419 
420 // kdDebug() << TQString(" original Pixmap :") << m_originalPixmap.rect() << endl;
421 // kdDebug() << TQString(" unzoomed Pixmap : %1 x %2 ").arg(m_unzoomedPixmap.width()).arg(m_unzoomedPixmap.height()) << endl;
422 
423  if ( !m_originalPixmap.isNull() &&
424  ( m_originalPixmap.width() > m_maxWidth ||
425  m_originalPixmap.height() > m_maxHeight ) )
426  {
427  /* We have to resize the pixmap to get it complete on the screen */
428  TQImage image=m_originalPixmap.convertToImage();
429  m_originalPixmap.convertFromImage( image.smoothScale( width, height, TQImage::ScaleMin ) );
430  double oldZoomFactor = m_zoomFactor;
431  m_zoomFactor=m_originalPixmap.width()/(double)m_unzoomedPixmap.width();
432 
433  if (m_selectedRegion.isValid())
434  {
435  m_selectedRegion=
436  TQRect((int)(m_selectedRegion.x()*m_zoomFactor/oldZoomFactor),
437  (int)(m_selectedRegion.y()*m_zoomFactor/oldZoomFactor),
438  (int)(m_selectedRegion.width()*m_zoomFactor/oldZoomFactor),
439  (int)(m_selectedRegion.height()*m_zoomFactor/oldZoomFactor) );
440  }
441  }
442 
443  if (!m_selectedRegion.isValid()) m_selectedRegion = m_originalPixmap.rect();
444 
445  m_linedPixmap=TQPixmap();
446  updatePixmap();
447  resize(m_label->width(), m_label->height());
448 }
449 
450 #include "kpixmapregionselectorwidget.moc"
KImageEffect::fade
static TQImage & fade(TQImage &image, float val, const TQColor &color)
KImageEffect::RotateDirection
RotateDirection
KImageEffect::Rotate270
Rotate270
KImageEffect::Rotate90
Rotate90
KImageEffect::rotate
static TQImage rotate(TQImage &src, RotateDirection r)
KPixmapRegionSelectorWidget::resetSelection
void resetSelection()
Resets the selection to use the whole image.
Definition: kpixmapregionselectorwidget.cpp:76
KPixmapRegionSelectorWidget::setSelectedRegion
void setSelectedRegion(const TQRect &rect)
Sets the selected region to be rect (in zoomed pixmap coordinates)
Definition: kpixmapregionselectorwidget.cpp:87
KPixmapRegionSelectorWidget::rotate
void rotate(KImageEffect::RotateDirection direction)
Rotates the image as specified by the direction parameter, also tries to rotate the selected region s...
Definition: kpixmapregionselectorwidget.cpp:160
KPixmapRegionSelectorWidget::rotateClockwise
void rotateClockwise()
Rotates the current image 90º clockwise.
Definition: kpixmapregionselectorwidget.cpp:199
KPixmapRegionSelectorWidget::KPixmapRegionSelectorWidget
KPixmapRegionSelectorWidget(TQWidget *parent=0L, const char *name=0L)
Constructor for a KPixmapRegionSelectorWidget.
Definition: kpixmapregionselectorwidget.cpp:40
KPixmapRegionSelectorWidget::selectedImage
TQImage selectedImage() const
Definition: kpixmapregionselectorwidget.cpp:396
KPixmapRegionSelectorWidget::setFreeSelectionAspectRatio
void setFreeSelectionAspectRatio()
Allows the user to do a selection which has any aspect ratio.
Definition: kpixmapregionselectorwidget.cpp:407
KPixmapRegionSelectorWidget::unzoomedSelectedRegion
TQRect unzoomedSelectedRegion() const
Returns the selected region ( in unzoomed, original pixmap coordinates )
Definition: kpixmapregionselectorwidget.cpp:388
KPixmapRegionSelectorWidget::pixmap
TQPixmap pixmap() const
Definition: kpixmapregionselectorwidget.h:69
KPixmapRegionSelectorWidget::setSelectionAspectRatio
void setSelectionAspectRatio(int width, int height)
Sets the aspect ration that the selected subimage should have.
Definition: kpixmapregionselectorwidget.cpp:402
KPixmapRegionSelectorWidget::createPopupMenu
virtual TDEPopupMenu * createPopupMenu()
Creates a TDEPopupMenu with the menu that appears when clicking with the right button on the label.
Definition: kpixmapregionselectorwidget.cpp:138
KPixmapRegionSelectorWidget::setPixmap
void setPixmap(const TQPixmap &pixmap)
Sets the pixmap which will be shown for the user to select a region from.
Definition: kpixmapregionselectorwidget.cpp:67
KPixmapRegionSelectorWidget::~KPixmapRegionSelectorWidget
~KPixmapRegionSelectorWidget()
Destructor for a KPixmapRegionSelectorWidget.
Definition: kpixmapregionselectorwidget.cpp:63
KPixmapRegionSelectorWidget::rotateCounterclockwise
void rotateCounterclockwise()
Rotates the current image 90º counterclockwise.
Definition: kpixmapregionselectorwidget.cpp:204
KPixmapRegionSelectorWidget::setMaximumWidgetSize
void setMaximumWidgetSize(int width, int height)
Sets the maximum size for the widget.
Definition: kpixmapregionselectorwidget.cpp:412
KPixmapRegionSelectorWidget::selectedRegion
TQRect selectedRegion() const
Returns the selected region ( in zoomed pixmap coordinates )
Definition: kpixmapregionselectorwidget.cpp:82
TDEAction
Class to encapsulate user-driven action or event.
Definition: tdeaction.h:203
TDEPopupMenu
A menu with title items.
Definition: tdepopupmenu.h:123
TDEPopupMenu::insertTitle
int insertTitle(const TQString &text, int id=-1, int index=-1)
Inserts a title item with no icon.
Definition: tdepopupmenu.cpp:181
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.1
This website is maintained by Timothy Pearson.