[latexila] Structure action: shift right: error if sub-paragraph already exists



commit 20bea7406445f6782816ff789e8b407e899616a2
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Tue Jul 5 19:58:24 2011 +0200

    Structure action: shift right: error if sub-paragraph already exists

 TODO                        |    2 -
 src/document_structure.vala |    5 ++++
 src/structure.vala          |   48 +++++++++++++++++++++++++++++++++++++-----
 src/structure_model.vala    |   29 ++++++++++++++++++++++++++
 4 files changed, 76 insertions(+), 8 deletions(-)
---
diff --git a/TODO b/TODO
index 267960d..4e200f5 100644
--- a/TODO
+++ b/TODO
@@ -9,8 +9,6 @@ LaTeXila 2.2
 - Structure (list of chapters, sections, etc. to easily navigate in a document):
 	- Right click:
 		- shift left
-		- shift right:
-			- display a warning if a subparagraph already exists
 		- display a warning if the action fails (most probably when the structure data
 		  is not up-to-date)
 
diff --git a/src/document_structure.vala b/src/document_structure.vala
index 44aead1..3f742b6 100644
--- a/src/document_structure.vala
+++ b/src/document_structure.vala
@@ -561,6 +561,7 @@ public class DocumentStructure : GLib.Object
     // Actions: cut, copy, delete, select, comment, shift left/right
 
     public void do_action (StructAction action_type, TreeIter tree_iter)
+        throws StructError
     {
         if (action_type == StructAction.COMMENT)
         {
@@ -575,6 +576,10 @@ public class DocumentStructure : GLib.Object
 
         if (action_type == StructAction.SHIFT_RIGHT)
         {
+            if (_model.item_contains_subparagraph (tree_iter))
+                throw new StructError.GENERAL (
+                    _("The structure item already contains a sub-paragraph."));
+
             _doc.begin_user_action ();
             shift_right (tree_iter);
             _doc.end_user_action ();
diff --git a/src/structure.vala b/src/structure.vala
index e8394a7..3ba8df6 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -40,7 +40,7 @@ public enum StructType
     // Second part: "high-level" only
     TABLE,
     FIGURE,
-    N_TYPES
+    NB_TYPES
 }
 
 public enum StructAction
@@ -51,7 +51,8 @@ public enum StructAction
     SELECT,
     COMMENT,
     SHIFT_LEFT,
-    SHIFT_RIGHT
+    SHIFT_RIGHT,
+    NB_ACTIONS
 }
 
 public class Structure : VBox
@@ -84,6 +85,7 @@ public class Structure : VBox
 
     private static string[] _icons = null;
     private static string[] _names = null;
+    private static string[] _action_names = null;
 
     public Structure (MainWindow main_window, UIManager ui_manager)
     {
@@ -515,7 +517,7 @@ public class Structure : VBox
 
 
     /*************************************************************************/
-    // Right-click: actions callbacks
+    // Right-click: actions
 
     private void show_popup_menu (Gdk.EventButton? event)
     {
@@ -548,14 +550,48 @@ public class Structure : VBox
 
         return_if_fail (selected_row != -1);
 
-        _document_structure.do_action (action_type, selected_iter);
+        try
+        {
+            _document_structure.do_action (action_type, selected_iter);
+        }
+        catch (StructError e)
+        {
+            MessageDialog dialog = new MessageDialog (_main_window,
+                DialogFlags.DESTROY_WITH_PARENT,
+                MessageType.ERROR,
+                ButtonsType.OK,
+                _("Structure action error: %s"),
+                get_action_name (action_type));
+
+            dialog.secondary_text = e.message;
+
+            dialog.run ();
+            dialog.destroy ();
+        }
+    }
+
+    private static string get_action_name (StructAction action_type)
+    {
+        if (_action_names == null)
+        {
+            _action_names = new string[StructAction.NB_ACTIONS];
+            _action_names[StructAction.CUT]         = _("cut");
+            _action_names[StructAction.COPY]        = _("copy");
+            _action_names[StructAction.DELETE]      = _("delete");
+            _action_names[StructAction.SELECT]      = _("select");
+            _action_names[StructAction.COMMENT]     = _("comment");
+            _action_names[StructAction.SHIFT_LEFT]  = _("shift left");
+            _action_names[StructAction.SHIFT_RIGHT] = _("shift right");
+        }
+
+        return _action_names[action_type];
     }
 
     public static string get_icon_from_type (StructType type)
     {
         if (_icons == null)
         {
-            _icons = new string[StructType.N_TYPES];
+            _icons = new string[StructType.NB_TYPES];
             _icons[StructType.PART]         = "tree_part";
             _icons[StructType.CHAPTER]      = "tree_chapter";
             _icons[StructType.SECTION]      = "tree_section";
@@ -579,7 +615,7 @@ public class Structure : VBox
     {
         if (_names == null)
         {
-            _names = new string[StructType.N_TYPES];
+            _names = new string[StructType.NB_TYPES];
             _names[StructType.PART]         = _("Part");
             _names[StructType.CHAPTER]      = _("Chapter");
             _names[StructType.SECTION]      = _("Section");
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 0dc18f4..2b713b3 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -493,6 +493,35 @@ public class StructureModel : TreeModel, GLib.Object
         reinsert_node (node);
     }
 
+    public bool item_contains_subparagraph (TreeIter iter)
+    {
+        return_val_if_fail (iter_is_valid (iter), false);
+
+        unowned Node<StructData?> node = get_node_from_iter (iter);
+        return node_contains_subparagraph (node);
+    }
+
+    private bool node_contains_subparagraph (Node<StructData?> node)
+    {
+        StructType type = node.data.type;
+
+        if (type == StructType.SUBPARAGRAPH)
+            return true;
+
+        if (! Structure.is_section (type))
+            return false;
+
+        unowned Node<StructData?>? child = node.first_child ();
+        while (child != null)
+        {
+            if (node_contains_subparagraph (child))
+                return true;
+            child = child.next_sibling ();
+        }
+
+        return false;
+    }
+
     private void insert_node (Node<StructData?> node, bool force_first_child = false)
     {
         new_stamp ();



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