diff options
author | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-17 09:19:59 (GMT) |
---|---|---|
committer | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-17 09:19:59 (GMT) |
commit | 4e8fd6c953f8570467652bb7ed57a49a17b26cc2 (patch) | |
tree | 680d64c3200889762a166dd03eedcd32087b2cd7 | |
parent | cd0167548b70263901e25b3b0fc4e715a7c0944b (diff) |
Add the functionality to add Journal Images to your articles in infoslicer.
Note: The drag and drop is still broken for Journam_Gallery_View
-rw-r--r-- | edit.py | 27 | ||||
-rw-r--r-- | infoslicer/processing/Article_Builder.py | 7 | ||||
-rw-r--r-- | infoslicer/processing/Journal_Getter.py | 29 | ||||
-rw-r--r-- | infoslicer/widgets/Journal_Gallery_View.py | 164 | ||||
-rw-r--r-- | infoslicer/widgets/Journal_Image_Pane.py | 62 |
5 files changed, 280 insertions, 9 deletions
@@ -22,10 +22,13 @@ from sugar3.graphics.toggletoolbutton import ToggleToolButton from infoslicer.widgets.Edit_Pane import Edit_Pane from infoslicer.widgets.Format_Pane import Format_Pane from infoslicer.widgets.Image_Pane import Image_Pane +from infoslicer.widgets.Journal_Image_Pane import Journal_Image_Pane + import book TABS = (Edit_Pane(), Image_Pane(), + Journal_Image_Pane(), Format_Pane()) class View(Gtk.Notebook): @@ -54,17 +57,24 @@ class ToolbarBuilder(): self.txt_toggle = ToggleToolButton('ascii') self.img_toggle = ToggleToolButton('image') + self.jimg_toggle = ToggleToolButton('image') self.txt_toggle.set_tooltip(_('Text')) self.txt_toggle.connect('toggled', self._toggle_cb, - [self.txt_toggle, self.img_toggle]) + [self.txt_toggle, self.img_toggle, self.jimg_toggle]) toolbar.insert(self.txt_toggle, -1) self.img_toggle.set_tooltip(_('Images')) self.img_toggle.connect('toggled', self._toggle_cb, - [self.txt_toggle, self.img_toggle]) + [self.txt_toggle, self.img_toggle, self.jimg_toggle]) toolbar.insert(self.img_toggle, -1) + self.jimg_toggle.set_tooltip(_('Journal Images')) + self.jimg_toggle.connect('toggled', self._toggle_cb, + [self.txt_toggle, self.img_toggle, self.jimg_toggle]) + toolbar.insert(self.jimg_toggle, -1) + + for tab in TABS: for i in tab.toolitems: toolbar.insert(i, -1) @@ -74,10 +84,12 @@ class ToolbarBuilder(): def sensitize_all(self): self.txt_toggle.set_sensitive(True) self.img_toggle.set_sensitive(True) + self.jimg_toggle.set_sensitive(True) def unsensitize_all(self): self.txt_toggle.set_sensitive(False) self.img_toggle.set_sensitive(False) + self.jimg_toggle.set_sensitive(False) def _toggle_cb(self, widget, toggles): for tab in TABS: @@ -87,14 +99,17 @@ class ToolbarBuilder(): if not widget.get_active(): index = 2 else: - another = toggles[0] == widget and 1 or 0 - toggles[another].set_active(False) - index = int(not another) + for t in range(0, len(toggles)): + if toggles[t] != widget: + toggles[t].set_active(False) + else: + index = t for i in TABS[index].toolitems: i.show() - if book.wiki.article: + # We don't require any article data to display jounal images + if book.wiki.article and index != 2: TABS[index].set_source_article(book.wiki.article) if book.custom.article: TABS[index].set_working_article(book.custom.article) diff --git a/infoslicer/processing/Article_Builder.py b/infoslicer/processing/Article_Builder.py index b6bd750..72a672f 100644 --- a/infoslicer/processing/Article_Builder.py +++ b/infoslicer/processing/Article_Builder.py @@ -136,6 +136,7 @@ def get_article_from_dita(image_path, dita): article_title = input.find("title").renderContents().replace("\n", "").strip()
+ # XXX: image_list are built here!!!
image_list = []
imglist_tag = input.find(True, attrs={"id" : "imagelist"})
if imglist_tag != None:
@@ -149,9 +150,9 @@ def get_article_from_dita(image_path, dita): logger.info('cannot find image %s' % img['href'])
else:
image_list.append((img['href'], caption, img['orig_href']))
-
- data = Article_Data(article_id, article_id, article_title, "theme", section_data_list, image_list)
-
+
+ data = Article_Data(article_id, article_id, article_title, "theme", section_data_list, image_list)
+
return data
diff --git a/infoslicer/processing/Journal_Getter.py b/infoslicer/processing/Journal_Getter.py new file mode 100644 index 0000000..763b3cb --- /dev/null +++ b/infoslicer/processing/Journal_Getter.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Copyright (C) Aneesh Dogra <lionaneesh@gmail.com> +# +# 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 +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this library; if not, write to the Free Software +# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA + +from sugar3.datastore import datastore + +def get_starred_images(): + ''' Find all the Starred images in journal. ''' + images = [] + dsobjects, nobjects = datastore.find({'keep': '1'}) + print "Found %d starred images" % (nobjects,) + for dsobj in dsobjects: + if hasattr(dsobj, 'metadata') and 'mime_type' in dsobj.metadata and \ + dsobj.metadata['mime_type'][0:5] == 'image': + if 'title' in dsobj.metadata: + title = dsobj.metadata['title'] + else: + title = '' + images.append((dsobj.file_path, title)) + + return images diff --git a/infoslicer/widgets/Journal_Gallery_View.py b/infoslicer/widgets/Journal_Gallery_View.py new file mode 100644 index 0000000..7191d80 --- /dev/null +++ b/infoslicer/widgets/Journal_Gallery_View.py @@ -0,0 +1,164 @@ +# -*- coding: utf-8 -*- +# Copyright (C) Aneesh Dogra <lionaneesh@gmail.com> +# +# 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 +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this library; if not, write to the Free Software +# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA + +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject +from gi.repository import GdkPixbuf +import os +import cPickle +import logging + +from Editable_Textbox import Editable_Textbox +from infoslicer.processing.Article_Data import * +from infoslicer.processing.Article import Article +import book +from infoslicer.processing import Journal_Getter as journal + +logger = logging.getLogger('infoslicer') + +# For journal images +IMG_WIDTH = 400 +IMG_HEIGHT = 300 + +class Journal_Gallery_View( Gtk.HBox ): + """ + Created by Aneesh Dogra + Journal Gallery View + + The journal gallery view displays the jounal images. + + Drag-and-drop methods have been added to set up the images as a drag + source. + The data returned by drag-data-get will be a list containing + an Image_Data object and a Sentence_Data object. + These correspond to the image + and caption respectively. + """ + + def __init__(self): + self.image_list = journal.get_starred_images() + GObject.GObject.__init__(self) + self.current_index = -1 + self.source_article_id = -1 + left_button = Gtk.Button(label="\n\n << \n\n") + right_button = Gtk.Button(label="\n\n >> \n\n") + self.imagenumberlabel = Gtk.Label() + self.image = Gtk.Image() + self.imagebox = Gtk.EventBox() + self.imagebox.add(self.image) + self.imagebox.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, + [], + Gdk.DragAction.COPY) + self.imagebox.drag_source_add_image_targets() + self.imagebox.connect("drag-begin", self.drag_begin_event, None) + logging.debug('##################### Galler_View.connect') + self.imagebox.connect("drag-data-get", self.drag_data_get_event, None) + + self.caption = Gtk.Label(label="") + self.caption.set_line_wrap(True) + + self.image_drag_container = Gtk.VBox() + self.image_drag_container.pack_start(self.imagenumberlabel, expand=False, + fill=False, padding=0) + self.image_drag_container.pack_start(self.imagebox, False, True, 0) + self.image_drag_container.pack_start(self.caption, False, True, 0) + + image_container = Gtk.VBox() + image_container.pack_start(Gtk.Label(" "), True, True, 0) + image_container.pack_start(self.image_drag_container, False, True, 0) + image_container.pack_start(Gtk.Label(" "), True, True, 0) + + left_button_container = Gtk.VBox() + left_button_container.pack_start(Gtk.Label(" "), True, True, 0) + left_button_container.pack_start(left_button, False, True, 0) + left_button_container.pack_start(Gtk.Label(" "), True, True, 0) + + right_button_container = Gtk.VBox() + right_button_container.pack_start(Gtk.Label(" "), True, True, 0) + right_button_container.pack_start(right_button, False, True, 0) + right_button_container.pack_start(Gtk.Label(" "), True, True, 0) + + + self.pack_start(left_button_container, False, True, 0) + self.pack_start(image_container, True, True, 0) + self.pack_start(right_button_container, False, True, 0) + + self.show_all() + right_button.connect("clicked", self.get_next_item, None) + left_button.connect("clicked", self.get_prev_item, None) + self.get_next_item(right_button, None) + + def get_next_item(self, button, param): + if self.image_list == []: + self.caption.set_text("No images were found in the journal.") + self.image.clear() + return + self.current_index += 1 + if self.current_index == len(self.image_list): + self.current_index = 0 + self.imagebuf = GdkPixbuf.Pixbuf.new_from_file(self.image_list[self.current_index][0]) + self.imagebuf = self.imagebuf.scale_simple(IMG_WIDTH, IMG_HEIGHT, + GdkPixbuf.InterpType.BILINEAR) + self.image.set_from_pixbuf(self.imagebuf) + self.caption.set_text("\n" + self.image_list[self.current_index][1]) + self.imagenumberlabel.set_text("(%d / %d)\n" % (self.current_index+1, len(self.image_list))) + + def get_prev_item(self, button, param): + if self.image_list == []: + self.caption.set_text("No images were found in the journal.") + self.image.clear() + return + if self.current_index == 0: + self.current_index = len(self.image_list) + self.current_index -= 1 + self.imagebuf = GdkPixbuf.Pixbuf.new_from_file(self.image_list[self.current_index][0]) + self.imagebuf = self.imagebuf.scale_simple(IMG_WIDTH, IMG_HEIGHT, + GdkPixbuf.InterpType.BILINEAR) + self.image.set_from_pixbuf(self.imagebuf) + self.caption.set_text("\n" + self.image_list[self.current_index][1]) + self.imagenumberlabel.set_text("(%d / %d)\n" % (self.current_index+1, len(self.image_list))) + + def get_first_item(self): + if self.image_list == []: + self.caption.set_text("No images were found in the journal.") + self.image.clear() + return + self.current_index = 0 + self.imagebuf = GdkPixbuf.Pixbuf.new_from_file(self.image_list[self.current_index][0]) + self.imagebuf = self.imagebuf.scale_simple(IMG_WIDTH, IMG_HEIGHT, + GdkPixbuf.InterpType.BILINEAR) + self.image.set_from_pixbuf(self.imagebuf) + self.caption.set_text("\n" + self.image_list[self.current_index][1]) + logger.debug("setting text to:") + logger.debug("(%d / %d)\n" % + (self.current_index+1, len(self.image_list))) + self.imagenumberlabel.set_text("(%d / %d)\n" % (self.current_index+1, len(self.image_list))) + + def drag_begin_event(self, widget, context, data): + logging.debug('########### Journal_Journal_Gallery_View.drag_begin_event called') + self.imagebox.drag_source_set_icon_pixbuf(self.imagebuf) + + def drag_data_get_event(self, widget, context, selection_data, info, timestamp, data): + logger.debug('############# Journal_Journal_Gallery_View.drag_data_get_event') + atom = Gdk.atom_intern("section", only_if_exists=False) + imagedata = Picture_Data(self.source_article_id, + self.image_list[self.current_index][0], None) + captiondata = Sentence_Data(0, self.source_article_id, 0, 0, 0, self.image_list[self.current_index][1]) + paragraph1data = Paragraph_Data(0, self.source_article_id, 0, 0, [imagedata]) + paragraph2data = Paragraph_Data(0, self.source_article_id, 0, 0, [captiondata]) + sectionsdata = [Section_Data(0, self.source_article_id, 0, [paragraph1data, paragraph2data])] + print sectionsdata + string = cPickle.dumps(sectionsdata) #XXX: Here it generates a weird error. + # Even though sectionsdata is not a string, cPickle prints out "cannot pickle + # string objects" + selection_data.set(atom, 8, string) diff --git a/infoslicer/widgets/Journal_Image_Pane.py b/infoslicer/widgets/Journal_Image_Pane.py new file mode 100644 index 0000000..2666bf5 --- /dev/null +++ b/infoslicer/widgets/Journal_Image_Pane.py @@ -0,0 +1,62 @@ +# Copyright (C) IBM Corporation 2008 +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject +import logging + +from gettext import gettext as _ +from Editing_View import Editing_View +from Journal_Gallery_View import Journal_Gallery_View +from infoslicer.processing.Article import Article + +logger = logging.getLogger('infoslicer') + +class Journal_Image_Pane(Gtk.HBox): + """ + Created by Aneesh Dogra + + See __init__.py for overview of panes. + + The Image Pane gives a side-by-side view of the jounal images and edit article. + The left hand side shows the jounal images. These can be dragged into + the edit article. + """ + + def __init__(self): + GObject.GObject.__init__(self) + self.toolitems = [] + + gallery_box = Gtk.VBox() + gallery_box.show() + + labeleb = Gtk.EventBox() + labeleb.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("#EEEEEE")) + gallery_box.pack_start(labeleb, False, False, 0) + labeleb.show() + + self.articletitle = Gtk.Label() + self.articletitle.set_justify(Gtk.Justification.CENTER) + labeleb.add(self.articletitle) + self.articletitle.show() + + self.gallery = Journal_Gallery_View() + self.gallery.set_size_request(Gdk.Screen.width()/2, -1) + gallery_box.pack_start(self.gallery, True, True, 0) + + self.pack_start(gallery_box, False, False, 0) + + self.editarticle = Editing_View() + self.pack_start(self.editarticle, True, True, 0) + self.editarticle.show_all() + + def set_working_article(self, article): + logger.debug("working received, title %s" % article.article_title) + + self.editarticle.articletitle.set_markup( + "<span size='medium'><b> %s </b> %s</span>"% \ + (_("Article:"), article.article_title)) + + if self.editarticle.textbox.get_article() != article: + self.editarticle.textbox.set_article(article) |