Put multi-dimension table rendering into a generic method in Data_grid class

This commit is contained in:
Timothy Warren 2012-04-11 12:50:06 -04:00
parent 91edf09ff7
commit 8c49715a55
3 changed files with 60 additions and 79 deletions

View File

@ -105,5 +105,61 @@ class Data_Grid extends GtkTreeView {
$col->set_visible(TRUE); $col->set_visible(TRUE);
$cell->set_property('text', $data); $cell->set_property('text', $data);
} }
// --------------------------------------------------------------------------
/**
* Create a datagrid from the provided data array
*
* @param array $data
* @return void
*/
public function render_data($data)
{
if ( ! is_array($data))
{
return;
}
$model = new StdClass();
$cols = ( ! empty($data)) ? array_keys($data[0]) : array();
// Add columns to model
$col_num = (count($cols) > 0) ? count($cols) : 1;
$model_args = array_fill(0, $col_num, Gobject::TYPE_PHP_VALUE);
$eval_string = '$model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$this->set_model($model);
$this->set_headers_clickable(TRUE);
// Set the data in the model
for($i=0, $c = count($data); $i < $c; $i++)
{
// Add a row
$row = $model->insert($i);
$j = -1;
$vals = array($row);
foreach($data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$this->insert_column_with_data_func($i, $c, $renderer, array($this, 'add_data_col'), $i);
}
}
} }
// End of data_grid.php // End of data_grid.php

View File

@ -251,42 +251,8 @@ class DB_tabs extends GTKNotebook {
if ( ! empty($tab_data)) if ( ! empty($tab_data))
{ {
$tab_model = new StdClass(); $tab = new Data_Grid();
$tab->render_data($tab_data);
$cols = array_keys($tab_data[0]);
// Add columns to model
$model_args = array_fill(0, count($cols), Gobject::TYPE_PHP_VALUE);
$eval_string = '$tab_model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$tab= new Data_Grid($tab_model);
// Set the data in the model
for($i=0, $c = count($tab_data); $i < $c; $i++)
{
// Add a row
$row = $tab_model->insert($i);
$j = -1;
$vals = array($row);
foreach($tab_data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($tab_model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$tab->insert_column_with_data_func($i, $c, $renderer, array($tab, 'add_data_col'), $i);
}
self::$instance->add_tab($tab_name, $tab); self::$instance->add_tab($tab_name, $tab);
} }

View File

@ -48,49 +48,8 @@ class DB_Table_Data extends GTKWindow {
*/ */
protected function _render($data) protected function _render($data)
{ {
if ( ! is_array($data)) $view = new Data_Grid();
{ $view->render_data($data);
return;
}
$model = new StdClass();
$cols = ( ! empty($data)) ? array_keys($data[0]) : array();
// Add columns to model
$col_num = (count($cols) > 0) ? count($cols) : 1;
$model_args = array_fill(0, $col_num, Gobject::TYPE_PHP_VALUE);
$eval_string = '$model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$view = new Data_Grid($model);
$view->set_headers_clickable(TRUE);
// Set the data in the model
for($i=0, $c = count($data); $i < $c; $i++)
{
// Add a row
$row = $model->insert($i);
$j = -1;
$vals = array($row);
foreach($data[$i] as $v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => $c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$view->insert_column_with_data_func($i, $c, $renderer, array($view, 'add_data_col'), $i);
}
// Add the grid to the window // Add the grid to the window
$this->win->add_with_viewport($view); $this->win->add_with_viewport($view);