[glom] DbAddDel: Some code rearrangement.



commit d44705f70c2c8a2ac4ab4382ee833465404107ea
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Sep 23 16:08:18 2010 +0200

    DbAddDel: Some code rearrangement.
    
    * glom/base_db_table_data_readonly.h:
    * glom/mode_data/db_adddel/db_adddel.[h|cc]: Split the db treemodel creation
      up into a separate function, ready for reuse by choices combo widgets.

 ChangeLog                             |    8 +++
 glom/base_db_table_data_readonly.h    |   10 ++--
 glom/mode_data/db_adddel/db_adddel.cc |   99 ++++++++++++++++++---------------
 glom/mode_data/db_adddel/db_adddel.h  |    4 +-
 4 files changed, 68 insertions(+), 53 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3c49768..21d4650 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2010-09-23  Murray Cumming  <murrayc murrayc com>
 
+	DbAddDel: Some code rearrangement.
+
+	* glom/base_db_table_data_readonly.h:
+	* glom/mode_data/db_adddel/db_adddel.[h|cc]: Split the db treemodel creation
+  up into a separate function, ready for reuse by choices combo widgets.
+
+2010-09-23  Murray Cumming  <murrayc murrayc com>
+
 	DbAddDel: Replace add_columns() with set_columns(), simplifying the code.
 
 	* glom/libglom/data_structure/layout/layoutgroup.[h|cc]: Added non-const
diff --git a/glom/base_db_table_data_readonly.h b/glom/base_db_table_data_readonly.h
index 0aff833..e19cb3f 100644
--- a/glom/base_db_table_data_readonly.h
+++ b/glom/base_db_table_data_readonly.h
@@ -29,12 +29,12 @@
 namespace Glom
 {
 
-/** A base class some database functionality 
+/** A base class some database functionality
  * for use with a specific database table, showing data from the table.
  */
 class Base_DB_Table_Data_ReadOnly : public Base_DB_Table
 {
-public: 
+public:
   Base_DB_Table_Data_ReadOnly();
   virtual ~Base_DB_Table_Data_ReadOnly();
 
@@ -45,12 +45,12 @@ protected:
   //TODO: Move these to Base_DB_Table_Data too?
   virtual sharedptr<Field> get_field_primary_key() const = 0;
   virtual Gnome::Gda::Value get_primary_key_value_selected() const = 0;
-  virtual Gnome::Gda::Value get_primary_key_value(const Gtk::TreeModel::iterator& row) const = 0;   
-      
+  virtual Gnome::Gda::Value get_primary_key_value(const Gtk::TreeModel::iterator& row) const = 0;
+
   FoundSet m_found_set;
 
   type_vec_fields m_TableFields; //A cache, so we don't have to repeatedly get them from the Document.
-  type_vecLayoutFields m_FieldsShown; //And any extra keys needed by shown fields.
+  type_vecLayoutFields m_FieldsShown; //And any extra keys needed by shown fields. //TODO: Move to the non-read-only class?
 };
 
 } //namespace Glom
diff --git a/glom/mode_data/db_adddel/db_adddel.cc b/glom/mode_data/db_adddel/db_adddel.cc
index 4f21478..b0f9c82 100644
--- a/glom/mode_data/db_adddel/db_adddel.cc
+++ b/glom/mode_data/db_adddel/db_adddel.cc
@@ -702,47 +702,24 @@ Gtk::CellRenderer* DbAddDel::construct_specified_columns_cellrenderer(const shar
   return pCellRenderer;
 }
 
