Skip to content

Commit

Permalink
Add Image viewer for source view
Browse files Browse the repository at this point in the history
How its works:
Select an image on the treeview, and It will show
If the selected file is audio/video, an icon (audio=audio-x-generic,
video=video-x-generic) will be showed.
However if you havent selected a file, a label will be showed.
If the selected file is Unknown for the view source
(no audio, text, image, video)will be show the unknown icon

Fixes SL#4273
  • Loading branch information
i5o authored and godiard committed Dec 10, 2014
1 parent 55f8e4d commit 4be3f9e
Showing 1 changed file with 82 additions and 16 deletions.
98 changes: 82 additions & 16 deletions src/jarabe/view/viewsource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (C) 2008 One Laptop Per Child
# Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer
# Copyright (C) 2011 Walter Bender
# Copyright (C) 2014 Ignacio Rodriguez
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -28,6 +29,7 @@
from gi.repository import Gdk
from gi.repository import GdkX11
from gi.repository import GtkSource
from gi.repository import GdkPixbuf
import dbus
from gi.repository import Gio

Expand Down Expand Up @@ -623,30 +625,49 @@ def __init__(self):
self.props.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC
self.props.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC

self._buffer = GtkSource.Buffer()
self._buffer.set_highlight_syntax(True)

self._source_view = GtkSource.View(buffer=self._buffer)
self._source_view.set_editable(False)
self._source_view.set_cursor_visible(True)
self._source_view.set_show_line_numbers(True)
self._source_view.set_show_right_margin(True)
self._source_view.set_right_margin_position(80)
# self._source_view.set_highlight_current_line(True) #FIXME: Ugly color
self._source_view.modify_font(_SOURCE_FONT)
self.add(self._source_view)
self._source_view.show()
self._box = Gtk.Box()
self.add(self._box)
self._box.show()

self._file_path = None

def _set_file_path(self, file_path):
self._file_path = file_path

for child in self._box.get_children():
self._box.remove(child)

if self._file_path is None:
self._buffer.set_text('')
self._show_no_file()
return

mime_type = mime.get_for_file(self._file_path)
if 'image/' in mime_type:
self._show_image_viewer(image=True)
elif 'audio/' in mime_type:
self._show_image_viewer(icon='audio-x-generic')
elif 'video/' in mime_type:
self._show_image_viewer(icon='video-x-generic')
else:
response = self._show_text_viewer()
if not response:
self._show_image_viewer(icon='application-x-generic')

def _show_text_viewer(self):
source_buffer = GtkSource.Buffer()
source_buffer.set_highlight_syntax(True)

source_view = GtkSource.View(buffer=source_buffer)
source_view.set_editable(False)
source_view.set_cursor_visible(True)
source_view.set_show_line_numbers(True)
source_view.set_show_right_margin(True)
source_view.set_right_margin_position(80)
source_view.modify_font(_SOURCE_FONT)
# source_view.set_highlight_current_line(True) #FIXME: Ugly color

mime_type = mime.get_for_file(self._file_path)

_logger.debug('Detected mime type: %r', mime_type)

language_manager = GtkSource.LanguageManager.get_default()
Expand All @@ -661,10 +682,55 @@ def _set_file_path(self, file_path):
_logger.debug('Detected language: %r',
detected_language.get_name())

self._buffer.set_language(detected_language)
self._buffer.set_text(open(self._file_path, 'r').read())
source_buffer.set_language(detected_language)
text = open(self._file_path, 'r').read()
works = True
try:
text.encode()
source_buffer.set_text(text)
self._box.pack_start(source_view, True, True, 0)
self._box.show_all()
except UnicodeDecodeError:
works = False

return works

def _get_file_path(self):
return self._file_path

file_path = property(_get_file_path, _set_file_path)

def _show_image_viewer(self, icon=None, image=False):
media_box = Gtk.EventBox()
media_box.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('white'))

c = media_box.get_child()
if c:
media_box.remove(c)

if image:
image = Gtk.Image()
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self._file_path)
image.set_from_pixbuf(pixbuf)
media_box.add(image)
image.show()

if icon:
h = Gdk.Screen.width() / 3
icon = Icon(icon_name=icon, pixel_size=h)
media_box.add(icon)
icon.show()

self._box.pack_start(media_box, True, True, 0)
self._box.show_all()

def _show_no_file(self):
nofile_label = Gtk.Label()
nofile_label.set_text(_("Please select a file in the left panel."))

nofile_box = Gtk.EventBox()
nofile_box.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('white'))

nofile_box.add(nofile_label)
self._box.pack_start(nofile_box, True, True, 0)
self._box.show_all()

0 comments on commit 4be3f9e

Please sign in to comment.