[glom/glom-1-18] Add libglom::layout_field_should_have_navigation().



commit 7a9c83307fafb1ac141a3134046f9b3d9baf1e93
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Sep 27 11:29:52 2011 +0200

    Add libglom::layout_field_should_have_navigation().
    
    	* glom/mode_data/datawidget/datawidget.cc: Constructor: Moved decision code
    	from here to here:
    	* glom/libglom/db_utils.[h|cc]: Added layout_field_should_have_navigation().

 ChangeLog                               |    8 +++++
 glom/libglom/db_utils.cc                |   44 +++++++++++++++++++++++++++++++
 glom/libglom/db_utils.h                 |    7 +++++
 glom/mode_data/datawidget/datawidget.cc |   27 +++++-------------
 4 files changed, 67 insertions(+), 19 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d1c4a0c..06ad2e0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-09-27  Murray Cumming  <murrayc murrayc com>
+
+	Add libglom::layout_field_should_have_navigation().
+
+	* glom/mode_data/datawidget/datawidget.cc: Constructor: Moved decision code
+	from here to here:
+	* glom/libglom/db_utils.[h|cc]: Added layout_field_should_have_navigation().
+
 1.18.4:
 
 2011-09-19  Murray Cumming  <murrayc murrayc com>
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index a2afd6c..a2f6ec1 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -1803,6 +1803,50 @@ bool query_execute(const Glib::RefPtr<const Gnome::Gda::SqlBuilder>& builder)
   return (exec_retval >= 0);
 }
 
+bool layout_field_should_have_navigation(const Glib::ustring& table_name, const sharedptr<LayoutItem_Field>& layout_item, const Document* document, bool& field_used_in_relationship_to_one)
+{
+  //Initialize output parameter:
+  field_used_in_relationship_to_one = false;
+  
+  if(!document)
+  {
+    std::cerr << G_STRFUNC << ": document was null." << std::endl;
+    return false;
+  }
+  
+  if(table_name.empty())
+  {
+    std::cerr << G_STRFUNC << ": table_name was empty." << std::endl;
+    return false;
+  } 
+  
+  if(!layout_item)
+  {
+    std::cerr << G_STRFUNC << ": layout_item was null." << std::endl;
+    return false;
+  }
+  
+  layout_item->set_full_field_details(
+     document->get_field(layout_item->get_table_used(table_name), layout_item->get_name()) ); //Otherwise get_primary_key() returns false always.
+
+
+  //Check whether the field controls a relationship,
+  //meaning it identifies a record in another table.
+  field_used_in_relationship_to_one = 
+    document->get_field_used_in_relationship_to_one(table_name, layout_item);
+  //std::cout << "DEBUG: table_name=" << table_name << ", table_used=" << field->get_table_used(table_name) << ", field=" << field->get_name() << ", field_used_in_relationship_to_one=" << field_used_in_relationship_to_one << std::endl;
+
+  //Check whether the field identifies a record in another table
+  //just because it is a primary key in that table:
+  const sharedptr<const Field> field_info = layout_item->get_full_field_details();
+  const bool field_is_related_primary_key =
+    layout_item->get_has_relationship_name() &&
+    field_info && field_info->get_primary_key();
+  //std::cout <<   "DEBUG: field->get_has_relationship_name()=" << field->get_has_relationship_name() << ", field_info->get_primary_key()=" <<  field_info->get_primary_key() << ", field_is_related_primary_key=" << field_is_related_primary_key << std::endl;
+
+  return field_used_in_relationship_to_one || field_is_related_primary_key;
+}
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/libglom/db_utils.h b/glom/libglom/db_utils.h
index d9b98b4..5c36664 100644
--- a/glom/libglom/db_utils.h
+++ b/glom/libglom/db_utils.h
@@ -104,6 +104,13 @@ Gnome::Gda::Value auto_increment_insert_first_if_necessary(const Glib::ustring&
 Gnome::Gda::Value get_next_auto_increment_value(const Glib::ustring& table_name, const Glib::ustring& field_name);
 
 
+/** Decides whether a field should have an Open button next to it,
+ * allowing the user to navigate to a related record.
+ *
+ * @param field_used_in_relationship_to_one Whether the field identifies a single record, so a Find button would also make sense, to choose the ID, in editing mode.
+ */
+bool layout_field_should_have_navigation(const Glib::ustring& table_name, const sharedptr<LayoutItem_Field>& layout_item, const Document* document, bool& field_used_in_relationship_to_one);
+
 } //namespace DbUtils
 
 } //namespace Glom
