Hi all,

attached are two patches, the first of them being the
merge-layer-button.patch which was a "suggested task" I found here
http://wiki.mypaint.info/Development/JuniorJobs .
The other one (file-preview.patch) is a little bit larger and adds a
preview widget to the "Open..." dialog.
I chose 256x256px² as the preview widget's preferred size because it's
the same size used for the thumbnail saved in .ora files. However,
images smaller than 256x256px² will be scaled up, I don't know if
there's an easy pixbuf method which will only scale down, but not up.
The file-preview.patch also contains little modifications to the
mimetype set to be used by the "recent_manager" (hence "and a half" [is
there an easy way to split patches?]).

Till
diff --git a/gui/filehandling.py b/gui/filehandling.py
index 2ab30a9..397851d 100644
--- a/gui/filehandling.py
+++ b/gui/filehandling.py
@@ -17,6 +17,9 @@ from gettext import ngettext
 from lib import document, helpers
 import drawwindow
 
+import zipfile
+import mimetypes
+
 SAVE_FORMAT_ANY = 0
 SAVE_FORMAT_ORA = 1
 SAVE_FORMAT_PNGSOLID = 2
@@ -232,17 +235,52 @@ class FileHandler(object):
             if not export:
                 self.filename = os.path.abspath(filename)
                 print 'Saved to', self.filename
+                extension = os.path.splitext(self.filename)[1]
+                # todo: handle mime types not in the standard map
+                # see http://docs.python.org/library/mimetypes.html
+                if extension == ".ora":
+                    mimetype = 'image/openraster'
+                else:
+                    try:
+                        mimetype = mimetypes.types_map[extension]
+                    except:
+                        mimetype = 'application/octet-stream'
                 gtk.recent_manager_get_default().add_full("file://" + self.filename,
                         {
                             'app_name': 'mypaint',
                             'app_exec': sys.argv[0],
-                            # todo: get mime_type
-                            'mime_type': 'application/octet-stream'
+                            'mime_type': mimetype
                         }
                 )
             else:
                 print 'Exported to', os.path.abspath(filename)
 
+    def update_preview_cb(self, file_chooser, preview):
+        filename = file_chooser.get_preview_filename()
+        pixbuf = self.get_preview_image(filename)
+        preview.set_from_pixbuf(pixbuf)
+        file_chooser.set_preview_widget_active(pixbuf != None)
+        return
+
+    def get_preview_image(self, filename):
+        if filename:
+            if os.path.splitext(filename)[1] == ".ora":
+                #TODO check for upper-/lowercase variants
+                ora = zipfile.ZipFile(file(filename))
+                data = ora.read("Thumbnails/thumbnail.png")
+                loader = gtk.gdk.PixbufLoader("png")
+                loader.write(data)
+                loader.close()
+                pixbuf = loader.get_pixbuf()
+                return pixbuf
+            else:
+                try:
+                    pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(filename, 256, 256)
+                    return pixbuf
+                except:
+                    return None
+        return
+
     def open_cb(self, action):
         if not self.confirm_destructive_action():
             return
@@ -251,7 +289,11 @@ class FileHandler(object):
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN, gtk.RESPONSE_OK))
         dialog.set_default_response(gtk.RESPONSE_OK)
-
+        
+        preview = gtk.Image()
+        dialog.set_preview_widget(preview)
+        dialog.connect("update-preview", self.update_preview_cb, preview)
+        
         add_filters_to_dialog(self.file_filters, dialog)
 
         if self.filename:
@@ -305,8 +347,6 @@ class FileHandler(object):
                             saveformat = SAVE_FORMAT_ORA
 
                 desc, ext_format, options = self.saveformats[saveformat]
-
-                # 
                 if ext:
                     if ext_format != ext:
                         # Minor ugliness: if the user types '.png' but
@@ -319,7 +359,7 @@ class FileHandler(object):
                     if action.get_name() == 'Export':
                         # Do not change working file
                         self.save_file(filename, True, **options)
-		    else:
+                    else:
                         self.save_file(filename, **options)
                     break
 
diff --git a/gui/layerswindow.py b/gui/layerswindow.py
index 73db4b9..a080bf3 100644
--- a/gui/layerswindow.py
+++ b/gui/layerswindow.py
@@ -361,17 +361,20 @@ class Window(windowing.SubWindow):
         add_button = stock_button(gtk.STOCK_ADD)
         move_up_button = stock_button(gtk.STOCK_GO_UP)
         move_down_button = stock_button(gtk.STOCK_GO_DOWN)
+        merge_down_button = stock_button(gtk.STOCK_DND_MULTIPLE)
         del_button = stock_button(gtk.STOCK_DELETE)
 
         add_button.connect('clicked', self.on_layer_add)
         move_up_button.connect('clicked', self.move_layer, 'up')
         move_down_button.connect('clicked', self.move_layer, 'down')
+        merge_down_button.connect('clicked', self.merge_layer_down)
         del_button.connect('clicked', self.on_layer_del)
 
         buttons_hbox = gtk.HBox()
         buttons_hbox.pack_start(add_button)
         buttons_hbox.pack_start(move_up_button)
         buttons_hbox.pack_start(move_down_button)
+        buttons_hbox.pack_start(merge_down_button)
         buttons_hbox.pack_start(del_button)
 
         # Pack and add to toplevel
@@ -424,6 +427,9 @@ class Window(windowing.SubWindow):
             doc.move_layer(current_layer_pos, new_layer_pos)
             doc.select_layer(new_layer_pos)
 
+    def merge_layer_down(self, widget):
+        self.app.doc.model.merge_layer_down()
+
     def on_layer_add(self, button):
         doc = self.app.doc.model
         doc.add_layer(after=doc.get_current_layer())
_______________________________________________
Mypaint-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/mypaint-discuss

Reply via email to