2 * Copyright (C) 2007 Luca Gugelmann <lucag@student.ethz.ch>
3 * Adapted for Kdenlive by Jean-Baptiste Mardelle (2008) jb@kdenlive.org
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License version 2 as
7 * published by the Free Software Foundation
9 * This program 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
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "regiongrabber.h"
23 #include <QMouseEvent>
24 #include <QApplication>
25 #include <QDesktopWidget>
29 #include <KWindowSystem>
31 RegionGrabber::RegionGrabber( ) :
32 QWidget( 0 ), selection(), mouseDown( false ), newSelection( false ),
33 handleSize( 10 ), mouseOverHandle( 0 ), idleTimer(),
34 showHelp( true ), grabbing( false ),
35 TLHandle(0,0,handleSize,handleSize), TRHandle(0,0,handleSize,handleSize),
36 BLHandle(0,0,handleSize,handleSize), BRHandle(0,0,handleSize,handleSize),
37 LHandle(0,0,handleSize,handleSize), THandle(0,0,handleSize,handleSize),
38 RHandle(0,0,handleSize,handleSize), BHandle(0,0,handleSize,handleSize)
40 handles << &TLHandle << &TRHandle << &BLHandle << &BRHandle
41 << &LHandle << &THandle << &RHandle << &BHandle;
42 setMouseTracking( true );
43 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
44 int timeout = KWindowSystem::compositingActive() ? 200 : 50;
45 QTimer::singleShot( timeout, this, SLOT(init()) );
46 connect( &idleTimer, SIGNAL( timeout() ), this, SLOT( displayHelp() ) );
47 idleTimer.start( 3000 );
50 RegionGrabber::~RegionGrabber()
54 void RegionGrabber::init()
56 pixmap = QPixmap::grabWindow( QApplication::desktop()->winId() );
58 resize( pixmap.size() );
60 setCursor( Qt::CrossCursor );
63 void RegionGrabber::displayHelp()
69 void RegionGrabber::paintEvent( QPaintEvent* e )
72 if ( grabbing ) // grabWindow() should just get the background
75 QPainter painter( this );
77 QPalette pal(QToolTip::palette());
78 QFont font = QToolTip::font();
80 QColor handleColor = pal.color( QPalette::Active, QPalette::Highlight );
81 handleColor.setAlpha( 160 );
82 QColor overlayColor( 0, 0, 0, 160 );
83 QColor textColor = pal.color( QPalette::Active, QPalette::Text );
84 QColor textBackgroundColor = pal.color( QPalette::Active, QPalette::Base );
85 painter.drawPixmap(0, 0, pixmap);
86 painter.setFont(font);
88 QRect r = selection.normalized().adjusted( 0, 0, -1, -1 );
89 if ( !selection.isNull() )
91 QRegion grey( rect() );
92 grey = grey.subtracted( r );
93 painter.setPen( handleColor );
94 painter.setBrush( overlayColor );
95 painter.setClipRegion( grey );
96 painter.drawRect( -1, -1, rect().width() + 1, rect().height() + 1 );
97 painter.setClipRect( rect() );
98 painter.setBrush( Qt::NoBrush );
99 painter.drawRect( r );
104 painter.setPen( textColor );
105 painter.setBrush( textBackgroundColor );
106 QString helpText = i18n( "Select a region using the mouse. To take the snapshot, press the Enter key. Press Esc to quit." );
107 QRect textRect = painter.boundingRect( rect().adjusted( 2, 2, -2, -2 ), Qt::TextWordWrap, helpText );
108 textRect.adjust( -2, -2, 4, 2 );
109 painter.drawRect( textRect );
110 textRect.moveTopLeft( QPoint( 3, 3 ) );
111 painter.drawText( textRect, helpText );
114 if ( selection.isNull() )
119 // The grabbed region is everything which is covered by the drawn
120 // rectangles (border included). This means that there is no 0px
121 // selection, since a 0px wide rectangle will always be drawn as a line.
122 QString txt = QString( "%1x%2" ).arg( selection.width() == 0 ? 2 : selection.width() )
123 .arg( selection.height() == 0 ? 2 : selection.height() );
124 QRect textRect = painter.boundingRect( rect(), Qt::AlignLeft, txt );
125 QRect boundingRect = textRect.adjusted( -4, 0, 0, 0);
127 if ( textRect.width() < r.width() - 2*handleSize &&
128 textRect.height() < r.height() - 2*handleSize &&
129 ( r.width() > 100 && r.height() > 100 ) ) // center, unsuitable for small selections
131 boundingRect.moveCenter( r.center() );
132 textRect.moveCenter( r.center() );
134 else if ( r.y() - 3 > textRect.height() &&
135 r.x() + textRect.width() < rect().right() ) // on top, left aligned
137 boundingRect.moveBottomLeft( QPoint( r.x(), r.y() - 3 ) );
138 textRect.moveBottomLeft( QPoint( r.x() + 2, r.y() - 3 ) );
140 else if ( r.x() - 3 > textRect.width() ) // left, top aligned
142 boundingRect.moveTopRight( QPoint( r.x() - 3, r.y() ) );
143 textRect.moveTopRight( QPoint( r.x() - 5, r.y() ) );
145 else if ( r.bottom() + 3 + textRect.height() < rect().bottom() &&
146 r.right() > textRect.width() ) // at bottom, right aligned
148 boundingRect.moveTopRight( QPoint( r.right(), r.bottom() + 3 ) );
149 textRect.moveTopRight( QPoint( r.right() - 2, r.bottom() + 3 ) );
151 else if ( r.right() + textRect.width() + 3 < rect().width() ) // right, bottom aligned
153 boundingRect.moveBottomLeft( QPoint( r.right() + 3, r.bottom() ) );
154 textRect.moveBottomLeft( QPoint( r.right() + 5, r.bottom() ) );
156 // if the above didn't catch it, you are running on a very tiny screen...
157 painter.setPen( textColor );
158 painter.setBrush( textBackgroundColor );
159 painter.drawRect( boundingRect );
160 painter.drawText( textRect, txt );
162 if ( ( r.height() > handleSize*2 && r.width() > handleSize*2 )
166 painter.setPen( handleColor );
167 handleColor.setAlpha( 60 );
168 painter.setBrush( handleColor );
169 painter.drawRects( handleMask().rects() );
173 void RegionGrabber::resizeEvent( QResizeEvent* e )
176 if ( selection.isNull() )
179 r.setTopLeft( limitPointToRect( r.topLeft(), rect() ) );
180 r.setBottomRight( limitPointToRect( r.bottomRight(), rect() ) );
181 if ( r.width() <= 1 || r.height() <= 1 ) //this just results in ugly drawing...
186 void RegionGrabber::mousePressEvent( QMouseEvent* e )
190 if ( e->button() == Qt::LeftButton )
193 dragStartPoint = e->pos();
194 selectionBeforeDrag = selection;
195 if ( !selection.contains( e->pos() ) )
203 setCursor( Qt::ClosedHandCursor );
206 else if ( e->button() == Qt::RightButton )
208 newSelection = false;
210 setCursor( Qt::CrossCursor );
215 void RegionGrabber::mouseMoveEvent( QMouseEvent* e )
223 selection = QRect( dragStartPoint, limitPointToRect( p, r ) ).normalized();
225 else if ( mouseOverHandle == 0 ) // moving the whole selection
227 QRect r = rect().normalized(), s = selectionBeforeDrag.normalized();
228 QPoint p = s.topLeft() + e->pos() - dragStartPoint;
229 r.setBottomRight( r.bottomRight() - QPoint( s.width(), s.height() ) );
230 if ( !r.isNull() && r.isValid() )
231 selection.moveTo( limitPointToRect( p, r ) );
233 else // dragging a handle
235 QRect r = selectionBeforeDrag;
236 QPoint offset = e->pos() - dragStartPoint;
238 if ( mouseOverHandle == &TLHandle || mouseOverHandle == &THandle
239 || mouseOverHandle == &TRHandle ) // dragging one of the top handles
241 r.setTop( r.top() + offset.y() );
244 if ( mouseOverHandle == &TLHandle || mouseOverHandle == &LHandle
245 || mouseOverHandle == &BLHandle ) // dragging one of the left handles
247 r.setLeft( r.left() + offset.x() );
250 if ( mouseOverHandle == &BLHandle || mouseOverHandle == &BHandle
251 || mouseOverHandle == &BRHandle ) // dragging one of the bottom handles
253 r.setBottom( r.bottom() + offset.y() );
256 if ( mouseOverHandle == &TRHandle || mouseOverHandle == &RHandle
257 || mouseOverHandle == &BRHandle ) // dragging one of the right handles
259 r.setRight( r.right() + offset.x() );
262 r.setTopLeft( limitPointToRect( r.topLeft(), rect() ) );
263 r.setBottomRight( limitPointToRect( r.bottomRight(), rect() ) );
270 if ( selection.isNull() )
273 foreach( QRect* r, handles )
275 if ( r->contains( e->pos() ) )
285 if ( selection.contains( e->pos() ) )
286 setCursor( Qt::OpenHandCursor );
288 setCursor( Qt::CrossCursor );
292 if ( mouseOverHandle == &TLHandle || mouseOverHandle == &BRHandle )
293 setCursor( Qt::SizeFDiagCursor );
294 if ( mouseOverHandle == &TRHandle || mouseOverHandle == &BLHandle )
295 setCursor( Qt::SizeBDiagCursor );
296 if ( mouseOverHandle == &LHandle || mouseOverHandle == &RHandle )
297 setCursor( Qt::SizeHorCursor );
298 if ( mouseOverHandle == &THandle || mouseOverHandle == &BHandle )
299 setCursor( Qt::SizeVerCursor );
304 void RegionGrabber::mouseReleaseEvent( QMouseEvent* e )
307 newSelection = false;
309 if ( mouseOverHandle == 0 && selection.contains( e->pos() ) )
310 setCursor( Qt::OpenHandCursor );
314 void RegionGrabber::mouseDoubleClickEvent( QMouseEvent* )
319 void RegionGrabber::keyPressEvent( QKeyEvent* e )
321 if ( e->key() == Qt::Key_Escape )
323 emit regionGrabbed( QRect() );
326 else if ( e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return )
336 void RegionGrabber::grabRect()
338 QRect r = selection.normalized();
339 if ( !r.isNull() && r.isValid() )
342 emit regionGrabbed( r );
347 void RegionGrabber::updateHandles()
349 QRect r = selection.normalized().adjusted( 0, 0, -1, -1 );
350 int s2 = handleSize / 2;
352 TLHandle.moveTopLeft( r.topLeft() );
353 TRHandle.moveTopRight( r.topRight() );
354 BLHandle.moveBottomLeft( r.bottomLeft() );
355 BRHandle.moveBottomRight( r.bottomRight() );
357 LHandle.moveTopLeft( QPoint( r.x(), r.y() + r.height() / 2 - s2) );
358 THandle.moveTopLeft( QPoint( r.x() + r.width() / 2 - s2, r.y() ) );
359 RHandle.moveTopRight( QPoint( r.right(), r.y() + r.height() / 2 - s2 ) );
360 BHandle.moveBottomLeft( QPoint( r.x() + r.width() / 2 - s2, r.bottom() ) );
363 QRegion RegionGrabber::handleMask() const
365 // note: not normalized QRects are bad here, since they will not be drawn
367 foreach( QRect* rect, handles ) mask += QRegion( *rect );
371 QPoint RegionGrabber::limitPointToRect( const QPoint &p, const QRect &r ) const
374 q.setX( p.x() < r.x() ? r.x() : p.x() < r.right() ? p.x() : r.right() );
375 q.setY( p.y() < r.y() ? r.y() : p.y() < r.bottom() ? p.y() : r.bottom() );
379 #include "regiongrabber.moc"