[glom/feature_choices_layoutitems: 2/3] In progress



commit 2251fd8371a1f82950160d759ab02b6b4dfbe06e
Author: Murray Cumming <murrayc murrayc com>
Date:   Sat Aug 7 00:38:52 2010 +0200

    In progress

 ChangeLog                                          |    1 +
 .../data_structure/layout/fieldformatting.cc       |   65 +++---------
 .../data_structure/layout/fieldformatting.h        |   18 ++--
 glom/libglom/data_structure/layout/layoutgroup.cc  |    2 -
 glom/libglom/document/document.cc                  |   24 +++--
 glom/libglom/utils.cc                              |   17 +--
 glom/libglom/utils.h                               |    4 +-
 glom/mode_data/datawidget/combo.cc                 |   13 ++-
 glom/mode_data/datawidget/combo.h                  |    2 +-
 .../mode_data/datawidget/combo_as_radio_buttons.cc |   22 ++--
 glom/mode_data/datawidget/combochoices.cc          |  106 ++++++++------------
 glom/mode_data/datawidget/combochoices.h           |   12 +--
 .../datawidget/combochoiceswithtreemodel.cc        |   18 +++-
 glom/mode_data/datawidget/comboentry.cc            |   12 ++-
 glom/mode_data/datawidget/comboentry.h             |    2 +-
 glom/mode_data/datawidget/datawidget.cc            |    7 +-
 glom/mode_data/flowtablewithfields.cc              |    5 +-
 .../layout/layout_item_dialogs/box_formatting.cc   |   15 ++-
 glom/utility_widgets/db_adddel/db_adddel.cc        |   32 +++---
 19 files changed, 166 insertions(+), 211 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index a200e6e..074d102 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+
 2010-08-06  Murray Cumming  <murrayc murrayc com>>
 
 	FieldFormatting: Default to show-all for related choices.
diff --git a/glom/libglom/data_structure/layout/fieldformatting.cc b/glom/libglom/data_structure/layout/fieldformatting.cc
index bb081db..2d0cd7f 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.cc
+++ b/glom/libglom/data_structure/layout/fieldformatting.cc
@@ -190,7 +190,7 @@ FieldFormatting::HorizontalAlignment FieldFormatting::get_horizontal_alignment()
 
 bool FieldFormatting::get_has_choices() const
 {
-  return ( m_choices_related && get_has_relationship_name() && !m_choices_related_field.empty() ) ||
+  return ( m_choices_related && get_has_relationship_name() && m_choices_related_field ) ||
          ( m_choices_custom && !m_choices_custom_list.empty() );
 }
 
@@ -231,23 +231,19 @@ bool FieldFormatting::get_has_related_choices() const
   return m_choices_related;
 }
 
-void FieldFormatting::set_has_related_choices(bool val)
+bool FieldFormatting::get_has_related_choices(bool& show_all, bool& with_second) const
 {
-  m_choices_related = val;
+  show_all = m_choices_related_show_all;
+  with_second = m_choices_related_field_second;
+  return m_choices_related;
 }
 
-void FieldFormatting::get_choices_related(sharedptr<const Relationship>& relationship, Glib::ustring& field, Glib::ustring& field_second, bool& show_all) const
+void FieldFormatting::set_has_related_choices(bool val)
 {
-  relationship = get_relationship();
-
-  field = m_choices_related_field;
-  field_second = m_choices_related_field_second;
-  show_all = m_choices_related_show_all;
-
-  //g_warning("FieldFormatting::get_choices, %s, %s, %s", m_choices_related_relationship->c_str(), m_choices_related_field.c_str(), m_choices_related_field_second.c_str());
+  m_choices_related = val;
 }
 
-void FieldFormatting::set_choices_related(const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all)
+void FieldFormatting::set_choices_related(const sharedptr<const Relationship>& relationship, const sharedptr<const LayoutItem_Field>& field, const sharedptr<const LayoutItem_Field>& field_second, bool show_all)
 {
   set_relationship(relationship);
 
@@ -256,48 +252,19 @@ 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
+void FieldFormatting::get_choices_related(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;
+  relationship = get_relationship();
 
-  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;
-  }
+  field = m_choices_related_field;
+  field_second = m_choices_related_field_second;
+  show_all = m_choices_related_show_all;
 }
 
