[glom/glom-1-20] LayoutGroup: Add a more useful remove_field().



commit 4d6ab8089b031f513ed0acc5893d29b8d8ea8ea8
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Nov 29 11:57:51 2011 +0100

    LayoutGroup: Add a more useful remove_field().
    
    * glom/libglom/data_structure/layout/layoutgroup.[h|cc]:
      Add a remove_field(parent_table_name, table_name, field_name) method overload,
      deprecating the existing method overloads.
    * glom/libglom/document/document.cc: Adapt.
    * tests/test_document_load_and_change.cc: Test a different example,
      so we can check for a related field on a layout.

 ChangeLog                                         |   11 +++++++
 glom/libglom/data_structure/layout/layoutgroup.cc |   31 +++++++++++++++++++++
 glom/libglom/data_structure/layout/layoutgroup.h  |   16 +++++++++++
 glom/libglom/document/document.cc                 |   18 +++++-------
 tests/test_document_load_and_change.cc            |   18 ++++++------
 5 files changed, 74 insertions(+), 20 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 73e6472..3af91ad 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2011-11-29  Murray Cumming  <murrayc murrayc com>
 
+	LayoutGroup: Add a more useful remove_field().
+
+	* glom/libglom/data_structure/layout/layoutgroup.[h|cc]:
+  Add a remove_field(parent_table_name, table_name, field_name) method overload,
+  deprecating the existing method overloads.
+	* glom/libglom/document/document.cc: Adapt.
+	* tests/test_document_load_and_change.cc: Test a different example,
+  so we can check for a related field on a layout.
+
+2011-11-29  Murray Cumming  <murrayc murrayc com>
+
 	LayoutGroup: Deprecate the old has_field() method and do not use it.
 
 	* glom/libglom/data_structure/layout/layoutgroup.h:
diff --git a/glom/libglom/data_structure/layout/layoutgroup.cc b/glom/libglom/data_structure/layout/layoutgroup.cc
index 5d6229a..e91fbb0 100644
--- a/glom/libglom/data_structure/layout/layoutgroup.cc
+++ b/glom/libglom/data_structure/layout/layoutgroup.cc
@@ -342,6 +342,37 @@ void LayoutGroup::remove_field(const Glib::ustring& table_name, const Glib::ustr
   }
 }
 
+void LayoutGroup::remove_field(const Glib::ustring& parent_table_name, const Glib::ustring& table_name, const Glib::ustring& field_name)
+{
+  //Look at each item:
+  LayoutGroup::type_list_items::iterator iterItem = m_list_items.begin();
+  while(iterItem != m_list_items.end())
+  {
+    sharedptr<LayoutItem> item = *iterItem;
+    sharedptr<LayoutItem_Field> field_item = sharedptr<LayoutItem_Field>::cast_dynamic(item);
+    if(field_item)
+    {
+      if(field_item->get_table_used(parent_table_name) == table_name)
+      {
+        if(field_item->get_name() == field_name)
+        {
+          m_list_items.erase(iterItem);
+          iterItem = m_list_items.begin(); //Start again, because we changed the container.AddDel
+          continue;
+        }
+      }
+    }
+    else
+    {
+      sharedptr<LayoutGroup> sub_group = sharedptr<LayoutGroup>::cast_dynamic(item);
+      if(sub_group)
+        sub_group->remove_field(parent_table_name, table_name, field_name);
+    }
+
+    ++iterItem;
+  }
+}
+
 void LayoutGroup::change_related_field_item_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new)
 {
   //Look at each item:
diff --git a/glom/libglom/data_structure/layout/layoutgroup.h b/glom/libglom/data_structure/layout/layoutgroup.h
index 8ed5086..1bcd5fe 100644
--- a/glom/libglom/data_structure/layout/layoutgroup.h
+++ b/glom/libglom/data_structure/layout/layoutgroup.h
@@ -80,13 +80,29 @@ public:
   void remove_item(const sharedptr<LayoutItem>& item);
 
   /** Remove any instance of the field (from the current table) from the layout.
+   * @deprecated Use remove_field(parent_table_name, table_name, field_name) instead.
    */
   virtual void remove_field(const Glib::ustring& field_name);
 
+  //TODO: Remove this unused method overload, and remove virtual from all these method overloads.
   /** Remove any instance of the related field from the layout.
+   *
+   * @param table_name The table to which the field, specified by @a field_name, belongs.
+   * @param field_name The name of the field to search for.
+   *
+   * @deprecated Use remove_field(parent_table_name, table_name, field_name) instead.
    */
   virtual void remove_field(const Glib::ustring& table_name, const Glib::ustring& field_name);
+  
+  /** Remove any instance of the field from the layout.
+   *
+   * @param parent_table_name The table to which this layout belongs.
+   * @param table_name The table to which the field, specified by @a field_name, belongs.
+   * @param field_name The name of the field to remove.
+   */
+  void remove_field(const Glib::ustring& parent_table_name, const Glib::ustring& table_name, const Glib::ustring& field_name);
 
+  //TODO: Do these need to be virtual?
   virtual void change_field_item_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new);
   virtual void change_related_field_item_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new);
 
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index a7ca56c..b9f1493 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -763,15 +763,14 @@ void Document::remove_field(const Glib::ustring& table_name, const Glib::ustring
       LayoutInfo& layout_info = *iterLayouts;
       for(type_list_layout_groups::iterator iter = layout_info.m_layout_groups.begin(); iter != layout_info.m_layout_groups.end(); ++iter)
       {
-        if(!(*iter))
+        sharedptr<LayoutGroup> group = *iter;
+        if(!group)
           continue;
 
-        //Remove regular fields if the field is in this layout's table:
-        if(info.m_info->get_name() == table_name)
-          (*iter)->remove_field(field_name);
 
-        //Remove the field wherever it is a related field:
-        (*iter)->remove_field(table_name, field_name);
+        //Remove regular field from the layout:
+        const Glib::ustring layout_table_name = info.m_info->get_name();
+        group->remove_field(layout_table_name, table_name, field_name);
       }
     }
 
