[glom/glom-1-20] Choices: Allow the field to be be other than the primary key.



commit 570536db031e103988cbd93547e77ea3781fff0d
Author: Murray Cumming <murrayc murrayc com>
Date:   Sun Jan 29 20:42:55 2012 +0100

    Choices: Allow the field to be be other than the primary key.
    
    * glom/mode_data/datawidget/treemodel_db.h: Make most methods and
    members private again, documentation what they *key methods and members
    really mean.
    * glom/mode_data/datawidget/treemodel_db_withextratext.[h|cc]: Add
    m_column_index_first and m_item_first members.
    Constructor: Set these by looking for the first LayoutItem_Field,
    because that is what we will want to show in the first cell of
    a combobox, which is where this model is used.
    Use these instead of the *key members, which were actually the primary
    key. We had previously assumed that the first field would always be
    the primary key, but we should not enforce that.
    This fixes crashing bug #668759 (alien).

 ChangeLog                                          |   17 +++++++++
 glom/mode_data/datawidget/treemodel_db.h           |   26 +++++++++++---
 .../datawidget/treemodel_db_withextratext.cc       |   36 +++++++++++++++-----
 .../datawidget/treemodel_db_withextratext.h        |    6 ++-
 4 files changed, 69 insertions(+), 16 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index de8ad30..d147020 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2012-01-29  Murray Cumming  <murrayc murrayc com>
+
+	Choices: Allow the field to be be other than the primary key.
+
+	* glom/mode_data/datawidget/treemodel_db.h: Make most methods and
+	members private again, documentation what they *key methods and members
+	really mean.
+	* glom/mode_data/datawidget/treemodel_db_withextratext.[h|cc]: Add
+	m_column_index_first and m_item_first members.
+	Constructor: Set these by looking for the first LayoutItem_Field, 
+	because that is what we will want to show in the first cell of 
+	a combobox, which is where this model is used.
+	Use these instead of the *key members, which were actually the primary
+	key. We had previously assumed that the first field would always be
+	the primary key, but we should not enforce that.
+	This fixes crashing bug #668759 (alien).
+
 2012-01-28  Murray Cumming  <murrayc murrayc com>
 
 	ComboChoicesWithTreeModel: Make sure the model always has the primary key.
