2 * Copyright (C) 2007 Montel Laurent <montel@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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 Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "kbackgroundsnapshot.h"
21 #include "kbackgroundsnapshot.moc"
22 #include "regiongrabber.h"
23 #include "ksnapshot_options.h"
24 #include <windowgrabber.h>
27 #include <kcmdlineargs.h>
29 #include <KApplication>
30 #include <KGlobalSettings>
31 #include <kio/netaccess.h>
33 #include <QMouseEvent>
35 #include <QDesktopWidget>
37 KBackgroundSnapshot::KBackgroundSnapshot(KSnapshotObject::CaptureMode mode
)
41 grabber
= new QWidget( 0, Qt::X11BypassWindowManagerHint
);
42 grabber
->move( -1000, -1000 );
43 grabber
->installEventFilter( this );
45 grabber
->grabMouse( Qt::WaitCursor
);
47 if ( mode
== KSnapshotObject::FullScreen
)
49 snapshot
= QPixmap::grabWindow( QApplication::desktop()->winId() );
50 savePictureOnDesktop();
55 case KSnapshotObject::WindowUnderCursor
:
60 case KSnapshotObject::ChildWindow
:
65 case KSnapshotObject::Region
:
75 //When we use argument to take snapshot we mustn't hide it.
76 if(mode
!= KSnapshotObject::ChildWindow
)
78 grabber
->releaseMouse();
84 KBackgroundSnapshot::~KBackgroundSnapshot()
86 //kDebug()<<" KBackgroundSnapshot::~KBackgroundSnapshot()\n";
89 void KBackgroundSnapshot::savePictureOnDesktop()
91 filename
= KUrl( KGlobalSettings::desktopPath()+'/'+i18n("snapshot")+"1.png" );
92 // Make sure the name is not already being used
93 while(KIO::NetAccess::exists( filename
, KIO::NetAccess::DestinationSide
, 0L )) {
100 void KBackgroundSnapshot::performGrab()
102 //kDebug()<<"KBackgroundSnapshot::performGrab()\n";
103 grabber
->releaseMouse();
105 if ( modeCapture
== ChildWindow
) {
106 WindowGrabber wndGrab
;
107 connect( &wndGrab
, SIGNAL( windowGrabbed( const QPixmap
& ) ),
108 SLOT( slotWindowGrabbed( const QPixmap
& ) ) );
110 savePictureOnDesktop();
112 else if ( modeCapture
== WindowUnderCursor
) {
113 snapshot
= WindowGrabber::grabCurrent( true );
114 savePictureOnDesktop();
117 snapshot
= QPixmap::grabWindow( QX11Info::appRootWindow() );
118 savePictureOnDesktop();
122 void KBackgroundSnapshot::slotWindowGrabbed( const QPixmap
&pix
)
124 //kDebug()<<" KBackgroundSnapshot::slotWindowGrabbed( const QPixmap &pix )\n";
127 QApplication::restoreOverrideCursor();
131 void KBackgroundSnapshot::slotGrab()
133 //kDebug()<<"KBackgroundSnapshot::slotGrab()\n";
135 grabber
->grabMouse( Qt::CrossCursor
);
139 void KBackgroundSnapshot::grabRegion()
141 rgnGrab
= new RegionGrabber();
142 connect( rgnGrab
, SIGNAL( regionGrabbed( const QPixmap
& ) ),
143 SLOT( slotRegionGrabbed( const QPixmap
& ) ) );
148 void KBackgroundSnapshot::slotRegionGrabbed( const QPixmap
&pix
)
152 rgnGrab
->deleteLater();
153 QApplication::restoreOverrideCursor();
154 savePictureOnDesktop();
157 bool KBackgroundSnapshot::eventFilter( QObject
* o
, QEvent
* e
)
159 if ( o
== grabber
&& e
->type() == QEvent::MouseButtonPress
) {
160 QMouseEvent
* me
= (QMouseEvent
*) e
;
161 if ( QWidget::mouseGrabber() != grabber
)
163 if ( me
->button() == Qt::LeftButton
)
170 #define KBACKGROUNDSNAPVERSION "0.1"
172 static const char description
[] = I18N_NOOP("KDE Background Screenshot Utility");
174 int main(int argc
, char **argv
)
176 KAboutData
aboutData( "kbackgroundsnapshot", "ksnapshot", ki18n("KBackgroundSnapshot"),
177 KBACKGROUNDSNAPVERSION
, ki18n(description
), KAboutData::License_GPL
,
178 ki18n("(c) 2007, Montel Laurent"));
180 KCmdLineArgs::init( argc
, argv
, &aboutData
);
181 KCmdLineArgs::addCmdLineOptions( ksnapshot_options() ); // Add our own options.
182 KCmdLineArgs
*args
= KCmdLineArgs::parsedArgs();
186 KBackgroundSnapshot
*toplevel
;
188 if ( args
->isSet( "current" ) )
189 toplevel
= new KBackgroundSnapshot( KSnapshotObject::WindowUnderCursor
);
190 else if(args
->isSet( "fullscreen" ))
191 toplevel
= new KBackgroundSnapshot( KSnapshotObject::FullScreen
);
192 else if(args
->isSet( "region" ))
193 toplevel
= new KBackgroundSnapshot( KSnapshotObject::Region
);
194 else if(args
->isSet( "child" ))
195 toplevel
= new KBackgroundSnapshot( KSnapshotObject::ChildWindow
);
197 toplevel
= new KBackgroundSnapshot();