1 | /* |
2 | * Copyright (C) 2007 Montel Laurent <montel@kde.org> |
3 | * Copyright (C) 2010 Pau Garcia i Quiles <pgquiles@elpauer.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU Lesser General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "kbackgroundsnapshot.h" |
22 | #include "kbackgroundsnapshot.moc" |
23 | #include "regiongrabber.h" |
24 | #include "ksnapshot_options.h" |
25 | #include <windowgrabber.h> |
26 | #include <kdebug.h> |
27 | #include <klocale.h> |
28 | #include <kcmdlineargs.h> |
29 | #include <KAboutData> |
30 | #include <KApplication> |
31 | #include <KGlobalSettings> |
32 | #include <kio/netaccess.h> |
33 | |
34 | #include <QMouseEvent> |
35 | #include <QPixmap> |
36 | #include <QDesktopWidget> |
37 | |
38 | KBackgroundSnapshot::KBackgroundSnapshot(KSnapshotObject::CaptureMode mode) |
39 | :KSnapshotObject() |
40 | { |
41 | modeCapture=mode; |
42 | grabber = new QWidget( 0, Qt::X11BypassWindowManagerHint ); |
43 | grabber->move( -1000, -1000 ); |
44 | grabber->installEventFilter( this ); |
45 | grabber->show(); |
46 | grabber->grabMouse( Qt::WaitCursor ); |
47 | |
48 | if ( mode == KSnapshotObject::FullScreen ) |
49 | { |
50 | snapshot = QPixmap::grabWindow( QApplication::desktop()->winId() ); |
51 | savePictureOnDesktop(); |
52 | } |
53 | else { |
54 | switch(mode) |
55 | { |
56 | case KSnapshotObject::WindowUnderCursor: |
57 | { |
58 | performGrab(); |
59 | break; |
60 | } |
61 | case KSnapshotObject::ChildWindow: |
62 | { |
63 | slotGrab(); |
64 | break; |
65 | } |
66 | case KSnapshotObject::Region: |
67 | { |
68 | grabRegion(); |
69 | break; |
70 | } |
71 | default: |
72 | break; |
73 | } |
74 | } |
75 | |
76 | //When we use argument to take snapshot we mustn't hide it. |
77 | if(mode != KSnapshotObject::ChildWindow) |
78 | { |
79 | grabber->releaseMouse(); |
80 | grabber->hide(); |
81 | } |
82 | |
83 | } |
84 | |
85 | KBackgroundSnapshot::~KBackgroundSnapshot() |
86 | { |
87 | //kDebug()<<" KBackgroundSnapshot::~KBackgroundSnapshot()\n"; |
88 | } |
89 | |
90 | void KBackgroundSnapshot::savePictureOnDesktop() |
91 | { |
92 | filename = KUrl( KGlobalSettings::desktopPath()+'/'+i18n("snapshot" )+"1.png" ); |
93 | // Make sure the name is not already being used |
94 | while(KIO::NetAccess::exists( filename, KIO::NetAccess::DestinationSide, 0L )) { |
95 | autoincFilename(); |
96 | } |
97 | save( filename, 0L); |
98 | exit( 0 ); |
99 | } |
100 | |
101 | void KBackgroundSnapshot::performGrab() |
102 | { |
103 | //kDebug()<<"KBackgroundSnapshot::performGrab()\n"; |
104 | grabber->releaseMouse(); |
105 | grabber->hide(); |
106 | if ( modeCapture == ChildWindow ) { |
107 | WindowGrabber wndGrab; |
108 | connect( &wndGrab, SIGNAL(windowGrabbed(QPixmap)), |
109 | SLOT(slotWindowGrabbed(QPixmap)) ); |
110 | wndGrab.exec(); |
111 | savePictureOnDesktop(); |
112 | } |
113 | else if ( modeCapture == WindowUnderCursor ) { |
114 | snapshot = WindowGrabber::grabCurrent( true ); |
115 | savePictureOnDesktop(); |
116 | } |
117 | else { |
118 | snapshot = QPixmap::grabWindow( QApplication::desktop()->winId() ); |
119 | savePictureOnDesktop(); |
120 | } |
121 | } |
122 | |
123 | void KBackgroundSnapshot::slotWindowGrabbed( const QPixmap &pix ) |
124 | { |
125 | //kDebug()<<" KBackgroundSnapshot::slotWindowGrabbed( const QPixmap &pix )\n"; |
126 | if ( !pix.isNull() ) |
127 | snapshot = pix; |
128 | QApplication::restoreOverrideCursor(); |
129 | } |
130 | |
131 | |
132 | void KBackgroundSnapshot::slotGrab() |
133 | { |
134 | //kDebug()<<"KBackgroundSnapshot::slotGrab()\n"; |
135 | grabber->show(); |
136 | grabber->grabMouse( Qt::CrossCursor ); |
137 | } |
138 | |
139 | |
140 | void KBackgroundSnapshot::grabRegion() |
141 | { |
142 | QRect emptySelection; |
143 | rgnGrab = new RegionGrabber(emptySelection); |
144 | connect( rgnGrab, SIGNAL(regionGrabbed(QPixmap)), |
145 | SLOT(slotRegionGrabbed(QPixmap)) ); |
146 | |
147 | } |
148 | |
149 | |
150 | void KBackgroundSnapshot::slotRegionGrabbed( const QPixmap &pix ) |
151 | { |
152 | if ( !pix.isNull() ) |
153 | snapshot = pix; |
154 | rgnGrab->deleteLater(); |
155 | QApplication::restoreOverrideCursor(); |
156 | savePictureOnDesktop(); |
157 | } |
158 | |
159 | bool KBackgroundSnapshot::eventFilter( QObject* o, QEvent* e) |
160 | { |
161 | if ( o == grabber && e->type() == QEvent::MouseButtonPress ) { |
162 | QMouseEvent* me = (QMouseEvent*) e; |
163 | if ( QWidget::mouseGrabber() != grabber ) |
164 | return false; |
165 | if ( me->button() == Qt::LeftButton ) |
166 | performGrab(); |
167 | } |
168 | return false; |
169 | } |
170 | |
171 | |
172 | #define KBACKGROUNDSNAPVERSION "0.1" |
173 | |
174 | static const char description[] = I18N_NOOP("KDE Background Screenshot Utility" ); |
175 | |
176 | int main(int argc, char **argv) |
177 | { |
178 | KAboutData aboutData( "kbackgroundsnapshot" , "ksnapshot" , ki18n("KBackgroundSnapshot" ), |
179 | KBACKGROUNDSNAPVERSION, ki18n(description), KAboutData::License_GPL, |
180 | ki18n("(c) 2007, Montel Laurent" )); |
181 | |
182 | KCmdLineArgs::init( argc, argv, &aboutData ); |
183 | KCmdLineArgs::addCmdLineOptions( ksnapshot_options() ); // Add our own options. |
184 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
185 | |
186 | KApplication app; |
187 | |
188 | if ( args->isSet( "current" ) ) |
189 | new KBackgroundSnapshot( KSnapshotObject::WindowUnderCursor ); |
190 | else if(args->isSet( "fullscreen" )) |
191 | new KBackgroundSnapshot( KSnapshotObject::FullScreen ); |
192 | else if(args->isSet( "region" )) |
193 | new KBackgroundSnapshot( KSnapshotObject::Region ); |
194 | else if(args->isSet( "freeregion" )) |
195 | new KBackgroundSnapshot( KSnapshotObject::FreeRegion ); |
196 | else if(args->isSet( "child" )) |
197 | new KBackgroundSnapshot( KSnapshotObject::ChildWindow ); |
198 | else |
199 | new KBackgroundSnapshot(); |
200 | args->clear(); |
201 | return app.exec(); |
202 | } |
203 | |