[meld] Remove GnomeEntry clone and use FileChoosers instead



commit 6df0ba87f35300f74a00841eedf276f7daa31c5c
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Aug 11 06:52:51 2013 +1000

    Remove GnomeEntry clone and use FileChoosers instead
    
    This is part of migrating to a more modern UI. It's possible that
    we'll want something like this again in the future, but if so then we
    can do it with a better UI. All current uses have been migrated to
    the standard GtkFileChooserButton, though these layouts will need
    polish to make this look less silly.

 data/ui/dirdiff.ui      |   21 ++--
 data/ui/filediff.ui     |   15 +--
 data/ui/vcview.ui       |    7 +-
 meld/dirdiff.py         |   13 +-
 meld/filediff.py        |    8 +-
 meld/ui/catalog.xml     |    6 -
 meld/ui/historyentry.py |  299 +----------------------------------------------
 meld/vcview.py          |    9 +-
 8 files changed, 34 insertions(+), 344 deletions(-)
---
diff --git a/data/ui/dirdiff.ui b/data/ui/dirdiff.ui
index ebaef4b..4a59757 100644
--- a/data/ui/dirdiff.ui
+++ b/data/ui/dirdiff.ui
@@ -110,12 +110,11 @@
             <property name="n_rows">2</property>
             <property name="n_columns">7</property>
             <child>
-              <object class="HistoryFileEntry" id="fileentry0">
+              <object class="GtkFileChooserButton" id="fileentry0">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="history_id">direntry</property>
-                <property name="directory_entry">True</property>
-                <signal name="activate" handler="on_fileentry_activate" swapped="no"/>
+                <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
+                <signal name="file-set" handler="on_fileentry_file_set" swapped="no"/>
               </object>
               <packing>
                 <property name="left_attach">1</property>
@@ -124,12 +123,11 @@
               </packing>
             </child>
             <child>
-              <object class="HistoryFileEntry" id="fileentry1">
+              <object class="GtkFileChooserButton" id="fileentry1">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="history_id">direntry</property>
-                <property name="directory_entry">True</property>
-                <signal name="activate" handler="on_fileentry_activate" swapped="no"/>
+                <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
+                <signal name="file-set" handler="on_fileentry_file_set" swapped="no"/>
               </object>
               <packing>
                 <property name="left_attach">3</property>
@@ -138,12 +136,11 @@
               </packing>
             </child>
             <child>
-              <object class="HistoryFileEntry" id="fileentry2">
+              <object class="GtkFileChooserButton" id="fileentry2">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="history_id">direntry</property>
-                <property name="directory_entry">True</property>
-                <signal name="activate" handler="on_fileentry_activate" swapped="no"/>
+                <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
+                <signal name="file-set" handler="on_fileentry_file_set" swapped="no"/>
               </object>
               <packing>
                 <property name="left_attach">5</property>
diff --git a/data/ui/filediff.ui b/data/ui/filediff.ui
index 96084f9..553fb08 100644
--- a/data/ui/filediff.ui
+++ b/data/ui/filediff.ui
@@ -221,10 +221,9 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="HistoryFileEntry" id="fileentry2">
+                  <object class="GtkFileChooserButton" id="fileentry2">
                     <property name="visible">True</property>
-                    <property name="history-id">fileentry</property>
-                    <signal handler="on_fileentry_activate" name="activate"/>
+                    <signal handler="on_fileentry_file_set" name="file-set"/>
                   </object>
                   <packing>
                     <property name="expand">True</property>
@@ -263,10 +262,9 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="HistoryFileEntry" id="fileentry1">
+                  <object class="GtkFileChooserButton" id="fileentry1">
                     <property name="visible">True</property>
-                    <property name="history-id">fileentry</property>
-                    <signal handler="on_fileentry_activate" name="activate"/>
+                    <signal handler="on_fileentry_file_set" name="file-set"/>
                   </object>
                   <packing>
                     <property name="expand">True</property>
@@ -305,10 +303,9 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="HistoryFileEntry" id="fileentry0">
+                  <object class="GtkFileChooserButton" id="fileentry0">
                     <property name="visible">True</property>
-                    <property name="history-id">fileentry</property>
-                    <signal handler="on_fileentry_activate" name="activate"/>
+                    <signal handler="on_fileentry_file_set" name="file-set"/>
                   </object>
                   <packing>
                     <property name="expand">True</property>
