This commit is contained in:
Juan Ferrer Toribio 2014-07-09 09:49:56 +02:00
parent 8418144ad8
commit b042af35db
15 changed files with 405 additions and 213 deletions

View File

@ -51,13 +51,16 @@ G_DEFINE_TYPE (DbCalc, db_calc, GVN_TYPE_PARAM);
*
* Return value: a new #DbCalc
**/
DbCalc * db_calc_new (DbModel * model, DbCalcType type, gint col)
DbCalc * db_calc_new (DbModel * model, DbCalcType type, const gchar * column_name)
{
g_return_val_if_fail (DB_IS_MODEL (model), NULL);
g_return_val_if_fail (0 <= col || col < db_model_get_ncols (model), NULL);
return g_object_new
(DB_TYPE_CALC, "model", model, "type", type, "col", col, NULL);
return g_object_new (DB_TYPE_CALC
,"model", model
,"type", type
,"column-name", column_name
,NULL
);
}
/**
@ -77,8 +80,13 @@ DbCalc * db_calc_new_with_func (DbModel * model, DbCalcType type, DbCalcFunc fun
g_return_val_if_fail (DB_IS_MODEL (model), NULL);
g_return_val_if_fail (func, NULL);
return g_object_new
(DB_TYPE_CALC, "model", model, "type", type, "function", func, "data", data, NULL);
return g_object_new (DB_TYPE_CALC
,"model", model
,"type", type
,"function", func
,"data", data
,NULL
);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
@ -111,108 +119,194 @@ static gdouble db_calc_value_to_double (const GValue * v)
}
static void db_calc_internal_func
(DbCalc * obj, DbIter * iter, GValue * out, gpointer data)
(DbCalc * self, DbIter * iter, GValue * out, gpointer data)
{
const GValue * value = db_model_get_value (obj->model, iter, obj->col, NULL);
g_value_copy (value, g_value_init (out, G_VALUE_TYPE (value)));
const GValue * value;
if (self->column_index != -1)
{
value = db_model_get_value (self->model, iter, self->column_index, NULL);
g_value_copy (value, g_value_init (out, G_VALUE_TYPE (value)));
}
else
g_value_init (out, GVN_TYPE_NULL);
}
static void db_calc_before_unset (DbModel * model, DbIter * iter, DbCalc * obj)
static void db_calc_reset (DbCalc * self)
{
g_free (self->column_name);
self->column_name = NULL;
self->column_index = -1;
self->func = db_calc_internal_func;
}
static void db_calc_refresh (DbCalc * self)
{
DbIter iter;
gdouble current = 0;
GValue val = G_VALUE_INIT;
DbModel * model = self->model;
self->count = 0;
if (model)
{
DbModelStatus status = db_model_get_status (model);
if (status == DB_MODEL_STATUS_READY && self->column_name)
self->column_index = db_model_get_column_index (model, self->column_name);
if (status == DB_MODEL_STATUS_READY
&& self->column_index != -1
&& db_model_get_iter_first (model, &iter))
{
do {
self->func (self, &iter, &val, self->data);
if (!gvn_value_is_null (&val))
{
current += db_calc_value_to_double (&val);
self->count++;
}
g_value_unset (&val);
}
while (db_model_iter_next (model, &iter));
if (self->type == DB_CALC_TYPE_AVG)
current = (self->count > 0) ? current / (gdouble) self->count : 0;
}
}
if (self->count > 0)
g_value_set_double (g_value_init (&val, G_TYPE_DOUBLE), current);
else
g_value_init (&val, GVN_TYPE_NULL);
GVN_PARAM_GET_CLASS (self)->put_value (GVN_PARAM (self), &val);
g_value_unset (&val);
}
static void db_calc_on_model_refresh (DbModel * model, DbModelStatus status, DbCalc * self)
{
db_calc_refresh (self);
}
static void db_calc_on_update (DbModel * model, DbIter * iter, DbCalc * self)
{
GValue value = G_VALUE_INIT;
obj->func (obj, iter, &value, obj->data);
self->func (self, iter, &value, self->data);
if (!gvn_value_is_null (&value))
{
gdouble current, old;
current = db_calc_value_to_double (gvn_param_get_value (GVN_PARAM (obj)));
current = db_calc_value_to_double (gvn_param_get_value (GVN_PARAM (self)));
old = db_calc_value_to_double (&value);
if (obj->type == DB_CALC_TYPE_AVG)
if (self->type == DB_CALC_TYPE_AVG)
{
if (obj->count != 1)
current = (current * (gdouble) obj->count - old) / (gdouble) (obj->count - 1);
if (self->count != 1)
current = (current * (gdouble) self->count - old) / (gdouble) (self->count - 1);
else
current = 0;
}
else if (obj->type == DB_CALC_TYPE_SUM)
else if (self->type == DB_CALC_TYPE_SUM)
current -= old;
obj->count--;
self->count--;
g_value_unset (&value);
g_value_set_double (g_value_init (&value, G_TYPE_DOUBLE), current);
GVN_PARAM_GET_CLASS (obj)->put_value (GVN_PARAM (obj), &value);
GVN_PARAM_GET_CLASS (self)->put_value (GVN_PARAM (self), &value);
}
g_value_unset (&value);
}
static void db_calc_before_delete (DbModel * model, gint path, DbCalc * obj)
{
DbIter iter;
db_model_get_iter (model, &iter, path);
db_calc_before_unset (model, &iter, obj);
}
static void db_calc_after_set (DbModel * model, DbIter * iter, DbCalc * obj)
static void db_calc_on_update_after (DbModel * model, DbIter * iter, DbCalc * self)
{
GValue value = G_VALUE_INIT;
obj->func (obj, iter, &value, obj->data);
self->func (self, iter, &value, self->data);
if (!gvn_value_is_null (&value))
{
gdouble current, new;
current = db_calc_value_to_double (gvn_param_get_value (GVN_PARAM (obj)));
current = db_calc_value_to_double (gvn_param_get_value (GVN_PARAM (self)));
new = db_calc_value_to_double (&value);
if (obj->type == DB_CALC_TYPE_AVG)
current = (current * (gdouble) obj->count + new) / (gdouble) (obj->count + 1);
else if (obj->type == DB_CALC_TYPE_SUM)
if (self->type == DB_CALC_TYPE_AVG)
current = (current * (gdouble) self->count + new) / (gdouble) (self->count + 1);
else if (self->type == DB_CALC_TYPE_SUM)
current += new;
obj->count++;
self->count++;
g_value_unset (&value);
g_value_set_double (g_value_init (&value, G_TYPE_DOUBLE), current);
GVN_PARAM_GET_CLASS (obj)->put_value (GVN_PARAM (obj), &value);
GVN_PARAM_GET_CLASS (self)->put_value (GVN_PARAM (self), &value);
}
g_value_unset (&value);
}
static void db_calc_refresh (DbModel * model, DbModelStatus status, DbCalc * obj)
static void db_calc_on_delete (DbModel * model, gint path, DbCalc * self)
{
DbIter iter;
gdouble current = 0;
GValue val = G_VALUE_INIT;
db_model_get_iter (model, &iter, path);
db_calc_on_update (model, &iter, self);
}
if (status != DB_MODEL_STATUS_READY) return;
if (!db_model_get_iter_first (model, &iter)) return;
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
obj->count = 0;
void db_calc_set_column_name (DbCalc * self, const gchar * name)
{
g_return_if_fail (DB_IS_CALC (self));
do {
obj->func (obj, &iter, &val, obj->data);
db_calc_reset (self);
self->column_name = g_strdup (name);
db_calc_refresh (self);
}
if (!gvn_value_is_null (&val))
{
current += db_calc_value_to_double (&val);
obj->count++;
}
void db_calc_set_column_index (DbCalc * self, gint index)
{
g_return_if_fail (DB_IS_CALC (self));
g_value_unset (&val);
db_calc_reset (self);
self->column_index = index;
db_calc_refresh (self);
}
void db_calc_set_model (DbCalc * self, DbModel * model)
{
g_return_if_fail (DB_IS_CALC (self));
g_return_if_fail (DB_IS_MODEL (model) || !model);
if (self->model)
{
g_object_disconnect (self->model
,"any_signal::line-updated", db_calc_on_update, self
,"any_signal::line-updated", db_calc_on_update_after, self
,"any_signal::line-deleted", db_calc_on_delete, self
,"any_signal::status-changed", db_calc_on_model_refresh, self
,NULL
);
g_clear_object (&self->model);
}
while (db_model_iter_next (model, &iter));
if (obj->type == DB_CALC_TYPE_AVG)
current = (obj->count > 0) ? current / (gdouble) obj->count : 0;
g_value_set_double (g_value_init (&val, G_TYPE_DOUBLE), current);
GVN_PARAM_GET_CLASS (obj)->put_value (GVN_PARAM (obj), &val);
g_value_unset (&val);
if (model)
{
self->model = g_object_ref (model);
g_object_connect (self->model
,"signal::line-updated", db_calc_on_update, self
,"signal-after::line-updated", db_calc_on_update_after, self
,"signal::line-deleted", db_calc_on_delete, self
,"signal::status-changed", db_calc_on_model_refresh, self
,NULL
);
}
db_calc_refresh (self);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
@ -223,98 +317,91 @@ enum
,PROP_TYPE
,PROP_FUNC
,PROP_DATA
,PROP_COL
,PROP_COLUMN_INDEX
,PROP_COLUMN_NAME
};
static void db_calc_set_property (DbCalc * obj, guint id,
static void db_calc_set_property (DbCalc * self, guint id,
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_MODEL:
obj->model = g_value_dup_object (value);
if (obj->model)
g_object_connect (obj->model
,"signal::line-updated", db_calc_before_unset, obj
,"signal-after::line-updated", db_calc_after_set, obj
,"signal::line-deleted", db_calc_before_delete, obj
,"signal::status-changed", db_calc_refresh, obj
, NULL);
db_calc_set_model (self, g_value_get_object (value));
break;
case PROP_TYPE:
obj->type = g_value_get_int (value);
self->type = g_value_get_enum (value);
db_calc_refresh (self);
break;
case PROP_FUNC:
db_calc_reset (self);
if (g_value_get_pointer (value))
obj->func = g_value_get_pointer (value);
self->func = g_value_get_pointer (value);
db_calc_refresh (self);
break;
case PROP_DATA:
obj->data = g_value_get_pointer (value);
self->data = g_value_get_pointer (value);
break;
case PROP_COL:
obj->col = g_value_get_int (value);
if (!obj->func)
obj->func = db_calc_internal_func;
case PROP_COLUMN_INDEX:
db_calc_set_column_index (self, g_value_get_int (value));
break;
case PROP_COLUMN_NAME:
db_calc_set_column_name (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
static void db_calc_get_property (DbCalc * obj, guint id,
static void db_calc_get_property (DbCalc * self, guint id,
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_MODEL:
g_value_set_object (value, obj->model);
g_value_set_object (value, self->model);
break;
case PROP_TYPE:
g_value_set_int (value, obj->type);
g_value_set_enum (value, self->type);
break;
case PROP_FUNC:
g_value_set_pointer (value, obj->func);
g_value_set_pointer (value, self->func);
break;
case PROP_DATA:
g_value_set_pointer (value, obj->data);
g_value_set_pointer (value, self->data);
break;
case PROP_COL:
g_value_set_int (value, obj->col);
case PROP_COLUMN_INDEX:
g_value_set_int (value, self->column_index);
break;
case PROP_COLUMN_NAME:
g_value_set_string (value, self->column_name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
static void db_calc_init (DbCalc * obj)
static void db_calc_init (DbCalc * self)
{
GvnParamSpec * spec;
obj->model = NULL;
obj->type = 0;
obj->func = NULL;
obj->data = NULL;
obj->col = 0;
obj->count = 0;
self->model = NULL;
self->type = 0;
self->func = NULL;
self->data = NULL;
self->column_index = -1;
self->column_name = NULL;
self->count = 0;
spec = gvn_param_spec_new_with_attrs (G_TYPE_DOUBLE, FALSE, TRUE, NULL);
GVN_PARAM_GET_CLASS (obj)->set_spec (GVN_PARAM (obj), spec);
GVN_PARAM_GET_CLASS (self)->set_spec (GVN_PARAM (self), spec);
}
static void db_calc_finalize (DbCalc * obj)
static void db_calc_finalize (DbCalc * self)
{
if (obj->model)
{
g_object_disconnect (obj->model
,"any_signal::line-updated", db_calc_before_unset, obj
,"any_signal::line-updated", db_calc_after_set, obj
,"any_signal::line-deleted", db_calc_before_delete, obj
,"any_signal::status-changed", db_calc_refresh, obj
,NULL
);
g_clear_object (&obj->model);
}
db_calc_set_model (self, NULL);
g_free (self->column_name);
G_OBJECT_CLASS (db_calc_parent_class)->finalize (G_OBJECT (self));
}
static void db_calc_class_init (DbCalcClass * k)
@ -355,13 +442,21 @@ static void db_calc_class_init (DbCalcClass * k)
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)
);
g_object_class_install_property (klass, PROP_COL
,g_param_spec_int ("col"
,_("Column")
g_object_class_install_property (klass, PROP_COLUMN_INDEX
,g_param_spec_int ("column-index"
,_("Column index")
,_("A column to apply the operations over it")
,0
,G_MAXINT32
,0
,-1
,G_MAXINT
,-1
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)
);
g_object_class_install_property (klass, PROP_COLUMN_NAME
,g_param_spec_string ("column-name"
,_("Column name")
,_("A column name to apply the operations over it")
,NULL
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)
);
}

View File

@ -27,11 +27,11 @@
#define DB_CALC_LOG_DOMAIN (g_quark_from_string ("DbCalc"))
#define DB_TYPE_CALC (db_calc_get_type ())
#define DB_CALC(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, DB_TYPE_CALC, DbCalc))
#define DB_IS_CALC(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, DB_TYPE_CALC))
#define DB_CALC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((obj), DB_TYPE_CALC, DbCalcClass))
#define DB_CALC(self) (G_TYPE_CHECK_INSTANCE_CAST (self, DB_TYPE_CALC, DbCalc))
#define DB_IS_CALC(self) (G_TYPE_CHECK_INSTANCE_TYPE (self, DB_TYPE_CALC))
#define DB_CALC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((self), DB_TYPE_CALC, DbCalcClass))
#define DB_IS_CALC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DB_TYPE_CLASS))
#define DB_CALC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DB_TYPE_CALC, DbCalcClass))
#define DB_CALC_GET_CLASS(self) (G_TYPE_INSTANCE_GET_CLASS ((self), DB_TYPE_CALC, DbCalcClass))
#define DB_TYPE_CALC_TYPE (db_calc_type_get_type ())
@ -40,7 +40,7 @@ typedef struct _DbCalcClass DbCalcClass;
/**
* DbCalcFunc:
* @obj: a #DbCalc
* @self: a #DbCalc
* @iter: a #DbIter pointing to a row of the model
* @out: the return position for the resulting value
* @user_data: (closure): the data passed to the function
@ -49,7 +49,7 @@ typedef struct _DbCalcClass DbCalcClass;
* on more than one column or to perform additional operations to a column,
* previously to add them to the calculations performed by the @DbCalc itself.
**/
typedef void (*DbCalcFunc) (DbCalc * obj, DbIter * iter, GValue * out, gpointer user_data);
typedef void (*DbCalcFunc) (DbCalc * self, DbIter * iter, GValue * out, gpointer user_data);
/**
* DbCalcType:
@ -61,7 +61,7 @@ typedef void (*DbCalcFunc) (DbCalc * obj, DbIter * iter, GValue * out, gpointer
typedef enum
{
DB_CALC_TYPE_SUM,
DB_CALC_TYPE_AVG
DB_CALC_TYPE_AVG
}
DbCalcType;
@ -73,7 +73,8 @@ struct _DbCalc
DbCalcType type;
DbCalcFunc func;
gpointer data;
guint col;
guint column_index;
gchar * column_name;
guint count;
};
@ -88,7 +89,7 @@ GType db_calc_type_get_type ();
DbCalc * db_calc_new (DbModel * model
,DbCalcType type
,gint col);
,const gchar * column_name);
DbCalc * db_calc_new_with_func (DbModel * model
,DbCalcType type
,DbCalcFunc func

View File

@ -73,12 +73,18 @@ static void db_iterator_unref_param (DbIterator * obj, DbParam * param)
*/
static void db_iterator_row_num_changed (DbIterator * obj)
{
if (obj->row_selected)
obj->row = db_model_get_path (obj->model, &obj->iter);
else if (obj->selected || !obj->remember_selection)
obj->row = -1;
gint new_row = obj->row;
g_signal_emit (obj, signals[ROW_NUM_CHANGED], 0);
if (obj->row_selected)
new_row = db_model_get_path (obj->model, &obj->iter);
else if (obj->selected || !obj->remember_selection)
new_row = -1;
if (new_row != obj->row)
{
obj->row = new_row;
g_signal_emit (obj, signals[ROW_NUM_CHANGED], 0);
}
}
/*
@ -918,6 +924,7 @@ static void db_iterator_set_property (DbIterator * obj, guint id,
break;
case PROP_REMEMBER_SELECTION:
obj->remember_selection = g_value_get_boolean (value);
db_iterator_row_num_changed (obj);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);

View File

@ -1312,6 +1312,7 @@ const gchar * db_model_get_column_name (DbModel * self, gint col)
&& col < self->priv->result->ncols)
&& self->priv->column)
return self->priv->column[col].alias;
return NULL;
}
@ -1327,12 +1328,15 @@ const gchar * db_model_get_column_name (DbModel * self, gint col)
**/
gint db_model_get_column_index (DbModel * self, const gchar * name)
{
gpointer column;
g_return_val_if_fail (DB_IS_MODEL (self), -1);
if (!name || !self->priv->column_index)
return -1;
if (name && self->priv->column_index
&& g_hash_table_lookup_extended (self->priv->column_index, name, NULL, &column))
return GPOINTER_TO_INT (column);
return GPOINTER_TO_INT (g_hash_table_lookup (self->priv->column_index, name));
return -1;
}
/**
@ -2292,7 +2296,7 @@ gboolean db_model_has_pending_operations (DbModel * self)
}
static SqlObject * db_model_create_where (DbModel * self,
TableInfo * tinfo, DbOperation * operation, gboolean for_insert)
TableInfo * tinfo, DbOperation * operation, gboolean use_new_values)
{
GSList * l;
DbUpdatedField * u;
@ -2322,7 +2326,7 @@ static SqlObject * db_model_create_where (DbModel * self,
g_value = &row->value[col];
if (!for_insert)
if (!use_new_values)
{
for (n = operation->updated; n && (u = n->data); n = n->next)
if (u->column == col)
@ -2350,7 +2354,7 @@ static SqlObject * db_model_create_where (DbModel * self,
g_object_unref (g_object_ref_sink (where));
return NULL;
}
equal = sql_operation_new (SQL_OPERATION_TYPE_EQUAL);
sql_list_add (and_operands, equal);

View File

@ -7,7 +7,7 @@ example_DATA = \
example-menu.xml \
consulter.glade \
consulter-menu.xml
# customer.glade
customer.glade
# signer.glade
EXTRA_DIST = $(example_DATA)

View File

@ -4,11 +4,45 @@
<requires lib="gtk+" version="3.0"/>
<requires lib="vn" version="0.0"/>
<!-- interface-local-resource-path ../image -->
<object class="DbIterator" id="homes">
<property name="sql">SELECT id, street, pc, city, province, ok, user_id FROM user_address WHERE #link ORDER BY id</property>
<object class="VnBatch" id="models">
<child>
<object class="VnModel" id="object-model">
<property name="sql">SELECT id, object FROM object</property>
</object>
</child>
<child>
<object class="VnModel" id="homes-model">
<property name="sql">SELECT id, street, pc, city, province, ok, user_id
FROM user_address WHERE #link ORDER BY id</property>
<property name="update_flags">DB_MODEL_INSERT | DB_MODEL_DELETE | DB_MODEL_UPDATE</property>
<links>
<link field="user_address.user_id" param="id-param" linked="True"/>
</links>
</object>
</child>
<child>
<object class="VnModel" id="info-model">
<property name="sql">SELECT id, name, credit, active, born, photo, object_id
FROM "user" ORDER BY id</property>
<property name="update_flags">DB_MODEL_INSERT | DB_MODEL_DELETE | DB_MODEL_UPDATE</property>
</object>
</child>
<child>
<object class="VnModel" id="searcher-model">
<property name="sql">SELECT id, #field FROM article WHERE #filter</property>
</object>
</child>
</object>
<object class="DbIterator" id="info">
<property name="sql">SELECT id, name, credit, active, born, photo, object_id FROM "user" ORDER BY id</property>
<object class="VnIterator" id="homes">
<property name="model">homes-model</property>
</object>
<object class="VnIterator" id="info">
<property name="model">info-model</property>
<child>
<object class="DbParam" id="id-param">
<property name="column_name">id</property>
</object>
</child>
</object>
<object class="GtkBox" id="main">
<property name="visible">True</property>
@ -55,6 +89,7 @@
<object class="VnCompletion" id="searcher">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">searcher-model</property>
<property name="field">name</property>
</object>
<packing>
@ -152,6 +187,7 @@
<property name="can_focus">False</property>
<property name="iterator">info</property>
<property name="column_name">object_id</property>
<property name="model">object-model</property>
</object>
<packing>
<property name="left_attach">1</property>
@ -214,6 +250,7 @@
<property name="can_focus">False</property>
<property name="iterator">info</property>
<property name="column_name">born</property>
<property name="show_time">True</property>
</object>
<packing>
<property name="expand">False</property>
@ -234,7 +271,7 @@
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
@ -250,7 +287,6 @@
<object class="VnHandler" id="customer-handler">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<property name="iterator">info</property>
<property name="show_flags">VN_HANDLER_SHOW_REFRESH | VN_HANDLER_SHOW_UNDO | VN_HANDLER_SHOW_SAVE | VN_HANDLER_SHOW_REMOVE | VN_HANDLER_SHOW_ADD | VN_HANDLER_SHOW_SCROLL</property>
</object>
@ -364,7 +400,6 @@
<object class="VnHandler" id="address-handler">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<property name="iterator">homes</property>
<property name="show_flags">VN_HANDLER_SHOW_REFRESH | VN_HANDLER_SHOW_UNDO | VN_HANDLER_SHOW_SAVE | VN_HANDLER_SHOW_REMOVE | VN_HANDLER_SHOW_ADD</property>
</object>

View File

@ -8,5 +8,11 @@
icon="system-run">
Consulter
</form>
<form
translatable="yes"
name="customer"
icon="system-run">
Customer
</form>
</form-group>
</module>

View File

@ -11,9 +11,9 @@ libexample_la_LDFLAGS = -avoid-version
libexample_la_LIBADD = $(top_builddir)/vn/libvn.la
libexample_la_SOURCES = \
vn-consulter.h \
vn-consulter.c
# vn-customer.h
# vn-customer.c
vn-consulter.c \
vn-customer.h \
vn-customer.c
example_querydir = $(module_querydir)/example
example_query_DATA = ../sql/example/consulter.sql

View File

@ -41,17 +41,18 @@ SqlBatch * sql_batch_new ()
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void sql_batch_item_changed (SqlObject * item, SqlBatch * obj)
static void sql_batch_item_changed (SqlObject * item, SqlBatch * self)
{
sql_batch_changed (obj);
if (!self->frozen)
sql_batch_changed (self);
}
static void sql_batch_free_item (SqlBatch * obj, SqlObject * item)
static void sql_batch_free_item (SqlBatch * self, SqlObject * item)
{
if (item)
{
g_signal_handlers_disconnect_by_func (item,
sql_batch_item_changed, obj);
sql_batch_item_changed, self);
g_object_unref (item);
}
}
@ -60,20 +61,20 @@ static void sql_batch_free_item (SqlBatch * obj, SqlObject * item)
/**
* sql_batch_is_ready:
* @obj: a #SqlBatch
* @self: a #SqlBatch
*
* Checks if @obj and all of its elements are ready to be rendered.
* Checks if @self and all of its elements are ready to be rendered.
*
* Return value: %TRUE if ready, %FALSE otherwise.
**/
gboolean sql_batch_is_ready (SqlBatch * obj)
gboolean sql_batch_is_ready (SqlBatch * self)
{
gpointer item;
GHashTableIter iter;
g_return_val_if_fail (SQL_IS_BATCH (obj), FALSE);
g_return_val_if_fail (SQL_IS_BATCH (self), FALSE);
g_hash_table_iter_init (&iter, obj->items);
g_hash_table_iter_init (&iter, self->items);
while (g_hash_table_iter_next (&iter, NULL, &item))
if (!item || !sql_object_is_ready (item))
@ -84,7 +85,7 @@ gboolean sql_batch_is_ready (SqlBatch * obj)
/**
* sql_batch_get:
* @obj: a #SqlBatch
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
*
* Gets a held object by its id.
@ -92,107 +93,108 @@ gboolean sql_batch_is_ready (SqlBatch * obj)
* Return value: (transfer none): the #SqlObject if an object with that id
* exists, %NULL otherwise
**/
SqlObject * sql_batch_get (SqlBatch * obj, const gchar * id)
SqlObject * sql_batch_get (SqlBatch * self, const gchar * id)
{
g_return_val_if_fail (SQL_IS_BATCH (obj), NULL);
g_return_val_if_fail (SQL_IS_BATCH (self), NULL);
g_return_val_if_fail (id, NULL);
if (obj->items)
return g_hash_table_lookup (obj->items, id);
if (self->items)
return g_hash_table_lookup (self->items, id);
return NULL;
}
/**
* sql_batch_add:
* @obj: a #SqlBatch
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
* @item: the #SqlObject
*
* Adds a new item to the batch.
**/
void sql_batch_add (SqlBatch * obj, const gchar * id, SqlObject * item)
void sql_batch_add (SqlBatch * self, const gchar * id, SqlObject * item)
{
g_return_if_fail (SQL_IS_BATCH (obj));
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
g_return_if_fail (SQL_IS_OBJECT (item) || !item);
sql_batch_remove (obj, id);
sql_batch_remove (self, id);
if (item)
{
g_object_ref_sink (item);
g_signal_connect (item, "changed",
G_CALLBACK (sql_batch_item_changed), obj);
G_CALLBACK (sql_batch_item_changed), self);
}
g_hash_table_replace (obj->items, g_strdup (id), item);
g_hash_table_replace (self->items, g_strdup (id), item);
sql_batch_item_changed (NULL, self);
}
/**
* sql_batch_add_from_param:
* @obj: a #SqlBatch
* sql_batch_add_param:
* @self: a #SqlBatch
* @id: the id assigned to the item
* @param: the parameter
**/
void sql_batch_add_from_param (SqlBatch * obj, const gchar * id, GvnParam * param)
void sql_batch_add_from_param (SqlBatch * self, const gchar * id, GvnParam * param)
{
g_return_if_fail (SQL_IS_BATCH (obj));
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
g_return_if_fail (GVN_IS_PARAM (param));
sql_batch_add (obj, id, sql_value_new_with_param (param));
sql_batch_add (self, id, sql_value_new_with_param (param));
}
/**
* sql_batch_add_from_value:
* @obj: a #SqlBatch
* sql_batch_add_value:
* @self: a #SqlBatch
* @id: the id assigned to the item
* @value: the value
**/
void sql_batch_add_from_value (SqlBatch * obj, const gchar * id, GType type, gpointer content)
void sql_batch_add_from_value (SqlBatch * self, const gchar * id, const GValue * value)
{
g_return_if_fail (SQL_IS_BATCH (obj));
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (id);
g_return_if_fail (G_IS_VALUE (value));
GValue gvalue = {0};
gvn_value_new_with_content (&gvalue, type, content);
sql_batch_add (obj, id, sql_value_new_with_value (&gvalue));
g_value_unset (&gvalue);
sql_batch_add (self, id, sql_value_new_with_value (value));
}
/**
* sql_batch_remove:
* @obj: a #SqlBatch
* @self: a #SqlBatch
* @id: the id of the #SqlHolder
*
* Removes a held object from the batch.
**/
void sql_batch_remove (SqlBatch * obj, const gchar * id)
void sql_batch_remove (SqlBatch * self, const gchar * id)
{
g_return_val_if_fail (SQL_IS_BATCH (obj), NULL);
g_return_val_if_fail (SQL_IS_BATCH (self), NULL);
g_return_val_if_fail (id, NULL);
SqlObject * item = sql_batch_get (obj, id);
SqlObject * item = sql_batch_get (self, id);
if (item)
{
sql_batch_free_item (obj, item);
g_hash_table_remove (obj->items, id);
sql_batch_free_item (self, item);
g_hash_table_remove (self->items, id);
}
}
/**
* sql_batch_merge:
* @obj: a #SqlBatch
* @self: a #SqlBatch
* @batch: a #SqlBatch
*
* Meges the batch with another batch.
**/
void sql_batch_merge (SqlBatch * obj, SqlBatch * batch)
void sql_batch_merge (SqlBatch * self, SqlBatch * batch)
{
GHashTableIter iter;
gpointer key, value;
g_return_if_fail (SQL_IS_BATCH (obj));
g_return_if_fail (SQL_IS_BATCH (self));
g_return_if_fail (SQL_IS_BATCH (batch) || !batch);
if (!batch)
@ -201,47 +203,74 @@ void sql_batch_merge (SqlBatch * obj, SqlBatch * batch)
g_hash_table_iter_init (&iter, batch->items);
while (g_hash_table_iter_next (&iter, &key, &value))
sql_batch_add (obj, key, value);
sql_batch_add (self, key, value);
}
/**
* sql_batch_changed:
* @obj: a #SqlBatch
* @self: a #SqlBatch
*
* Emits the changed signal on #SqlBatch.
**/
void sql_batch_changed (SqlBatch * obj)
void sql_batch_changed (SqlBatch * self)
{
g_return_if_fail (SQL_IS_BATCH (obj));
g_return_if_fail (SQL_IS_BATCH (self));
g_signal_emit (obj, signals[CHANGED], 0);
g_signal_emit (self, signals[CHANGED], 0);
}
/**
* sql_batch_freeze:
* @self: a #SqlBatch
*
* Freezes the changed signal on #SqlBatch.
**/
void sql_batch_freeze (SqlBatch * self)
{
g_return_if_fail (SQL_IS_BATCH (self));
self->frozen = TRUE;
}
/**
* sql_batch_unfreeze:
* @self: a #SqlBatch
*
* Unfreezes the changed signal on #SqlBatch.
**/
void sql_batch_unfreeze (SqlBatch * self)
{
g_return_if_fail (SQL_IS_BATCH (self));
self->frozen = FALSE;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void sql_batch_init (SqlBatch * obj)
static void sql_batch_init (SqlBatch * self)
{
obj->items = g_hash_table_new_full (
self->items = g_hash_table_new_full (
g_str_hash
,g_str_equal
,g_free
,NULL
);
self->frozen = FALSE;
}
static void sql_batch_finalize (SqlBatch * obj)
static void sql_batch_finalize (SqlBatch * self)
{
gpointer item;
GHashTableIter iter;
g_hash_table_iter_init (&iter, obj->items);
g_hash_table_iter_init (&iter, self->items);
while (g_hash_table_iter_next (&iter, NULL, &item))
sql_batch_free_item (obj, item);
sql_batch_free_item (self, item);
g_hash_table_destroy (obj->items);
g_hash_table_destroy (self->items);
G_OBJECT_CLASS (sql_batch_parent_class)->finalize (G_OBJECT (obj));
G_OBJECT_CLASS (sql_batch_parent_class)->finalize (G_OBJECT (self));
}
static void sql_batch_class_init (SqlBatchClass * klass)
@ -250,7 +279,7 @@ static void sql_batch_class_init (SqlBatchClass * klass)
/**
* SqlBatch::changed:
* @obj: the object which emit the signal.
* @self: the object which emit the signal.
*
* This signal is emitted every time the batch or one of its attributes
* is modified.

View File

@ -21,10 +21,10 @@
#include <glib-object.h>
#define SQL_TYPE_BATCH (sql_batch_get_type ())
#define SQL_BATCH(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, SQL_TYPE_BATCH, SqlBatch))
#define SQL_IS_BATCH(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, SQL_TYPE_BATCH))
#define SQL_BATCH(self) (G_TYPE_CHECK_INSTANCE_CAST (self, SQL_TYPE_BATCH, SqlBatch))
#define SQL_IS_BATCH(self) (G_TYPE_CHECK_INSTANCE_TYPE (self, SQL_TYPE_BATCH))
#define SQL_BATCH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST (klass, SQL_TYPE_BATCH, SqlBatchClass))
#define SQL_BATCH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS (obj, SQL_TYPE_BATCH, SqlBatchClass))
#define SQL_BATCH_GET_CLASS(self) (G_TYPE_INSTANCE_GET_CLASS (self, SQL_TYPE_BATCH, SqlBatchClass))
typedef struct _SqlBatch SqlBatch;
typedef struct _SqlBatchClass SqlBatchClass;
@ -33,6 +33,7 @@ struct _SqlBatch
{
GInitiallyUnowned parent;
GHashTable * items;
gboolean frozen;
};
struct _SqlBatchClass
@ -45,13 +46,15 @@ struct _SqlBatchClass
GType sql_batch_get_type ();
SqlBatch * sql_batch_new ();
gboolean sql_batch_is_ready (SqlBatch * obj);
SqlObject * sql_batch_get (SqlBatch * obj, const gchar * id);
void sql_batch_add (SqlBatch * obj, const gchar * id, SqlObject * item);
void sql_batch_add_from_param (SqlBatch * obj, const gchar * id, GvnParam * param);
void sql_batch_add_from_value (SqlBatch * obj, const gchar * id, GType type, gpointer content);
void sql_batch_remove (SqlBatch * obj, const gchar * id);
void sql_batch_merge (SqlBatch * obj, SqlBatch * batch);
void sql_batch_changed (SqlBatch * obj);
gboolean sql_batch_is_ready (SqlBatch * self);
SqlObject * sql_batch_get (SqlBatch * self, const gchar * id);
void sql_batch_add (SqlBatch * self, const gchar * id, SqlObject * item);
void sql_batch_add_from_param (SqlBatch * self, const gchar * id, GvnParam * param);
void sql_batch_add_from_value (SqlBatch * self, const gchar * id, const GValue * value);
void sql_batch_remove (SqlBatch * self, const gchar * id);
void sql_batch_merge (SqlBatch * self, SqlBatch * batch);
void sql_batch_changed (SqlBatch * self);
void sql_batch_freeze (SqlBatch * self);
void sql_batch_unfreeze (SqlBatch * self);
#endif

View File

@ -63,7 +63,10 @@ static void sql_value_render (SqlValue * obj, SqlRender * render)
sql_render_printf (render, "'%c'", g_value_get_schar (value));
break;
case G_TYPE_INT:
sql_render_printf (render, "%i", g_value_get_int (value));
sql_render_printf (render, "%d", g_value_get_int (value));
break;
case G_TYPE_INT64:
sql_render_printf (render, "%lld", g_value_get_int64 (value));
break;
case G_TYPE_UINT:
sql_render_printf (render, "%u", g_value_get_uint (value));

View File

@ -210,8 +210,14 @@ static void vn_column_spin_init (VnColumnSpin * obj)
{
GtkCellRenderer * cell = gtk_cell_renderer_spin_new ();
obj->adjustment = gtk_adjustment_new (0.0, G_MININT, G_MAXINT, 0.0, 1.0, 0.0);
g_object_set (cell, "adjustment", obj->adjustment, NULL);
obj->adjustment = gtk_adjustment_new (0.0, G_MININT, G_MAXINT, 0.0, 1.0, 0.0);
g_object_set (cell
,"adjustment", obj->adjustment
,"xalign", 1.0
,"alignment", PANGO_ALIGN_RIGHT
,NULL
);
obj->format = NULL;

View File

@ -93,7 +93,7 @@ static void vn_completion_cb_status_changed (DbModel * model,
static void vn_completion_cb_changed (GtkEditable * entry, VnCompletion * obj)
{
const gchar * text = gtk_entry_get_text (GTK_ENTRY (entry));
if (obj->invalid)
{
obj->invalid = FALSE;
@ -124,7 +124,7 @@ static void vn_completion_cb_changed (GtkEditable * entry, VnCompletion * obj)
static void vn_completion_iter_changed (VnCompletion * obj, DbIter * iter)
{
const GValue * value = db_model_get_value (obj->model, iter, 0, NULL);
if (value)
VN_FIELD_GET_CLASS (obj)->value_changed (VN_FIELD (obj), value);

View File

@ -76,7 +76,8 @@ static void vn_column_data_func (GtkTreeViewColumn * col, GtkCellRenderer * cell
else
background = NULL;
g_object_set (cell, "cell-background-rgba", background, NULL);
if (background)
g_object_set (cell, "cell-background-rgba", background, NULL);
if (obj->style_func)
obj->style_func (VN_COLUMN (col), cell, iter, &value);
@ -420,7 +421,7 @@ static void vn_column_set_property (VnColumn * obj, guint id,
case PROP_STYLE_FUNC:
obj->style_func = g_value_get_pointer (value);
break;
default:
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
}
}

View File

@ -473,7 +473,9 @@ static void vn_handler_init (VnHandler * obj)
obj->buttons = GTK_WIDGET (g_object_new (GTK_TYPE_BUTTON_BOX
,"layout-style", GTK_BUTTONBOX_EXPAND
,"homogeneous", FALSE
,NULL));
,"halign", GTK_ALIGN_END
,NULL
));
context = gtk_widget_get_style_context (obj->buttons);
provider = gtk_css_provider_get_default ();