[wiican: 4/16] [m][ui] Mark a mapping as default mapping using the mapping manager dialog



commit d51fd32e43acf2d12fc6c26dcd71db5614423d44
Author: J. FÃlix OntaÃÃn <fontanon emergya es>
Date:   Wed Feb 22 22:53:52 2012 +0100

    [m][ui] Mark a mapping as default mapping using the mapping manager dialog

 data/mapping.ui         |   41 +++++++++++++++++++++----
 wiican/ui/managerdlg.py |   77 ++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 98 insertions(+), 20 deletions(-)
---
diff --git a/data/mapping.ui b/data/mapping.ui
index dc15c63..05dd6dc 100644
--- a/data/mapping.ui
+++ b/data/mapping.ui
@@ -1,6 +1,13 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <requires lib="gtk+" version="2.16"/>
+  <!-- interface-requires gtk+ 3.0 -->
+  <object class="GtkCellRendererPixbuf" id="autostart_cell"/>
+  <object class="GtkToggleAction" id="autostart_toggleaction">
+    <property name="label" translatable="yes">Autostart running this mapping</property>
+    <property name="short_label" translatable="yes">Autostart</property>
+    <property name="tooltip" translatable="yes">Launch this mapping automatically at desktop session start</property>
+    <property name="icon_name">emblem-favorite</property>
+  </object>
   <object class="GtkAction" id="delete_action">
     <property name="label" translatable="yes">Delete mapping</property>
     <property name="short_label" translatable="yes">_Delete</property>
@@ -608,6 +615,19 @@ Changes will be saved in a new mapping.</property>
                   </packing>
                 </child>
                 <child>
+                  <object class="GtkToggleToolButton" id="toolbutton7">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="related_action">autostart_toggleaction</property>
+                    <property name="label" translatable="yes">toolbutton7</property>
+                    <property name="use_underline">True</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="homogeneous">True</property>
+                  </packing>
+                </child>
+                <child>
                   <object class="GtkSeparatorToolItem" id="toolbutton6">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
@@ -667,8 +687,9 @@ Changes will be saved in a new mapping.</property>
                         <property name="can_focus">True</property>
                         <property name="model">mapping_store</property>
                         <property name="headers_clickable">False</property>
-                        <property name="search_column">1</property>
-                        <property name="tooltip_column">2</property>
+                        <property name="search_column">2</property>
+                        <property name="tooltip_column">3</property>
+                        <signal name="cursor-changed" handler="mapping_list_cursor_changed_cb" swapped="no"/>
                         <signal name="key-release-event" handler="mapping_list_key_release_event_cb" swapped="no"/>
                         <signal name="drag-data-received" handler="mapping_list_drag_data_received_cb" swapped="no"/>
                         <signal name="row-activated" handler="mapping_list_row_activated_cb" swapped="no"/>
@@ -683,7 +704,7 @@ Changes will be saved in a new mapping.</property>
                                 <signal name="toggled" handler="visible_cell_toggled_cb" swapped="no"/>
                               </object>
                               <attributes>
-                                <attribute name="active">3</attribute>
+                                <attribute name="activatable">4</attribute>
                               </attributes>
                             </child>
                           </object>
@@ -693,6 +714,12 @@ Changes will be saved in a new mapping.</property>
                             <property name="resizable">True</property>
                             <property name="title">Name</property>
                             <child>
+                              <object class="GtkCellRendererPixbuf" id="autostart_cell1"/>
+                              <attributes>
+                                <attribute name="pixbuf">1</attribute>
+                              </attributes>
+                            </child>
+                            <child>
                               <object class="GtkCellRendererPixbuf" id="icon_cell"/>
                               <attributes>
                                 <attribute name="pixbuf">0</attribute>
@@ -701,8 +728,8 @@ Changes will be saved in a new mapping.</property>
                             <child>
                               <object class="GtkCellRendererText" id="mappingname_cell"/>
                               <attributes>
-                                <attribute name="markup">1</attribute>
-                                <attribute name="text">1</attribute>
+                                <attribute name="markup">2</attribute>
+                                <attribute name="text">2</attribute>
                               </attributes>
                             </child>
                           </object>
@@ -787,6 +814,8 @@ Changes will be saved in a new mapping.</property>
     <columns>
       <!-- column-name icon -->
       <column type="GdkPixbuf"/>
