[glom/feature_choices_show_all] Formatting: Add get_choices_related() overload to reduce copy/pasted code.



commit c9e654f7385e08c320f3e9fcae097a23551f5993
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Aug 6 17:22:41 2010 +0200

    Formatting: Add get_choices_related() overload to reduce copy/pasted code.
    
    * glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
      Added a get_choices_related() overload that returns the LayoutItem_Fields
      instead of just the field names. This reduces the copy/pasted code in the
      callers, and makes it easier to support related fields in future.
    * glom/libglom/utils.[h|cc]:
    * glom/mode_data/datawidget/combochoices.[h|cc]:
    * glom/utility_widgets/db_adddel/db_adddel.cc: Adapted and made necessary
      const corrections.

 ChangeLog                                          |   13 +++++++
 .../data_structure/layout/fieldformatting.cc       |   36 ++++++++++++++++++-
 .../data_structure/layout/fieldformatting.h        |   13 ++++---
 glom/libglom/utils.cc                              |   36 +++-----------------
 glom/libglom/utils.h                               |    4 +-
 glom/mode_data/datawidget/combochoices.cc          |   19 +---------
 glom/mode_data/datawidget/combochoices.h           |    4 +-
 glom/utility_widgets/db_adddel/db_adddel.cc        |    8 ++--
 8 files changed, 70 insertions(+), 63 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5eb32f5..a3142eb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-08-06  Murray Cumming  <murrayc murrayc-desktop>
+
+	Formatting: Add get_choices_related() overload to reduce copy/pasted code.
+
+	* glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
+  Added a get_choices_related() overload that returns the LayoutItem_Fields
+  instead of just the field names. This reduces the copy/pasted code in the
+  callers, and makes it easier to support related fields in future.
+	* glom/libglom/utils.[h|cc]:
+	* glom/mode_data/datawidget/combochoices.[h|cc]:
+	* glom/utility_widgets/db_adddel/db_adddel.cc: Adapted and made necessary
+  const corrections.
+
 2010-08-06  Murray Cumming  <murrayc murrayc com>
 
 	Choices: Fix !show-all lists.
diff --git a/glom/libglom/data_structure/layout/fieldformatting.cc b/glom/libglom/data_structure/layout/fieldformatting.cc
index 5de8380..3c02151 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.cc
+++ b/glom/libglom/data_structure/layout/fieldformatting.cc
@@ -21,6 +21,7 @@
 #include "fieldformatting.h"
 #include <libglom/data_structure/layout/fieldformatting.h>
 #include <libglom/data_structure/glomconversions.h>
+#include <libglom/document/document.h>
 #include <glibmm/i18n.h>
 
 const guint MULTILINE_TEXT_DEFAULT_HEIGHT_LINES = 6;
@@ -79,7 +80,7 @@ bool FieldFormatting::operator==(const FieldFormatting& src) const
     (m_text_multiline_height_lines == src.m_text_multiline_height_lines) &&
     (m_text_font == src.m_text_font) &&
     (m_text_color_foreground == src.m_text_color_foreground) &&
-    (m_text_color_background == src.m_text_color_background) && 
+    (m_text_color_background == src.m_text_color_background) &&
     (m_horizontal_alignment == src.m_horizontal_alignment) &&
     (m_choices_related_show_all == src.m_choices_related_show_all);
 }
@@ -255,6 +256,37 @@ void FieldFormatting::set_choices_related(const sharedptr<const Relationship>& r
   m_choices_related_show_all = show_all;
 }
 
+void FieldFormatting::get_choices_related(const Document* document, sharedptr<const Relationship>& relationship, sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& field_second, bool& show_all) const
+{
+  //Initialize output parameters:
+  field.clear();
+  field_second.clear();
+
+  Glib::ustring choice_field, choice_second;
+  get_choices_related(relationship, choice_field, choice_second, show_all);
+
+  if(!relationship)
+    return;
+
+  if(choice_field.empty())
+    return;
+
+  const Glib::ustring to_table = relationship->get_to_table();
+
+  sharedptr<LayoutItem_Field> temp = sharedptr<LayoutItem_Field>::create();
+  sharedptr<const Field> field_details = document->get_field(to_table, choice_field);
+  temp->set_full_field_details(field_details);
+  field = temp;
+
+  if(!choice_second.empty())
+  {
+    sharedptr<LayoutItem_Field> temp = sharedptr<LayoutItem_Field>::create();
+    sharedptr<const Field> field_details = document->get_field(to_table, choice_second);
+    temp->set_full_field_details(field_details);
+    field_second = temp;
+  }
+}
+
 void FieldFormatting::change_field_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new)
 {
   //Update choices:
@@ -264,7 +296,7 @@ void FieldFormatting::change_field_name(const Glib::ustring& table_name, const G
        m_choices_related_field = field_name_new;
 
     if(m_choices_related_field_second == field_name)
-       m_choices_related_field_second = field_name_new; 
+       m_choices_related_field_second = field_name_new;
   }
 }
 
