26 #include "kpixmapregionselectorwidget.h"
27 #include <tqpainter.h>
31 #include <kimageeffect.h>
34 #include <tdepopupmenu.h>
35 #include <tdeaction.h>
38 #include <tqapplication.h>
41 const char *name) : TQWidget( parent, name)
43 TQHBoxLayout * hboxLayout=
new TQHBoxLayout(
this );
45 hboxLayout->addStretch();
46 TQVBoxLayout * vboxLayout=
new TQVBoxLayout( hboxLayout );
48 vboxLayout->addStretch();
49 m_label =
new TQLabel(
this,
"pixmapHolder");
50 m_label->setBackgroundMode( TQt::NoBackground );
51 m_label->installEventFilter(
this );
53 vboxLayout->addWidget(m_label);
54 vboxLayout->addStretch();
56 hboxLayout->addStretch();
58 m_forcedAspectRatio=0;
69 Q_ASSERT(!
pixmap.isNull());
72 m_label->setPixmap(
pixmap );
78 m_selectedRegion = m_originalPixmap.rect();
84 return m_selectedRegion;
92 m_selectedRegion=rect;
99 void KPixmapRegionSelectorWidget::updatePixmap()
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() );
106 if (m_linedPixmap.isNull())
108 m_linedPixmap = m_originalPixmap;
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) );
116 TQImage image=m_linedPixmap.convertToImage();
118 m_linedPixmap.convertFromImage(image);
121 TQPixmap
pixmap = m_linedPixmap;
124 painter.drawPixmap( m_selectedRegion.topLeft(),
125 m_originalPixmap, m_selectedRegion );
127 painter.setPen( TQColor(255,255,255) );
128 painter.setRasterOp( TQt::XorROP );
130 painter.drawRect( m_selectedRegion );
134 m_label->setPixmap(
pixmap);
145 popup,
"rotateclockwise");
148 action =
new TDEAction(i18n(
"Rotate &Counterclockwise"),
"object-rotate-left",
150 popup,
"rotatecounterclockwise");
162 int w=m_originalPixmap.width();
163 int h=m_originalPixmap.height();
164 TQImage img=m_unzoomedPixmap.convertToImage();
166 m_unzoomedPixmap.convertFromImage(img);
168 img=m_originalPixmap.convertToImage();
170 m_originalPixmap.convertFromImage(img);
172 m_linedPixmap=TQPixmap();
174 if (m_forcedAspectRatio>0 && m_forcedAspectRatio!=1)
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() );
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() );
209 bool KPixmapRegionSelectorWidget::eventFilter(TQObject *obj, TQEvent *ev)
211 if ( ev->type() == TQEvent::MouseButtonPress )
213 TQMouseEvent *mev= (TQMouseEvent *)(ev);
216 if ( mev->button() == TQt::RightButton )
219 popup->exec( mev->globalPos() );
226 if ( m_selectedRegion.contains( mev->pos() )
227 && m_selectedRegion!=m_originalPixmap.rect() )
230 cursor.setShape( TQt::SizeAllCursor );
235 cursor.setShape( TQt::CrossCursor );
237 TQApplication::setOverrideCursor(cursor);
239 m_tempFirstClick=mev->pos();
245 if ( ev->type() == TQEvent::MouseMove )
247 TQMouseEvent *mev= (TQMouseEvent *)(ev);
251 if ( m_state == Resizing )
254 calcSelectionRectangle( m_tempFirstClick, mev->pos() ) );
256 else if (m_state == Moving )
260 bool mouseOutside=
false;
263 m_selectedRegion.moveBy(-m_selectedRegion.x(),0);
266 else if ( mevx > m_originalPixmap.width() )
268 m_selectedRegion.moveBy(m_originalPixmap.width()-m_selectedRegion.width()-m_selectedRegion.x(),0);
273 m_selectedRegion.moveBy(0,-m_selectedRegion.y());
276 else if ( mevy > m_originalPixmap.height() )
278 m_selectedRegion.moveBy(0,m_originalPixmap.height()-m_selectedRegion.height()-m_selectedRegion.y());
281 if (mouseOutside) { updatePixmap();
return TRUE; };
283 m_selectedRegion.moveBy( mev->x()-m_tempFirstClick.x(),
284 mev->y()-m_tempFirstClick.y() );
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);
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()));
297 m_tempFirstClick=mev->pos();
303 if ( ev->type() == TQEvent::MouseButtonRelease )
305 TQMouseEvent *mev= (TQMouseEvent *)(ev);
307 if ( m_state == Resizing && mev->pos() == m_tempFirstClick)
311 TQApplication::restoreOverrideCursor();
316 TQWidget::eventFilter(obj, ev);
320 TQRect KPixmapRegionSelectorWidget::calcSelectionRectangle(
const TQPoint & startPoint,
const TQPoint & _endPoint )
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());
330 if (m_forcedAspectRatio>0)
332 double aspectRatio=w/double(h);
334 if (aspectRatio>m_forcedAspectRatio)
335 h=(int)(w/m_forcedAspectRatio);
337 w=(int)(h*m_forcedAspectRatio);
341 if ( startPoint.x() < endPoint.x() )
345 if ( startPoint.y() < endPoint.y() )
354 h=(int)(w/m_forcedAspectRatio);
356 if ( startPoint.y() > endPoint.y() )
359 else if (x+w>m_originalPixmap.width())
361 w=m_originalPixmap.width()-x;
362 h=(int)(w/m_forcedAspectRatio);
364 if ( startPoint.y() > endPoint.y() )
371 w=(int)(h*m_forcedAspectRatio);
373 if ( startPoint.x() > endPoint.x() )
376 else if (y+h>m_originalPixmap.height())
378 h=m_originalPixmap.height()-y;
379 w=(int)(h*m_forcedAspectRatio);
381 if ( startPoint.x() > endPoint.x() )
385 return TQRect(x,y,w,h);
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));
398 TQImage origImage=m_unzoomedPixmap.convertToImage();
404 m_forcedAspectRatio=width/double(height);
409 m_forcedAspectRatio=0;
417 m_originalPixmap=m_unzoomedPixmap;
418 if (m_selectedRegion == m_originalPixmap.rect()) m_selectedRegion=TQRect();
423 if ( !m_originalPixmap.isNull() &&
424 ( m_originalPixmap.width() > m_maxWidth ||
425 m_originalPixmap.height() > m_maxHeight ) )
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();
433 if (m_selectedRegion.isValid())
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) );
443 if (!m_selectedRegion.isValid()) m_selectedRegion = m_originalPixmap.rect();
445 m_linedPixmap=TQPixmap();
447 resize(m_label->width(), m_label->height());
450 #include "kpixmapregionselectorwidget.moc"
static TQImage & fade(TQImage &image, float val, const TQColor &color)
static TQImage rotate(TQImage &src, RotateDirection r)
Class to encapsulate user-driven action or event.