diff --git a/data/ui/vcview.ui b/data/ui/vcview.ui
index 2e7affe..e870244 100644
--- a/data/ui/vcview.ui
+++ b/data/ui/vcview.ui
@@ -154,11 +154,10 @@
             <property name="homogeneous">False</property>
             <property name="spacing">6</property>
             <child>
-              <object class="HistoryFileEntry" id="fileentry">
+              <object class="GtkFileChooserButton" id="fileentry">
                 <property name="visible">True</property>
-                <property name="history-id">vc_directory</property>
-                <property name="directory-entry">True</property>
-                <signal handler="on_fileentry_activate" name="activate"/>
+                <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
+                <signal handler="on_fileentry_file_set" name="file-set"/>
               </object>
               <packing>
                 <property name="padding">0</property>
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index f3efcd6..7eb68ac 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -561,10 +561,10 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
             it = self.model.iter_parent(it)
         self._update_diffmaps()
 
-    def on_fileentry_activate(self, entry):
-        locs = [e.get_full_path() for e in self.fileentry[:self.num_panes]]
-        locs = [l.decode('utf8') for l in locs]
-        self.set_locations(locs)
+    def on_fileentry_file_set(self, entry):
+        files = [e.get_file() for e in self.fileentry[:self.num_panes]]
+        paths = [f.get_path() for f in files]
+        self.set_locations(paths)
 
     def set_locations(self, locations):
         self.set_num_panes(len(locations))
@@ -581,7 +581,6 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.model.clear()
         for pane, loc in enumerate(locations):
             self.fileentry[pane].set_filename(loc)
-            self.fileentry[pane].prepend_history(loc)
         child = self.model.add_entries(None, locations)
         self.treeview0.grab_focus()
         self._update_item_state(child)
@@ -1347,7 +1346,7 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
                 widget.hide()
             if self.num_panes != 0: # not first time through
                 self.num_panes = n
-                self.on_fileentry_activate(None)
+                self.on_fileentry_file_set(None)
             else:
                 self.num_panes = n
 
@@ -1434,7 +1433,7 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
             self.treeview[pane].set_cursor(path)
 
     def on_refresh_activate(self, *extra):
-        self.on_fileentry_activate(None)
+        self.on_fileentry_file_set(None)
 
     def on_delete_event(self, appquit=0):
         for h in self.app_handlers:
diff --git a/meld/filediff.py b/meld/filediff.py
index acb297d..f2951a9 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -1063,7 +1063,6 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
                 files[i] = f = f.decode('utf8')
             absfile = os.path.abspath(f)
             self.fileentry[i].set_filename(absfile)
-            self.fileentry[i].prepend_history(absfile)
             self.textbuffer[i].reset_buffer(absfile)
             self.msgarea_mgr[i].clear()
 
@@ -1578,7 +1577,6 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
                 bufdata.filename = bufdata.label = os.path.abspath(filename)
                 bufdata.savefile = None
                 self.fileentry[pane].set_filename(bufdata.filename)
-                self.fileentry[pane].prepend_history(bufdata.filename)
             else:
                 return False
         start, end = buf.get_bounds()
@@ -1660,11 +1658,11 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
             if self.textbuffer[i].data.modified:
                 self.save_file(i)
 
-    def on_fileentry_activate(self, entry):
+    def on_fileentry_file_set(self, entry):
         if self.check_save_modified() != gtk.RESPONSE_CANCEL:
             entries = self.fileentry[:self.num_panes]
-            paths = [e.get_full_path() for e in entries]
-            paths = [p.decode('utf8') for p in paths]
+            files = [e.get_file() for e in entries]
+            paths = [f.get_path() for f in files]
             self.set_files(paths)
         return True
 
diff --git a/meld/ui/catalog.xml b/meld/ui/catalog.xml
index e867e52..896fbbc 100644
--- a/meld/ui/catalog.xml
+++ b/meld/ui/catalog.xml
@@ -5,9 +5,6 @@
     <glade-widget-classes>
         <glade-widget-class title="DiffMap" name="DiffMap" generic-name="diffmap"/>
         <glade-widget-class title="LinkMap" name="LinkMap" generic-name="linkmap"/>
