Change qty and add items

Current quantity can be changed by clicking add or remove
Add new series
This commit is contained in:
Tom Willemsen 2010-10-19 23:59:55 +02:00
parent c34819ee2c
commit 6628dddb27
4 changed files with 146 additions and 24 deletions

View file

@ -10,7 +10,8 @@
G_DEFINE_TYPE(CMainWindow, c_main_window, HILDON_TYPE_STACKABLE_WINDOW) G_DEFINE_TYPE(CMainWindow, c_main_window, HILDON_TYPE_STACKABLE_WINDOW)
enum { enum {
COL_NAME = 0, COL_ID = 0,
COL_NAME,
COL_CURRENT, COL_CURRENT,
COL_TOTAL, COL_TOTAL,
NUM_COLS NUM_COLS
@ -20,6 +21,9 @@ static void c_main_window_add_menu(CMainWindow *window);
static void c_main_window_on_new(GtkWidget *widget, GtkWindow *window); static void c_main_window_on_new(GtkWidget *widget, GtkWindow *window);
static void c_main_window_on_selection_changed(GtkTreeSelection *selection, static void c_main_window_on_selection_changed(GtkTreeSelection *selection,
gpointer user_data); gpointer user_data);
static void c_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data);
static void c_main_window_on_remove_clicked(GtkWidget *widget,
gpointer user_data);
GtkWidget *c_main_window_new(void) GtkWidget *c_main_window_new(void)
{ {
@ -36,7 +40,8 @@ void c_main_window_load(CMainWindow *self)
while (list) { while (list) {
struct collection *col = list->data; struct collection *col = list->data;
c_main_window_add_line(self, col->name, col->current_qty, col->total_qty); c_main_window_add_line(self, col->id, col->name,
col->current_qty, col->total_qty);
list = g_list_next(list); list = g_list_next(list);
} }
@ -44,12 +49,14 @@ void c_main_window_load(CMainWindow *self)
} }
void c_main_window_add_line(CMainWindow *window, void c_main_window_add_line(CMainWindow *window,
gint id,
const gchar *name, const gchar *name,
gint current_qty, gint current_qty,
gint total_qty) gint total_qty)
{ {
gtk_list_store_append(window->store, &window->iter); gtk_list_store_append(window->store, &window->iter);
gtk_list_store_set(window ->store, &window->iter, gtk_list_store_set(window ->store, &window->iter,
COL_ID, id,
COL_NAME, name, COL_NAME, name,
COL_CURRENT, current_qty, COL_CURRENT, current_qty,
COL_TOTAL, total_qty, COL_TOTAL, total_qty,
@ -81,8 +88,8 @@ static void c_main_window_init(CMainWindow *window)
GtkWidget *view; GtkWidget *view;
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *hbuttonbox; GtkWidget *hbuttonbox;
GtkWidget *pannablearea;
GtkTreeViewColumn *current_column; GtkTreeViewColumn *current_column;
GtkTreeSelection *selection;
int index; int index;
index = -1; index = -1;
@ -93,17 +100,25 @@ static void c_main_window_init(CMainWindow *window)
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
window->store = gtk_list_store_new(NUM_COLS, window->store = gtk_list_store_new(NUM_COLS,
G_TYPE_INT,
G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_INT, G_TYPE_INT,
G_TYPE_INT); G_TYPE_INT);
vbox = gtk_vbox_new(FALSE, 0); vbox = gtk_vbox_new(FALSE, 0);
view = gtk_tree_view_new(); pannablearea = hildon_pannable_area_new();
gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 0); g_object_set(G_OBJECT(pannablearea),
"mov-mode", HILDON_MOVEMENT_MODE_VERT,
NULL);
gtk_box_pack_start(GTK_BOX(vbox), pannablearea, TRUE, TRUE, 0);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view)); view = gtk_tree_view_new();
g_signal_connect(selection, "changed", hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(pannablearea),
view);
window->selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
g_signal_connect(window->selection, "changed",
G_CALLBACK(c_main_window_on_selection_changed), G_CALLBACK(c_main_window_on_selection_changed),
(gpointer)window); (gpointer)window);
@ -149,11 +164,28 @@ static void c_main_window_init(CMainWindow *window)
gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox), GTK_BUTTONBOX_END); gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox), GTK_BUTTONBOX_END);
gtk_box_pack_start(GTK_BOX(vbox), hbuttonbox, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbuttonbox, FALSE, TRUE, 0);
window->add_button = gtk_button_new_from_stock(GTK_STOCK_ADD); window->add_button =
hildon_button_new_with_text(HILDON_SIZE_AUTO_WIDTH |
HILDON_SIZE_FINGER_HEIGHT,
HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
"Add",
NULL);
g_signal_connect(window->add_button, "clicked",
G_CALLBACK(c_main_window_on_add_clicked),
(gpointer)window);
gtk_box_pack_start(GTK_BOX(hbuttonbox), window->add_button, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbuttonbox), window->add_button, FALSE, FALSE, 0);
window->remove_button = gtk_button_new_from_stock(GTK_STOCK_REMOVE); window->remove_button =
gtk_box_pack_start(GTK_BOX(hbuttonbox), window->remove_button, FALSE, FALSE, 0); hildon_button_new_with_text(HILDON_SIZE_AUTO_WIDTH |
HILDON_SIZE_FINGER_HEIGHT,
HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
"Remove",
NULL);
g_signal_connect(window->remove_button, "clicked",
G_CALLBACK(c_main_window_on_remove_clicked),
(gpointer)window);
gtk_box_pack_start(GTK_BOX(hbuttonbox),
window->remove_button, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox); gtk_container_add(GTK_CONTAINER(window), vbox);
@ -223,3 +255,54 @@ static void c_main_window_on_selection_changed(GtkTreeSelection *selection,
else else
c_main_window_set_has_select(self); c_main_window_set_has_select(self);
} }
static void c_main_window_on_add_clicked(GtkWidget *widget, gpointer user_data)
{
CMainWindow *self;
gint count;
self = (CMainWindow *)user_data;
count = gtk_tree_selection_count_selected_rows(self->selection);
if (count > 0) {
GtkTreeModel *model;
if (gtk_tree_selection_get_selected(self->selection, &model, &self->iter)) {
gint id;
gint current_count;
gtk_tree_model_get(model, &self->iter, COL_ID, &id, -1);
gtk_tree_model_get(model, &self->iter, COL_CURRENT, &current_count, -1);
if (data_add_to_series(id, 1))
gtk_list_store_set(GTK_LIST_STORE(self->store), &self->iter,
COL_CURRENT, current_count + 1, -1);
}
}
}
static void c_main_window_on_remove_clicked(GtkWidget *widget,
gpointer user_data)
{
CMainWindow *self;
gint count;
self = (CMainWindow *)user_data;
count = gtk_tree_selection_count_selected_rows(self->selection);
if (count > 0) {
GtkTreeModel *model;
if (gtk_tree_selection_get_selected(self->selection, &model, &self->iter)) {
gint id;
gint current_count;
gtk_tree_model_get(model, &self->iter, COL_ID, &id, -1);
gtk_tree_model_get(model, &self->iter, COL_CURRENT, &current_count, -1);
if (current_count > 0 && data_add_to_series(id, -1))
gtk_list_store_set(GTK_LIST_STORE(self->store), &self->iter,
COL_CURRENT, current_count - 1, -1);
}
}
}

