28 #include "kpixmapregionselectorwidget.h"
29 #include <tqpainter.h>
33 #include <kimageeffect.h>
35 #include <tdelocale.h>
36 #include <tdepopupmenu.h>
37 #include <tdeaction.h>
40 #include <tqapplication.h>
45 const char *name) : TQWidget( parent, name)
47 TQHBoxLayout * hboxLayout=
new TQHBoxLayout(
this );
49 hboxLayout->addStretch();
50 TQVBoxLayout * vboxLayout=
new TQVBoxLayout( hboxLayout );
52 vboxLayout->addStretch();
53 m_label =
new TQLabel(
this,
"pixmapHolder");
54 m_label->setBackgroundMode( TQt::NoBackground );
55 m_label->installEventFilter(
this );
57 vboxLayout->addWidget(m_label);
58 vboxLayout->addStretch();
60 hboxLayout->addStretch();
62 m_forcedAspectRatio=0;
73 Q_ASSERT(!
pixmap.isNull());
76 m_label->setPixmap(
pixmap );
82 m_selectedRegion = m_originalPixmap.rect();
88 return m_selectedRegion;
96 m_selectedRegion=rect;
103 void KPixmapRegionSelectorWidget::updatePixmap()
105 Q_ASSERT(!m_originalPixmap.isNull());
if(m_originalPixmap.isNull()) { m_label->setPixmap(m_originalPixmap);
return; }
106 if (m_selectedRegion.width()>m_originalPixmap.width()) m_selectedRegion.setWidth( m_originalPixmap.width() );
107 if (m_selectedRegion.height()>m_originalPixmap.height()) m_selectedRegion.setHeight( m_originalPixmap.height() );
110 if (m_linedPixmap.isNull())
112 m_linedPixmap = m_originalPixmap;
114 painter.begin(&m_linedPixmap);
115 painter.setRasterOp( TQt::XorROP );
116 painter.fillRect(0,0,m_linedPixmap.width(), m_linedPixmap.height(),
117 TQBrush( TQColor(255,255,255), TQt::BDiagPattern) );
120 TQImage image=m_linedPixmap.convertToImage();
121 image=KImageEffect::fade(image, 0.4, TQColor(0,0,0));
122 m_linedPixmap.convertFromImage(image);
125 TQPixmap
pixmap = m_linedPixmap;
128 painter.drawPixmap( m_selectedRegion.topLeft(),
129 m_originalPixmap, m_selectedRegion );
131 painter.setPen( TQColor(255,255,255) );
132 painter.setRasterOp( TQt::XorROP );
134 painter.drawRect( m_selectedRegion );
138 m_label->setPixmap(
pixmap);
143 TDEPopupMenu *popup=
new TDEPopupMenu(
this,
"PixmapRegionSelectorPopup");
144 popup->insertTitle(i18n(
"Image Operations"));
146 TDEAction *action =
new TDEAction(i18n(
"&Rotate Clockwise"),
"object-rotate-right",
148 popup,
"rotateclockwise");
151 action =
new TDEAction(i18n(
"Rotate &Counterclockwise"),
"object-rotate-left",
153 popup,
"rotatecounterclockwise");
165 int w=m_originalPixmap.width();
166 int h=m_originalPixmap.height();
167 TQImage img=m_unzoomedPixmap.convertToImage();
168 img= KImageEffect::rotate(img, direction);
169 m_unzoomedPixmap.convertFromImage(img);
171 img=m_originalPixmap.convertToImage();
172 img= KImageEffect::rotate(img, direction);
173 m_originalPixmap.convertFromImage(img);
175 m_linedPixmap=TQPixmap();
177 if (m_forcedAspectRatio>0 && m_forcedAspectRatio!=1)
183 case ( KImageEffect::Rotate90 ):
185 int x=h-m_selectedRegion.y()-m_selectedRegion.height();
186 int y=m_selectedRegion.x();
187 m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
190 case ( KImageEffect::Rotate270 ):
192 int x=m_selectedRegion.y();
193 int y=w-m_selectedRegion.x()-m_selectedRegion.width();
194 m_selectedRegion.setRect(x, y, m_selectedRegion.height(), m_selectedRegion.width() );
204 rotate(KImageEffect::Rotate90);
209 rotate(KImageEffect::Rotate270);
214 bool KPixmapRegionSelectorWidget::eventFilter(TQObject *obj, TQEvent *ev)
216 if ( ev->type() == TQEvent::MouseButtonPress )
218 TQMouseEvent *mev= (TQMouseEvent *)(ev);
221 if ( mev->button() == TQt::RightButton )
224 popup->exec( mev->globalPos() );
230 if ( m_selectedRegion.contains( mev->pos() )
231 && m_selectedRegion!=m_originalPixmap.rect() )
234 cursor=TQCursor(TQt::SizeAllCursor);
239 cursor=TQCursor(TQt::CrossCursor);
241 TQApplication::setOverrideCursor(cursor);
243 m_tempFirstClick=mev->pos();
248 if ( ev->type() == TQEvent::MouseMove )
250 TQMouseEvent *mev= (TQMouseEvent *)(ev);
254 if ( m_state == Resizing )
257 calcSelectionRectangle( m_tempFirstClick, mev->pos() ) );
259 else if (m_state == Moving )
263 bool mouseOutside=
false;
266 m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
269 else if ( mevx > m_originalPixmap.width() )
271 m_selectedRegion.moveBy(m_originalPixmap.width()-m_selectedRegion.width()-m_selectedRegion.x(),0);
276 m_selectedRegion.moveBy(0,-m_selectedRegion.y());
279 else if ( mevy > m_originalPixmap.height() )
281 m_selectedRegion.moveBy(0,m_originalPixmap.height()-m_selectedRegion.height()-m_selectedRegion.y());
284 if (mouseOutside) { updatePixmap();
return TRUE; };
286 m_selectedRegion.moveBy( mev->x()-m_tempFirstClick.x(),
287 mev->y()-m_tempFirstClick.y() );
290 if (m_selectedRegion.x() < 0)
291 m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
292 else if (m_selectedRegion.right() > m_originalPixmap.width())
293 m_selectedRegion.moveBy(-(m_selectedRegion.right()-m_originalPixmap.width()),0);
295 if (m_selectedRegion.y() < 0)
296 m_selectedRegion.moveBy(0,-m_selectedRegion.y());
297 else if (m_selectedRegion.bottom() > m_originalPixmap.height())
298 m_selectedRegion.moveBy(0,-(m_selectedRegion.bottom()-m_originalPixmap.height()));
300 m_tempFirstClick=mev->pos();
306 if ( ev->type() == TQEvent::MouseButtonRelease )
308 TQMouseEvent *mev= (TQMouseEvent *)(ev);
310 if ( m_state == Resizing && mev->pos() == m_tempFirstClick)
314 TQApplication::restoreOverrideCursor();
319 TQWidget::eventFilter(obj, ev);
323 TQRect KPixmapRegionSelectorWidget::calcSelectionRectangle(
const TQPoint & startPoint,
const TQPoint & _endPoint )
325 TQPoint endPoint = _endPoint;
326 if ( endPoint.x() < 0 ) endPoint.setX(0);
327 else if ( endPoint.x() > m_originalPixmap.width() ) endPoint.setX(m_originalPixmap.width());
328 if ( endPoint.y() < 0 ) endPoint.setY(0);
329 else if ( endPoint.y() > m_originalPixmap.height() ) endPoint.setY(m_originalPixmap.height());
330 int w=abs(startPoint.x()-endPoint.x());
331 int h=abs(startPoint.y()-endPoint.y());
333 if (m_forcedAspectRatio>0)
335 double aspectRatio=w/double(h);
337 if (aspectRatio>m_forcedAspectRatio)
338 h=(int)(w/m_forcedAspectRatio);
340 w=(int)(h*m_forcedAspectRatio);
344 if ( startPoint.x() < endPoint.x() )
348 if ( startPoint.y() < endPoint.y() )
357 h=(int)(w/m_forcedAspectRatio);
359 if ( startPoint.y() > endPoint.y() )
362 else if (x+w>m_originalPixmap.width())
364 w=m_originalPixmap.width()-x;
365 h=(int)(w/m_forcedAspectRatio);
367 if ( startPoint.y() > endPoint.y() )
374 w=(int)(h*m_forcedAspectRatio);
376 if ( startPoint.x() > endPoint.x() )
379 else if (y+h>m_originalPixmap.height())
381 h=m_originalPixmap.height()-y;
382 w=(int)(h*m_forcedAspectRatio);
384 if ( startPoint.x() > endPoint.x() )
388 return TQRect(x,y,w,h);
393 return TQRect((
int)(m_selectedRegion.x()/m_zoomFactor),
394 (
int)(m_selectedRegion.y()/m_zoomFactor),
395 (
int)(m_selectedRegion.width()/m_zoomFactor),
396 (
int)(m_selectedRegion.height()/m_zoomFactor));
401 TQImage origImage=m_unzoomedPixmap.convertToImage();
407 m_forcedAspectRatio=width/double(height);
412 m_forcedAspectRatio=0;
420 m_originalPixmap=m_unzoomedPixmap;
421 if (m_selectedRegion == m_originalPixmap.rect()) m_selectedRegion=TQRect();
426 if ( !m_originalPixmap.isNull() &&
427 ( m_originalPixmap.width() > m_maxWidth ||
428 m_originalPixmap.height() > m_maxHeight ) )
431 TQImage image=m_originalPixmap.convertToImage();
432 m_originalPixmap.convertFromImage( image.smoothScale( width, height, TQImage::ScaleMin ) );
434 double oldZoomFactor = m_zoomFactor;
435 m_zoomFactor=m_originalPixmap.width()/(double)m_unzoomedPixmap.width();
437 if (m_selectedRegion.isValid())
440 TQRect((
int)(m_selectedRegion.x()*m_zoomFactor/oldZoomFactor),
441 (
int)(m_selectedRegion.y()*m_zoomFactor/oldZoomFactor),
442 (
int)(m_selectedRegion.width()*m_zoomFactor/oldZoomFactor),
443 (
int)(m_selectedRegion.height()*m_zoomFactor/oldZoomFactor) );
447 if (!m_selectedRegion.isValid()) m_selectedRegion = m_originalPixmap.rect();
449 m_linedPixmap=TQPixmap();
451 resize(m_label->width(), m_label->height());
454 #include "kpixmapregionselectorwidget.moc"
TDEPIM classes for drag and drop of mails.