+      <!-- column-name autostart_icon -->
+      <column type="GdkPixbuf"/>
       <!-- column-name name -->
       <column type="gchararray"/>
       <!-- column-name comment -->
diff --git a/wiican/ui/managerdlg.py b/wiican/ui/managerdlg.py
index 5041f46..c73aa99 100644
--- a/wiican/ui/managerdlg.py
+++ b/wiican/ui/managerdlg.py
@@ -28,11 +28,16 @@ from wiican.mapping import Mapping, MappingManager, MappingManagerError
 from wiican.ui.editordlg import MappingEditorDialog
 from wiican.ui import UIPrefStore
 
-ICON_COL, NAME_COL, COMMENT_COL, VISIBLE_COL, MAPPING_ID_COL = range(5)
+ICON_COL, AUTOSTART_COL, NAME_COL, COMMENT_COL, VISIBLE_COL, \
+    MAPPING_ID_COL = range(6)
 
 mapping_manager = MappingManager()
 pref_store = UIPrefStore()
 
+# Load icons
+theme = gtk.icon_theme_get_default()
+autostart_icon = theme.load_icon('emblem-favorite', 24, 0)
+
 class MappingManagerDialog(object):
     mapping_filter = gtk.FileFilter()
     mapping_filter.set_name(_('Wiican Mapping Package'))
@@ -44,13 +49,14 @@ class MappingManagerDialog(object):
         if not builder.add_objects_from_file(MAPPING_UI, 
                 ['mapping_manager_dlg', 'image3', 'image4', 'mapping_store',
                     'new_action', 'edit_action', 'delete_action', 
-                    'import_action', 'export_action']):
+                    'import_action', 'export_action', 'autostart_toggleaction']):
             raise 'Cant load %s' % MAPPING_UI
         builder.connect_signals(self)
 
         self.mapping_dlg = builder.get_object('mapping_manager_dlg')
         self.mapping_store = builder.get_object('mapping_store')
         self.mapping_list = builder.get_object('mapping_list')
+        self.autostart_toggleaction = builder.get_object('autostart_toggleaction')
 
         # Enable Drag&Drop
         target_entries = [('catalog', gtk.TARGET_SAME_WIDGET, 0)]
@@ -63,13 +69,17 @@ class MappingManagerDialog(object):
             # Prevent for loading a not found icon path
             icon_path = mapping.get_icon()
             if not os.path.exists(icon_path): icon_path = ICON_DEFAULT
-            
+
             icon = gtk.gdk.pixbuf_new_from_file_at_size(icon_path, 24, 24)
             visible = mapping_manager.is_visible(mapping_id)
-            
+
+            autostart = None
+            if mapping_manager.is_default_mapping(mapping_id):
+                autostart = autostart_icon
+
             mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                 mapping.get_comment())
-            self.mapping_store.append([icon, mapping_name, 
+            self.mapping_store.append([icon, autostart, mapping_name, 
                 mapping.get_comment(), visible, mapping_id])
 
         def catch_window_size(widget, allocate):
@@ -78,6 +88,11 @@ class MappingManagerDialog(object):
 
         self.mapping_dlg.connect('size_allocate', catch_window_size)
 
+        # Hack? Save this signal id for blocking at
+        # MappingManagerDialog::mapping_list_cursor_changed_cb()
+        self.autostart_toggle_signal_id = self.autostart_toggleaction.connect('toggled',
+                self.autostart_toggle_action_toggled_cb)
+
         pref_store.loadconf()
         self.mapping_dlg.resize(pref_store.options['mapping_dlg_width'], 
             pref_store.options['mapping_dlg_height'])
@@ -104,8 +119,8 @@ class MappingManagerDialog(object):
 
             mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                     mapping.get_comment())
-            self.mapping_store.append([icon, mapping_name,
-                mapping.get_comment(), True, mapping_id])
+            self.mapping_store.append([icon, None, mapping_name,
+                mapping.get_comment(), True, None, mapping_id])
 
         mapping_editor_dlg.destroy()
 
@@ -129,8 +144,8 @@ class MappingManagerDialog(object):
 
                     mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                             mapping.get_comment())