diff --git a/glom/mode_data/datawidget/treemodel_db.h b/glom/mode_data/datawidget/treemodel_db.h
index 3df46a8..bc51ae6 100644
--- a/glom/mode_data/datawidget/treemodel_db.h
+++ b/glom/mode_data/datawidget/treemodel_db.h
@@ -66,7 +66,7 @@ class DbTreeModel
     public Gtk::TreeModel
 {
 public:
-  typedef unsigned int size_type;
+  //typedef unsigned int size_type;
 
   typedef Base_DB::type_vecConstLayoutFields type_vec_const_fields;
 
@@ -78,15 +78,27 @@ public:
   
 protected:
 
+  /**
+   * @param found_set This specifies the table and where clause for the data to show in the treemodel.
+   * @param layout_items The items to show in the treemodel. This should include the primary key for the table at least once.
+   * @param get_records
+   * @param find_mode
+   * @param fields_shown This will be filled with a list of the LayoutItem_Fields that will be shown in the treemodel.
+   */
   DbTreeModel(const FoundSet& found_set, const type_vec_const_layout_items& layout_items, bool get_records, bool find_mode, Base_DB::type_vecConstLayoutFields& fields_shown);
 
   virtual ~DbTreeModel();
-public:
-
 
+public:
  
   /** A convenience method, creating the model from a list of LayoutItems,
    * instead of a list of LayoutItem_Fields.
+   *
+   * @param found_set This specifies the table and where clause for the data to show in the treemodel.
+   * @param layout_items The items to show in the treemodel. This should include the primary key for the table at least once.
+   * @param get_records
+   * @param find_mode
+   * @param fields_shown This will be filled with a list of the LayoutItem_Fields that will be shown in the treemodel.
    */
   static Glib::RefPtr<DbTreeModel> create(const FoundSet& found_set, const type_vec_layout_items& layout_items, bool get_records, bool find_mode, Base_DB::type_vecConstLayoutFields& fields_shown);
 
@@ -101,7 +113,12 @@ public:
   void set_is_not_placeholder(const TreeModel::iterator& iter);
   bool get_is_placeholder(const TreeModel::iterator& iter) const;
 
+  /** Set the value of the primary key for the specified row.
+   */
   void set_key_value(const TreeModel::iterator& iter, const DbValue& value);
+
+  /** Get the value of the primary key for the specified row.
+   */
   DbValue get_key_value(const TreeModel::iterator& iter) const;
 
   /** Get the last row.
@@ -159,7 +176,6 @@ private:
 
    virtual void set_value_impl(const iterator& row, int column, const Glib::ValueBase& value);
 
-protected: //TODO: Make some things private again if possible.
    typedef DbTreeModelRow typeRow; //X columns, all of type Value.
 
    //We use a std::list instead of a std::vector, though it is slower to access via an index,
@@ -180,7 +196,7 @@ protected: //TODO: Make some things private again if possible.
    unsigned int m_columns_count;
    FoundSet m_found_set;
    type_vec_const_fields m_column_fields;
-   int m_column_index_key;
+   int m_column_index_key; //The index of the primary key in the Gda::DataModel.
 
    //Data:
    sharedptr<SharedConnection> m_connection;
diff --git a/glom/mode_data/datawidget/treemodel_db_withextratext.cc b/glom/mode_data/datawidget/treemodel_db_withextratext.cc
index b644500..cae37d9 100644
--- a/glom/mode_data/datawidget/treemodel_db_withextratext.cc
+++ b/glom/mode_data/datawidget/treemodel_db_withextratext.cc
@@ -35,14 +35,32 @@ typedef Glib::Value<Glib::ustring> type_value_string;
 
 DbTreeModelWithExtraText::DbTreeModelWithExtraText(const FoundSet& found_set, const type_vec_const_layout_items& layout_items, bool get_records, bool find_mode, Base_DB::type_vecConstLayoutFields& fields_shown)
 : Glib::ObjectBase( typeid(DbTreeModel) ), //register a custom GType.
-  DbTreeModel(found_set, layout_items, get_records, find_mode, fields_shown)
+  DbTreeModel(found_set, layout_items, get_records, find_mode, fields_shown),
+  m_column_index_first(-1)
 {
-  //Remember the key field details so we can use it later to get a text representation.
-  if(m_column_index_key != -1 && (guint)m_column_index_key < fields_shown.size())
-    m_item_key = fields_shown[m_column_index_key];
+  //Remember the first field details so we can use it later to get a text representation.
+  int column_index = 0;
+  for(type_vec_const_layout_items::const_iterator iter = layout_items.begin(); iter != layout_items.end(); ++iter)
+  {
+    const sharedptr<const LayoutItem_Field> item_field = 
+      sharedptr<const LayoutItem_Field>::cast_dynamic(*iter);
+    if(item_field)
+    {
+      m_item_first = item_field;
+      break;
+    }
+    
+    ++column_index;
+  }
+
+  
+  if(m_item_first)
+  {
+   m_column_index_first = column_index;
+  }
   else
   {
-    std::cerr << G_STRFUNC << ": m_column_index_key is not set, or is out of range. m_column_index_key=" << m_column_index_key << ", size=" << fields_shown.size() << std::endl;
+    std::cerr << G_STRFUNC << ": The first field was found in the list." << std::endl;
   }
 }
 
@@ -85,19 +103,19 @@ void DbTreeModelWithExtraText::get_value_vfunc(const TreeModel::iterator& iter,
   {
     Glib::ustring text;
     
-    if(m_column_index_key == -1)
+    if(m_column_index_first == -1)
     {
-      std::cerr << G_STRFUNC << ": m_column_index_key is not set." << std::endl;
+      std::cerr << G_STRFUNC << ": m_column_index_first is not set." << std::endl;
       //TODO: This then causes a crash later. Find out why.
     }
     else
     {
       Glib::Value<Gnome::Gda::Value> value_db;
-      get_value_vfunc(iter, m_column_index_key, value_db);
+      get_value_vfunc(iter, m_column_index_first, value_db);
       const DbValue dbvalue = value_db.get();
       
       text =
-        Conversions::get_text_for_gda_value(m_item_key->get_glom_type(), dbvalue, m_item_key->get_formatting_used().m_numeric_format);
+        Conversions::get_text_for_gda_value(m_item_first->get_glom_type(), dbvalue, m_item_first->get_formatting_used().m_numeric_format);
       //std::cout << "debug: text=" << text << std::endl;
       //std::cout << "  debug: m_item_key name=" << m_item_key->get_name() << std::endl;
       //std::cout << "  debug: dbvalue=" << dbvalue.to_string() << std::endl;
diff --git a/glom/mode_data/datawidget/treemodel_db_withextratext.h b/glom/mode_data/datawidget/treemodel_db_withextratext.h
index 3b65024..ad14403 100644
--- a/glom/mode_data/datawidget/treemodel_db_withextratext.h
+++ b/glom/mode_data/datawidget/treemodel_db_withextratext.h
@@ -52,7 +52,7 @@ public:
    */
   static Glib::RefPtr<DbTreeModelWithExtraText> create(const FoundSet& found_set, const type_vec_const_layout_items& layout_items, bool get_records, bool find_mode, Base_DB::type_vecConstLayoutFields& fields_shown);
   
-  /** This column is a text representation of the primary key column.
+  /** This column is a text representation of the first field column.
    */
   int get_text_column() const;
 
@@ -61,7 +61,9 @@ private:
   virtual GType get_column_type_vfunc(int index) const;
   virtual void get_value_vfunc(const TreeModel::iterator& iter, int column, Glib::ValueBase& value) const;
   
-  sharedptr<const LayoutItem_Field> m_item_key;
+  
+  int m_column_index_first; //The index of the first field in the TreeModel.
+  sharedptr<const LayoutItem_Field> m_item_first;
 };
 
 } //namespace Glom



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