-void DbAddDel::construct_specified_columns()
+typedef std::vector< sharedptr<LayoutItem> > type_vec_layout_items;
+static Glib::RefPtr<DbTreeModel> create_model_db(const FoundSet& found_set, const type_vec_layout_items& layout_items, bool get_records, bool find_mode, Base_DB::type_vecLayoutFields& fields_shown)
 {
-  InnerIgnore innerIgnore(this);
-
-  //TODO_optimisation: This is called many times, just to simplify the API.
-
-  //Delay actual use of set_column_*() stuff until this method is called.
-
-  if(m_column_items.empty())
-  {
-    //std::cout << "debug: " << G_STRFUNC << ": showing hint model: m_find_mode=" << m_find_mode << std::endl;
-
-    m_refListStore.reset();
-    if(m_table_name.empty())
-    {
-      #ifdef GLOM_ENABLE_MAEMO
-      //TODO: Needs a newer hildonmm: m_TreeView.append_column(m_refListStore);
-      //TODO: Remove all previous columns?
-      hildon_touch_selector_append_column(m_TreeView.gobj(), m_refListStore->gobj(), 0, static_cast<char*>(0), true);
-      #else
-      m_TreeView.set_model(m_refListStore); // clear old model from treeview
-      #endif
-    }
-    else
-      show_hint_model();
-    return;
-  }
+  Glib::RefPtr<DbTreeModel> result;
 
   typedef Gtk::TreeModelColumn<Gnome::Gda::Value> type_modelcolumn_value;
   typedef std::vector< type_modelcolumn_value* > type_vecModelColumns;
-  type_vecModelColumns vecModelColumns(m_column_items.size(), 0);
+  type_vecModelColumns vecModelColumns(layout_items.size(), 0);
 
   //Create the Gtk ColumnRecord:
 
   Gtk::TreeModel::ColumnRecord record;
 
   //Database columns:
-  type_model_store::type_vec_fields fields;
+  DbTreeModel::type_vec_fields fields;
   {
     type_vecModelColumns::size_type i = 0;
-    for(type_column_items::iterator iter = m_column_items.begin(); iter != m_column_items.end(); ++iter)
+    for(type_vec_layout_items::const_iterator iter = layout_items.begin(); iter != layout_items.end(); ++iter)
     {
       sharedptr<LayoutItem_Field> item_field = sharedptr<LayoutItem_Field>::cast_dynamic(*iter);
       if(item_field)
@@ -761,13 +738,13 @@ void DbAddDel::construct_specified_columns()
     }
   }
 
-  m_FieldsShown = fields; //Needed by Base_DB_Table_Data::record_new().
+  fields_shown = fields;
 
   {
     //Find the primary key:
     int column_index_key = 0;
     bool key_found = false;
-    for(type_model_store::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+    for(DbTreeModel::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
     {
       sharedptr<LayoutItem_Field> layout_item = *iter;
       if( !(layout_item->get_has_relationship_name()) )
@@ -787,21 +764,61 @@ void DbAddDel::construct_specified_columns()
     {
       //Create the model from the ColumnRecord:
       //Note that the model will use a dummy Gda DataModel if m_find_mode is true.
-      //std::cout << "debug: Creating new type_model_store() for table=" << m_found_set.m_table_name << std::endl;
-      m_refListStore = type_model_store::create(record, m_found_set, fields, column_index_key, m_allow_view, m_find_mode);
+      //std::cout << "debug: Creating new DbTreeModel() for table=" << m_found_set.m_table_name << std::endl;
+      result =  DbTreeModel::create(record, found_set, fields, column_index_key, get_records, find_mode);
     }
     else
     {
       g_warning("%s: no primary key field found.", __FUNCTION__);
-      //for(type_model_store::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
+      //for(DbTreeModel::type_vec_fields::const_iterator iter = fields.begin(); iter != fields.end(); ++iter)
       //{
       //  g_warning("  field: %s", (iter->get_name().c_str());
       //}
+    }
+  }
 
-      m_refListStore = Glib::RefPtr<type_model_store>();
+  //Delete the vector's items:
+  for(type_vecModelColumns::iterator iter = vecModelColumns.begin(); iter != vecModelColumns.end(); ++iter)
+  {
+     type_modelcolumn_value* pModelColumn = *iter;
+     if(pModelColumn)
+       delete pModelColumn;
+  }
+
+  return result;
+}
+
+void DbAddDel::construct_specified_columns()
+{
+  InnerIgnore innerIgnore(this);
+
+  //TODO_optimisation: This is called many times, just to simplify the API.
+
+  //Delay actual use of set_column_*() stuff until this method is called.
+
+  if(m_column_items.empty())
+  {
+    //std::cout << "debug: " << G_STRFUNC << ": showing hint model: m_find_mode=" << m_find_mode << std::endl;
+
+    m_refListStore.reset();
+    if(m_table_name.empty())
+    {
+      #ifdef GLOM_ENABLE_MAEMO
+      //TODO: Needs a newer hildonmm: m_TreeView.append_column(m_refListStore);
+      //TODO: Remove all previous columns?
+      hildon_touch_selector_append_column(m_TreeView.gobj(), m_refListStore->gobj(), 0, static_cast<char*>(0), true);
+      #else
+      m_TreeView.set_model(m_refListStore); // clear old model from treeview
+      #endif
     }
+    else
+      show_hint_model();
+    return;
   }
 
+  m_refListStore = create_model_db(m_found_set, m_column_items, m_allow_view, m_find_mode, m_FieldsShown);
+  //m_FieldsShown is needed by Base_DB_Table_Data::record_new().
+
   #ifdef GLOM_ENABLE_MAEMO
   //Remove all View columns:
   while(m_TreeView.get_num_columns())
@@ -859,9 +876,9 @@ void DbAddDel::construct_specified_columns()
   const bool has_expandable_column = get_column_to_expand(column_to_expand);
   //std::cout << "DEBUG: column_to_expand=" << column_to_expand  << ", has=" << has_expandable_column << std::endl;
 
-  for(type_vecModelColumns::iterator iter = vecModelColumns.begin(); iter != vecModelColumns.end(); ++iter)
+  for(type_column_items::iterator iter = m_column_items.begin(); iter != m_column_items.end(); ++iter)
   {
-    const sharedptr<LayoutItem> layout_item = m_column_items[model_column_index];
+    const sharedptr<LayoutItem> layout_item = m_column_items[model_column_index]; //TODO: Inefficient.
     if(layout_item) //column_info.m_visible)
     {
       no_columns_used = false;
@@ -906,14 +923,6 @@ void DbAddDel::construct_specified_columns()
     ++model_column_index;
   } //for
 
-  //Delete the vector's items:
-  for(type_vecModelColumns::iterator iter = vecModelColumns.begin(); iter != vecModelColumns.end(); ++iter)
-  {
-     type_modelcolumn_value* pModelColumn = *iter;
-     if(pModelColumn)
-       delete pModelColumn;
-  }
-
 
   if(no_columns_used)
   {
diff --git a/glom/mode_data/db_adddel/db_adddel.h b/glom/mode_data/db_adddel/db_adddel.h
index 1a5c977..1152531 100644
--- a/glom/mode_data/db_adddel/db_adddel.h
+++ b/glom/mode_data/db_adddel/db_adddel.h
@@ -382,9 +382,7 @@ private:
   Gtk::TreeView m_TreeView;
   #endif
 
-  //typedef Gtk::ListStore type_model_store;
-  typedef DbTreeModel type_model_store;
-  Glib::RefPtr<type_model_store> m_refListStore;
+  Glib::RefPtr<DbTreeModel> m_refListStore;
 
   //Columns, not including the hidden internal columns:
   typedef LayoutGroup::type_list_items type_column_items;



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