diff --git a/glom/mode_data/datawidget/datawidget.cc b/glom/mode_data/datawidget/datawidget.cc
index 0209182..5f8a2a5 100644
--- a/glom/mode_data/datawidget/datawidget.cc
+++ b/glom/mode_data/datawidget/datawidget.cc
@@ -36,6 +36,7 @@
 #include <glom/mode_design/layout/layout_item_dialogs/dialog_field_layout.h>
 #include <glom/utils_ui.h>
 #include <glom/glade_utils.h>
+#include <libglom/db_utils.h>
 
 #ifdef GLOM_ENABLE_MAEMO
 #include <hildonmm/button.h>
@@ -186,26 +187,14 @@ DataWidget::DataWidget(const sharedptr<LayoutItem_Field>& field, const Glib::ust
 
     bool child_added = false; //Don't use an extra container unless necessary.
 
-    //Check whether the field controls a relationship,
-    //meaning it identifies a record in another table.
-    const bool field_used_in_relationship_to_one = document->get_field_used_in_relationship_to_one(table_name, field);
-    //std::cout << "DEBUG: table_name=" << table_name << ", table_used=" << field->get_table_used(table_name) << ", field=" << field->get_name() << ", field_used_in_relationship_to_one=" << field_used_in_relationship_to_one << std::endl;
-
-    //Check whether the field identifies a record in another table
-    //just because it is a primary key in that table:
-    bool field_is_related_primary_key = false;
-    if(document)
-      field->set_full_field_details( document->get_field(field->get_table_used(table_name), field->get_name()) ); //Otherwise get_primary_key() returns false always.
-    sharedptr<const Field> field_info = field->get_full_field_details();
-    field_is_related_primary_key =
-      field->get_has_relationship_name() &&
-      field_info && field_info->get_primary_key();
-    //std::cout <<   "DEBUG: field->get_has_relationship_name()=" << field->get_has_relationship_name() << ", field_info->get_primary_key()=" <<  field_info->get_primary_key() << ", field_is_related_primary_key=" << field_is_related_primary_key << std::endl;
-
+    bool field_used_in_relationship_to_one = false;
+    const bool add_open_button = 
+       DbUtils::layout_field_should_have_navigation(table_name, field, document, 
+         field_used_in_relationship_to_one);
 
     Gtk::HBox* hbox_parent = 0; //Only used if there are extra widgets.
 
-    const bool with_extra_widgets = field_used_in_relationship_to_one || field_is_related_primary_key || (glom_type == Field::TYPE_DATE);
+    const bool with_extra_widgets = field_used_in_relationship_to_one || add_open_button || (glom_type == Field::TYPE_DATE);
     if(with_extra_widgets)
     {
       hbox_parent = Gtk::manage( new Gtk::HBox() ); //We put the child (and any extra stuff) in this:
@@ -232,7 +221,7 @@ DataWidget::DataWidget(const sharedptr<LayoutItem_Field>& field, const Glib::ust
       button_date->signal_clicked().connect(sigc::mem_fun(*this, &DataWidget::on_button_choose_date));
     }
 
-    if((field_used_in_relationship_to_one || field_is_related_primary_key) && hbox_parent)
+    if(hbox_parent && add_open_button)
     {
       //Add a button for related record navigation:
       #ifndef GLOM_ENABLE_MAEMO
@@ -244,7 +233,7 @@ DataWidget::DataWidget(const sharedptr<LayoutItem_Field>& field, const Glib::ust
       hbox_parent->pack_start(*m_button_go_to_details);
       m_button_go_to_details->signal_clicked().connect(sigc::mem_fun(*this, &DataWidget::on_button_open_details));
 
-      //Add a button to make it easier to choose an ID for this field.
+      //Add an additional button to make it easier to choose an ID for this field.
       //Don't add this for simple related primary key fields, because they
       //can generally not be edited via another table's layout.
       if(field_used_in_relationship_to_one)



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