[glom] List: Custom choices: Make them work here too.



commit 2c8586ff8051ecc8b36e03ee8de2fd990b830825
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Jan 6 14:53:49 2012 +0100

    List: Custom choices: Make them work here too.
    
    * glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
    Added get_custom_choice_original_for_translated_text() and
    get_custom_choice_translated() to convert between original and
    translated choice text.
    * glom/libglom/data_structure/layout/layoutitem_field.[h|cc]:
    Add a get_formatting_used_has_translatable_choices() convenience method.
    * tests/test_document_load_translations.cc: Test these new methods.
    
    * glom/mode_data/datawidget/cellcreation.cc: create_cell():
    Pass the restricted bool to set_choices_restricted() so that the
    translations can be used.
    * glom/mode_data/db_adddel/db_adddel.cc:
    on_treeview_cell_edited(): store only the original (not translated)
    text in the database so that other locales can also show their own
    translations.
    treeviewcolumn_on_cell_data(): Show the translated text, not the
    original.

 ChangeLog                                          |   22 ++++++++++++
 .../data_structure/layout/fieldformatting.cc       |   31 +++++++++++++++++
 .../data_structure/layout/fieldformatting.h        |   10 ++++++
 .../data_structure/layout/layoutitem_field.cc      |   13 +++++++
 .../data_structure/layout/layoutitem_field.h       |    6 +++
 glom/mode_data/datawidget/cellcreation.cc          |    6 ++--
 glom/mode_data/db_adddel/db_adddel.cc              |   35 ++++++++++++++++++-
 tests/test_document_load_translations.cc           |   11 ++++++
 8 files changed, 129 insertions(+), 5 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index da7671e..10f6267 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2012-01-06  Murray Cumming  <murrayc murrayc com>
+
+	List: Custom choices: Make them work here too.
+
+	* glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
+	Added get_custom_choice_original_for_translated_text() and
+	get_custom_choice_translated() to convert between original and 
+	translated choice text.
+	* glom/libglom/data_structure/layout/layoutitem_field.[h|cc]:
+	Add a get_formatting_used_has_translatable_choices() convenience method.
+	* tests/test_document_load_translations.cc: Test these new methods.
+
+	* glom/mode_data/datawidget/cellcreation.cc: create_cell():
+	Pass the restricted bool to set_choices_restricted() so that the
+	translations can be used.
+	* glom/mode_data/db_adddel/db_adddel.cc:
+	on_treeview_cell_edited(): store only the original (not translated) 
+	text in the database so that other locales can also show their own 
+	translations.
+	treeviewcolumn_on_cell_data(): Show the translated text, not the 
+	original.
+
 2012-01-05  Murray Cumming  <murrayc murrayc com>
 
 	DTD: formatting: Mention choices_restricted to fix make check.
diff --git a/glom/libglom/data_structure/layout/fieldformatting.cc b/glom/libglom/data_structure/layout/fieldformatting.cc
index a6a61b7..2d34957 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.cc
+++ b/glom/libglom/data_structure/layout/fieldformatting.cc
@@ -207,6 +207,37 @@ void FieldFormatting::set_choices_custom(const type_list_values& choices)
   m_choices_custom_list = choices;
 }
 
