OpenSQLManager/sys/widgets/data_grid.php
Timothy Warren 91edf09ff7 Fix #12
Show Database table data in a popup
2012-04-11 11:16:44 -04:00

109 lines
2.1 KiB
PHP

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
*Class to simplify dealing with GtkTreeView
*/
class Data_Grid extends GtkTreeView {
protected $model;
/**
* Create the object
*
* @param object $model
*/
public function __construct($model = null)
{
$this->model = ( ! is_null($model))
? $model
: new GtkTreeStore(Gobject::TYPE_PHP_VALUE);
parent::__construct($this->model);
}
// --------------------------------------------------------------------------
/**
* Get the value of the model for the current selection
*
* @param int pos
* @return mixed
*/
public function get($pos = 0)
{
if ( ! is_numeric($pos))
{
return parent::get($pos);
}
// Get the selection object of the row
$sel = $this->get_selection();
// Get the model and iterator for the selected row
list($model, $iter) = $sel->get_selected();
// Return on lack of $iter
if (is_null($iter))
{
return;
}
// Get the data from the model
return $model->get_value($iter, $pos);
}
// --------------------------------------------------------------------------
/**
* Empty the model
*/
public function reset()
{
$this->model->clear();
$cols = $this->get_columns();
foreach($cols as $c)
{
$this->remove_column($c);
}
}
// --------------------------------------------------------------------------
/**
* Adds a column of data to the model
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @param int $i
* @return void
*/
public function add_data_col($col, $cell, $model, $iter, $i=0)
{
$data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
$col->set_visible(TRUE);
$cell->set_property('text', $data);
}
}
// End of data_grid.php