Ran qt3to4
[basket4.git] / src / regiongrabber.cpp
blobb9ae5e91c2e059b826b0d7f0b0af9052cdebe67a
1 // Code from KSnapshot!
3 /*
4 Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or ( at your option ) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include "regiongrabber.h"
24 #include <qapplication.h>
25 #include <qpainter.h>
26 #include <qpalette.h>
27 #include <qstyle.h>
28 #include <qtimer.h>
29 #include <qtooltip.h>
30 //Added by qt3to4:
31 #include <QPixmap>
32 #include <QLabel>
33 #include <QMouseEvent>
34 #include <Q3Frame>
35 #include <QKeyEvent>
37 #include <kglobalsettings.h>
39 SizeTip::SizeTip( QWidget *parent, const char *name )
40 : QLabel( parent, name, WStyle_Customize | WX11BypassWM |
41 WStyle_StaysOnTop | WStyle_NoBorder | WStyle_Tool )
43 setMargin( 2 );
44 setIndent( 0 );
45 setFrameStyle( Q3Frame::Plain | Q3Frame::Box );
47 setPalette( QToolTip::palette() );
50 void SizeTip::setTip( const QRect &rect )
52 QString tip = QString( "%1x%2" ).arg( rect.width() )
53 .arg( rect.height() );
55 setText( tip );
56 adjustSize();
58 positionTip( rect );
61 void SizeTip::positionTip( const QRect &rect )
63 QRect tipRect = geometry();
64 tipRect.moveTopLeft( QPoint( 0, 0 ) );
66 if ( rect.intersects( tipRect ) )
68 QRect deskR = KGlobalSettings::desktopGeometry( QPoint( 0, 0 ) );
70 tipRect.moveCenter( QPoint( deskR.width()/2, deskR.height()/2 ) );
71 if ( !rect.contains( tipRect, true ) && rect.intersects( tipRect ) )
72 tipRect.moveBottomRight( geometry().bottomRight() );
75 move( tipRect.topLeft() );
78 RegionGrabber::RegionGrabber(int delay)
79 : QWidget( 0, 0 ),
80 mouseDown( false ), sizeTip( 0L )
82 sizeTip = new SizeTip( ( QWidget * )0L );
84 tipTimer = new QTimer( this );
85 connect( tipTimer, SIGNAL( timeout() ), SLOT( updateSizeTip() ) );
87 QTimer::singleShot( delay, this, SLOT( initGrabber() ) );
90 RegionGrabber::~RegionGrabber()
92 delete sizeTip;
95 void RegionGrabber::initGrabber()
97 pixmap = QPixmap::grabWindow( qt_xrootwin() );
98 setPaletteBackgroundPixmap( pixmap );
100 QDesktopWidget desktopWidget;
101 QRect desktopSize;
102 if ( desktopWidget.isVirtualDesktop() )
103 desktopSize = desktopWidget.geometry();
104 else
105 desktopSize = desktopWidget.screenGeometry( qt_xrootwin() );
107 setGeometry( desktopSize );
108 showFullScreen();
110 QApplication::setOverrideCursor( crossCursor );
113 void RegionGrabber::mousePressEvent( QMouseEvent *e )
115 if ( e->button() == LeftButton )
117 mouseDown = true;
118 grabRect = QRect( e->pos(), e->pos() );
119 drawRubber();
123 void RegionGrabber::mouseMoveEvent( QMouseEvent *e )
125 if ( mouseDown )
127 sizeTip->hide();
128 tipTimer->start( 250, true );
130 drawRubber();
131 grabRect.setBottomRight( e->pos() );
132 drawRubber();
136 void RegionGrabber::mouseReleaseEvent( QMouseEvent *e )
138 mouseDown = false;
139 drawRubber();
140 sizeTip->hide();
142 grabRect.setBottomRight( e->pos() );
143 grabRect = grabRect.normalize();
145 QPixmap region = QPixmap::grabWindow( winId(), grabRect.x(), grabRect.y(),
146 grabRect.width(), grabRect.height() );
148 QApplication::restoreOverrideCursor();
150 emit regionGrabbed( region );
153 void RegionGrabber::keyPressEvent( QKeyEvent *e )
155 if ( e->key() == Key_Escape )
157 QApplication::restoreOverrideCursor();
158 emit regionGrabbed( QPixmap() );
160 else
161 e->ignore();
164 void RegionGrabber::updateSizeTip()
166 QRect rect = grabRect.normalize();
168 sizeTip->setTip( rect );
169 sizeTip->show();
172 void RegionGrabber::drawRubber()
174 QPainter p;
175 p.begin( this );
176 p.setRasterOp( NotROP );
177 p.setPen( QPen( color0, 1 ) );
178 p.setBrush( NoBrush );
180 style().drawPrimitive( QStyle::PE_FocusRect, &p, grabRect, colorGroup(),
181 QStyle::Style_Default, QStyleOption( colorGroup().base() ) );
183 p.end();
186 #include "regiongrabber.moc"