-void FieldFormatting::change_field_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new)
+sharedptr<const Relationship> FieldFormatting::get_choices_related_relationship(bool& show_all) const
 {
-  //Update choices:
-  if(get_has_relationship_name() && get_table_used(Glib::ustring()) == table_name)
-  {
-    if(m_choices_related_field == field_name)
-       m_choices_related_field = field_name_new;
-
-    if(m_choices_related_field_second == field_name)
-       m_choices_related_field_second = field_name_new;
-  }
+  show_all = m_choices_related_show_all;
+  return get_relationship();
 }
 
 } //namespace Glom
diff --git a/glom/libglom/data_structure/layout/fieldformatting.h b/glom/libglom/data_structure/layout/fieldformatting.h
index b9e5a55..0ee8406 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.h
+++ b/glom/libglom/data_structure/layout/fieldformatting.h
@@ -30,7 +30,6 @@
 namespace Glom
 {
 
-class Document;
 class LayoutItem_Field;
 
 //TODO: This should probably be renamed to Formatting, because it is used for static text items too.
@@ -48,6 +47,7 @@ public:
   bool get_has_choices() const;
 
   bool get_has_related_choices() const;
+  bool get_has_related_choices(bool& show_all, bool& with_second) const;
   void set_has_related_choices(bool val = true);
 
   bool get_has_custom_choices() const;
@@ -68,12 +68,15 @@ public:
    */
   void set_choices_restricted(bool val = true, bool as_radio_buttons = false);
 
-  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(sharedptr<const Relationship>& relationship_name, sharedptr<const LayoutItem_Field>& field, sharedptr<const LayoutItem_Field>& field_second, bool& show_all) const;
+  void set_choices_related(const sharedptr<const Relationship>& relationship_name, const sharedptr<const LayoutItem_Field>& field, const sharedptr<const LayoutItem_Field>& field_second, bool show_all);
+  
+  //Just for convenience:
+  sharedptr<const Relationship> get_choices_related_relationship(bool& show_all) const;
+  
 
 
-  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
@@ -148,8 +151,6 @@ public:
   void set_horizontal_alignment(HorizontalAlignment alignment);
   HorizontalAlignment get_horizontal_alignment() const;
 
-  void change_field_name(const Glib::ustring& table_name, const Glib::ustring& field_name, const Glib::ustring& field_name_new);
-
   NumericFormat m_numeric_format; //Only used for numeric fields.
 
 private:
@@ -166,7 +167,8 @@ private:
   Glib::ustring m_text_color_foreground, m_text_color_background;
   HorizontalAlignment m_horizontal_alignment;
 
-  Glib::ustring m_choices_related_field, m_choices_related_field_second;
+  sharedptr<const LayoutItem_Field> m_choices_related_field;
+  sharedptr<const LayoutItem_Field> m_choices_related_field_second;
   bool m_choices_related_show_all;
 };
 
diff --git a/glom/libglom/data_structure/layout/layoutgroup.cc b/glom/libglom/data_structure/layout/layoutgroup.cc
index 9e37b24..0fd490c 100644
--- a/glom/libglom/data_structure/layout/layoutgroup.cc
+++ b/glom/libglom/data_structure/layout/layoutgroup.cc
@@ -330,8 +330,6 @@ void LayoutGroup::change_field_item_name(const Glib::ustring& table_name, const
         if(field_item->get_name() == field_name)
           field_item->set_name(field_name_new); //Change it.
       }
-
-      field_item->m_formatting.change_field_name(table_name, field_name, field_name_new);
     }
     else
     {
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 112fbb2..f830de7 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -930,13 +930,6 @@ void Document::change_field_name(const Glib::ustring& table_name, const Glib::us
         }
       }
 
-      //Look at all field formatting:
-      for(type_vec_fields::iterator iterFields = iter->second.m_fields.begin(); iterFields != iter->second.m_fields.end(); ++iterFields)
-      {
-        (*iterFields)->m_default_formatting.change_field_name(table_name, strFieldNameOld, strFieldNameNew);
-      }
-
-
       const bool is_parent_table = (iter->second.m_info->get_name() == table_name);
 
       //Look at each layout:
@@ -1999,11 +1992,14 @@ void Document::load_after_layout_item_formatting(const xmlpp::Element* element,
         show_all = true; //This was the behaviour before this checkbox existed.
       }
       
