1 | import os |
---|
2 | import gi |
---|
3 | gi.require_version('Gtk', '3.0') |
---|
4 | gi.require_version('Webkit', '3.0') |
---|
5 | |
---|
6 | from gi.repository import Gtk,GdkPixbuf,GLib,Gdk,WebKit |
---|
7 | import Screenshot |
---|
8 | |
---|
9 | |
---|
10 | class ScreenshotViewer(Gtk.EventBox): |
---|
11 | |
---|
12 | def __init__(self): |
---|
13 | |
---|
14 | Gtk.EventBox.__init__(self) |
---|
15 | self.set_valign(Gtk.Align.START) |
---|
16 | self.set_halign(Gtk.Align.START) |
---|
17 | |
---|
18 | self.html_skel='<html><body bgcolor=black><div align=center><iframe height=97% width=90% align=center src="%%URL%%" frameborder="0" allowfullscreen></iframe></div></body></html>' |
---|
19 | self.border=20 |
---|
20 | |
---|
21 | try: |
---|
22 | cache_dir=os.environ["XDG_CACHE_HOME"] |
---|
23 | except: |
---|
24 | cache_dir=os.path.expanduser("~/.cache/") |
---|
25 | |
---|
26 | self.image_dir=cache_dir+"/lliurex-store/" |
---|
27 | |
---|
28 | |
---|
29 | self.revealer=Gtk.Revealer() |
---|
30 | self.revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_DOWN) |
---|
31 | self.revealer.set_transition_duration(500) |
---|
32 | |
---|
33 | |
---|
34 | self.content_box=Gtk.VBox() |
---|
35 | self.content_box.set_name("POPUP_SHADOW_TOPBOTTOM") |
---|
36 | hbox=Gtk.HBox() |
---|
37 | |
---|
38 | self.image=Screenshot.ScreenshotNeo() |
---|
39 | self.wv=WebKit.WebView() |
---|
40 | |
---|
41 | self.wv=WebKit.WebView() |
---|
42 | self.w_vp=Gtk.Viewport() |
---|
43 | self.w_sw=Gtk.ScrolledWindow() |
---|
44 | self.w_vp.add(self.w_sw) |
---|
45 | self.w_sw.add(self.wv) |
---|
46 | |
---|
47 | self.image.add_titled(self.w_vp,"html","Html") |
---|
48 | |
---|
49 | |
---|
50 | hbox.pack_start(self.image,True,True,self.border) |
---|
51 | self.content_box.pack_start(hbox,True,True,self.border) |
---|
52 | |
---|
53 | self.buttons_box=Gtk.HBox() |
---|
54 | self.buttons_box.set_valign(Gtk.Align.CENTER) |
---|
55 | |
---|
56 | |
---|
57 | for x in range(0,4): |
---|
58 | b=Gtk.Button(str(x)) |
---|
59 | b.set_size_request(100,100) |
---|
60 | b.set_name("RELATED_BUTTON") |
---|
61 | self.buttons_box.pack_start(b,False,False,5) |
---|
62 | |
---|
63 | self.sw=Gtk.ScrolledWindow() |
---|
64 | |
---|
65 | self.vp=Gtk.Viewport() |
---|
66 | self.vp.set_halign(Gtk.Align.CENTER) |
---|
67 | self.sw.add(self.vp) |
---|
68 | self.sw.set_halign(Gtk.Align.CENTER) |
---|
69 | self.sw.set_margin_bottom(30) |
---|
70 | self.vp.add(self.buttons_box) |
---|
71 | |
---|
72 | self.content_box.pack_end(self.sw,False,False,0) |
---|
73 | |
---|
74 | self.revealer.add(self.content_box) |
---|
75 | self.revealer.show_all() |
---|
76 | self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) |
---|
77 | self.connect("button-press-event",self.area_clicked) |
---|
78 | |
---|
79 | self.add(self.revealer) |
---|
80 | |
---|
81 | #def init |
---|
82 | |
---|
83 | def wait_to_reveal(self,x,y,data,url=False): |
---|
84 | |
---|
85 | if not self.revealer.get_child_revealed(): |
---|
86 | |
---|
87 | return True |
---|
88 | |
---|
89 | if url==False: |
---|
90 | image_info={} |
---|
91 | image_info["x"]=x |
---|
92 | image_info["y"]=y |
---|
93 | image_info["pixbuf"]=data |
---|
94 | image_info["aspect_ratio"]=True |
---|
95 | self.image.set_from_pixbuf(image_info) |
---|
96 | else: |
---|
97 | self.show_url(None,data) |
---|
98 | |
---|
99 | #def wait_to_reveal |
---|
100 | |
---|
101 | def set_screenshot(self,current_id,screenshots_box,current_id_is_url=False): |
---|
102 | |
---|
103 | |
---|
104 | self.image.spinner.start() |
---|
105 | self.image.spinner.show() |
---|
106 | self.image.set_visible_child_name("spinner") |
---|
107 | |
---|
108 | |
---|
109 | for child in self.buttons_box.get_children(): |
---|
110 | |
---|
111 | self.buttons_box.remove(child) |
---|
112 | |
---|
113 | for x in screenshots_box: |
---|
114 | |
---|
115 | tmp=x.get_children()[0] |
---|
116 | pixbuf=tmp.image.get_pixbuf() |
---|
117 | id=x.get_children()[0].image_info["image_id"] |
---|
118 | b=Gtk.Button() |
---|
119 | b.add(Gtk.Image.new_from_pixbuf(pixbuf)) |
---|
120 | if tmp.image_info["video_url"]!=None: |
---|
121 | b.connect("clicked",self.show_url,tmp.image_info["video_url"]) |
---|
122 | else: |
---|
123 | b.connect("clicked",self.screenshot_button_clicked,id) |
---|
124 | b.set_size_request(100,100) |
---|
125 | b.set_name("RELATED_BUTTON") |
---|
126 | self.buttons_box.pack_start(b,False,False,5) |
---|
127 | self.buttons_box.show_all() |
---|
128 | |
---|
129 | |
---|
130 | if not current_id_is_url: |
---|
131 | |
---|
132 | image=Gtk.Image.new_from_file(self.image_dir+current_id) |
---|
133 | |
---|
134 | pixbuf=image.get_pixbuf() |
---|
135 | x=pixbuf.get_width() |
---|
136 | y=pixbuf.get_height() |
---|
137 | |
---|
138 | w_x,w_y=self.content_box.get_size_request() |
---|
139 | w_x-=self.border*2 |
---|
140 | w_y-=self.border*2 + 240 |
---|
141 | |
---|
142 | ratio=min(w_x*1.0/x,w_y*1.0/y) |
---|
143 | pixbuf=pixbuf.scale_simple(x*ratio,y*ratio,GdkPixbuf.InterpType.BILINEAR) |
---|
144 | |
---|
145 | new_x=x*ratio |
---|
146 | new_y=y*ratio |
---|
147 | |
---|
148 | GLib.timeout_add(30,self.wait_to_reveal,new_x,new_y,pixbuf,False) |
---|
149 | |
---|
150 | else: |
---|
151 | |
---|
152 | new_x=0 |
---|
153 | new_y=0 |
---|
154 | GLib.timeout_add(30,self.wait_to_reveal,new_x,new_y,current_id,True) |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | #def set_screenshot |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | def show_url(self,widget,url): |
---|
163 | |
---|
164 | self.wv.load_html_string(self.html_skel.replace("%%URL%%",url),"") |
---|
165 | self.image.set_visible_child_name("html") |
---|
166 | |
---|
167 | #def show url |
---|
168 | |
---|
169 | def screenshot_button_clicked(self,widget,current_id): |
---|
170 | |
---|
171 | image=Gtk.Image.new_from_file(self.image_dir+current_id) |
---|
172 | |
---|
173 | pixbuf=image.get_pixbuf() |
---|
174 | x=pixbuf.get_width() |
---|
175 | y=pixbuf.get_height() |
---|
176 | |
---|
177 | w_x,w_y=self.content_box.get_size_request() |
---|
178 | w_x-=self.border*2 |
---|
179 | w_y-=self.border*2 + 240 |
---|
180 | |
---|
181 | ratio=min(w_x*1.0/x,w_y*1.0/y) |
---|
182 | |
---|
183 | new_x=x*ratio |
---|
184 | new_y=y*ratio |
---|
185 | |
---|
186 | pixbuf=pixbuf.scale_simple(x*ratio,y*ratio,GdkPixbuf.InterpType.BILINEAR) |
---|
187 | self.image.spinner.hide() |
---|
188 | |
---|
189 | image_info={} |
---|
190 | image_info["x"]=new_x |
---|
191 | image_info["y"]=new_y |
---|
192 | image_info["pixbuf"]=pixbuf |
---|
193 | image_info["aspect_ratio"]=True |
---|
194 | self.image.set_from_pixbuf(image_info) |
---|
195 | |
---|
196 | self.image.set_from_pixbuf(image_info) |
---|
197 | |
---|
198 | #def screenshot_button_clicked |
---|
199 | |
---|
200 | |
---|
201 | def area_clicked(self,widget,event): |
---|
202 | |
---|
203 | self.revealer.set_reveal_child(False) |
---|
204 | |
---|
205 | #def area_clicked |
---|
206 | |
---|
207 | |
---|
208 | #class ScreenshotViewer |
---|