From e6a7d9e1a74f94f4c6c7086500c20f6224b185ee Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 20 Jan 2019 13:11:38 -0300 Subject: [PATCH] start work on assign vehicle --- data/ui/main.window.ui | 8 - data/ui/tour.assign.vehicle.ui | 165 ++++++++++++++++++ data/ui/tour.editor.ui | 21 ++- data/ui/tour.gresource.xml | 1 + po/POTFILES | 2 + src/meson.build | 1 + src/tour_assign_vehicle.vala | 307 +++++++++++++++++++++++++++++++++ src/tour_editor.vala | 29 ++++ 8 files changed, 525 insertions(+), 9 deletions(-) create mode 100644 data/ui/tour.assign.vehicle.ui create mode 100644 src/tour_assign_vehicle.vala diff --git a/data/ui/main.window.ui b/data/ui/main.window.ui index 2484dca..c13caf1 100644 --- a/data/ui/main.window.ui +++ b/data/ui/main.window.ui @@ -92,14 +92,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - - - True - False - Vehicles - - - True diff --git a/data/ui/tour.assign.vehicle.ui b/data/ui/tour.assign.vehicle.ui new file mode 100644 index 0000000..86bb5ad --- /dev/null +++ b/data/ui/tour.assign.vehicle.ui @@ -0,0 +1,165 @@ + + + + + + diff --git a/data/ui/tour.editor.ui b/data/ui/tour.editor.ui index d5ab5ed..f83f58a 100644 --- a/data/ui/tour.editor.ui +++ b/data/ui/tour.editor.ui @@ -319,6 +319,25 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 1 + + + Vehicle + True + True + True + Assign a vehicle to this tour. + 5 + 5 + 5 + 5 + + + + False + True + 2 + + Save @@ -335,7 +354,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND False True - 2 + 3 diff --git a/data/ui/tour.gresource.xml b/data/ui/tour.gresource.xml index 49f5e2c..06d81bf 100644 --- a/data/ui/tour.gresource.xml +++ b/data/ui/tour.gresource.xml @@ -18,5 +18,6 @@ tour.editor.ui tour.places.ui tour.place.editor.ui + tour.assign.vehicle.ui diff --git a/po/POTFILES b/po/POTFILES index ac1e98b..a688b2f 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -17,6 +17,8 @@ data/ui/main.splash.ui data/ui/tour.list.ui data/ui/tour.editor.ui data/ui/tour.places.ui +data/ui/tour.place.editor.ui +data/ui/tour.assign.vehicle.ui data/ui/place.editor.ui data/ui/query.window.ui data/ui/query.tree.ui diff --git a/src/meson.build b/src/meson.build index 06fc5ef..1f3fbe0 100644 --- a/src/meson.build +++ b/src/meson.build @@ -20,6 +20,7 @@ vala_sources = files( 'tour_editor.vala', 'tour_places.vala', 'tour_place_editor.vala', + 'tour_assign_vehicle.vala', 'place_editor.vala', 'queries.vala', 'query_window.vala') diff --git a/src/tour_assign_vehicle.vala b/src/tour_assign_vehicle.vala new file mode 100644 index 0000000..afecee2 --- /dev/null +++ b/src/tour_assign_vehicle.vala @@ -0,0 +1,307 @@ +/* + * Copyright 2018-2019 Chris Cromer + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +namespace Sernatur { + using LibSernatur.DB; + + /** + * The tour assign vehicle window class + */ + [GtkTemplate (ui = "/cl/cromer/ubb/sernatur/tour.assign.vehicle.ui")] + public class TourAssignVehicle : Gtk.ApplicationWindow { + /** + * The open database connection + */ + private Connection conn; + /** + * The tour to associate with the vehicle + */ + private Tour tour; + /** + * The vehicle data stored in the list store + */ + private enum Column { + /** + * The details of the vehicle + */ + VEHICLE_DETAILS, + /** + * The vehicle object + */ + VEHICLE, + /** + * The number of colums in this enum + */ + N_COLUMNS + } + /** + * The chofer of the vehicle + */ + [GtkChild] + private Gtk.Entry chofer; + /** + * The vehicle comboxbox + */ + [GtkChild] + private Gtk.ComboBoxText vehicle; + /** + * The save button + */ + [GtkChild] + private Gtk.Button save; + /** + * The cancel button + */ + [GtkChild] + private Gtk.Button cancel; + /** + * A list of the vehicles from the database + */ + private List vehicles; + /** + * The list that stores the vehicles for the combo box + */ + private Gtk.ListStore vehicle_list_store; + /** + * This signal is called when a vehicle is assigned + */ + public signal void save_vehicle (Vehiculo vehicle); + + /** + * Validate the car data before trying to insert it into the database + * @return Returns true if the data is valid + */ + private bool validate_car_data () { + /*if (tour.nombre_tour.strip () == "") { + var msg = new Gtk.MessageDialog (this, + Gtk.DialogFlags.MODAL, + Gtk.MessageType.ERROR, + Gtk.ButtonsType.CLOSE, + _ ("Error: Tour name cannot be left blank!")); + msg.response.connect ((response_id) => { + msg.destroy (); + }); + msg.set_title (_ ("Error")); + msg.run (); + return false; + } + bool list_success = true; + try { + List list = Tour.get_all_tours (conn); + list.foreach ((entry) => { + if (tour.nombre_tour.down () == entry.nombre_tour.down () && tour.id_tour != entry.id_tour) { + var msg = new Gtk.MessageDialog (this, + Gtk.DialogFlags.MODAL, + Gtk.MessageType.ERROR, + Gtk.ButtonsType.CLOSE, + _ ("Error: A tour named \"%s\" already exists!"), entry.nombre_tour); + msg.response.connect ((response_id) => { + msg.destroy (); + }); + msg.set_title (_ ("Error")); + msg.run (); + list_success = false; + } + }); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + return false; + #endif + } + return list_success;*/ + return true; + } + + /** + * Called when a combobox changes + * @param combobox The combobox that changed + */ + [GtkCallback] + public void on_changed_combobox (Gtk.ComboBox combobox) { + if (combobox == vehicle) { + if (vehicle.get_active () > 0) { + Gtk.TreeIter iter; + vehicle.get_active_iter (out iter); + Vehiculo temp_vehicle; + vehicle_list_store.get (iter, + Column.VEHICLE, out temp_vehicle); + } + else { + // Unassigned + } + } + } + + /** + * This callback is called when a button is clicked + * @param button The button that was clicked + */ + [GtkCallback] + public void on_clicked_button (Gtk.Button button) { + if (button == save) { + /*if (update_tour_instance () && validate_tour_data ()) { + if (tour.id_tour == 0) { + try { + Tour.insert_tour (conn, tour); + if (list_asociado != null) { + // Insert all the pending associations + list_asociado.foreach ((entry) => { + try { + Asociado.insert_asociado (conn, entry); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + }); + } + save_tour (); // Signal the parent to update itself + this.close (); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + } + else { + try { + Tour.update_tour (conn, tour); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + finally { + save_tour (); // Signal the parent to update itself + this.close (); + } + } + }*/ + } + else if (button == cancel) { + this.close (); + } + } + + /** + * Update the the vehicle object with new info from the editor + */ + private bool update_vehicle_instance () { + /*tour.nombre_tour = tour_name.get_text ().strip (); + tour.costo_indiv = (uint) int.parse (indiv_cost.get_text ()); + tour.costo_grupal = (uint) int.parse (group_cost.get_text ()); + tour.minima_personas = (uint) minimum_people.get_value_as_int (); + if (region.get_active () == -1) { + Region new_region = new Region (0, region.get_active_text ().strip ()); + try { + if (validate_region_data (new_region)) { + new_region.id_region = Region.insert_region (conn, new_region); + return update_tour_instance_city (new_region); + } + else { + return false; + } + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + } + else { + Region new_region; + Gtk.TreeIter iter; + region.get_active_iter (out iter); + if (region_list_store.iter_is_valid (iter)) { + region_list_store.get (iter, + RegionColumn.REGION, out new_region); + } + else { + new_region = new Region (); + } + return update_tour_instance_city (new_region); + }*/ + return true; + } + + /** + * Initialize the tour editor class + * @param application The application used to make the GLib object + * @param conn The database connection to use + * @param tour The tour to edit + */ + public TourAssignVehicle (Gtk.Application application, Connection conn, Tour tour) { + Object (application: application); + this.conn = conn; + this.tour = tour; + } + + /** + * Initialize what is needed for this window + */ + public void initialize () { + try { + vehicles = Vehiculo.get_all_vehiculos (conn); + } + catch (Error e) { + #if DEBUG + error (e.message); + #else + warning (e.message); + #endif + } + + vehicles.sort_with_data ((a, b) => { + return strcmp (a.patente, b.patente); + }); + + vehicle_list_store = new Gtk.ListStore (Column.N_COLUMNS, + typeof (string), + typeof (Vehiculo)); + + vehicle.set_model (vehicle_list_store); + + Gtk.TreeIter iter; + vehicle_list_store.append (out iter); + vehicle_list_store.set (iter, + Column.VEHICLE_DETAILS, _ ("Unassigned"), + Column.VEHICLE, new Vehiculo ()); + vehicle.set_active (0); + vehicles.foreach ((entry) => { + vehicle_list_store.append (out iter); + vehicle_list_store.set (iter, + Column.VEHICLE_DETAILS, entry.patente + " " + entry.marca + " " + entry.ano_vehiculo.to_string () + " " + entry.capacidad.to_string (), + Column.VEHICLE, entry); + /*if (entry.patente == assigned_vehicle.patente) { + vehicle.set_active_iter (iter); + }*/ + }); + } + } +} diff --git a/src/tour_editor.vala b/src/tour_editor.vala index 6e1792f..daeb0d8 100644 --- a/src/tour_editor.vala +++ b/src/tour_editor.vala @@ -97,6 +97,11 @@ namespace Sernatur { */ [GtkChild] private Gtk.Button places; + /** + * The vehicle button + */ + [GtkChild] + private Gtk.Button vehicle; /** * The cancel button */ @@ -132,11 +137,19 @@ namespace Sernatur { * A list of associated places, this is only used in the case of a new tour */ private List list_asociado = null; + /** + * The vehicle to be inserted if the new tour is saved + */ + private Vehiculo vehiculo = null; /** * This signal is called when a tour is saved */ public signal void save_tour (); + /** + * Called when a combobox changes + * @param combobox The combobox that changed + */ [GtkCallback] public void on_changed_combobox (Gtk.ComboBox combobox) { if (combobox == region) { @@ -409,6 +422,13 @@ namespace Sernatur { tour_places.show_all (); tour_places.save_places.connect (on_save_places); } + else if (button == vehicle) { + var tour_assign_vehicle = new TourAssignVehicle (application, conn, tour); + tour_assign_vehicle.set_transient_for (this); // Set this window as the parent of the new window + tour_assign_vehicle.initialize (); + tour_assign_vehicle.show_all (); + tour_assign_vehicle.save_vehicle.connect (on_save_vehicle); + } else if (button == cancel) { this.close (); } @@ -424,6 +444,15 @@ namespace Sernatur { places.sensitive = false; } + /** + * Called when a vehcile is assigned to the tour, and the tour is not in the database yet + * @param tour_assign_vehicle The TourAssignVehicle instance that called this signal + * @param vehicle The vehicle to assign to this tour + */ + private void on_save_vehicle (TourAssignVehicle tour_assign_vehicle, Vehiculo vehiculo) { + this.vehiculo = vehiculo; + } + /** * Update the the tour object with new info from the editor */