/
clipper.py
240 lines (191 loc) · 8.49 KB
/
clipper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from gi.repository import Gdk, Gtk
import cairo
import subprocess
import StringIO
import datetime
from enapi import ENAPI
from config import ConfigManager
from i18n import _
class Clipper:
"""
This class contains Gtk parts of application like capturing
screen or window.
"""
def __init__(self):
pass
def capture_screen(self):
"""
Find root window, get capture with cairo and create new note with that
attachment. Attachment's mime is 'image/png' and note title is
Screenshot {date}
=== TO DO: Add some error handling code ===
"""
self.play_capture_sound()
root_win = Gdk.get_default_root_window()
width, height = root_win.get_width(), root_win.get_height()
thumb_surface = Gdk.Window.create_similar_surface(root_win,
cairo.CONTENT_COLOR,
width, height)
cairo_context = cairo.Context(thumb_surface)
Gdk.cairo_set_source_window(cairo_context, root_win, 0, 0)
cairo_context.paint()
dummy_file = StringIO.StringIO()
thumb_surface.write_to_png(dummy_file)
now = datetime.datetime.now()
ENAPI.create_note(title=_("Screenshot ") + now.strftime("%Y-%m-%d %H:%M"),
attachment_data=dummy_file.getvalue(),
attachment_mime='image/png'
)
def capture_window(self):
"""
Find active window, get capture with cairo and create new note with that
attachment. Attachment's mime is 'image/png' and note title is
Screenshot {date}
=== TO DO: Add some error handling code ===
"""
self.play_capture_sound()
screen = Gdk.Screen.get_default()
active_win = screen.get_active_window()
width, height = active_win.get_width(), active_win.get_height()
thumb_surface = Gdk.Window.create_similar_surface(active_win,
cairo.CONTENT_COLOR,
width, height)
cairo_context = cairo.Context(thumb_surface)
Gdk.cairo_set_source_window(cairo_context, active_win, 0, 0)
cairo_context.paint()
dummy_file = StringIO.StringIO()
thumb_surface.write_to_png(dummy_file)
now = datetime.datetime.now()
ENAPI.create_note(title=_("Screenshot ") + now.strftime("%Y-%m-%d %H:%M"),
attachment_data=dummy_file.getvalue(),
attachment_mime='image/png'
)
def capture_selection(self):
""" Create new SelectionWindow instance """
SelectionWindow()
def play_capture_sound(self):
""" Play sound effect when screen or window captured. """
if not ConfigManager.get_conf('play-sound'):
return
subprocess.call(['/usr/bin/canberra-gtk-play',
'--id', 'screen-capture'])
class SelectionWindow(Gtk.Window):
def __init__(self):
""" Init Selection Window """
super(SelectionWindow, self).__init__()
# basic window properties
self.set_app_paintable(True)
self.set_decorated(False)
self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 1))
self.fullscreen()
# configure masks and connect events
event_mask = (Gdk.EventMask.BUTTON_PRESS_MASK |
Gdk.EventMask.BUTTON_RELEASE_MASK |
Gdk.EventMask.EXPOSURE_MASK |
Gdk.EventMask.KEY_PRESS_MASK |
Gdk.EventMask.KEY_RELEASE_MASK |
Gdk.EventMask.ENTER_NOTIFY_MASK |
Gdk.EventMask.LEAVE_NOTIFY_MASK |
Gdk.EventMask.POINTER_MOTION_MASK |
Gdk.EventMask.POINTER_MOTION_HINT_MASK)
self.add_events(event_mask)
self.connect("draw", self.on_draw)
self.connect("button-press-event", self.on_button_press)
self.connect("button-release-event", self.on_button_release)
self.connect("motion-notify-event", self.on_motion_notify)
self.connect('key-press-event', self.on_keypress)
# init true rgba transparency
self.screen = self.get_screen()
self.visual = self.screen.get_rgba_visual()
if self.visual != None and self.screen.is_composited():
self.set_visual(self.visual)
# get screenshot
root_win = Gdk.get_default_root_window()
width, height = root_win.get_width(), root_win.get_height()
self.screenshot = Gdk.Window.create_similar_surface(root_win,
cairo.CONTENT_COLOR,
width, height)
cairo_context = cairo.Context(self.screenshot)
Gdk.cairo_set_source_window(cairo_context, root_win, 0, 0)
cairo_context.paint()
# show window and init default rectangle
self.show_all()
self.clear_rectangle()
def clear_rectangle(self):
""" Set default rectangle """
self.drawing = False
self.finished = False
self.mousex1 = 0
self.mousey1 = 0
self.mousex2 = 0
self.mousey2 = 0
self.queue_draw()
def on_motion_notify(self, widget, event):
""" Started draw and moving mouse. """
if self.drawing:
self.mousex2 = event.x
self.mousey2 = event.y
# call on_draw function to update window.
self.queue_draw()
def on_button_press(self, widget, event):
""" Start drawing """
self.drawing = True
self.mousex1 = event.x
self.mousey1 = event.y
def on_button_release(self, widget, event):
""" Finish drawing """
self.finished = True
self.drawing = False
self.mousex2 = event.x
self.mousey2 = event.y
def on_draw(self, widget, cr):
""" This function drawing rectangle and other things. """
# fill window with semi-transparent screenshot
cr.set_source_surface(self.screenshot, 0, 0)
cr.paint_with_alpha(0.6)
# if we have a rectangle to draw.
if self.drawing or self.finished:
# draw normal screenshot into rectangle area
cr.set_source_surface(self.screenshot, 0, 0)
cr.rectangle(self.mousex1,self.mousey1,self.mousex2 - self.mousex1,self.mousey2 - self.mousey1)
cr.fill()
# draw border for rectangle.
cr.set_source_rgba(1, 0, 0, 0.5)
cr.set_dash([5,5,10,5], 0)
cr.set_line_width(1.0)
cr.rectangle(self.mousex1,self.mousey1,self.mousex2 - self.mousex1,self.mousey2 - self.mousey1)
cr.stroke()
def on_keypress(self, widget, event):
""" This function looks for key events. """
# get key name
keyval = Gdk.keyval_name(event.keyval).lower()
if keyval == 'escape':
# close window
self.hide()
self.destroy()
elif keyval == 'delete':
# delete current rectangle and set it to default
self.clear_rectangle()
elif keyval == 'return' and self.finished:
# hide selection window and play capture sound.
self.hide()
Clipper().play_capture_sound()
# create new image surface
# mousex2 - mousex1 = width
# mousey2 - mousey1 = height
# abs: because people can start drawing right to left
# int: because mouse coordinates are float.
clip = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(abs(self.mousex2 - self.mousex1)), int(abs(self.mousey2 - self.mousey1)))
clip_context = cairo.Context(clip)
# min: (look abs).
clip_context.set_source_surface(self.screenshot, 0 - min(int(self.mousex1), int(self.mousex2)), 0 - min(int(self.mousey1), int(self.mousey2)))
clip_context.paint()
# write to stream and upload
dummy_file = StringIO.StringIO()
clip.write_to_png(dummy_file)
now = datetime.datetime.now()
ENAPI.create_note(title=_("Clip ") + now.strftime("%Y-%m-%d %H:%M"),
attachment_data=dummy_file.getvalue(),
attachment_mime='image/png'
)
self.destroy()