+      const Glib::ustring field_first = get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_FIELD);
+      const Glib::ustring field_second = get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_SECOND);
+      /* TODO:
       sharedptr<Relationship> relationship = get_relationship(table_name, relationship_name);
       format.set_choices_related(relationship,
-        get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_FIELD),
-        get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_SECOND),
+        field_first, field_second,
         show_all);
+      */
       //Full details are updated in filled-in ().
     }
   }
@@ -3001,10 +2997,16 @@ void Document::save_before_layout_item_formatting(xmlpp::Element* nodeItem, cons
     set_node_attribute_value_as_bool(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED, format.get_has_related_choices() );
 
     sharedptr<const Relationship> choice_relationship;
-    Glib::ustring choice_field, choice_second;
+    sharedptr<const LayoutItem_Field> choice_layout_first, choice_layout_second;
     bool choice_show_all = false;
-    format.get_choices_related(choice_relationship, choice_field, choice_second, choice_show_all);
+    format.get_choices_related(choice_relationship, choice_layout_first, choice_layout_second, choice_show_all);
 
+    Glib::ustring choice_field, choice_second;
+    if(choice_layout_first)
+      choice_field = choice_layout_first->get_name();
+    if(choice_layout_second)
+      choice_second = choice_layout_first->get_name();
+    
     set_node_attribute_value(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_RELATIONSHIP, glom_get_sharedptr_name(choice_relationship));
     set_node_attribute_value(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_FIELD, choice_field);
     set_node_attribute_value(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_RELATED_SECOND, choice_second);
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index e4669a2..5aa0b8b 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -480,19 +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<const LayoutItem_Field>& layout_choice_first, sharedptr<const 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)
 {
   return get_choice_values(document, field,
-    Gnome::Gda::Value() /* means get all with no WHERE clause */,
-    layout_choice_first, layout_choice_second);
+    Gnome::Gda::Value() /* means get all with no WHERE clause */);
 }
 