+Glib::ustring FieldFormatting::get_custom_choice_original_for_translated_text(const Glib::ustring& text) const
+{
+  for(FieldFormatting::type_list_values::const_iterator iter = m_choices_custom_list.begin(); iter != m_choices_custom_list.end(); ++iter)
+  {
+    const sharedptr<const ChoiceValue> value = *iter;
+    if(!value)
+      continue;
+
+    if(value->get_title() == text)
+      return value->get_title_original();
+  }
+
+  return Glib::ustring();
+}
+
+Glib::ustring FieldFormatting::get_custom_choice_translated(const Glib::ustring& original_text) const
+{
+  for(FieldFormatting::type_list_values::const_iterator iter = m_choices_custom_list.begin(); iter != m_choices_custom_list.end(); ++iter)
+  {
+    const sharedptr<const ChoiceValue> value = *iter;
+    if(!value)
+      continue;
+
+    if(value->get_title_original() == original_text)
+      return value->get_title();
+  }
+
+  return Glib::ustring();
+}
+
+
 bool FieldFormatting::get_choices_restricted(bool& as_radio_buttons) const
 {
   as_radio_buttons = m_choices_restricted_as_radio_buttons;
diff --git a/glom/libglom/data_structure/layout/fieldformatting.h b/glom/libglom/data_structure/layout/fieldformatting.h
index a447711..2fc23c4 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.h
+++ b/glom/libglom/data_structure/layout/fieldformatting.h
@@ -59,6 +59,16 @@ public:
   virtual type_list_values get_choices_custom() const;
   virtual void set_choices_custom(const type_list_values& choices);
 
+  /** Get the original text that corresponds to the translated choice for the 
+   * current locale.
+   */
+  Glib::ustring get_custom_choice_original_for_translated_text(const Glib::ustring& text) const;
+
+  /** Get the translated choice text, for the 
+   * current locale, that corresponds to the original text .
+   */
+  Glib::ustring get_custom_choice_translated(const Glib::ustring& original_text) const;
+
   typedef std::pair< sharedptr<const LayoutItem_Field>, bool /* is_ascending */> type_pair_sort_field;
   typedef std::list<type_pair_sort_field> type_list_sort_fields;
 
diff --git a/glom/libglom/data_structure/layout/layoutitem_field.cc b/glom/libglom/data_structure/layout/layoutitem_field.cc
index 3cd9b35..d80a2c1 100644
--- a/glom/libglom/data_structure/layout/layoutitem_field.cc
+++ b/glom/libglom/data_structure/layout/layoutitem_field.cc
@@ -264,6 +264,19 @@ FieldFormatting::HorizontalAlignment LayoutItem_Field::get_formatting_used_horiz
   return alignment;
 }
 
