| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2009 Enrico Ros * |
| 3 |
* 2009 Enrico Ros <enrico.ros@email.it> * |
| 4 |
* 2009 Alberto Scarpa <skaal.sl@gmail.com> * |
| 5 |
* * |
| 6 |
* Permission is hereby granted, free of charge, to any person * |
| 7 |
* obtaining a copy of this software and associated documentation * |
| 8 |
* files (the "Software"), to deal in the Software without * |
| 9 |
* restriction, including without limitation the rights to use, * |
| 10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell * |
| 11 |
* copies of the Software, and to permit persons to whom the * |
| 12 |
* Software is furnished to do so, subject to the following * |
| 13 |
* conditions: * |
| 14 |
* * |
| 15 |
* The above copyright notice and this permission notice shall be * |
| 16 |
* included in all copies or substantial portions of the Software. * |
| 17 |
* * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include "ScreenCapture.h" |
| 21 |
#include <QCoreApplication> |
| 22 |
#include <QDirIterator> |
| 23 |
#include <QImage> |
| 24 |
#include <QTimerEvent> |
| 25 |
#ifdef Q_WS_X11 |
| 26 |
#include <QX11Info> |
| 27 |
#else |
| 28 |
#include <QApplication> |
| 29 |
#endif |
| 30 |
#include <QDesktopWidget> |
| 31 |
#include <QPixmap> |
| 32 |
|
| 33 |
ScreenCapture::ScreenCapture( QObject * parent ) |
| 34 |
: QObject( parent ) |
| 35 |
, m_enabled( false ) |
| 36 |
, m_fps( 0 ) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
void ScreenCapture::setEnabled( bool enabled ) |
| 41 |
{ |
| 42 |
m_enabled = enabled; |
| 43 |
} |
| 44 |
|
| 45 |
bool ScreenCapture::enabled() const |
| 46 |
{ |
| 47 |
return m_enabled; |
| 48 |
} |
| 49 |
|
| 50 |
void ScreenCapture::setGeometry( const QRect & geometry ) |
| 51 |
{ |
| 52 |
m_geometry = geometry; |
| 53 |
} |
| 54 |
|
| 55 |
QRect ScreenCapture::geometry() const |
| 56 |
{ |
| 57 |
return m_geometry; |
| 58 |
} |
| 59 |
|
| 60 |
void ScreenCapture::setFrequency( int fps ) |
| 61 |
{ |
| 62 |
m_fps = fps; |
| 63 |
m_timer.start( 100 * 1000 / m_fps, this ); |
| 64 |
} |
| 65 |
|
| 66 |
int ScreenCapture::frequency() const |
| 67 |
{ |
| 68 |
return m_fps; |
| 69 |
} |
| 70 |
|
| 71 |
QPixmap ScreenCapture::lastPixmap() const |
| 72 |
{ |
| 73 |
return m_pixmap; |
| 74 |
} |
| 75 |
|
| 76 |
void ScreenCapture::timerEvent( QTimerEvent * event ) |
| 77 |
{ |
| 78 |
if ( event->timerId() != m_timer.timerId() || m_geometry.isNull() ) |
| 79 |
return QObject::timerEvent( event ); |
| 80 |
|
| 81 |
if ( !m_enabled ) |
| 82 |
return; |
| 83 |
|
| 84 |
m_pixmap = QPixmap::grabWindow( |
| 85 |
#if defined(Q_WS_X11) |
| 86 |
QX11Info::appRootWindow(), |
| 87 |
#else |
| 88 |
QApplication::desktop()->winId(), |
| 89 |
#endif |
| 90 |
m_geometry.left(), m_geometry.top(), m_geometry.width(), m_geometry.height() ); |
| 91 |
|
| 92 |
emit gotPixmap( m_pixmap, QCursor::pos() - QPoint( m_geometry.topLeft() ) ); |
| 93 |
} |