SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / ksnapshot / kbackgroundsnapshot.cpp
blob7a4e73007151d845ef934165db6e4d66c60fcfaa
1 /*
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>
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <kcmdlineargs.h>
28 #include <KAboutData>
29 #include <KApplication>
30 #include <KGlobalSettings>
31 #include <kio/netaccess.h>
33 #include <QMouseEvent>
34 #include <QPixmap>
35 #include <QDesktopWidget>
37 KBackgroundSnapshot::KBackgroundSnapshot(KSnapshotObject::CaptureMode mode)
38 :KSnapshotObject()
40 modeCapture=mode;
41 grabber = new QWidget( 0, Qt::X11BypassWindowManagerHint );
42 grabber->move( -1000, -1000 );
43 grabber->installEventFilter( this );
44 grabber->show();
45 grabber->grabMouse( Qt::WaitCursor );
47 if ( mode == KSnapshotObject::FullScreen )
49 snapshot = QPixmap::grabWindow( QApplication::desktop()->winId() );
50 savePictureOnDesktop();
52 else {
53 switch(mode)
55 case KSnapshotObject::WindowUnderCursor:
57 performGrab();
58 break;
60 case KSnapshotObject::ChildWindow:
62 slotGrab();
63 break;
65 case KSnapshotObject::Region:
67 grabRegion();
68 break;
70 default:
71 break;
75 //When we use argument to take snapshot we mustn't hide it.
76 if(mode != KSnapshotObject::ChildWindow)
78 grabber->releaseMouse();
79 grabber->hide();
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 )) {
94 autoincFilename();
96 save( filename, 0L);
97 exit( 0 );
100 void KBackgroundSnapshot::performGrab()
102 //kDebug()<<"KBackgroundSnapshot::performGrab()\n";
103 grabber->releaseMouse();
104 grabber->hide();
105 if ( modeCapture == ChildWindow ) {
106 WindowGrabber wndGrab;
107 connect( &wndGrab, SIGNAL( windowGrabbed( const QPixmap & ) ),
108 SLOT( slotWindowGrabbed( const QPixmap & ) ) );
109 wndGrab.exec();
110 savePictureOnDesktop();
112 else if ( modeCapture == WindowUnderCursor ) {
113 snapshot = WindowGrabber::grabCurrent( true );
114 savePictureOnDesktop();
116 else {
117 snapshot = QPixmap::grabWindow( QX11Info::appRootWindow() );
118 savePictureOnDesktop();
122 void KBackgroundSnapshot::slotWindowGrabbed( const QPixmap &pix )
124 //kDebug()<<" KBackgroundSnapshot::slotWindowGrabbed( const QPixmap &pix )\n";
125 if ( !pix.isNull() )
126 snapshot = pix;
127 QApplication::restoreOverrideCursor();
131 void KBackgroundSnapshot::slotGrab()
133 //kDebug()<<"KBackgroundSnapshot::slotGrab()\n";
134 grabber->show();
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 )
150 if ( !pix.isNull() )
151 snapshot = 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 )
162 return false;
163 if ( me->button() == Qt::LeftButton )
164 performGrab();
166 return false;
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();
184 KApplication app;
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 );
196 else
197 toplevel = new KBackgroundSnapshot();
198 args->clear();
199 return app.exec();