View file

@ -45,9 +45,11 @@ struct _CMainWindow
GtkTreeIter iter; GtkTreeIter iter;
GtkListStore *store; GtkListStore *store;
GtkTreeSelection *selection;
GtkWidget *add_button; GtkWidget *add_button;
GtkWidget *remove_button; GtkWidget *remove_button;
GtkWidget *edit_button;
}; };
GType c_main_window_get_type(void); GType c_main_window_get_type(void);
@ -56,6 +58,7 @@ GtkWidget *c_main_window_new(void);
void c_main_window_load(CMainWindow *self); void c_main_window_load(CMainWindow *self);
void c_main_window_add_line(CMainWindow *window, void c_main_window_add_line(CMainWindow *window,
gint id,
const gchar *name, const gchar *name,
gint current_qty, gint current_qty,
gint total_qty); gint total_qty);

View file

@ -9,7 +9,7 @@
#include "collections.h" #include "collections.h"
static gboolean data_check_and_create_database(gchar *data_file); static gboolean data_check_and_create_database(gchar *data_file);
static gint data_create_new_database(const char *filename); static gint data_create_new_database(const gchar *filename);
GList *data_get_series(void) GList *data_get_series(void)
{ {
@ -24,7 +24,8 @@ GList *data_get_series(void)
if (sqlite3_open(data_file, &database) == SQLITE_OK) { if (sqlite3_open(data_file, &database) == SQLITE_OK) {
int res; int res;
const char *sqlStatement = const char *sqlStatement =
" SELECT name, " " SELECT id, "
" name, "
" current_qty, " " current_qty, "
" total_qty " " total_qty "
" FROM collection " " FROM collection "
@ -38,11 +39,12 @@ GList *data_get_series(void)
if (res == SQLITE_OK) { if (res == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) { while (sqlite3_step(statement) == SQLITE_ROW) {
struct collection *col = struct collection *col =
(struct collection *)malloc(sizeof(struct collection *)); (struct collection *)malloc(sizeof(struct collection));
col->name = g_strdup(sqlite3_column_text(statement, 0)); col->id = sqlite3_column_int(statement, 0);
col->current_qty = sqlite3_column_int(statement, 1); col->name = g_strdup(sqlite3_column_text(statement, 1));
col->total_qty = sqlite3_column_int(statement, 2); col->current_qty = sqlite3_column_int(statement, 2);
col->total_qty = sqlite3_column_int(statement, 3);
list = g_list_append(list, (gpointer)col); list = g_list_append(list, (gpointer)col);
} }
@ -51,14 +53,14 @@ GList *data_get_series(void)
g_print("error %d: %s\n", res, sqlite3_errmsg(database)); g_print("error %d: %s\n", res, sqlite3_errmsg(database));
/* Release the compiled statement from memory */ /* Release the compiled statement from memory */
sqlite3_finalize(statement); sqlite3_finalize(statement);
} }
sqlite3_close(database); sqlite3_close(database);
} }
return list; return list;
} }
gboolean data_add_series(gchar *name, int total_qty) gboolean data_add_series(gchar *name, gint total_qty)
{ {
sqlite3 *database; sqlite3 *database;
sqlite3_stmt *statement; sqlite3_stmt *statement;
@ -87,6 +89,38 @@ gboolean data_add_series(gchar *name, int total_qty)
return FALSE; return FALSE;
} }
gboolean data_add_to_series(gint collection_id, gint count)
{
sqlite3 *database;
sqlite3_stmt *statement;
gchar *data_file;
g_print("collection_id: %d, count: %d\n", collection_id, count);
data_file = collections_get_data_file();
if (data_check_and_create_database(data_file)) {
if (sqlite3_open(data_file, &database) == SQLITE_OK) {
int res;
const char *sqlStatement =
g_strdup_printf("UPDATE collection "
" SET current_qty = current_qty + %d "
" WHERE id = %d", count, collection_id);
res = sqlite3_prepare_v2(database,
sqlStatement,
strlen(sqlStatement),
&statement, NULL);
if (res == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE)
return TRUE;
}
}
}
return FALSE;
}
static gboolean data_check_and_create_database(gchar *data_file) static gboolean data_check_and_create_database(gchar *data_file)
{ {
if (!access(data_file, R_OK) == 0) if (!access(data_file, R_OK) == 0)
@ -98,7 +132,7 @@ static gboolean data_check_and_create_database(gchar *data_file)
return TRUE; return TRUE;
} }
static gint data_create_new_database(const char *filename) static gint data_create_new_database(const gchar *filename)
{ {
sqlite3 *db; sqlite3 *db;
char *zErrMsg = 0; char *zErrMsg = 0;

View file

@ -2,10 +2,12 @@
struct collection struct collection
{ {
const gchar *name; int id;
gint current_qty; int current_qty;
gint total_qty; int total_qty;
char *name;
}; };
GList *data_get_series(void); GList *data_get_series(void);
gboolean data_add_series(gchar *name, int total_qty); gboolean data_add_series(gchar *name, gint total_qty);
gboolean data_add_to_series(gint id, gint count);