-        <glade-widget-class title="HistoryCombo" name="HistoryCombo" generic-name="historycombo"/>
-        <glade-widget-class title="HistoryEntry" name="HistoryEntry" generic-name="historyentry"/>
-        <glade-widget-class title="HistoryFileEntry" name="HistoryFileEntry" 
generic-name="historyfileentry"/>
         <glade-widget-class title="MeldSourceView" name="MeldSourceView" generic-name="meldsourceview"/>
         <glade-widget-class title="MsgAreaController" name="MsgAreaController" 
generic-name="msgareacontroller"/>
     </glade-widget-classes>
@@ -15,9 +12,6 @@
     <glade-widget-group name="meld" title="Meld">
         <glade-widget-class-ref name="DiffMap"/>
         <glade-widget-class-ref name="LinkMap"/>
-        <glade-widget-class-ref name="HistoryCombo"/>
-        <glade-widget-class-ref name="HistoryEntry"/>
-        <glade-widget-class-ref name="HistoryFileEntry"/>
         <glade-widget-class-ref name="MeldSourceView"/>
         <glade-widget-class-ref name="MsgAreaController"/>
     </glade-widget-group>
diff --git a/meld/ui/historyentry.py b/meld/ui/historyentry.py
index ad97ccf..52415f2 100644
--- a/meld/ui/historyentry.py
+++ b/meld/ui/historyentry.py
@@ -15,19 +15,11 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 # USA.
 
-import os
-import sys
-
 import glib
-import gio
 import gtk
 import gobject
 import pango
-import atk
 # gconf is also imported; see end of HistoryEntry class for details
-from gettext import gettext as _
-
-from ..util.compat import text_type
 
 # This file is a Python translation of:
 #  * gedit/gedit/gedit-history-entry.c
@@ -201,6 +193,8 @@ class HistoryWidget(object):
             self.set_cell_data_func(cells[0], None, None)
 
 
+# TODO: There is no point having this separation now
+
 class HistoryCombo(gtk.ComboBox, HistoryWidget):
     __gtype_name__ = "HistoryCombo"
 
@@ -270,21 +264,6 @@ class HistoryCombo(gtk.ComboBox, HistoryWidget):
             store.append((firstline, item))
 
 
-class HistoryEntry(gtk.ComboBoxEntry, HistoryWidget):
-    __gtype_name__ = "HistoryEntry"
-
-    __gproperties__ = {
-        "history-id": (str, "History ID",
-                       "Identifier associated with entry's history store",
-                       None, gobject.PARAM_READWRITE),
-    }
-
-    def __init__(self, history_id=None, enable_completion=False, **kwargs):
-        super(HistoryEntry, self).__init__(**kwargs)
-        HistoryWidget.__init__(self, history_id, enable_completion)
-        self.props.text_column = 0
-
-
 try:
     import gconf
     # Verify that gconf is actually working (bgo#666136)
@@ -298,277 +277,3 @@ except (ImportError, glib.GError):
         setattr(HistoryWidget, m, do_nothing)
         setattr(HistoryCombo, m, do_nothing)
 
