[glom] CellRendererDbList: Show related choices.



commit 0f9753764ed7a8973b5225cfdb5064b48e432229
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Oct 4 18:19:29 2010 +0200

    CellRendererDbList: Show related choices.
    
    * glom/mode_data/datawidget/combochoiceswithtreemodel.[h|cc]:
    Rename type_model_columns and co with a _fixed suffix, to avoid using them
    for the related case.
    * glom/mode_data/datawidget/cellrenderer_dblist.[h|cc]: on_editing_started():
    Pack extra cells for the related case too, separating the fixed and related
    cases into separate methods.

 ChangeLog                                          |   11 +++
 glom/mode_data/datawidget/cellrenderer_dblist.cc   |   89 +++++++++++++++++--
 glom/mode_data/datawidget/cellrenderer_dblist.h    |    3 +
 .../datawidget/combochoiceswithtreemodel.cc        |   12 ++--
 .../datawidget/combochoiceswithtreemodel.h         |    6 +-
 5 files changed, 102 insertions(+), 19 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0a61cff..3ff6520 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,17 @@
 
 2010-10-04  Murray Cumming  <murrayc murrayc com>
 
+	CellRendererDbList: Show related choices.
+
+	* glom/mode_data/datawidget/combochoiceswithtreemodel.[h|cc]: 
+	Rename type_model_columns and co with a _fixed suffix, to avoid using them 
+	for the related case.
+	* glom/mode_data/datawidget/cellrenderer_dblist.[h|cc]: on_editing_started():
+	Pack extra cells for the related case too, separating the fixed and related 
+	cases into separate methods.
+
+2010-10-04  Murray Cumming  <murrayc murrayc com>
+
 	ComboEntry: Actually show the text value in the drop-down.
 
 	* glom/mode_data/datawidget/treemodel_db_withextratext.cc:
diff --git a/glom/mode_data/datawidget/cellrenderer_dblist.cc b/glom/mode_data/datawidget/cellrenderer_dblist.cc
index 7b9f08e..69d65d8 100644
--- a/glom/mode_data/datawidget/cellrenderer_dblist.cc
+++ b/glom/mode_data/datawidget/cellrenderer_dblist.cc
@@ -89,14 +89,15 @@ void CellRendererDbList::set_restrict_values_to_list(bool val)
   property_has_entry() = !val;
 }
 
