1 // Code from KSnapshot!
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>
33 #include <QMouseEvent>
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
)
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() );
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
)
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()
95 void RegionGrabber::initGrabber()
97 pixmap
= QPixmap::grabWindow( qt_xrootwin() );
98 setPaletteBackgroundPixmap( pixmap
);
100 QDesktopWidget desktopWidget
;
102 if ( desktopWidget
.isVirtualDesktop() )
103 desktopSize
= desktopWidget
.geometry();
105 desktopSize
= desktopWidget
.screenGeometry( qt_xrootwin() );
107 setGeometry( desktopSize
);
110 QApplication::setOverrideCursor( crossCursor
);
113 void RegionGrabber::mousePressEvent( QMouseEvent
*e
)
115 if ( e
->button() == LeftButton
)
118 grabRect
= QRect( e
->pos(), e
->pos() );
123 void RegionGrabber::mouseMoveEvent( QMouseEvent
*e
)
128 tipTimer
->start( 250, true );
131 grabRect
.setBottomRight( e
->pos() );
136 void RegionGrabber::mouseReleaseEvent( QMouseEvent
*e
)
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() );
164 void RegionGrabber::updateSizeTip()
166 QRect rect
= grabRect
.normalize();
168 sizeTip
->setTip( rect
);
172 void RegionGrabber::drawRubber()
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() ) );
186 #include "regiongrabber.moc"