@@ -782,11 +781,8 @@ void Document::remove_field(const Glib::ustring& table_name, const Glib::ustring
       sharedptr<LayoutGroup> group = report->m_layout_group;
 
       //Remove regular fields if the field is in this layout's table:
-      if(info.m_info->get_name() == table_name)
-        group->remove_field(field_name);
-
-      //Remove the field wherever it is a related field:
-      group->remove_field(table_name, field_name);
+      const Glib::ustring layout_table_name = info.m_info->get_name();
+      group->remove_field(layout_table_name, table_name, field_name);
     }
   }
 }
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
index 932bb9e..cbe5f40 100644
--- a/tests/test_document_load_and_change.cc
+++ b/tests/test_document_load_and_change.cc
@@ -55,7 +55,7 @@ int main()
   {
     const std::string path =
        Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
-         "example_music_collection.glom");
+         "example_smallbusiness.glom");
     uri = Glib::filename_to_uri(path);
   }
   catch(const Glib::ConvertError& ex)
@@ -84,8 +84,8 @@ int main()
   document.set_allow_autosave(false);
 
   //Change a field name throughout the document:
-  const Glib::ustring table_name = "songs";
-  const Glib::ustring field_name_original = "song_id";
+  const Glib::ustring table_name = "products";
+  const Glib::ustring field_name_original = "product_id";
   const Glib::ustring field_name_new = "newfieldname";
   document.change_field_name(table_name, field_name_original, field_name_new);
 
@@ -104,14 +104,14 @@ int main()
   }
 
   //Check that the original field name is no longer used in the relationship:
-  const Glom::sharedptr<const Glom::Relationship> relationship = document.get_relationship("songs", "album");
+  const Glom::sharedptr<const Glom::Relationship> relationship = document.get_relationship("invoice_lines", "products");
   if(!relationship)
   {
     std::cerr << "Failure: The relationship could not be found in the document." << std::endl;
     return false;
   }
 
-  if(relationship->get_from_field() == field_name_original)
+  if(relationship->get_to_field() == field_name_original)
   {
     std::cerr << "Failure: The relationship still uses the original field name." << std::endl;
     return false;
@@ -121,16 +121,16 @@ int main()
   const std::vector<Glib::ustring> table_names = document.get_table_names();
   for(std::vector<Glib::ustring>::const_iterator iter = table_names.begin(); iter != table_names.end(); ++iter)
   {
-    const Glib::ustring table_name = *iter;
+    const Glib::ustring layout_table_name = *iter;
     const Glom::Document::type_list_layout_groups groups = 
-      document.get_data_layout_groups("details", table_name);
+      document.get_data_layout_groups("details", layout_table_name);
 
     for(Glom::Document::type_list_layout_groups::const_iterator iter = groups.begin(); iter != groups.end(); ++iter)
     {
       const Glom::sharedptr<Glom::LayoutGroup> group = *iter;
-      if(group->has_field(field_name_original))
+      if(group->has_field(layout_table_name, table_name, field_name_original))
       {
-        std::cerr << "Failure: The field is still used on a layout for table: " << table_name << std::endl;
+        std::cerr << "Failure: The field is still used on a layout for table: " << layout_table_name << std::endl;
         return false;
       }
     }



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