diff --git a/glom/libglom/data_structure/layout/fieldformatting.h b/glom/libglom/data_structure/layout/fieldformatting.h
index cb19b0d..b9e5a55 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.h
+++ b/glom/libglom/data_structure/layout/fieldformatting.h
@@ -30,6 +30,9 @@
 namespace Glom
 {
 
+class Document;
+class LayoutItem_Field;
+
 //TODO: This should probably be renamed to Formatting, because it is used for static text items too.
 class FieldFormatting : public UsesRelationship //The UsesRelationship base has the relationship for the choices.
 {
@@ -54,9 +57,9 @@ public:
   virtual type_list_values get_choices_custom() const;
   virtual void set_choices_custom(const type_list_values& choices);
 
-  /** Discover whether the entered data should only be one of the available 
+  /** Discover whether the entered data should only be one of the available
    * choices.
-   * @param [out] as_radio_buttons: Whether the choices should be displayed as 
+   * @param [out] as_radio_buttons: Whether the choices should be displayed as
    * radio buttons instead of a combo box.
    */
   bool get_choices_restricted(bool& as_radio_buttons) const;
@@ -68,6 +71,9 @@ public:
   void get_choices_related(sharedptr<const Relationship>& relationship_name, Glib::ustring& field, Glib::ustring& field_second, bool& show_all) const;
   void set_choices_related(const sharedptr<const Relationship>& relationship_name, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all);
 
+
+  void get_choices_related(const Document* document, sharedptr<const Relationship>& relationship_name, sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& field_second, bool& show_all) const;
+
   /** Get whether the text should be displayed with multiple lines in the
    * details view. Text is displayed with a single line in the list view.
    * @returns whether the text should be displayed with multiple lines
@@ -167,6 +173,3 @@ private:
 } //namespace Glom
 
 #endif //GLOM_DATASTRUCTURE_FIELDFORMATTING_H
-
-
-
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index 8e73617..e4669a2 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -480,14 +480,14 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_select_with_key(const Glib
     sharedptr<const Relationship>(), sort_clause, limit);
 }
 
-Utils::type_list_values_with_second Utils::get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field, sharedptr<LayoutItem_Field>& layout_choice_first, sharedptr<LayoutItem_Field>& layout_choice_second)
+Utils::type_list_values_with_second Utils::get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second)
 {
   return get_choice_values(document, field,
     Gnome::Gda::Value() /* means get all with no WHERE clause */,
     layout_choice_first, layout_choice_second);
 }
 
-Utils::type_list_values_with_second Utils::get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value, sharedptr<LayoutItem_Field>& layout_choice_first, sharedptr<LayoutItem_Field>& layout_choice_second)
+Utils::type_list_values_with_second Utils::get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value, sharedptr<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second)
 {
   //Initialize output parameters:
   layout_choice_first = sharedptr<LayoutItem_Field>();
@@ -510,7 +510,7 @@ Utils::type_list_values_with_second Utils::get_choice_values(const Document* doc
   sharedptr<const Relationship> choice_relationship;
   Glib::ustring choice_field, choice_second;
   bool choice_show_all = false;
-  format.get_choices_related(choice_relationship, choice_field, choice_second, choice_show_all);
+  format.get_choices_related(document, choice_relationship, layout_choice_first, layout_choice_second, choice_show_all);
 
   if(!choice_relationship)
   {
@@ -518,38 +518,12 @@ Utils::type_list_values_with_second Utils::get_choice_values(const Document* doc
     return result;
   }
 
-  const Glib::ustring to_table = choice_relationship->get_to_table();
-  sharedptr<const Field> field_details = document->get_field(to_table, choice_field);
-  if(!field_details)
-  {
-    std::cerr << G_STRFUNC << ": !field_details." << std::endl;
-    return result;
-  }
-
-  layout_choice_first = sharedptr<LayoutItem_Field>::create();
-  layout_choice_first->set_full_field_details(field_details);
-
-
-  if(!choice_second.empty())
-  {
-    sharedptr<const Field> field_details = document->get_field(to_table, choice_second);
-    if(!field_details)
-    {
-      std::cerr << G_STRFUNC << ": !field_details (second)." << std::endl;
-      return result;
-    }
-
-    layout_choice_second = sharedptr<LayoutItem_Field>::create();
-    layout_choice_second->set_full_field_details(field_details);
-  }
-
-
-
-  Utils::type_vecLayoutFields fields;
+  Utils::type_vecConstLayoutFields fields;
   fields.push_back(layout_choice_first);
   if(layout_choice_second)
     fields.push_back(layout_choice_second);
 
+  const Glib::ustring to_table = choice_relationship->get_to_table();
   sharedptr<Field> to_field = document->get_field(to_table, choice_relationship->get_to_field());
 
   if(!to_field)
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index f24ed11..13339c6 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -113,9 +113,9 @@ Gnome::Gda::SqlExpr get_find_where_clause_quick(const Document* document, const
 
 
 typedef std::list< std::pair<Gnome::Gda::Value, Gnome::Gda::Value> > type_list_values_with_second;
-type_list_values_with_second get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field, sharedptr<LayoutItem_Field>& layout_choice_first, sharedptr<LayoutItem_Field>& layout_choice_second);
+type_list_values_with_second get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second);
 
-type_list_values_with_second get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value, sharedptr<LayoutItem_Field>& layout_choice_first, sharedptr<LayoutItem_Field>& layout_choice_second);
+type_list_values_with_second get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value, sharedptr<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second);
 
 /// Get the full query string suitable for use with std::cout.
 std::string sqlbuilder_get_full_query(
diff --git a/glom/mode_data/datawidget/combochoices.cc b/glom/mode_data/datawidget/combochoices.cc
index df6ff7d..3d6f00f 100644
--- a/glom/mode_data/datawidget/combochoices.cc
+++ b/glom/mode_data/datawidget/combochoices.cc
@@ -64,28 +64,13 @@ bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* d
 
     //TODO: Avoid repeating this tedious code in so many places:
     const FieldFormatting& format = layout_item->get_formatting_used();
-    Glib::ustring choice_field, choice_second;
-    format.get_choices_related(m_related_relationship, choice_field, choice_second, m_related_show_all);
+    format.get_choices_related(document, m_related_relationship, m_related_field, m_related_field_second, m_related_show_all);
 
     if(!m_related_relationship)
     {
       std::cerr << G_STRFUNC << ": !m_related_relationship." << std::endl;
       return false;
     }
-
-    const Glib::ustring to_table = m_related_relationship->get_to_table();
-
-    m_related_field = sharedptr<LayoutItem_Field>::create();
-    sharedptr<const Field> field_details = document->get_field(to_table, choice_field);
-    m_related_field->set_full_field_details(field_details);
-
-    m_related_field_second.clear();
-    if(!choice_second.empty())
-    {
-      m_related_field_second = sharedptr<LayoutItem_Field>::create();
-      field_details = document->get_field(to_table, choice_second);
-      m_related_field_second->set_full_field_details(field_details);
-    }
   }
 
   if(!m_related_field)