-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)
+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)
 {
-  //Initialize output parameters:
-  layout_choice_first = sharedptr<LayoutItem_Field>();
-  layout_choice_second = sharedptr<LayoutItem_Field>();
-
   //TODO: Reduce duplication between this and get_choice_values(field).
 
   type_list_values_with_second result;
@@ -508,9 +503,9 @@ Utils::type_list_values_with_second Utils::get_choice_values(const Document* doc
 
   const FieldFormatting& format = field->get_formatting_used();
   sharedptr<const Relationship> choice_relationship;
-  Glib::ustring choice_field, choice_second;
+  sharedptr<const LayoutItem_Field> layout_choice_first, layout_choice_second;
   bool choice_show_all = false;
-  format.get_choices_related(document, choice_relationship, layout_choice_first, layout_choice_second, choice_show_all);
+  format.get_choices_related(choice_relationship, layout_choice_first, layout_choice_second, choice_show_all);
 
   if(!choice_relationship)
   {
@@ -524,7 +519,7 @@ Utils::type_list_values_with_second Utils::get_choice_values(const Document* doc
     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());
+  const sharedptr<const 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 13339c6..07ef714 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<const LayoutItem_Field>& layout_choice_first, sharedptr<const LayoutItem_Field>& layout_choice_second);
+type_list_values_with_second get_choice_values_all(const Document* document, const sharedptr<const LayoutItem_Field>& field);
 
-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);
+type_list_values_with_second get_choice_values(const Document* document, const sharedptr<const LayoutItem_Field>& field, const Gnome::Gda::Value& foreign_key_value);
 
 /// Get the full query string suitable for use with std::cout.
 std::string sqlbuilder_get_full_query(
diff --git a/glom/mode_data/datawidget/combo.cc b/glom/mode_data/datawidget/combo.cc
index 6283ece..fe0d980 100644
--- a/glom/mode_data/datawidget/combo.cc
+++ b/glom/mode_data/datawidget/combo.cc
@@ -73,10 +73,17 @@ ComboGlom::~ComboGlom()
 {
 }
 
-void ComboGlom::set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all)
+void ComboGlom::set_choices_related(const Document* document)
 {
+  //TODO: Remove duplication with ComboEntry:
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+  bool choice_show_all = false;
+  bool choice_has_second = false;
+  layout_item->get_formatting_used().get_has_related_choices(choice_show_all, choice_has_second);
+  
   //Add the extra cell if necessary:
-  if(!m_cell_second && !field_second.empty())
+  if(!m_cell_second && choice_has_second)
   {
     #ifndef GLOM_ENABLE_MAEMO
     //We don't use this convenience method, because we want more control over the renderer.
@@ -99,7 +106,7 @@ void ComboGlom::set_choices_related(const Document* document, const sharedptr<co
     #endif //GLOM_ENABLE_MAEMO
   }
 
-  ComboChoicesWithTreeModel::set_choices_related(document, relationship, field, field_second, show_all);
+  ComboChoicesWithTreeModel::set_choices_related(document);
 }
 
 void ComboGlom::check_for_change()
diff --git a/glom/mode_data/datawidget/combo.h b/glom/mode_data/datawidget/combo.h
index c48c96c..830cb5c 100644
--- a/glom/mode_data/datawidget/combo.h
+++ b/glom/mode_data/datawidget/combo.h
@@ -72,7 +72,7 @@ public:
 
   virtual Gnome::Gda::Value get_value() const;
 
-  virtual void set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all);
+  virtual void set_choices_related(const Document* document);
 
 private:
   void init();
diff --git a/glom/mode_data/datawidget/combo_as_radio_buttons.cc b/glom/mode_data/datawidget/combo_as_radio_buttons.cc
index 2a31a4b..2551795 100644
--- a/glom/mode_data/datawidget/combo_as_radio_buttons.cc
+++ b/glom/mode_data/datawidget/combo_as_radio_buttons.cc
@@ -47,11 +47,6 @@ ComboAsRadioButtons::ComboAsRadioButtons()
 
 void ComboAsRadioButtons::init()
 {
-  if(m_related_field_second)
-  {
-    //TODO
-  }
-
   //if(m_glom_type == Field::TYPE_NUMERIC)
    // get_entry()->set_alignment(1.0); //Align numbers to the right.
 }
@@ -67,18 +62,25 @@ void ComboAsRadioButtons::set_choices_with_second(const type_list_values_with_se
   }
   m_map_buttons.clear();
 
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+  const FieldFormatting& format = layout_item->get_formatting_used();
+  sharedptr<const Relationship> choice_relationship;
+  sharedptr<const LayoutItem_Field> layout_choice_first, layout_choice_second;
+  bool choice_show_all = false;
+  format.get_choices_related(choice_relationship, layout_choice_first, layout_choice_second, choice_show_all);
+  
   //Add new buttons:
   Gtk::RadioButton::Group group;
   for(type_list_values_with_second::const_iterator iter = list_values.begin(); iter != list_values.end(); ++iter)
   {
-    sharedptr<const LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
-    if(layout_item)
+    if(layout_choice_first)
     {
-      const Glib::ustring value_first = Conversions::get_text_for_gda_value(layout_item->get_glom_type(), iter->first, layout_item->get_formatting_used().m_numeric_format);
+      const Glib::ustring value_first = Conversions::get_text_for_gda_value(layout_choice_first->get_glom_type(), iter->first, layout_choice_first->get_formatting_used().m_numeric_format);
       Glib::ustring title = value_first;
-      if(m_related_field_second)
+      if(layout_choice_second)
       {
-        const Glib::ustring value_second = Conversions::get_text_for_gda_value(m_related_field_second->get_glom_type(), iter->second, m_related_field_second->get_formatting_used().m_numeric_format);
+        const Glib::ustring value_second = Conversions::get_text_for_gda_value(layout_choice_second->get_glom_type(), iter->second, layout_choice_second->get_formatting_used().m_numeric_format);
         title += " - " + value_second; //TODO: Find a better way to join them?
       }
       
diff --git a/glom/mode_data/datawidget/combochoices.cc b/glom/mode_data/datawidget/combochoices.cc
index 3d6f00f..1a5f1bd 100644
--- a/glom/mode_data/datawidget/combochoices.cc
+++ b/glom/mode_data/datawidget/combochoices.cc
@@ -38,7 +38,6 @@ namespace DataWidgetChildren
 {
 
 ComboChoices::ComboChoices()
-: m_related_show_all(false)
 {
   init();
 }
@@ -53,64 +52,51 @@ ComboChoices::~ComboChoices()
 
 bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* document, const Gnome::Gda::Value& foreign_key_value)
 {
-  //Get the choice information, then cache it:
-  if(!m_related_relationship || !m_related_field)
-  {
-    sharedptr<LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
-    if(!layout_item)
-    {
-      return false;
-    }
-
-    //TODO: Avoid repeating this tedious code in so many places:
-    const FieldFormatting& format = layout_item->get_formatting_used();
-    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;
-    }
-  }
-
-  if(!m_related_field)
-  {
-    std::cerr << G_STRFUNC << ": !m_related_field." << std::endl;
-    return false;
-  }
-
-  if(Conversions::value_is_empty(foreign_key_value))
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+    
+  if(!layout_item || Conversions::value_is_empty(foreign_key_value))
   {
     //Clear the choices list:
     type_list_values_with_second list_values;
     set_choices_with_second(list_values);
     return true;
   }
+  
+  sharedptr<const Relationship> choice_relationship;
+  sharedptr<const LayoutItem_Field> choice_field, choice_field_second;
+  bool choice_show_all = false;
+  layout_item->get_formatting_used().get_choices_related(choice_relationship, choice_field, choice_field_second, choice_show_all); 
 
-  if(m_related_show_all)
+  if(!choice_field)
+  {
+    std::cerr << G_STRFUNC << ": !choice_field." << std::endl;
+    return false;
+  }
+
+  
+  if(choice_show_all)
   {
     //The list should be set in set_choices_related() instead.
-    std::cerr << G_STRFUNC << ": Called with m_related_show_all=true." << std::endl;
+    std::cerr << G_STRFUNC << ": Called with choice_show_all=true." << std::endl;
     return false;
   }
 
   Utils::type_vecConstLayoutFields fields;
-  fields.push_back(m_related_field);
-  if(m_related_field_second)
-    fields.push_back(m_related_field_second);
+  fields.push_back(choice_field);
+  if(choice_field_second)
+    fields.push_back(choice_field_second);
 
-  //std::cout << G_STRFUNC << "debug: m_related_field=" << m_related_field->get_name() << ", m_related_field_second" << m_related_field_second->get_name() << std::endl;
+  //std::cout << G_STRFUNC << "debug: choice_field=" << choice_field->get_name() << ", choice_field_second" << choice_field_second->get_name() << std::endl;
 
-  if(!m_related_to_field)
-  {
-    std::cerr << G_STRFUNC << ": m_related_to_field is null." << std::endl;
-  }
+  const Glib::ustring to_table = choice_relationship->get_to_table();
+  const sharedptr<const Field> to_field = document->get_field(to_table, choice_relationship->get_to_field());
 
   //TODO: Support related relationships (in the UI too):
   Glib::RefPtr<Gnome::Gda::SqlBuilder> builder = Utils::build_sql_select_with_key(
-    m_related_relationship->get_to_table(),
+    to_table,
     fields,
-    m_related_to_field,
+    to_field,
     foreign_key_value);
 
   if(!builder)
@@ -133,7 +119,8 @@ bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* d
   const std::string sql_query =
     Utils::sqlbuilder_get_full_query(builder);
   //std::cout << "get_choice_values: Executing SQL: " << sql_query << std::endl;
-  Glib::RefPtr<Gnome::Gda::DataModel> datamodel = connection->get_gda_connection()->statement_execute_select(sql_query);
+  Glib::RefPtr<Gnome::Gda::DataModel> datamodel = 
+    connection->get_gda_connection()->statement_execute_select(sql_query);
 
   if(datamodel)
   {
@@ -148,7 +135,7 @@ bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* d
       std::pair<Gnome::Gda::Value, Gnome::Gda::Value> itempair;
       itempair.first = datamodel->get_value_at(0, row);
 
-      if(m_related_field_second && (cols_count > 1))
+      if(choice_field_second && (cols_count > 1))
         itempair.second = datamodel->get_value_at(1, row);
 
       list_values.push_back(itempair);
@@ -168,29 +155,24 @@ bool ComboChoices::refresh_data_from_database_with_foreign_key(const Document* d
   return true;
 }
 
-void ComboChoices::set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& /* field */, const Glib::ustring& /* field_second */, bool show_all)
+void ComboChoices::set_choices_related(const Document* document)
 {
-  //Note that field_second is used in derived classes.
-
-  m_related_relationship = relationship;
-  m_related_show_all = show_all;
-
-  m_related_field.clear();
-  m_related_field_second.clear();
-  if(m_related_relationship)
-  {
-    const Glib::ustring to_table = m_related_relationship->get_to_table();
-    m_related_to_field = document->get_field(to_table, m_related_relationship->get_to_field());
-  }
-
   type_list_values_with_second list_values;
-
-  //Set the values now because if it will be the same regardless of the foreign key value.
-  //Otherwise show them when refresh_data_from_database_with_foreign_key() is called.
-  if(relationship && show_all)
+  
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+  if(layout_item)
   {
-    sharedptr<LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
-    list_values = Utils::get_choice_values_all(document, layout_item, m_related_field, m_related_field_second);
+    bool choice_show_all = false;
+    const sharedptr<const Relationship> choice_relationship = 
+      layout_item->get_formatting_used().get_choices_related_relationship(choice_show_all); 
+
+    //Set the values now because if it will be the same regardless of the foreign key value.
+    //Otherwise show them when refresh_data_from_database_with_foreign_key() is called.
+    if(choice_relationship && choice_show_all)
+    {
+      list_values = Utils::get_choice_values_all(document, layout_item);
+    }
   }
 
   const Gnome::Gda::Value old_value = get_value();
diff --git a/glom/mode_data/datawidget/combochoices.h b/glom/mode_data/datawidget/combochoices.h
index 762e80b..8f85323 100644
--- a/glom/mode_data/datawidget/combochoices.h
+++ b/glom/mode_data/datawidget/combochoices.h
@@ -50,11 +50,8 @@ public:
 
   /**
    * See also refresh_data_from_database_with_foreign_key().
-   * @param relationship Just to save some extra lookup in the format, from get_layout_item().
-   * @param field Just to save some extra lookup in the format, from get_layout_item().
-   * @param field_second Just to save some extra lookup in the format, from get_layout_item().
    */
-  virtual void set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all);
+  virtual void set_choices_related(const Document* document);
 
   /** Update a choices widget's list of related choices if a relevant value in its parent table has changed.
    *
@@ -69,13 +66,6 @@ protected:
   virtual void set_choices_with_second(const type_list_values_with_second& list_values) = 0;
 
   //Gnome::Gda::Value m_value; //The last-stored value. We have this because the displayed value might be unparseable.
-
-  //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<const LayoutItem_Field> m_related_field;
-  sharedptr<const LayoutItem_Field> m_related_field_second;
-  bool m_related_show_all;
 };
 
 } //namespace DataWidetChildren
diff --git a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
index 6cf8ac6..0b64031 100644
--- a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
+++ b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
@@ -53,19 +53,27 @@ void ComboChoicesWithTreeModel::set_choices_with_second(const type_list_values_w
 {
   m_refModel->clear();
 
+  //TODO: Remove duplication with ComboEntry:
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+  const FieldFormatting& format = layout_item->get_formatting_used();
+  sharedptr<const Relationship> choice_relationship;
+  sharedptr<const LayoutItem_Field> layout_choice_first, layout_choice_second;
+  bool choice_show_all = false;
+  format.get_choices_related(choice_relationship, layout_choice_first, layout_choice_second, choice_show_all);
+  
   for(type_list_values_with_second::const_iterator iter = list_values.begin(); iter != list_values.end(); ++iter)
   {
     Gtk::TreeModel::iterator iterTree = m_refModel->append();
     Gtk::TreeModel::Row row = *iterTree;
 
-    sharedptr<const LayoutItem_Field> layout_item = sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
-    if(layout_item)
+    if(layout_choice_first)
     {
-      row[m_Columns.m_col_first] = Conversions::get_text_for_gda_value(layout_item->get_glom_type(), iter->first, layout_item->get_formatting_used().m_numeric_format);
+      row[m_Columns.m_col_first] = Conversions::get_text_for_gda_value(layout_choice_first->get_glom_type(), iter->first, layout_choice_first->get_formatting_used().m_numeric_format);
 
-      if(m_related_field_second)
+      if(layout_choice_second)
       {
-        row[m_Columns.m_col_second] = Conversions::get_text_for_gda_value(m_related_field_second->get_glom_type(), iter->second, m_related_field_second->get_formatting_used().m_numeric_format);
+        row[m_Columns.m_col_second] = Conversions::get_text_for_gda_value(layout_choice_second->get_glom_type(), iter->second, layout_choice_second->get_formatting_used().m_numeric_format);
       }
     }
   }
diff --git a/glom/mode_data/datawidget/comboentry.cc b/glom/mode_data/datawidget/comboentry.cc
index 051fc65..5188330 100644
--- a/glom/mode_data/datawidget/comboentry.cc
+++ b/glom/mode_data/datawidget/comboentry.cc
@@ -112,9 +112,15 @@ ComboEntry::~ComboEntry()
 {
 }
 
-void ComboEntry::set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all)
+void ComboEntry::set_choices_related(const Document* document)
 {
-  if(!m_cell_second && !field_second.empty())
+  sharedptr<LayoutItem_Field> layout_item = 
+    sharedptr<LayoutItem_Field>::cast_dynamic(get_layout_item());
+  bool choice_show_all = false;
+  bool choice_has_second = false;
+  layout_item->get_formatting_used().get_has_related_choices(choice_show_all, choice_has_second);
+  
+  if(!m_cell_second && choice_has_second) //Use a more efficient way of discovering if there is a second column.
   {
     #ifndef GLOM_ENABLE_MAEMO
     //We don't use this convenience method, because we want more control over the renderer.
@@ -135,7 +141,7 @@ void ComboEntry::set_choices_related(const Document* document, const sharedptr<c
     #endif //GLOM_ENABLE_MAEMO
   }
 
-  ComboChoicesWithTreeModel::set_choices_related(document, relationship, field, field_second, show_all);
+  ComboChoicesWithTreeModel::set_choices_related(document);
 }
 
 void ComboEntry::set_layout_item(const sharedptr<LayoutItem>& layout_item, const Glib::ustring& table_name)
diff --git a/glom/mode_data/datawidget/comboentry.h b/glom/mode_data/datawidget/comboentry.h
index bbff22f..87fa061 100644
--- a/glom/mode_data/datawidget/comboentry.h
+++ b/glom/mode_data/datawidget/comboentry.h
@@ -71,7 +71,7 @@ public:
 
   virtual void set_read_only(bool read_only = true);
 
-  virtual void set_choices_related(const Document* document, const sharedptr<const Relationship>& relationship, const Glib::ustring& field, const Glib::ustring& field_second, bool show_all);
+  virtual void set_choices_related(const Document* document);
 
 private:
   void init();
diff --git a/glom/mode_data/datawidget/datawidget.cc b/glom/mode_data/datawidget/datawidget.cc
index a5051f3..fcb0185 100644
--- a/glom/mode_data/datawidget/datawidget.cc
+++ b/glom/mode_data/datawidget/datawidget.cc
@@ -130,12 +130,7 @@ DataWidget::DataWidget(const sharedptr<LayoutItem_Field>& field, const Glib::ust
         combo = create_combo_widget_for_field(field);
         combo->set_layout_item( get_layout_item(), table_name);
 
-        sharedptr<const Relationship> choice_relationship;
-        Glib::ustring choice_field, choice_second;
-        bool choice_show_all = false;
-        field->get_formatting_used().get_choices_related(choice_relationship, choice_field, choice_second, choice_show_all);
-
-        combo->set_choices_related(document, choice_relationship, choice_field, choice_second, choice_show_all);
+        combo->set_choices_related(document);
       }
       else
       {
diff --git a/glom/mode_data/flowtablewithfields.cc b/glom/mode_data/flowtablewithfields.cc
index 8569903..3a5460e 100644
--- a/glom/mode_data/flowtablewithfields.cc
+++ b/glom/mode_data/flowtablewithfields.cc
@@ -930,10 +930,9 @@ FlowTableWithFields::type_choice_widgets FlowTableWithFields::get_choice_widgets
 
     const FieldFormatting& format = field->get_formatting_used();
 
-    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);
+    const sharedptr<const Relationship> choice_relationship =
+      format.get_choices_related_relationship(choice_show_all);
     if(choice_show_all)
       continue; //"Show All" choices don't use the ID field values.
 
diff --git a/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc b/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
index 4133781..ccba10a 100644
--- a/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
+++ b/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
@@ -237,14 +237,14 @@ void Box_Formatting::set_formatting(const FieldFormatting& format, bool show_num
     m_combo_choices_relationship->set_relationships(vecRelationships);
 
     sharedptr<const Relationship> choices_relationship;
-    Glib::ustring choices_field, choices_field_second;
+    sharedptr<const LayoutItem_Field> choices_field, choices_field_second;
     bool choices_show_all = false;
     format.get_choices_related(choices_relationship, choices_field, choices_field_second, choices_show_all);
 
     m_combo_choices_relationship->set_selected_relationship(choices_relationship);
     on_combo_choices_relationship_changed(); //Fill the combos so we can set their active items.
-    m_combo_choices_field->set_selected_field(choices_field);
-    m_combo_choices_field_second->set_selected_field(choices_field_second);
+    m_combo_choices_field->set_selected_field(choices_field ? choices_field->get_name() : Glib::ustring());
+    m_combo_choices_field_second->set_selected_field(choices_field_second ? choices_field_second->get_name() : Glib::ustring());
     m_checkbutton_choices_related_show_all->set_active(choices_show_all);
 
     //Custom choices:
@@ -312,10 +312,13 @@ bool Box_Formatting::get_formatting(FieldFormatting& format) const
       m_checkbutton_choices_restricted->get_active(), 
       m_checkbutton_choices_restricted_as_radio_buttons->get_active());
 
-    sharedptr<Relationship> choices_relationship = m_combo_choices_relationship->get_selected_relationship();
+   const sharedptr<const Relationship> choices_relationship = m_combo_choices_relationship->get_selected_relationship();
+   sharedptr<LayoutItem_Field> layout_choice_first = sharedptr<LayoutItem_Field>::create();
+   layout_choice_first->set_name(m_combo_choices_field->get_selected_field_name());
+   sharedptr<LayoutItem_Field> layout_choice_second = sharedptr<LayoutItem_Field>::create();
+   layout_choice_second->set_name(m_combo_choices_field_second->get_selected_field_name());
     m_format.set_choices_related(choices_relationship,
-      m_combo_choices_field->get_selected_field_name(),
-      m_combo_choices_field_second->get_selected_field_name(),
+      layout_choice_first, layout_choice_second,
       m_checkbutton_choices_related_show_all->get_active());
 
     //Custom choices:
diff --git a/glom/utility_widgets/db_adddel/db_adddel.cc b/glom/utility_widgets/db_adddel/db_adddel.cc
index fe0fb23..2aa7274 100644
--- a/glom/utility_widgets/db_adddel/db_adddel.cc
+++ b/glom/utility_widgets/db_adddel/db_adddel.cc
@@ -832,25 +832,23 @@ Gtk::CellRenderer* DbAddDel::construct_specified_columns_cellrenderer(const shar
       else if(item_field && item_field->get_formatting_used().get_has_related_choices())
       {
         sharedptr<const Relationship> choice_relationship;
-        Glib::ustring choice_field, choice_second;
+        sharedptr<const LayoutItem_Field> choice_field, choice_second;
         bool choice_show_all;
         item_field->get_formatting_used().get_choices_related(choice_relationship, choice_field, choice_second, choice_show_all);
 
-        if(choice_relationship && !choice_field.empty())
+        if(choice_relationship && choice_field)
         {
           const Glib::ustring to_table = choice_relationship->get_to_table();
 
-          const bool use_second = !choice_second.empty();
+          const bool use_second = choice_second;
           pCellRendererCombo->set_use_second(use_second);
 
           //TODO: Update this when the relationship's field value changes:
           if(choice_show_all) //Otherwise it must change whenever the relationships's ID value changes.
           {
-            Document* document = get_document();
-            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);
+            const Utils::type_list_values_with_second list_values = 
+              Utils::get_choice_values_all(get_document(), item_field);
+            set_cell_choices(pCellRendererCombo, choice_field, choice_second, list_values);
           }
         }
       }
@@ -1294,13 +1292,14 @@ void DbAddDel::refresh_cell_choices_data_from_database_with_foreign_key(guint mo
     return;
   }
 
+  const Utils::type_list_values_with_second list_values =
+    Utils::get_choice_values(get_document(), layout_field, foreign_key_value);
 
-  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);
-
+  sharedptr<const Relationship> choice_relationship;
+  sharedptr<const LayoutItem_Field> layout_choice_first, layout_choice_second;
+  bool choice_show_all = false;
+  layout_field->get_formatting_used().get_choices_related(choice_relationship, layout_choice_first, layout_choice_second, choice_show_all); 
+    
   set_cell_choices(cell, layout_choice_first, layout_choice_second, list_values);
 }
 
@@ -1443,10 +1442,9 @@ DbAddDel::type_list_indexes DbAddDel::get_choice_index(const sharedptr<const Lay
 
     const FieldFormatting& format = field->get_formatting_used();
 
-    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);
+    const sharedptr<const Relationship> choice_relationship =
+      format.get_choices_related_relationship(choice_show_all);
     if(choice_relationship && !choice_show_all) //"Show All" choices don't use the ID field values.
     {
       if(choice_relationship->get_from_field() == from_key_name)



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