-
-
-def _expand_filename(filename, default_dir):
-    if not filename:
-        return ""
-    if os.path.isabs(filename):
-        return filename
-    expanded = os.path.expanduser(filename)
-    if expanded != filename:
-        return expanded
-    elif default_dir:
-        return os.path.expanduser(os.path.join(default_dir, filename))
-    else:
-        return os.path.join(os.getcwd(), filename)
-
-
-last_open = {}
-
-
-class HistoryFileEntry(gtk.HBox, gtk.Editable):
-    __gtype_name__ = "HistoryFileEntry"
-
-    __gsignals__ = {
-        "browse_clicked" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
-        "activate" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [])
-    }
-
-    __gproperties__ = {
-        "dialog-title":    (str, "Default path",
-                            "Default path for file chooser",
-                            "~", gobject.PARAM_READWRITE),
-        "default-path":    (str, "Default path",
-                            "Default path for file chooser",
-                            "~", gobject.PARAM_READWRITE),
-        "directory-entry": (bool, "File or directory entry",
-                            "Whether the created file chooser should select directories instead of files",
-                            False, gobject.PARAM_READWRITE),
-        "filename":        (str, "Filename",
-                            "Filename of the selected file",
-                            "", gobject.PARAM_READWRITE),
-        "history-id":      (str, "History ID",
-                            "Identifier associated with entry's history store",
-                            None, gobject.PARAM_READWRITE),
-        "modal":           (bool, "File chooser modality",
-                            "Whether the created file chooser is modal",
-                            False, gobject.PARAM_READWRITE),
-    }
-
-
-    def __init__(self, **kwargs):
-        super(HistoryFileEntry, self).__init__(**kwargs)
-
-        self.fsw = None
-        self.__browse_dialog_title = None
-        self.__filechooser_action = gtk.FILE_CHOOSER_ACTION_OPEN
-        self.__default_path = "~"
-        self.__directory_entry = False
-        self.__modal = False
-
-        self.set_spacing(3)
-
-        # TODO: completion would be nice, but some quirks make it currently too irritating to turn on by 
default
-        self.__gentry = HistoryEntry()
-        entry = self.__gentry.get_entry()
-        entry.connect("changed", lambda *args: self.emit("changed"))
-        entry.connect("activate", lambda *args: self.emit("activate"))
-
-        # We need to get rid of the pre-existing drop site on the entry
-        self.__gentry.get_entry().drag_dest_unset()
-        self.drag_dest_set(gtk.DEST_DEFAULT_MOTION |
-                           gtk.DEST_DEFAULT_HIGHLIGHT |
-                           gtk.DEST_DEFAULT_DROP,
-                           [], gtk.gdk.ACTION_COPY)
-        self.drag_dest_add_uri_targets()
-        self.connect("drag_data_received",
-                     self.history_entry_drag_data_received)
-
-        self.pack_start(self.__gentry, True, True, 0)
-        self.__gentry.show()
-
-        button = gtk.Button(_("_Browse..."))
-        button.connect("clicked", self.__browse_clicked)
-        self.pack_start(button, False, False, 0)
-        button.show()
-
-        access_entry = self.__gentry.get_accessible()
-        access_button = button.get_accessible()
-        if access_entry and access_button:
-            access_entry.set_name(_("Path"))
-            access_entry.set_description(_("Path to file"))
-            access_button.set_description(_("Pop up a file selector to choose a file"))
-            access_button.add_relationship(atk.RELATION_CONTROLLER_FOR, access_entry)
-            access_entry.add_relationship(atk.RELATION_CONTROLLED_BY, access_button)
-
-    def do_get_property(self, pspec):
-        if pspec.name == "dialog-title":
-            return self.__browse_dialog_title
-        elif pspec.name == "default-path":
-            return self.__default_path
-        elif pspec.name == "directory-entry":
-            return self.__directory_entry
-        elif pspec.name == "filename":
-            return self.get_full_path()
-        elif pspec.name == "history-id":
-            return self.__gentry.props.history_id
-        elif pspec.name == "modal":
-            return self.__modal
-        else:
-            raise AttributeError("Unknown property: %s" % pspec.name)
-
-    def do_set_property(self, pspec, value):
-        if pspec.name == "dialog-title":
-            self.__browse_dialog_title = value
-        elif pspec.name == "default-path":
-            if value:
-                self.__default_path = os.path.abspath(value)
-            else:
-                self.__default_path = None
-        elif pspec.name == "directory-entry":
-            self.__directory_entry = value
-        elif pspec.name == "filename":
-            self.set_filename(value)
-        elif pspec.name == "history-id":
-            self.__gentry.props.history_id = value
-        elif pspec.name == "modal":
-            self.__modal = value
-        else:
-            raise AttributeError("Unknown property: %s" % pspec.name)
-
-    def _get_last_open(self):
-        try:
-            return last_open[self.props.history_id]
-        except KeyError:
-            return None
-
-    def _set_last_open(self, path):
-        last_open[self.props.history_id] = path
-
-    def append_history(self, text):
-        self.__gentry.append_history(text)
-
-    def prepend_history(self, text):
-        self.__gentry.prepend_history(text)
-
-    def focus_entry(self):
-        self.__gentry.focus_entry()
-
-    def set_default_path(self, path):
-        if path:
-            self.__default_path = os.path.abspath(path)
-        else:
-            self.__default_path = None
-
-    def set_directory_entry(self, is_directory_entry):
-        self.directory_entry = is_directory_entry
-
-    def get_directory_entry(self):
-        return self.directory_entry
-
-    def _get_default(self):
-        default = self.__default_path
-        last_path = self._get_last_open()
-        if last_path and os.path.exists(last_path):
-            default = last_path
-        return default
-
-    def get_full_path(self):
-        text = self.__gentry.get_entry().get_text()
-        if not text:
-            return None
-        sys_text = gobject.filename_from_utf8(text)
-        filename = _expand_filename(sys_text, self._get_default())
-        if not filename:
-            return None
-        return filename
-
-    def set_filename(self, filename):
-        self.__gentry.get_entry().set_text(filename)
-
-    def __browse_dialog_ok(self, filewidget):
-        filename = filewidget.get_filename()
-        if not filename:
-            return
-
-        encoding = sys.getfilesystemencoding()
-        if encoding:
-            filename = text_type(filename, encoding)
-        entry = self.__gentry.get_entry()
-        entry.set_text(filename)
-        self._set_last_open(filename)
-        entry.activate()
-
-    def __browse_dialog_response(self, widget, response):
-        if response == gtk.RESPONSE_ACCEPT:
-            self.__browse_dialog_ok(widget)
-        widget.destroy()
-        self.fsw = None
-
-    def __build_filename(self):
-        default = self._get_default()
-
-        text = self.__gentry.get_entry().get_text()
-        if not text:
-            return default + os.sep
-
-        locale_text = gobject.filename_from_utf8(text)
-        if not locale_text:
-            return default + os.sep
-
-        filename = _expand_filename(locale_text, default)
-        if not filename:
-            return default + os.sep
-
-        if not filename.endswith(os.sep) and (self.__directory_entry or os.path.isdir(filename)):
-            filename += os.sep
-        return filename
-
-    def __browse_clicked(self, *args):
-        if self.fsw:
-            self.fsw.show()
-            if self.fsw.window:
-                self.fsw.window.raise_()
-            return
-
-        if self.__directory_entry:
-            action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
-            filefilter = gtk.FileFilter()
-            filefilter.add_mime_type("x-directory/normal")
-            title = self.__browse_dialog_title or _("Select directory")
-        else:
-            action = self.__filechooser_action
-            filefilter = None
-            title = self.__browse_dialog_title or _("Select file")
-
-        if action == gtk.FILE_CHOOSER_ACTION_SAVE:
-            buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT)
-        else:
-            buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT)
-
-        self.fsw = gtk.FileChooserDialog(title, None, action, buttons, None)
-        self.fsw.props.filter = filefilter
-        self.fsw.set_default_response(gtk.RESPONSE_ACCEPT)
-        self.fsw.set_filename(self.__build_filename())
-        self.fsw.connect("response", self.__browse_dialog_response)
-
-        toplevel = self.get_toplevel()
-        modal_fentry = False
-        if toplevel.flags() & gtk.TOPLEVEL:
-            self.fsw.set_transient_for(toplevel)
-            modal_fentry = toplevel.get_modal()
-        if self.__modal or modal_fentry:
-            self.fsw.set_modal(True)
-
-        self.fsw.show()
-
-    def history_entry_drag_data_received(self, widget, context, x, y, selection_data, info, time):
-        uris = selection_data.data.split()
-        if not uris:
-            context.finish(False, False, time)
-            return
-
-        for uri in uris:
-            path = gio.File(uri=uri).get_path()
-            if path:
-                break
-        else:
-            context.finish(False, False, time)
-            return
-
-        entry = self.__gentry.get_entry()
-        entry.set_text(path)
-        context.finish(True, False, time)
-        self._set_last_open(path)
-        entry.activate()
diff --git a/meld/vcview.py b/meld/vcview.py
index 043a181..db7a58d 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -329,7 +329,6 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
         self.current_path = None
         self.model.clear()
         self.fileentry.set_filename(location)
-        self.fileentry.prepend_history(location)
         it = self.model.add_entries(None, [location])
         self.treeview.grab_focus()
         self.treeview.get_selection().select_iter(it)
@@ -418,8 +417,10 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
                 if any(e.state != tree.STATE_NORMAL for e in entries):
                     self.treeview.expand_to_path(treepath)
 
-    def on_fileentry_activate(self, fileentry):
-        path = fileentry.get_full_path()
+    # TODO: This doesn't fire when the user selects a shortcut folder
+    def on_fileentry_file_set(self, fileentry):
+        directory = fileentry.get_file()
+        path = directory.get_path()
         self.set_location(path)
 
     def on_delete_event(self, appquit=0):
@@ -821,7 +822,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
             self.treeview.set_cursor(path)
 
     def on_refresh_activate(self, *extra):
-        self.on_fileentry_activate(self.fileentry)
+        self.on_fileentry_file_set(self.fileentry)
 
     def on_find_activate(self, *extra):
         self.treeview.emit("start-interactive-search")


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]