@@ -109,7 +94,7 @@ bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* d
     return false;
   }
 
-  Utils::type_vecLayoutFields fields;
+  Utils::type_vecConstLayoutFields fields;
   fields.push_back(m_related_field);
   if(m_related_field_second)
     fields.push_back(m_related_field_second);
diff --git a/glom/mode_data/datawidget/combochoices.h b/glom/mode_data/datawidget/combochoices.h
index 2af9778..762e80b 100644
--- a/glom/mode_data/datawidget/combochoices.h
+++ b/glom/mode_data/datawidget/combochoices.h
@@ -73,8 +73,8 @@ protected:
   //These are used if it is related choices:
   sharedptr<const Relationship> m_related_relationship;
   sharedptr<const Field> m_related_to_field; //To avoid retrieving it each time.
-  sharedptr<LayoutItem_Field> m_related_field;
-  sharedptr<LayoutItem_Field> m_related_field_second;
+  sharedptr<const LayoutItem_Field> m_related_field;
+  sharedptr<const LayoutItem_Field> m_related_field_second;
   bool m_related_show_all;
 };
 
diff --git a/glom/utility_widgets/db_adddel/db_adddel.cc b/glom/utility_widgets/db_adddel/db_adddel.cc
index bb5a644..fe0fb23 100644
--- a/glom/utility_widgets/db_adddel/db_adddel.cc
+++ b/glom/utility_widgets/db_adddel/db_adddel.cc
@@ -847,8 +847,8 @@ Gtk::CellRenderer* DbAddDel::construct_specified_columns_cellrenderer(const shar
           if(choice_show_all) //Otherwise it must change whenever the relationships's ID value changes.
           {
             Document* document = get_document();
-            sharedptr<LayoutItem_Field> layout_field_first;
-            sharedptr<LayoutItem_Field> layout_field_second;
+            sharedptr<const LayoutItem_Field> layout_field_first;
+            sharedptr<const LayoutItem_Field> layout_field_second;
             Utils::type_list_values_with_second list_values = Utils::get_choice_values_all(document, item_field, layout_field_first, layout_field_second);
             set_cell_choices(pCellRendererCombo,  layout_field_first, layout_field_second, list_values);
           }
@@ -1295,8 +1295,8 @@ void DbAddDel::refresh_cell_choices_data_from_database_with_foreign_key(guint mo
   }
 
 
-  sharedptr<LayoutItem_Field> layout_choice_first;
-  sharedptr<LayoutItem_Field> layout_choice_second;
+  sharedptr<const LayoutItem_Field> layout_choice_first;
+  sharedptr<const LayoutItem_Field> layout_choice_second;
   Utils::type_list_values_with_second list_values =
     Utils::get_choice_values(get_document(), layout_field, foreign_key_value,
       layout_choice_first, layout_choice_second);



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