-                    self.mapping_store.append([icon, mapping_name,
-                        mapping.get_comment(), True, mapping_id])
+                    self.mapping_store.append([icon, None, mapping_name,
+                        mapping.get_comment(), True, None, mapping_id])
                 else:
                     mapping_manager[mapping_id] = new_mapping
                     model[selected][ICON_COL] = gtk.gdk.pixbuf_new_from_file_at_size(new_mapping.get_icon(), 
@@ -144,6 +159,19 @@ class MappingManagerDialog(object):
     def mapping_list_row_activated_cb(self, widget, path, view_column):
         self.edit_action_activate_cb(widget)
 
+    def mapping_list_cursor_changed_cb(self, user_data):
+        selection = self.mapping_list.get_selection()
+        model, selected = selection.get_selected()
+
+        if selected is not None:
+            mapping_id = model[selected][MAPPING_ID_COL]
+            is_def_mapping = mapping_manager.is_default_mapping(mapping_id)
+
+            # Hack? Prevent triggering autostart_toggle_action_toggled_cb
+            self.autostart_toggleaction.handler_block(self.autostart_toggle_signal_id)
+            self.autostart_toggleaction.set_active(is_def_mapping)
+            self.autostart_toggleaction.handler_unblock(self.autostart_toggle_signal_id)
+
     def delete_action_activate_cb(self, widget):
         selection = self.mapping_list.get_selection()
         model, selected = selection.get_selected()
@@ -163,6 +191,8 @@ class MappingManagerDialog(object):
                 mapping_id = model[selected][MAPPING_ID_COL]
                 try:
                     del(mapping_manager[mapping_id])
+                    if mapping_manager.is_default_mapping(mapping_id):
+                        mapping_manager.set_default_mapping(None)
                 except MappingManagerError, e:
                     delete_dlg.destroy()
                     error_importing_dlg = gtk.MessageDialog(parent = self.mapping_dlg,
@@ -219,7 +249,7 @@ class MappingManagerDialog(object):
 
         mapping_name = '<b>%s</b>\n<i>%s</i>' % (mapping.get_name(), 
                 mapping.get_comment())
-        self.mapping_store.append([icon, mapping_name,
+        self.mapping_store.append([icon, None, mapping_name,
             mapping.get_comment(), True, mapping_id])
 
         pref_store.options['import_dir'] = import_dlg.get_current_folder()
@@ -256,7 +286,26 @@ class MappingManagerDialog(object):
         mapping_id = self.mapping_store[path][MAPPING_ID_COL]
         mapping_manager.set_visible(mapping_id, not self.mapping_store[path][VISIBLE_COL])
         self.mapping_store[path][VISIBLE_COL] = not self.mapping_store[path][VISIBLE_COL]
-              
+
+    def autostart_toggle_action_toggled_cb(self, widget):
+        selection = self.mapping_list.get_selection()
+        model, selected = selection.get_selected()
+
+        if selected is not None:
+            mapping_id = model[selected][MAPPING_ID_COL]
+
+            if mapping_manager.is_default_mapping(mapping_id):
+                self.autostart_toggleaction.set_active(False)
+                mapping_manager.set_default_mapping(None)
+            else:
+                # This is really hacky
+                # TODO: autostart as radiobuttons-type widgets
+                for item in model: item[AUTOSTART_COL] = None
+
+                self.autostart_toggleaction.set_active(True)
+                model[selected][AUTOSTART_COL] = autostart_icon
+                mapping_manager.set_default_mapping(mapping_id)
+
     def up_btn_clicked_cb(self, widget):
         # From PyGTK FAQ Entry 13.51
         # http://faq.pygtk.org/index.py?req=show&file=faq13.051.htp
@@ -296,9 +345,9 @@ class MappingManagerDialog(object):
 
         selection = self.mapping_list.get_selection()
         model, selected = selection.get_selected()
-        data = [model[selected][ICON_COL], model[selected][NAME_COL], \
-            model[selected][COMMENT_COL], model[selected][VISIBLE_COL], \
-            model[selected][MAPPING_ID_COL]]
+        data = [model[selected][ICON_COL], model[selected][AUTOSTART_COL], \
+            model[selected][NAME_COL], model[selected][COMMENT_COL], \
+            model[selected][VISIBLE_COL], model[selected][MAPPING_ID_COL]]
 
         drop_info = treeview.get_dest_row_at_pos(x, y)
         if drop_info:



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