add gitignore
[chaz/homebank] / src / rep_vehicle.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #include "homebank.h"
22
23 #include "rep_vehicle.h"
24
25 #include "list_operation.h"
26 #include "gtk-chart.h"
27 #include "gtk-dateentry.h"
28
29 #include "dsp_mainwindow.h"
30 #include "ui-category.h"
31
32
33 /****************************************************************************/
34 /* Debug macros */
35 /****************************************************************************/
36 #define MYDEBUG 0
37
38 #if MYDEBUG
39 #define DB(x) (x);
40 #else
41 #define DB(x);
42 #endif
43
44 /* our global datas */
45 extern struct HomeBank *GLOBALS;
46 extern struct Preferences *PREFS;
47
48
49 /* prototypes */
50 static void repvehicle_date_change(GtkWidget *widget, gpointer user_data);
51 static void repvehicle_range_change(GtkWidget *widget, gpointer user_data);
52 static void repvehicle_compute(GtkWidget *widget, gpointer user_data);
53 static void repvehicle_update(GtkWidget *widget, gpointer user_data);
54 static void repvehicle_toggle_minor(GtkWidget *widget, gpointer user_data);
55 static void repvehicle_setup(struct repvehicle_data *data);
56 static gboolean repvehicle_window_dispose(GtkWidget *widget, GdkEvent *event, gpointer user_data);
57 static GtkWidget *create_list_repvehicle(void);
58
59
60 static void repvehicle_date_change(GtkWidget *widget, gpointer user_data)
61 {
62 struct repvehicle_data *data;
63
64 DB( g_print("(repvehicle) date change\n") );
65
66 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
67
68 data->filter->mindate = gtk_dateentry_get_date(GTK_DATE_ENTRY(data->PO_mindate));
69 data->filter->maxdate = gtk_dateentry_get_date(GTK_DATE_ENTRY(data->PO_maxdate));
70
71 // set min/max date for both widget
72 gtk_dateentry_set_maxdate(GTK_DATE_ENTRY(data->PO_mindate), data->filter->maxdate);
73 gtk_dateentry_set_mindate(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->mindate);
74
75 g_signal_handler_block(data->CY_range, data->handler_id[HID_REPVEHICLE_RANGE]);
76 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_range), 11);
77 g_signal_handler_unblock(data->CY_range, data->handler_id[HID_REPVEHICLE_RANGE]);
78
79
80 repvehicle_compute(widget, NULL);
81
82 }
83
84
85
86 static void repvehicle_range_change(GtkWidget *widget, gpointer user_data)
87 {
88 struct repvehicle_data *data;
89 gint range;
90
91 DB( g_print("(repvehicle) range change\n") );
92
93 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
94
95 range = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_range));
96
97 if(range != 11)
98 {
99 filter_preset_daterange_set(data->filter, range);
100
101 g_signal_handler_block(data->PO_mindate, data->handler_id[HID_REPVEHICLE_MINDATE]);
102 g_signal_handler_block(data->PO_maxdate, data->handler_id[HID_REPVEHICLE_MAXDATE]);
103
104 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_mindate), data->filter->mindate);
105 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->maxdate);
106
107 g_signal_handler_unblock(data->PO_mindate, data->handler_id[HID_REPVEHICLE_MINDATE]);
108 g_signal_handler_unblock(data->PO_maxdate, data->handler_id[HID_REPVEHICLE_MAXDATE]);
109
110 repvehicle_compute(widget, NULL);
111 }
112 }
113
114 static gint repvehicle_transaction_compare_func(CarCost *a, CarCost *b)
115 {
116 gint retval;
117
118 //retval = (gint)(a->ope->date - b->ope->date);
119 //if( retval == 0 )
120 retval = a->meter - b->meter;
121
122 return retval;
123 }
124
125
126 //#1277622
127 static CarCost *repvehicle_eval_memofield(CarCost *item, gchar *text)
128 {
129 gchar *d, *v1, *v2;
130 gint len;
131
132 if( text != NULL)
133 {
134 len = strlen(text);
135 d = g_strstr_len(text, len, "d=");
136 v1 = g_strstr_len(text, len, "v=");
137 v2 = g_strstr_len(text, len, "v~");
138 if(d && (v1 || v2))
139 {
140 item->meter = atol(d+2);
141 if(v1)
142 {
143 item->fuel = g_strtod(v1+2, NULL);
144 item->partial = FALSE;
145 }
146 else
147 {
148 item->fuel = g_strtod(v2+2, NULL);
149 item->partial = TRUE;
150 }
151 }
152 }
153
154 return item;
155 }
156
157
158 static void repvehicle_compute(GtkWidget *widget, gpointer user_data)
159 {
160 struct repvehicle_data *data;
161 GList *list;
162 guint32 catkey;
163
164 DB( g_print("(repvehicle) compute\n") );
165
166 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
167
168 // clear the glist
169 da_vehiclecost_destroy(data->vehicle_list);
170 data->vehicle_list = NULL;
171
172 // get the category key
173 catkey = ui_cat_comboboxentry_get_key(GTK_COMBO_BOX(data->PO_cat));
174
175 /* do nothing if no transaction or cat is 0 */
176 if( g_list_length(GLOBALS->ope_list) == 0 || catkey == 0 )
177 goto end1;
178
179 DB( g_print(" -> active cat is %d\n", catkey) );
180
181 // collect transactions
182 // the purpose here is to collect all cat transaction
183 // and precompute some datas
184 list = g_list_first(GLOBALS->ope_list);
185 while (list != NULL)
186 {
187 Transaction *ope = list->data;
188 Category *cat;
189 CarCost *item;
190 Account *acc;
191
192 acc = da_acc_get(ope->kacc);
193 if(acc == NULL) goto next1;
194 if((acc->flags & (AF_CLOSED|AF_NOREPORT))) goto next1;
195 if((ope->flags & OF_REMIND)) goto next1;
196
197 // eval normal transaction
198 if(!(ope->flags & OF_SPLIT))
199 {
200 cat = da_cat_get(ope->kcat);
201 if( cat && (cat->key == catkey || cat->parent == catkey) )
202 {
203 item = da_vehiclecost_malloc();
204 item->date = ope->date;
205 item->wording = ope->wording;
206 item->amount = ope->amount;
207
208 item = repvehicle_eval_memofield(item, ope->wording);
209 data->vehicle_list = g_list_append(data->vehicle_list, item);
210 DB( g_print(" -> store acc=%d '%s' %.2f\n", ope->kacc, ope->wording, ope->amount) );
211 }
212 }
213 // eval split transaction
214 else
215 {
216 guint i, nbsplit = da_transaction_splits_count(ope);
217 Split *split;
218
219 DB( g_print(" -> nb split %d\n", nbsplit) );
220
221 for(i=0;i<nbsplit;i++)
222 {
223 split = ope->splits[i];
224 cat = da_cat_get(split->kcat);
225
226 DB( g_print(" -> eval split '%s'\n", split->memo) );
227
228 if( cat && (cat->key == catkey || cat->parent == catkey) )
229 {
230 item = da_vehiclecost_malloc();
231 item->date = ope->date;
232 item->wording = split->memo;
233 item->amount = split->amount;
234
235 item = repvehicle_eval_memofield(item, split->memo);
236 data->vehicle_list = g_list_append(data->vehicle_list, item);
237 DB( g_print(" -> store split part acc=%d '%s' %.2f\n", ope->kacc, split->memo, split->amount) );
238 }
239 }
240 }
241
242 next1:
243 list = g_list_next(list);
244 }
245
246 // sort by meter #399170
247 data->vehicle_list = g_list_sort(data->vehicle_list, (GCompareFunc)repvehicle_transaction_compare_func);
248
249 end1:
250 repvehicle_update(widget, NULL);
251 }
252
253
254 static void repvehicle_update(GtkWidget *widget, gpointer user_data)
255 {
256 struct repvehicle_data *data;
257 GtkTreeModel *model;
258 GtkTreeIter iter;
259 GList *list;
260 gchar *buf;
261 gint nb_refuel = 0;
262 guint lastmeter = 0;
263
264 DB( g_print("(repvehicle) update\n") );
265
266 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
267
268 // clear and detach our model
269 model = gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report));
270 gtk_list_store_clear (GTK_LIST_STORE(model));
271 g_object_ref(model);
272 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_report), NULL);
273
274 data->total_misccost = 0;
275 data->total_fuelcost = 0;
276 data->total_fuel = 0;
277 data->total_dist = 0;
278
279 gdouble partial_fuel = 0;
280 guint partial_dist = 0;
281
282
283 list = g_list_first(data->vehicle_list);
284 while (list != NULL)
285 {
286 CarCost *item = list->data;
287 gint dist;
288 gdouble centkm;
289 guint distbyvol;
290 gdouble trn_amount;
291 //Account *acc;
292
293 if( (item->date >= data->filter->mindate) && (item->date <= data->filter->maxdate) )
294 {
295 // get amount in base currency
296 //acc = da_acc_get(item->ope->kacc);
297 //trn_amount = to_base_amount(item->ope->amount, acc->kcur);
298 trn_amount = item->amount;
299
300
301 if( item->meter == 0 )
302 {
303 data->total_misccost += trn_amount;
304 }
305 else
306 {
307 if(nb_refuel > 0 )
308 {
309 //previtem = g_list_nth_data(data->vehicle_list, nb_refuel-1);
310 //if(previtem != NULL) previtem->dist = item->meter - previtem->meter;
311 //DB( g_print(" + previous item dist = %d\n", item->meter - previtem->meter) );
312 item->dist = item->meter - lastmeter;
313
314 //DB( g_print(" + last meter = %d\n", lastmeter) );
315
316 }
317
318 lastmeter = item->meter;
319 nb_refuel++;
320
321 DB( g_print(" eval : d=%d v=%4.2f $%8.2f dist=%d\n", item->meter, item->fuel, trn_amount, item->dist) );
322
323 //bugfix #159066
324 if(item->partial == FALSE)
325 {
326 // full refuel after partial
327 if(partial_fuel && partial_dist)
328 {
329 DB( g_print(" + full refuel after partial\n") );
330 partial_fuel += item->fuel;
331 partial_dist += item->dist;
332 dist = item->dist;
333 centkm = partial_dist != 0 ? partial_fuel * 100 / partial_dist : 0;
334 }
335 else
336 {
337 DB( g_print(" + real full refuel\n") );
338 dist = item->dist;
339 centkm = item->dist != 0 ? item->fuel * 100 / item->dist : 0;
340 }
341 partial_fuel = 0;
342 partial_dist = 0;
343 }
344 // partial refuel
345 else
346 {
347 DB( g_print(" + partial refuel\n") );
348 partial_fuel += item->fuel;
349 partial_dist += item->dist;
350 dist = item->dist;
351 centkm = 0;
352 }
353
354 distbyvol = 0;
355 if(centkm != 0)
356 distbyvol = arrondi((1/centkm)*100, 0);
357
358 DB( g_print(" + pf=%.2f pd=%d :: dbv=%d\n", partial_fuel, partial_dist, distbyvol) );
359
360
361
362 gtk_list_store_append (GTK_LIST_STORE(model), &iter);
363
364 gtk_list_store_set (GTK_LIST_STORE(model), &iter,
365 LST_CAR_DATE , item->date,
366 LST_CAR_WORDING , item->wording,
367 LST_CAR_METER , item->meter,
368 LST_CAR_FUEL , item->fuel,
369 LST_CAR_PRICE , ABS(trn_amount) / item->fuel,
370 LST_CAR_AMOUNT , trn_amount,
371 LST_CAR_DIST , dist,
372 LST_CAR_100KM , centkm,
373 LST_CAR_DISTBYVOL, distbyvol,
374 LST_CAR_PARTIAL, item->partial,
375 -1);
376
377 DB( g_print(" + insert d=%d v=%4.2f $%8.2f %d %5.2f\n", item->meter, item->fuel, trn_amount, dist, centkm) );
378
379 if(item->dist)
380 {
381 data->total_fuelcost += trn_amount;
382 data->total_fuel += item->fuel;
383 data->total_dist += item->dist;
384 }
385
386
387 }
388 }
389 list = g_list_next(list);
390 }
391
392 gtk_tree_view_set_model(GTK_TREE_VIEW(data->LV_report), model);
393 g_object_unref(model);
394
395
396 gdouble coef = data->total_dist ? 100 / (gdouble)data->total_dist : 0;
397
398 GLOBALS->minor = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_minor));
399
400
401 DB( g_print(" coef = 100 / %.2f = %.2f\n", (gdouble)data->total_dist, coef) );
402
403 // row 1 is for 100km
404 /*
405 gtk_label_set_text(GTK_LABEL(data->LA_total[1][1]), "1:1"); //Consumption
406 gtk_label_set_text(GTK_LABEL(data->LA_total[2][1]), "2:1"); //Fuel cost
407 gtk_label_set_text(GTK_LABEL(data->LA_total[3][1]), "3:1"); //Other cost
408 gtk_label_set_text(GTK_LABEL(data->LA_total[4][1]), "4:1"); //Total cost
409 */
410
411 // 100km fuel
412 buf = g_strdup_printf(PREFS->vehicle_unit_vol, data->total_fuel * coef);
413 gtk_label_set_text(GTK_LABEL(data->LA_avera[CAR_RES_FUEL]), buf);
414 g_free(buf);
415
416 // 100km fuelcost
417 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_avera[CAR_RES_FUELCOST]), data->total_fuelcost * coef, GLOBALS->kcur);
418 hb_label_set_colvalue(GTK_LABEL(data->LA_avera[CAR_RES_FUELCOST]), data->total_fuelcost * coef, GLOBALS->minor);
419
420 // 100km other cost
421 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_avera[CAR_RES_OTHERCOST]), data->total_misccost * coef, GLOBALS->kcur);
422 hb_label_set_colvalue(GTK_LABEL(data->LA_avera[CAR_RES_OTHERCOST]), data->total_misccost * coef, GLOBALS->minor);
423
424 // 100km cost
425 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_avera[CAR_RES_TOTALCOST]), (data->total_fuelcost + data->total_misccost) * coef, GLOBALS->kcur);
426 hb_label_set_colvalue(GTK_LABEL(data->LA_avera[CAR_RES_TOTALCOST]), (data->total_fuelcost + data->total_misccost) * coef, GLOBALS->minor);
427
428
429 // row 2 is for total
430 /*
431 gtk_label_set_text(GTK_LABEL(data->LA_total[1][2]), "1:2"); //Consumption
432 gtk_label_set_text(GTK_LABEL(data->LA_total[2][2]), "2:2"); //Fuel cost
433 gtk_label_set_text(GTK_LABEL(data->LA_total[3][2]), "3:2"); //Other cost
434 gtk_label_set_text(GTK_LABEL(data->LA_total[4][2]), "4:2"); //Total
435 */
436
437 // total distance
438 buf = g_strdup_printf(PREFS->vehicle_unit_dist, data->total_dist);
439 gtk_label_set_text(GTK_LABEL(data->LA_total[CAR_RES_METER]), buf);
440 g_free(buf);
441
442 // total fuel
443 buf = g_strdup_printf(PREFS->vehicle_unit_vol, data->total_fuel);
444 gtk_label_set_text(GTK_LABEL(data->LA_total[CAR_RES_FUEL]), buf);
445 g_free(buf);
446
447 // total fuelcost
448 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_total[CAR_RES_FUELCOST]), data->total_fuelcost, GLOBALS->kcur);
449 hb_label_set_colvalue(GTK_LABEL(data->LA_total[CAR_RES_FUELCOST]), data->total_fuelcost, GLOBALS->minor);
450
451 // total other cost
452 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_total[CAR_RES_OTHERCOST]), data->total_misccost, GLOBALS->kcur);
453 hb_label_set_colvalue(GTK_LABEL(data->LA_total[CAR_RES_OTHERCOST]), data->total_misccost, GLOBALS->minor);
454
455 // total cost
456 //hb_label_set_colvaluecurr(GTK_LABEL(data->LA_total[CAR_RES_TOTALCOST]), data->total_fuelcost + data->total_misccost, GLOBALS->kcur);
457 hb_label_set_colvalue(GTK_LABEL(data->LA_total[CAR_RES_TOTALCOST]), data->total_fuelcost + data->total_misccost, GLOBALS->minor);
458
459
460 }
461
462 static void repvehicle_toggle_minor(GtkWidget *widget, gpointer user_data)
463 {
464 struct repvehicle_data *data;
465
466 DB( g_print("(repvehicle) toggle\n") );
467
468 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
469
470 repvehicle_update(widget, NULL);
471 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(data->LV_report));
472
473
474 /*
475 statistic_update_total(widget,NULL);
476
477 //hbfile_update(data->LV_acc, (gpointer)4);
478 gtk_tree_view_columns_autosize (GTK_TREE_VIEW(data->LV_report));
479
480 minor = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_minor));
481 gtk_chart_show_minor(GTK_CHART(data->RE_bar), minor);
482 gtk_chart_show_minor(GTK_CHART(data->RE_pie), minor);
483 */
484
485 }
486
487 /*
488 **
489 */
490 static void repvehicle_setup(struct repvehicle_data *data)
491 {
492 DB( g_print("(repvehicle) setup\n") );
493
494 data->vehicle_list = NULL;
495
496 data->filter = da_filter_malloc();
497 filter_default_all_set(data->filter);
498
499 /* 3.4 : make int transfer out of stats */
500 data->filter->option[FILTER_PAYMODE] = 1;
501 data->filter->paymode[PAYMODE_INTXFER] = FALSE;
502
503 filter_preset_daterange_set(data->filter, PREFS->date_range_rep);
504
505 g_signal_handler_block(data->PO_mindate, data->handler_id[HID_REPVEHICLE_MINDATE]);
506 g_signal_handler_block(data->PO_maxdate, data->handler_id[HID_REPVEHICLE_MAXDATE]);
507
508 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_mindate), data->filter->mindate);
509 gtk_dateentry_set_date(GTK_DATE_ENTRY(data->PO_maxdate), data->filter->maxdate);
510
511 g_signal_handler_unblock(data->PO_mindate, data->handler_id[HID_REPVEHICLE_MINDATE]);
512 g_signal_handler_unblock(data->PO_maxdate, data->handler_id[HID_REPVEHICLE_MAXDATE]);
513
514
515 ui_cat_comboboxentry_populate(GTK_COMBO_BOX(data->PO_cat), GLOBALS->h_cat);
516
517 g_signal_handler_block(data->PO_cat, data->handler_id[HID_REPVEHICLE_VEHICLE]);
518 ui_cat_comboboxentry_set_active(GTK_COMBO_BOX(data->PO_cat), GLOBALS->vehicle_category);
519 g_signal_handler_unblock(data->PO_cat, data->handler_id[HID_REPVEHICLE_VEHICLE]);
520
521
522
523 }
524
525
526 /*
527 **
528 */
529 static gboolean repvehicle_window_dispose(GtkWidget *widget, GdkEvent *event, gpointer user_data)
530 {
531 struct repvehicle_data *data = user_data;
532 struct WinGeometry *wg;
533
534 DB( g_print("(repvehicle) dispose\n") );
535
536 da_vehiclecost_destroy(data->vehicle_list);
537
538 da_filter_free(data->filter);
539
540 g_free(data);
541
542 //store position and size
543 wg = &PREFS->cst_wg;
544 gtk_window_get_position(GTK_WINDOW(widget), &wg->l, &wg->t);
545 gtk_window_get_size(GTK_WINDOW(widget), &wg->w, &wg->h);
546
547 DB( g_print(" window: l=%d, t=%d, w=%d, h=%d\n", wg->l, wg->t, wg->w, wg->h) );
548
549 //enable define windows
550 GLOBALS->define_off--;
551 ui_mainwindow_update(GLOBALS->mainwindow, GINT_TO_POINTER(UF_SENSITIVE));
552
553 return FALSE;
554 }
555
556
557 // the window creation
558 GtkWidget *repcost_window_new(void)
559 {
560 struct repvehicle_data *data;
561 struct WinGeometry *wg;
562 GtkWidget *window, *mainvbox, *hbox, *vbox, *treeview;
563 GtkWidget *label, *widget, *table, *alignment;
564 gint row, col;
565
566 data = g_malloc0(sizeof(struct repvehicle_data));
567 if(!data) return NULL;
568
569 DB( g_print("(repvehicle) new\n") );
570
571 //disable define windows
572 GLOBALS->define_off++;
573 ui_mainwindow_update(GLOBALS->mainwindow, GINT_TO_POINTER(2));
574
575 /* create window, etc */
576 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
577 data->window = window;
578
579 //store our window private data
580 g_object_set_data(G_OBJECT(window), "inst_data", (gpointer)data);
581
582 gtk_window_set_title (GTK_WINDOW (window), _("Vehicle cost report"));
583
584 //set the window icon
585 // homebank_window_set_icon_from_file(GTK_WINDOW (window), "report_vehicle.svg");
586 gtk_window_set_icon_name(GTK_WINDOW (window), HB_STOCK_REP_CAR);
587
588
589
590 //window contents
591 mainvbox = gtk_vbox_new (FALSE, 0);
592 gtk_container_add (GTK_CONTAINER (window), mainvbox);
593
594 hbox = gtk_hbox_new(FALSE, 0);
595 gtk_box_pack_start (GTK_BOX (mainvbox), hbox, TRUE, TRUE, 0);
596
597 //control part
598 table = gtk_table_new (6, 3, FALSE);
599 // gtk_alignment_new(xalign, yalign, xscale, yscale)
600 alignment = gtk_alignment_new(0.0, 0.0, 0.0, 0.0);
601 gtk_container_add(GTK_CONTAINER(alignment), table);
602 gtk_box_pack_start (GTK_BOX (hbox), alignment, FALSE, FALSE, 0);
603
604 gtk_container_set_border_width (GTK_CONTAINER (table), HB_BOX_SPACING);
605 gtk_table_set_row_spacings (GTK_TABLE (table), HB_TABROW_SPACING);
606 gtk_table_set_col_spacings (GTK_TABLE (table), HB_TABCOL_SPACING);
607
608 row = 0;
609 label = make_label(_("Display"), 0.0, 0.5);
610 gimp_label_set_attributes(GTK_LABEL(label), PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD, -1);
611 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 3, row, row+1);
612
613 row++;
614 label = make_label(_("Vehi_cle:"), 0, 0.5);
615 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
616
617 widget = ui_cat_comboboxentry_new(label);
618 data->PO_cat = widget;
619 gtk_table_attach_defaults (GTK_TABLE (table), widget, 2, 3, row, row+1);
620
621 row++;
622 widget = gtk_check_button_new_with_mnemonic (_("_Minor currency"));
623 data->CM_minor = widget;
624 gtk_table_attach_defaults (GTK_TABLE (table), widget, 1, 3, row, row+1);
625
626 row++;
627 widget = gtk_hseparator_new();
628 gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 3, row, row+1);
629
630 row++;
631 label = make_label(_("Date filter"), 0.0, 0.5);
632 gimp_label_set_attributes(GTK_LABEL(label), PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD, -1);
633 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 3, row, row+1);
634
635 row++;
636 label = make_label(_("_Range:"), 0, 0.5);
637 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
638 data->CY_range = make_daterange(label, FALSE);
639 gtk_table_attach_defaults (GTK_TABLE (table), data->CY_range, 2, 3, row, row+1);
640
641 row++;
642 label = make_label(_("_From:"), 0, 0.5);
643 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
644 data->PO_mindate = gtk_dateentry_new();
645 gtk_table_attach_defaults (GTK_TABLE (table), data->PO_mindate, 2, 3, row, row+1);
646
647 row++;
648 label = make_label(_("_To:"), 0, 0.5);
649 gtk_table_attach (GTK_TABLE (table), label, 1, 2, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
650 data->PO_maxdate = gtk_dateentry_new();
651 gtk_table_attach_defaults (GTK_TABLE (table), data->PO_maxdate, 2, 3, row, row+1);
652
653
654 //part: info + report
655 vbox = gtk_vbox_new (FALSE, 0);
656 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
657
658 //toobar
659 //toolbar = create_repvehicle_toolbar(data);
660 //gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
661
662 //infos
663 //hbox = gtk_hbox_new (FALSE, HB_BOX_SPACING);
664 //gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
665 //gtk_container_set_border_width (GTK_CONTAINER(hbox), HB_BOX_SPACING);
666 //label = gtk_label_new(NULL);
667 //data->TX_info = label;
668 //gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
669
670 // total
671 table = gtk_table_new (3, 6, FALSE);
672 // gtk_alignment_new(xalign, yalign, xscale, yscale)
673 alignment = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
674 gtk_container_add(GTK_CONTAINER(alignment), table);
675 gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);
676
677 gtk_container_set_border_width (GTK_CONTAINER (table), HB_BOX_SPACING);
678 gtk_table_set_row_spacings (GTK_TABLE (table), HB_TABROW_SPACING);
679 gtk_table_set_col_spacings (GTK_TABLE (table), HB_TABCOL_SPACING);
680
681 row = 0; col = 1;
682
683 label = make_label(_("Meter:"), 1.0, 0.5);
684 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
685
686 col++;
687 label = make_label(_("Consumption:"), 1.0, 0.5);
688 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
689
690 col++;
691 label = make_label(_("Fuel cost:"), 1.0, 0.5);
692 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
693
694 col++;
695 label = make_label(_("Other cost:"), 1.0, 0.5);
696 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
697
698 col++;
699 label = make_label(_("Total cost:"), 1.0, 0.5);
700 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
701
702 row++;
703 col = 0;
704 label = make_label(PREFS->vehicle_unit_100, 1.0, 0.5);
705 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
706
707 for(col = 1;col<MAX_CAR_RES;col++)
708 {
709 label = make_label(NULL, 1.0, 0.5);
710 gtk_table_attach_defaults (GTK_TABLE (table), label, col, col+1, row, row+1);
711 data->LA_avera[col] = label;
712 }
713
714 row++;
715 col = 0;
716 label = make_label(_("Total"), 1.0, 0.5);
717 gtk_table_attach (GTK_TABLE (table), label, col, col+1, row, row+1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
718
719 for(col = 1;col<MAX_CAR_RES;col++)
720 {
721 label = make_label(NULL, 1.0, 0.5);
722 gtk_table_attach_defaults (GTK_TABLE (table), label, col, col+1, row, row+1);
723 data->LA_total[col] = label;
724 }
725
726
727 //detail
728 widget = gtk_scrolled_window_new (NULL, NULL);
729 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_ETCHED_IN);
730 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (widget), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
731
732 treeview = create_list_repvehicle();
733 data->LV_report = treeview;
734 gtk_container_add (GTK_CONTAINER(widget), treeview);
735 gtk_box_pack_start (GTK_BOX (vbox), widget, TRUE, TRUE, 0);
736
737
738 //todo:should move this
739 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->CM_minor),GLOBALS->minor);
740
741
742
743 /* attach our minor to treeview */
744 g_object_set_data(G_OBJECT(gtk_tree_view_get_model(GTK_TREE_VIEW(data->LV_report))), "minor", (gpointer)data->CM_minor);
745
746
747
748 /* signal connect */
749 g_signal_connect (window, "delete-event", G_CALLBACK (repvehicle_window_dispose), (gpointer)data);
750
751 g_signal_connect (data->CM_minor, "toggled", G_CALLBACK (repvehicle_toggle_minor), NULL);
752
753 data->handler_id[HID_REPVEHICLE_MINDATE] = g_signal_connect (data->PO_mindate, "changed", G_CALLBACK (repvehicle_date_change), (gpointer)data);
754 data->handler_id[HID_REPVEHICLE_MAXDATE] = g_signal_connect (data->PO_maxdate, "changed", G_CALLBACK (repvehicle_date_change), (gpointer)data);
755
756 data->handler_id[HID_REPVEHICLE_RANGE] = g_signal_connect (data->CY_range, "changed", G_CALLBACK (repvehicle_range_change), NULL);
757
758 data->handler_id[HID_REPVEHICLE_VEHICLE] = g_signal_connect (data->PO_cat, "changed", G_CALLBACK (repvehicle_compute), NULL);
759
760
761 //setup, init and show window
762 repvehicle_setup(data);
763
764 /* toolbar */
765 /*
766 if(PREFS->toolbar_style == 0)
767 gtk_toolbar_unset_style(GTK_TOOLBAR(data->TB_bar));
768 else
769 gtk_toolbar_set_style(GTK_TOOLBAR(data->TB_bar), PREFS->toolbar_style-1);
770 */
771
772 //setup, init and show window
773 wg = &PREFS->cst_wg;
774 gtk_window_move(GTK_WINDOW(window), wg->l, wg->t);
775 gtk_window_resize(GTK_WINDOW(window), wg->w, wg->h);
776
777 gtk_widget_show_all (window);
778
779 //minor ?
780 if( PREFS->euro_active )
781 gtk_widget_show(data->CM_minor);
782 else
783 gtk_widget_hide(data->CM_minor);
784
785
786
787 if( PREFS->date_range_rep )
788 gtk_combo_box_set_active(GTK_COMBO_BOX(data->CY_range), PREFS->date_range_rep);
789 else
790 repvehicle_compute(window, NULL);
791
792 return(window);
793 }
794
795 /*
796 ** ============================================================================
797 */
798
799 static void repvehicle_date_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
800 {
801 GDate *date;
802 guint32 julian;
803 gchar buf[256];
804
805 gtk_tree_model_get(model, iter,
806 LST_CAR_DATE, &julian,
807 -1);
808
809 date = g_date_new_julian (julian);
810 g_date_strftime (buf, 256-1, PREFS->date_format, date);
811 g_date_free(date);
812
813 g_object_set(renderer, "text", buf, NULL);
814 }
815
816 static void repvehicle_distance_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
817 {
818 guint distance;
819 gchar *text;
820
821 gtk_tree_model_get(model, iter, user_data, &distance, -1);
822
823 if(distance != 0)
824 {
825 text = g_strdup_printf(PREFS->vehicle_unit_dist, distance);
826 g_object_set(renderer, "text", text, NULL);
827 g_free(text);
828 }
829 else
830 g_object_set(renderer, "text", "-", NULL);
831 }
832
833 static void repvehicle_volume_cell_data_function (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
834 {
835 gdouble volume;
836 gboolean partial;
837 gchar *text;
838
839 gtk_tree_model_get(model, iter, user_data, &volume, LST_CAR_PARTIAL, &partial, -1);
840
841 if(volume != 0)
842 {
843 text = g_strdup_printf(PREFS->vehicle_unit_vol, volume);
844 g_object_set(renderer,
845 "text", text,
846 "style-set", TRUE,
847 "style", partial ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL,
848 NULL);
849
850 g_free(text);
851 }
852 else
853 g_object_set(renderer, "text", "-", NULL);
854 }
855
856 static void repvehicle_amount_cell_data_function (GtkTreeViewColumn *col,
857 GtkCellRenderer *renderer,
858 GtkTreeModel *model,
859 GtkTreeIter *iter,
860 gpointer user_data)
861 {
862 gdouble value;
863 gchar *color;
864 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
865
866 gtk_tree_model_get(model, iter,
867 user_data, &value,
868 -1);
869
870 if( value )
871 {
872 mystrfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->minor);
873 //hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, GLOBALS->kcur);
874
875 color = get_normal_color_amount(value);
876
877 g_object_set(renderer,
878 "foreground", color,
879 "text", buf,
880 NULL); }
881 else
882 {
883 g_object_set(renderer, "text", "", NULL);
884 }
885 }
886
887 static GtkTreeViewColumn *volume_list_repvehicle_column(gchar *name, gint id)
888 {
889 GtkTreeViewColumn *column;
890 GtkCellRenderer *renderer;
891
892 column = gtk_tree_view_column_new();
893 gtk_tree_view_column_set_title(column, name);
894 renderer = gtk_cell_renderer_text_new ();
895 g_object_set(renderer, "xalign", 1.0, NULL);
896 gtk_tree_view_column_pack_start(column, renderer, TRUE);
897 gtk_tree_view_column_set_cell_data_func(column, renderer, repvehicle_volume_cell_data_function, GINT_TO_POINTER(id), NULL);
898 gtk_tree_view_column_set_alignment (column, 0.5);
899 //gtk_tree_view_column_set_sort_column_id (column, id);
900 return column;
901 }
902
903 static GtkTreeViewColumn *distance_list_repvehicle_column(gchar *name, gint id)
904 {
905 GtkTreeViewColumn *column;
906 GtkCellRenderer *renderer;
907
908 column = gtk_tree_view_column_new();
909 gtk_tree_view_column_set_title(column, name);
910 renderer = gtk_cell_renderer_text_new ();
911 g_object_set(renderer, "xalign", 1.0, NULL);
912 gtk_tree_view_column_pack_start(column, renderer, TRUE);
913 gtk_tree_view_column_set_cell_data_func(column, renderer, repvehicle_distance_cell_data_function, GINT_TO_POINTER(id), NULL);
914 gtk_tree_view_column_set_alignment (column, 0.5);
915 //gtk_tree_view_column_set_sort_column_id (column, id);
916 return column;
917 }
918
919 static GtkTreeViewColumn *amount_list_repvehicle_column(gchar *name, gint id)
920 {
921 GtkTreeViewColumn *column;
922 GtkCellRenderer *renderer;
923
924 column = gtk_tree_view_column_new();
925 gtk_tree_view_column_set_title(column, name);
926 renderer = gtk_cell_renderer_text_new ();
927 g_object_set(renderer, "xalign", 1.0, NULL);
928 gtk_tree_view_column_pack_start(column, renderer, TRUE);
929 gtk_tree_view_column_set_cell_data_func(column, renderer, repvehicle_amount_cell_data_function, GINT_TO_POINTER(id), NULL);
930 gtk_tree_view_column_set_alignment (column, 0.5);
931 //gtk_tree_view_column_set_sort_column_id (column, id);
932 return column;
933 }
934
935
936 /*
937 ** create our statistic list
938 */
939 static GtkWidget *create_list_repvehicle(void)
940 {
941 GtkListStore *store;
942 GtkWidget *view;
943 GtkCellRenderer *renderer;
944 GtkTreeViewColumn *column;
945
946 /* create list store */
947 store = gtk_list_store_new(
948 NUM_LST_CAR,
949 G_TYPE_UINT,
950 G_TYPE_STRING,
951 G_TYPE_UINT,
952 G_TYPE_DOUBLE,
953 G_TYPE_DOUBLE,
954 G_TYPE_DOUBLE,
955 G_TYPE_UINT,
956 G_TYPE_DOUBLE,
957 G_TYPE_UINT,
958 G_TYPE_BOOLEAN );
959
960 //treeview
961 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
962 g_object_unref(store);
963
964 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), PREFS->rules_hint);
965
966 /* column date */
967 column = gtk_tree_view_column_new();
968 gtk_tree_view_column_set_title(column, _("Date"));
969 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
970 renderer = gtk_cell_renderer_text_new();
971 gtk_tree_view_column_pack_start(column, renderer, TRUE);
972 //gtk_tree_view_column_add_attribute(column, renderer, "text", LST_CAR_DATE);
973 gtk_tree_view_column_set_alignment (column, 0.5);
974 gtk_tree_view_column_set_cell_data_func(column, renderer, repvehicle_date_cell_data_function, NULL, NULL);
975
976 /*
977 LST_CAR_DATE,
978 LST_CAR_WORDING,
979 LST_CAR_METER,
980 LST_CAR_FUEL,
981 LST_CAR_PRICE,
982 LST_CAR_AMOUNT,
983 LST_CAR_DIST,
984 LST_CAR_100KM
985
986 */
987
988 /* column: Wording */
989 /*
990 column = gtk_tree_view_column_new();
991 gtk_tree_view_column_set_title(column, _("Wording"));
992 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
993 renderer = gtk_cell_renderer_text_new();
994 gtk_tree_view_column_pack_start(column, renderer, TRUE);
995 gtk_tree_view_column_add_attribute(column, renderer, "text", LST_CAR_WORDING);
996 //gtk_tree_view_column_set_cell_data_func(column, renderer, repvehicle_text_cell_data_function, NULL, NULL);
997 */
998
999 /* column: Meter */
1000 column = distance_list_repvehicle_column(_("Meter"), LST_CAR_METER);
1001 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1002
1003 /* column: Fuel load */
1004 column = volume_list_repvehicle_column(_("Fuel"), LST_CAR_FUEL);
1005 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1006
1007 /* column: Price by unit */
1008 column = amount_list_repvehicle_column(_("Price"), LST_CAR_PRICE);
1009 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1010
1011 /* column: Amount */
1012 column = amount_list_repvehicle_column(_("Amount"), LST_CAR_AMOUNT);
1013 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1014
1015 /* column: Distance done */
1016 column = distance_list_repvehicle_column(_("Dist."), LST_CAR_DIST);
1017 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1018
1019 /* column: consumption for 100Km */
1020 column = volume_list_repvehicle_column(PREFS->vehicle_unit_100, LST_CAR_100KM);
1021 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1022
1023 /* column: km by liter (distance by volume */
1024 column = distance_list_repvehicle_column(PREFS->vehicle_unit_distbyvol, LST_CAR_DISTBYVOL);
1025 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1026
1027
1028 /* column last: empty */
1029 column = gtk_tree_view_column_new();
1030 gtk_tree_view_append_column (GTK_TREE_VIEW(view), column);
1031
1032 return(view);
1033 }
This page took 0.105047 seconds and 4 git commands to generate.