+bool LayoutItem_Field::get_formatting_used_has_translatable_choices() const
+{
+  const FieldFormatting& formatting = get_formatting_used();
+  if(!formatting.get_has_custom_choices())
+    return false;
+
+  bool as_radio_buttons = false; //Ignored.
+  if(!formatting.get_choices_restricted(as_radio_buttons))
+    return false;
+
+  return true;
+}
+
 
 void LayoutItem_Field::set_full_field_details(const sharedptr<const Field>& field)
 {
diff --git a/glom/libglom/data_structure/layout/layoutitem_field.h b/glom/libglom/data_structure/layout/layoutitem_field.h
index 33fb777..fb2e1ae 100644
--- a/glom/libglom/data_structure/layout/layoutitem_field.h
+++ b/glom/libglom/data_structure/layout/layoutitem_field.h
@@ -151,6 +151,12 @@ public:
    */
   virtual FieldFormatting::HorizontalAlignment get_formatting_used_horizontal_alignment(bool for_details_view = false) const;
 
+  /** A convenience method to discover whether the formatting that is used
+   * has custom choices with the values restricted to those choices,
+   * meaning that those choices could be translated.
+   */
+  bool get_formatting_used_has_translatable_choices() const;
+
   /** Compare the name, relationship, and related_relationship.
    */
   bool is_same_field(const sharedptr<const LayoutItem_Field>& field) const;
diff --git a/glom/mode_data/datawidget/cellcreation.cc b/glom/mode_data/datawidget/cellcreation.cc
index 59db73d..5011b1b 100644
--- a/glom/mode_data/datawidget/cellcreation.cc
+++ b/glom/mode_data/datawidget/cellcreation.cc
@@ -98,14 +98,14 @@ Gtk::CellRenderer* create_cell(const sharedptr<const LayoutItem>& layout_item, c
           sharedptr<LayoutItem> unconst = sharedptr<LayoutItem>::cast_const(layout_item); //TODO: Avoid this.
           rendererList->set_layout_item(unconst, table_name);
           bool as_radio_buttons = false; //Can't really be done in a list, so we ignore it.
-          rendererList->set_restrict_values_to_list(
-            formatting.get_choices_restricted(as_radio_buttons));
+          const bool restricted = formatting.get_choices_restricted(as_radio_buttons);
+          rendererList->set_restrict_values_to_list(restricted);
 
           //Set the choices.
           //For related choices, that gets set when the value of a dependent field is set.
           if(formatting.get_has_custom_choices())
           {
-            rendererList->set_choices_fixed( formatting.get_choices_custom() );
+            rendererList->set_choices_fixed( formatting.get_choices_custom(), restricted);
           }
 
           cell = rendererList;
diff --git a/glom/mode_data/db_adddel/db_adddel.cc b/glom/mode_data/db_adddel/db_adddel.cc
index 4b41419..ed2590f 100644
--- a/glom/mode_data/db_adddel/db_adddel.cc
+++ b/glom/mode_data/db_adddel/db_adddel.cc
@@ -1475,7 +1475,23 @@ void DbAddDel::on_treeview_cell_edited(const Glib::ustring& path_string, const G
       //Make sure that the entered data is suitable for this field type:
       bool success = false;
 
-      const Gnome::Gda::Value value = Conversions::parse_value(field_type, new_text, item_field->get_formatting_used().m_numeric_format, success);
+      Glib::ustring new_text_to_save = new_text;
+
+      //If this layout field uses a translatable set of custom choices,
+      //then make sure that we only write the original to the database, though we display the translated version:
+      if(item_field->get_formatting_used_has_translatable_choices())
+      {
+        const FieldFormatting& formatting = item_field->get_formatting_used();
+        new_text_to_save = formatting.get_custom_choice_original_for_translated_text(new_text);
+
+        //If somehow (though this should be impossible), the user entered a 
+        //translated value with no corresponding original text, then
+        //store the translated version rather than losing data.
+        if(new_text_to_save.empty())
+          new_text_to_save = new_text;
+      }
+
+      const Gnome::Gda::Value value = Conversions::parse_value(field_type, new_text_to_save, item_field->get_formatting_used().m_numeric_format, success);
       if(!success)
       {  
           //Tell the user and offer to revert or try again:
@@ -1959,8 +1975,23 @@ void DbAddDel::treeviewcolumn_on_cell_data(Gtk::CellRenderer* renderer, const Gt
           if(pDerived)
           {
             //std::cout << "debug: " << G_STRFUNC << ": field name=" << field->get_name() << ", glom type=" << field->get_glom_type() << std::endl;
-            const Glib::ustring text = Conversions::get_text_for_gda_value(field->get_glom_type(), value, field->get_formatting_used().m_numeric_format);
+            Glib::ustring text = Conversions::get_text_for_gda_value(field->get_glom_type(), value, field->get_formatting_used().m_numeric_format);
             //g_assert(text != "NULL");
+
+            //If this layout field uses a translatable set of custom choices,
+            //then make sure that we show the translated version, never showing the original text from the database:
+            if(field->get_formatting_used_has_translatable_choices())
+            {
+              const FieldFormatting& formatting = field->get_formatting_used();
+              const Glib::ustring text_to_show = formatting.get_custom_choice_translated(text);
+
+              //Use the translation if there is one.
+              //Otherwise, show the original data rather than showing nothing:
+              if(!text_to_show.empty())
+               text = text_to_show;
+            }
+
+            //TODO: Use the C++ API here and elsewhere:
             g_object_set(pDerived->gobj(), "text", text.c_str(), (gpointer)0);
           }
 
diff --git a/tests/test_document_load_translations.cc b/tests/test_document_load_translations.cc
index 5ebfd75..e6b44a1 100644
--- a/tests/test_document_load_translations.cc
+++ b/tests/test_document_load_translations.cc
@@ -232,6 +232,10 @@ int main()
 
   Glom::TranslatableItem::set_current_locale(locale_de);
   g_assert( value->get_title_original() == "Day" );
+  g_assert( formatting.get_custom_choice_original_for_translated_text("Nacht") == "Night" );
+  g_assert( formatting.get_custom_choice_original_for_translated_text("aaaa") == "" );
+  g_assert( formatting.get_custom_choice_translated("Night") == "Nacht" );
+  g_assert( formatting.get_custom_choice_translated("aaaa") == "" );
   Glom::TranslatableItem::set_current_locale(locale_original);
   g_assert( value->get_title_original() == "Day" );
 
@@ -245,6 +249,13 @@ int main()
   g_assert(field_on_layout);
   check_title(field_on_layout, "Actor's Name", "Schauspieler Name" );
 
+  field_on_layout = 
+    get_field_on_layout(document, "scenes", "scenes", "day_or_night");
+  g_assert(field_on_layout);
+  check_title(field_on_layout,  "Day/Night", "Tag/Nacht");
+  g_assert(field_on_layout->get_formatting_used_has_translatable_choices());
+
+  //Check a print layout:
   const Glom::sharedptr<const Glom::PrintLayout> print_layout = document.get_print_layout("contacts", "contact_details");
   g_assert(print_layout);
   check_title(print_layout, "Contact Details", "Kontakt Details" );



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