-void CellRendererDbList::on_editing_started(Gtk::CellEditable* cell_editable, const Glib::ustring& path)
+void CellRendererDbList::repack_cells_fixed(Gtk::CellLayout* combobox)
 {
-  g_assert(cell_editable);
-
-  Gtk::CellLayout* combobox = dynamic_cast<Gtk::CellLayout*>(cell_editable);
-  if(!combobox)
-    return;
-
+  //We need an actual widget, to guess the fixed cell height.
+  Gtk::Widget* widget = dynamic_cast<Gtk::Widget*>(combobox);
+  if(!widget)
+  {
+    std::cerr << G_STRFUNC << ": widget is null." << std::endl;
+  }
+  
   if(!m_repacked_first_cell)
   {
     //Get the default column, created by set_text_column():
@@ -119,9 +120,9 @@ void CellRendererDbList::on_editing_started(Gtk::CellEditable* cell_editable, co
 
   //Add extra cells:
   Glib::ListHandle<Gtk::CellRenderer*> cells = combobox->get_cells();
-  if(cells.size() < m_vec_model_columns.size())
+  if(cells.size() < m_vec_model_columns_fixed.size())
   {
-    for(guint col = cells.size(); col != m_vec_model_columns.size(); ++col)
+    for(guint col = cells.size(); col != m_vec_model_columns_fixed.size(); ++col)
     {
       Gtk::CellRenderer* cell = 0;
       if(m_db_layout_items.empty())
@@ -129,7 +130,7 @@ void CellRendererDbList::on_editing_started(Gtk::CellEditable* cell_editable, co
       else if(col < m_db_layout_items.size())
       {
         sharedptr<const LayoutItem_Field> layout_item = m_db_layout_items[col];
-        cell = create_cell(layout_item, m_table_name, m_document, 0 /* fixed_cell_height */);
+        cell = create_cell(layout_item, m_table_name, m_document, get_fixed_cell_height(*widget));
       }
 
       if(!cell)
@@ -144,6 +145,74 @@ void CellRendererDbList::on_editing_started(Gtk::CellEditable* cell_editable, co
       cell->property_xalign() = 0.0f;
     }
   }
+}
+
+void CellRendererDbList::repack_cells_related(Gtk::CellLayout* combobox)
+{
+  //We need an actual widget, to guess the fixed cell height.
+  Gtk::Widget* widget = dynamic_cast<Gtk::Widget*>(combobox);
+  if(!widget)
+  {
+    std::cerr << G_STRFUNC << ": widget is null." << std::endl;
+  }
+  
+  const std::list<Gtk::CellRenderer*> cells = combobox->get_cells();
+  const guint initial_cells_count = cells.size();
+  
+  guint i = 0;
+  for(type_vec_const_layout_items::const_iterator iter = m_db_layout_items.begin(); iter != m_db_layout_items.end(); ++iter)
+  {
+    const sharedptr<const LayoutItem> layout_item = *iter;
+    Gtk::CellRenderer* cell = 0;
+
+    if(i == 0 && !m_repacked_first_cell)
+    {
+      //Get the default column, created by set_text_column():
+      cell = combobox->get_first_cell();
+      if(!cell)
+        std::cerr << G_STRFUNC << ": get_first_cell() returned null." << std::endl;
+      else
+      {
+        //Unpack and repack it with expand=false instead of expand=true:
+        //We don't expand the first column, so we can align the other columns.
+        cell->reference();
+        combobox->clear();
+        combobox->pack_start(*cell, false);
+        cell->unreference();
+        cell_connect_cell_data_func(combobox, cell, i);
+        
+         m_repacked_first_cell = true;
+      }
+    }
+    else if(i >= initial_cells_count)
+    {
+      //Create the cell:
+      cell = create_cell(layout_item, m_table_name, m_document, get_fixed_cell_height(*widget));
+      combobox->pack_start(*cell, true);
+      
+      cell_connect_cell_data_func(combobox, cell, i);
+    }
+    
+    ++i;
+  }
+}
+
+void CellRendererDbList::on_editing_started(Gtk::CellEditable* cell_editable, const Glib::ustring& path)
+{
+  g_assert(cell_editable);
+
+  Gtk::CellLayout* combobox = dynamic_cast<Gtk::CellLayout*>(cell_editable);
+  if(!combobox)
+    return;
+
+  //The DB model has a special virtual text column,
+  //and the simple model just has text in all columns:
+  Glib::RefPtr<DbTreeModelWithExtraText> model_db = 
+    Glib::RefPtr<DbTreeModelWithExtraText>::cast_dynamic(get_choices_model());
+  if(model_db)
+    repack_cells_related(combobox);
+  else
+    repack_cells_fixed(combobox); 
 
   Gtk::CellRenderer::on_editing_started(cell_editable, path);
 }
diff --git a/glom/mode_data/datawidget/cellrenderer_dblist.h b/glom/mode_data/datawidget/cellrenderer_dblist.h
index 71e60a2..88b7e42 100644
--- a/glom/mode_data/datawidget/cellrenderer_dblist.h
+++ b/glom/mode_data/datawidget/cellrenderer_dblist.h
@@ -55,6 +55,9 @@ private:
 
   virtual void set_value(const Gnome::Gda::Value& value);
   virtual Gnome::Gda::Value get_value() const;
+  
+  void repack_cells_fixed(Gtk::CellLayout* combobox);
+  void repack_cells_related(Gtk::CellLayout* combobox);
 
   void set_text(const Glib::ustring& text);
   Glib::ustring get_text() const;
diff --git a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
index 7422a58..65e0ced 100644
--- a/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
+++ b/glom/mode_data/datawidget/combochoiceswithtreemodel.cc
@@ -59,13 +59,13 @@ void ComboChoicesWithTreeModel::create_model_non_db(guint columns_count)
   Gtk::TreeModel::ColumnRecord record;
 
   //Create the TreeModelColumns, adding them to the ColumnRecord:
-  m_vec_model_columns.resize(columns_count, 0);
+  m_vec_model_columns_fixed.resize(columns_count, 0);
   for(guint i = 0; i < columns_count; ++i)
   {
-    type_model_column* model_column = new type_model_column();
+    type_model_column_fixed* model_column = new type_model_column_fixed();
 
     //Store it so we can use it and delete it later:
-    m_vec_model_columns[i] = model_column;
+    m_vec_model_columns_fixed[i] = model_column;
 
     record.add(*model_column);
   }
@@ -77,13 +77,13 @@ void ComboChoicesWithTreeModel::create_model_non_db(guint columns_count)
 void ComboChoicesWithTreeModel::delete_model()
 {
   //Delete the vector's items:
-  for(type_vec_model_columns::iterator iter = m_vec_model_columns.begin(); iter != m_vec_model_columns.end(); ++iter)
+  for(type_vec_model_columns_fixed::iterator iter = m_vec_model_columns_fixed.begin(); iter != m_vec_model_columns_fixed.end(); ++iter)
   {
-    type_model_column* model_column = *iter;
+    type_model_column_fixed* model_column = *iter;
      if(model_column)
        delete model_column;
   }
-  m_vec_model_columns.clear();
+  m_vec_model_columns_fixed.clear();
 
   m_refModel.reset();
 }
diff --git a/glom/mode_data/datawidget/combochoiceswithtreemodel.h b/glom/mode_data/datawidget/combochoiceswithtreemodel.h
index bbfdcd4..4a185a2 100644
--- a/glom/mode_data/datawidget/combochoiceswithtreemodel.h
+++ b/glom/mode_data/datawidget/combochoiceswithtreemodel.h
@@ -56,9 +56,9 @@ protected:
    */
   int get_fixed_cell_height(Gtk::Widget& widget);
   
-  typedef Gtk::TreeModelColumn<Glib::ustring> type_model_column;
-  typedef std::vector< type_model_column* > type_vec_model_columns;
-  type_vec_model_columns m_vec_model_columns;
+  typedef Gtk::TreeModelColumn<Glib::ustring> type_model_column_fixed;
+  typedef std::vector< type_model_column_fixed* > type_vec_model_columns_fixed;
+  type_vec_model_columns_fixed m_vec_model_columns_fixed; //If set_choices_fixed() was used.
 
   typedef std::vector< sharedptr<const LayoutItem_Field> > type_vec_const_layout_items;
   type_vec_const_layout_items m_db_layout_items; //If set_choices_related() was used.



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