[gnome-menus] editor: port to pygobject-based introspection bindings
- From: Vincent Untz <vuntz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-menus] editor: port to pygobject-based introspection bindings
- Date: Wed, 6 Oct 2010 10:01:09 +0000 (UTC)
commit cd550ce70f391ba3511800a1e100213bc3ff6c45
Author: Vincent Untz <vuntz gnome org>
Date: Wed Oct 6 11:56:48 2010 +0200
editor: port to pygobject-based introspection bindings
https://bugzilla.gnome.org/show_bug.cgi?id=626256
simple-editor/GMenuSimpleEditor/main.py | 4 +-
simple-editor/GMenuSimpleEditor/maindialog.py | 86 +++++++++++---------
simple-editor/GMenuSimpleEditor/menufilewriter.py | 59 ++++++++------
simple-editor/GMenuSimpleEditor/menutreemodel.py | 65 +++++++---------
4 files changed, 109 insertions(+), 105 deletions(-)
---
diff --git a/simple-editor/GMenuSimpleEditor/main.py b/simple-editor/GMenuSimpleEditor/main.py
index 8d6d56b..de75f41 100644
--- a/simple-editor/GMenuSimpleEditor/main.py
+++ b/simple-editor/GMenuSimpleEditor/main.py
@@ -25,7 +25,7 @@ def main (args):
import gobject
from gobject.option import OptionParser, make_option
- import gtk
+ from gi.repository import Gtk
import maindialog
import config
@@ -49,4 +49,4 @@ def main (args):
print _("Simple Menu Editor %s") % (config.VERSION)
else:
dialog = maindialog.MenuEditorDialog (args)
- gtk.main ()
+ Gtk.main ()
diff --git a/simple-editor/GMenuSimpleEditor/maindialog.py b/simple-editor/GMenuSimpleEditor/maindialog.py
index 3cd9a31..eb8c596 100644
--- a/simple-editor/GMenuSimpleEditor/maindialog.py
+++ b/simple-editor/GMenuSimpleEditor/maindialog.py
@@ -17,7 +17,7 @@
#
import os.path
-import gtk
+from gi.repository import Gtk
import gmenu
from config import *
@@ -28,12 +28,12 @@ import menufilewriter
class MenuEditorDialog:
def __init__ (self, menu_files):
ui_file = os.path.join (UI_DIR, "gmenu-simple-editor.ui")
- self.ui = gtk.Builder()
+ self.ui = Gtk.Builder()
self.ui.set_translation_domain (PACKAGE)
self.ui.add_from_file(ui_file)
self.window = self.ui.get_object ("menu_editor_dialog")
- self.window.connect ("destroy", gtk.main_quit)
- self.window.set_default_response (gtk.RESPONSE_ACCEPT)
+ self.window.connect ("destroy", self.__dialog_destroyed)
+ self.window.set_default_response (Gtk.ResponseType.ACCEPT)
self.window.set_icon_name ("gnome-main-menu")
self.help_button = self.ui.get_object ("help_button")
@@ -59,96 +59,104 @@ class MenuEditorDialog:
def __revert_to_system_default (self, parent_iter = None):
model = self.menu_tree_model
- iter = model.iter_children (parent_iter)
- while iter:
- if model[iter][model.COLUMN_IS_ENTRY]:
- model[iter][model.COLUMN_USER_VISIBLE] = model[iter][model.COLUMN_SYSTEM_VISIBLE]
+ (has_iter, iter) = model.iter_children (parent_iter)
+ while has_iter:
+ if model.get_value (iter, model.COLUMN_IS_ENTRY):
+ visible = model.get_value (iter, model.COLUMN_SYSTEM_VISIBLE)
+ model.set_value (iter, model.COLUMN_USER_VISIBLE, visible)
else:
self.__revert_to_system_default (iter)
- iter = model.iter_next (iter)
+ has_iter = model.iter_next (iter)
+
+ def __dialog_destroyed (self, dialog):
+ Gtk.main_quit ()
def __dialog_response (self, dialog, response_id):
- if response_id == gtk.RESPONSE_REJECT:
+ if response_id == Gtk.ResponseType.REJECT:
self.__revert_to_system_default ()
- iter = self.menu_tree_model.get_iter_first ()
- while iter:
+ (has_iter, iter) = self.menu_tree_model.get_iter_first ()
+ while has_iter:
self.menu_file_writer.queue_sync (iter)
- iter = self.menu_tree_model.iter_next (iter)
+ has_iter = self.menu_tree_model.iter_next (iter)
return
dialog.destroy ()
- def __is_menu_tree_directory (self, model, iter):
- return not model[iter][self.menu_tree_model.COLUMN_IS_ENTRY]
+ def __is_menu_tree_directory (self, model, iter, data):
+ return not model.get_value(iter, self.menu_tree_model.COLUMN_IS_ENTRY)
def __setup_menus_tree (self):
- self.menus_model = self.menu_tree_model.filter_new ()
- self.menus_model.set_visible_func (self.__is_menu_tree_directory)
+ self.menus_model = self.menu_tree_model.filter_new (None)
+ self.menus_model.set_visible_func (self.__is_menu_tree_directory, None)
self.menus_tree.set_model (self.menus_model)
- self.menus_tree.get_selection ().set_mode (gtk.SELECTION_BROWSE)
+ self.menus_tree.get_selection ().set_mode (Gtk.SelectionMode.BROWSE)
self.menus_tree.get_selection ().connect ("changed", self.__menus_selection_changed)
self.menus_tree.set_headers_visible (False)
- column = gtk.TreeViewColumn (_("Name"))
+ column = Gtk.TreeViewColumn (_("Name"))
column.set_spacing (6)
- cell = gtk.CellRendererPixbuf ()
+ cell = Gtk.CellRendererPixbuf ()
column.pack_start (cell, False)
- column.set_attributes (cell, pixbuf = self.menu_tree_model.COLUMN_ICON)
+ column.add_attribute (cell, 'pixbuf', self.menu_tree_model.COLUMN_ICON)
- cell = gtk.CellRendererText ()
+ cell = Gtk.CellRendererText ()
column.pack_start (cell, True)
- column.set_attributes (cell, text = self.menu_tree_model.COLUMN_NAME)
+ column.add_attribute (cell, 'text', self.menu_tree_model.COLUMN_NAME)
self.menus_tree.append_column (column)
self.menus_tree.expand_all ()
def __setup_entries_list (self):
- self.entries_list.get_selection ().set_mode (gtk.SELECTION_SINGLE)
+ self.entries_list.get_selection ().set_mode (Gtk.SelectionMode.SINGLE)
self.entries_list.set_headers_visible (True)
- column = gtk.TreeViewColumn (_("Show"))
+ column = Gtk.TreeViewColumn (_("Show"))
self.entries_list.append_column (column)
- cell = gtk.CellRendererToggle ()
+ cell = Gtk.CellRendererToggle ()
cell.connect ("toggled", self.__on_hide_toggled)
column.pack_start (cell, False)
- column.set_attributes (cell, active = self.menu_tree_model.COLUMN_USER_VISIBLE)
+ column.add_attribute (cell, 'active', self.menu_tree_model.COLUMN_USER_VISIBLE)
- column = gtk.TreeViewColumn (_("Name"))
+ column = Gtk.TreeViewColumn (_("Name"))
column.set_spacing (6)
self.entries_list.append_column (column)
- cell = gtk.CellRendererPixbuf ()
+ cell = Gtk.CellRendererPixbuf ()
column.pack_start (cell, False)
- column.set_attributes (cell, pixbuf = self.menu_tree_model.COLUMN_ICON)
+ column.add_attribute (cell, 'pixbuf', self.menu_tree_model.COLUMN_ICON)
- cell = gtk.CellRendererText ()
+ cell = Gtk.CellRendererText ()
column.pack_start (cell, True)
- column.set_attributes (cell, text = self.menu_tree_model.COLUMN_NAME)
+ column.add_attribute (cell, 'text', self.menu_tree_model.COLUMN_NAME)
def __on_hide_toggled (self, toggle, path):
def toggle_value (model, iter, column):
- model[iter][column] = not model[iter][column]
+ value = model.get_value (iter, column)
+ model.set_value (iter, column, not value)
- child_path = self.entries_model.convert_path_to_child_path (path)
- child_iter = self.menu_tree_model.get_iter (child_path)
+ real_path = Gtk.TreePath.new_from_string (path)
+ child_path = self.entries_model.convert_path_to_child_path (real_path)
+ (has_iter, child_iter) = self.menu_tree_model.get_iter (child_path)
+ if not has_iter:
+ return
+
toggle_value (self.menu_tree_model, child_iter, self.menu_tree_model.COLUMN_USER_VISIBLE)
self.menu_file_writer.queue_sync (child_iter)
def __menus_selection_changed (self, selection):
- (model, iter) = selection.get_selected ()
- if not iter:
+ (is_selected, model, iter) = selection.get_selected ()
+ if not is_selected:
self.entries_list.set_model (None)
return
- if iter:
- iter = model.convert_iter_to_child_iter (iter)
+ iter = model.convert_iter_to_child_iter (iter)
self.entries_model = self.menu_tree_model.filter_new (self.menu_tree_model.get_path (iter))
self.entries_model.set_visible_column (self.menu_tree_model.COLUMN_IS_ENTRY)
diff --git a/simple-editor/GMenuSimpleEditor/menufilewriter.py b/simple-editor/GMenuSimpleEditor/menufilewriter.py
index 16bb46a..5f8ee75 100644
--- a/simple-editor/GMenuSimpleEditor/menufilewriter.py
+++ b/simple-editor/GMenuSimpleEditor/menufilewriter.py
@@ -20,6 +20,8 @@ import os
import errno
import pwd
import gobject
+from gi.repository import Gtk
+
import menutreemodel
DTD_DECLARATION = '<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"\n' \
@@ -83,7 +85,7 @@ class MenuFileWriter:
self.sync_idle_handlers = {}
def __del__ (self):
- for (id, iter) in self.sync_idle_handlers:
+ for (path, id) in self.sync_idle_handlers.items():
gobject.source_remove (id)
def __append_menu (self, contents, indent, iter, system_menu_file = None):
@@ -91,7 +93,7 @@ class MenuFileWriter:
orig_contents = contents
contents += indent + "<Menu>\n"
- contents += indent + " <Name>%s</Name>\n" % self.model[iter][self.model.COLUMN_ID]
+ contents += indent + " <Name>%s</Name>\n" % self.model.get_value (iter, self.model.COLUMN_ID)
if system_menu_file:
contents += indent + ' <MergeFile type="parent">%s</MergeFile>\n' % system_menu_file
@@ -99,19 +101,19 @@ class MenuFileWriter:
includes = []
excludes = []
- child_iter = self.model.iter_children (iter)
- while child_iter:
- if self.model[child_iter][self.model.COLUMN_IS_ENTRY]:
- desktop_file_id = self.model[child_iter][self.model.COLUMN_ID]
- system_visible = self.model[child_iter][self.model.COLUMN_SYSTEM_VISIBLE]
- user_visible = self.model[child_iter][self.model.COLUMN_USER_VISIBLE]
+ (has_iter, child_iter) = self.model.iter_children (iter)
+ while has_iter:
+ if self.model.get_value (child_iter, self.model.COLUMN_IS_ENTRY):
+ desktop_file_id = self.model.get_value (child_iter, self.model.COLUMN_ID)
+ system_visible = self.model.get_value (child_iter, self.model.COLUMN_SYSTEM_VISIBLE)
+ user_visible = self.model.get_value (child_iter, self.model.COLUMN_USER_VISIBLE)
if not system_visible and user_visible:
includes.append (desktop_file_id)
elif system_visible and not user_visible:
excludes.append (desktop_file_id)
- child_iter = self.model.iter_next (child_iter)
+ has_iter = self.model.iter_next (child_iter)
if len (includes) > 0:
contents += indent + " <Include>\n"
@@ -127,16 +129,16 @@ class MenuFileWriter:
contents += indent + " </Exclude>\n"
has_changes = True
- child_iter = self.model.iter_children (iter)
- while child_iter:
- if not self.model[child_iter][self.model.COLUMN_IS_ENTRY]:
+ (has_iter, child_iter) = self.model.iter_children (iter)
+ while has_iter:
+ if not self.model.get_value (child_iter, self.model.COLUMN_IS_ENTRY):
(contents, subdir_has_changes) = self.__append_menu (contents,
indent + " ",
child_iter)
if not has_changes:
has_changes = subdir_has_changes
- child_iter = self.model.iter_next (child_iter)
+ has_iter = self.model.iter_next (child_iter)
if has_changes:
return (contents + indent + "</Menu>\n", True)
@@ -144,7 +146,7 @@ class MenuFileWriter:
return (orig_contents, False)
def sync (self, iter):
- menu_file = self.model[iter][self.model.COLUMN_MENU_FILE]
+ menu_file = self.model.get_value (iter, self.model.COLUMN_MENU_FILE)
system_menu_file = menutreemodel.lookup_system_menu_file (menu_file)
(contents, has_changes) = self.__append_menu (DTD_DECLARATION,
@@ -161,32 +163,37 @@ class MenuFileWriter:
write_file (get_user_menu_file_path (menu_file), contents)
- def __sync_idle_handler_func (self, iter):
+ def __sync_idle_handler_func (self, path):
+ del self.sync_idle_handlers[path]
+
+ real_path = Gtk.TreePath.new_from_string (path)
+ (has_iter, iter) = self.model.get_iter(real_path)
+
+ if not has_iter:
+ return False
+
self.sync (iter)
- del self.sync_idle_handlers[iter]
return False
def queue_sync (self, iter):
def find_menu_file_parent (model, iter):
- if model[iter][model.COLUMN_MENU_FILE]:
+ if model.get_value (iter, model.COLUMN_MENU_FILE):
return iter
- parent_iter = model.iter_parent (iter)
- if not parent_iter:
+ (has_iter, parent_iter) = model.iter_parent (iter)
+ if not has_iter:
return None
return find_menu_file_parent (model, parent_iter)
menu_file_iter = find_menu_file_parent (self.model, iter)
- if not menu_file_iter:
+ if menu_file_iter is None:
return
menu_file_path = self.model.get_string_from_iter (menu_file_iter)
- for iter in self.sync_idle_handlers:
- path = self.model.get_string_from_iter (iter)
- if path == menu_file_path:
- return
- id = gobject.idle_add (self.__sync_idle_handler_func, menu_file_iter)
+ if self.sync_idle_handlers.has_key (menu_file_path):
+ return
- self.sync_idle_handlers[menu_file_iter] = id
+ id = gobject.idle_add (self.__sync_idle_handler_func, menu_file_path)
+ self.sync_idle_handlers[menu_file_path] = id
diff --git a/simple-editor/GMenuSimpleEditor/menutreemodel.py b/simple-editor/GMenuSimpleEditor/menutreemodel.py
index 214319e..a067ea9 100644
--- a/simple-editor/GMenuSimpleEditor/menutreemodel.py
+++ b/simple-editor/GMenuSimpleEditor/menutreemodel.py
@@ -18,8 +18,9 @@
import os
import os.path
-import gtk
-import gtk.gdk
+import gobject
+from gi.repository import Gtk
+from gi.repository import GdkPixbuf
import gmenu
def lookup_system_menu_file (menu_file):
@@ -39,7 +40,7 @@ def lookup_system_menu_file (menu_file):
def load_icon_from_path (icon_path):
if os.path.isfile (icon_path):
try:
- return gtk.gdk.pixbuf_new_from_file_at_size (icon_path, 24, 24)
+ return GdkPixbuf.new_from_file_at_size (icon_path, 24, 24)
except:
pass
return None
@@ -85,7 +86,7 @@ def load_icon (icon_theme, icon_value):
except:
return load_icon_from_data_dirs (icon_value)
-class MenuTreeModel (gtk.TreeStore):
+class MenuTreeModel (Gtk.TreeStore):
(
COLUMN_IS_ENTRY,
COLUMN_ID,
@@ -97,11 +98,11 @@ class MenuTreeModel (gtk.TreeStore):
) = range (7)
def __init__ (self, menu_files):
- gtk.TreeStore.__init__ (self, bool, str, str, gtk.gdk.Pixbuf, str, bool, bool)
+ Gtk.TreeStore.__init__ (self, bool, str, str, GdkPixbuf.Pixbuf, str, bool, bool)
self.entries_list_iter = None
- self.icon_theme = gtk.icon_theme_get_default ()
+ self.icon_theme = Gtk.IconTheme.get_default ()
if (len (menu_files) < 1):
menu_files = ["applications.menu", "settings.menu"]
@@ -124,28 +125,21 @@ class MenuTreeModel (gtk.TreeStore):
if not directory:
return
- iter = self.iter_children (parent_iter)
- while iter:
- if self[iter][self.COLUMN_ID] == directory.menu_id:
+ (has_iter, iter) = self.iter_children (parent_iter)
+ while has_iter:
+ if self.get_value(iter, self.COLUMN_ID) == directory.menu_id:
break
- iter = self.iter_next (iter)
+ has_iter = self.iter_next (iter)
- if not iter:
- iter = self.append (parent_iter)
-
- self[iter][self.COLUMN_IS_ENTRY] = False
- self[iter][self.COLUMN_ID] = directory.menu_id
- self[iter][self.COLUMN_NAME] = directory.name
- self[iter][self.COLUMN_ICON] = load_icon (self.icon_theme, directory.icon)
-
- if not menu_file is None:
- self[iter][self.COLUMN_MENU_FILE] = menu_file
+ if not has_iter:
+ row = (False, directory.menu_id, directory.name, load_icon (self.icon_theme, directory.icon), menu_file, False, False)
+ iter = self.append (parent_iter, row)
if system:
- self[iter][self.COLUMN_SYSTEM_VISIBLE] = True
+ self.set_value (iter, self.COLUMN_SYSTEM_VISIBLE, True)
else:
- self[iter][self.COLUMN_USER_VISIBLE] = True
-
+ self.set_value (iter, self.COLUMN_USER_VISIBLE, True)
+
for child_item in directory.contents:
if isinstance (child_item, gmenu.Directory):
self.__append_directory (child_item, iter, system, None)
@@ -153,24 +147,19 @@ class MenuTreeModel (gtk.TreeStore):
if not isinstance (child_item, gmenu.Entry):
continue
- child_iter = self.iter_children (iter)
- while child_iter:
+ (has_iter, child_iter) = self.iter_children (iter)
+ while has_iter:
if child_item.type == gmenu.TYPE_ENTRY and \
- self[child_iter][self.COLUMN_IS_ENTRY] and \
- self[child_iter][self.COLUMN_ID] == child_item.desktop_file_id:
+ self.get_value(child_iter, self.COLUMN_IS_ENTRY) and \
+ self.get_value(child_iter, self.COLUMN_ID) == child_item.desktop_file_id:
break
- child_iter = self.iter_next (child_iter)
-
- if not child_iter:
- child_iter = self.append (iter)
+ has_iter = self.iter_next (child_iter)
- self[child_iter][self.COLUMN_IS_ENTRY] = True
- self[child_iter][self.COLUMN_ID] = child_item.desktop_file_id
- self[child_iter][self.COLUMN_NAME] = child_item.display_name
- self[child_iter][self.COLUMN_ICON] = load_icon (self.icon_theme,
- child_item.icon)
+ if not has_iter:
+ row = (True, child_item.desktop_file_id, child_item.display_name, load_icon (self.icon_theme, child_item.icon), None, False, False)
+ child_iter = self.append (iter, row)
if system:
- self[child_iter][self.COLUMN_SYSTEM_VISIBLE] = not child_item.is_excluded
+ self.set_value (child_iter, self.COLUMN_SYSTEM_VISIBLE, not child_item.is_excluded,)
else:
- self[child_iter][self.COLUMN_USER_VISIBLE] = not child_item.is_excluded
+ self.set_value (child_iter, self.COLUMN_USER_VISIBLE, not child_item.is_excluded,)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]