sernatur/src/tour_places.vala

552 lines
15 KiB
Vala

/*
* 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.Misc;
using LibSernatur.DB;
/**
* The tour list window class
*/
[GtkTemplate (ui = "/cl/cromer/ubb/sernatur/tour.places.ui")]
public class TourPlaces : Gtk.ApplicationWindow {
/**
* The open database connection
*/
private Connection conn;
/**
* The tour to edit
*/
private Tour tour;
/**
* The columns of the tree view
*/
private enum Column {
/**
* The name of the place
*/
PLACE_NAME,
/**
* The individual cost
*/
TICKET_PRICE,
/**
* The level of difficulty
*/
DIFFICULTY,
/**
* The date of arrival
*/
ARRIVAL_DATE,
/**
* The time of arrival
*/
ARRIVAL_TIME,
/**
* The date of departure
*/
DEPARTURE_DATE,
/**
* The departure time
*/
DEPARTURE_TIME,
/**
* The associated object
*/
ASSOCIATED,
/**
* The number of colums in this enum
*/
N_COLUMNS
}
/**
* The list that stores the contents in the tree view
*/
private Gtk.ListStore list_store;
/**
* The list of tours
*/
private List<Asociado> associated_list;
/**
* The tree view widget
*/
[GtkChild]
private Gtk.TreeView place_tree;
/**
* The row selection
*/
[GtkChild]
private Gtk.TreeSelection selection;
/**
* The place name column
*/
[GtkChild]
private Gtk.TreeViewColumn place_name;
/**
* The ticket price column
*/
[GtkChild]
private Gtk.TreeViewColumn ticket_price;
/**
* The difficulty column
*/
[GtkChild]
private Gtk.TreeViewColumn difficulty;
/**
* The arrival date column
*/
[GtkChild]
private Gtk.TreeViewColumn arrival_date;
/**
* The arrival time column
*/
[GtkChild]
private Gtk.TreeViewColumn arrival_time;
/**
* The departure date column
*/
[GtkChild]
private Gtk.TreeViewColumn departure_date;
/**
* The departure time column
*/
[GtkChild]
private Gtk.TreeViewColumn departure_time;
/**
* Thew new place button
*/
[GtkChild]
private Gtk.Button new_place;
/**
* The edit place button
*/
[GtkChild]
private Gtk.Button edit_place;
/**
* The delete place button
*/
[GtkChild]
private Gtk.Button delete_place;
/**
* The close place button
*/
[GtkChild]
private Gtk.Button close_place;
/**
* The add place button
*/
[GtkChild]
private Gtk.Button add_place;
/**
* This callback is called when the user clicks on a row
* @param selection The selection object
*/
[GtkCallback]
private void on_changed_selection(Gtk.TreeSelection selection) {
if (selection.count_selected_rows () == 1) {
edit_place.sensitive = true;
delete_place.sensitive =true;
}
else if (selection.count_selected_rows () > 1) {
edit_place.sensitive = false;
delete_place.sensitive = true;
}
else {
edit_place.sensitive = false;
delete_place.sensitive = false;
}
}
/**
* This callback is run when the user clicks on a button
* @param button The button that was clicked
*/
[GtkCallback]
private void on_clicked_button (Gtk.Button button) {
if (button == new_place) {
/*var tour_editor = new TourEditor (application, conn, null);
tour_editor.set_transient_for (this); // Set this window as the parent of the new window
tour_editor.initialize ();
tour_editor.show_all ();*/
}
else if (button == add_place) {
}
else if (button == edit_place) {
selection.selected_foreach ((model, path, iter) => {
Asociado associated;
model.get (iter,
Column.ASSOCIATED, out associated);
print (associated.lugar.nombre_lugar + "\n");
});
/*if (selection.get_selected (out model, out iter)) {
Asociado associated;
model.get (iter,
Column.ASSOCIATED, out associated);
var tour_editor = new TourEditor (application, conn, tour);
tour_editor.set_transient_for (this); // Set this window as the parent of the new window
tour_editor.initialize ();
tour_editor.show_all ();
tour_editor.save_tour.connect (on_save);
}*/
}
else if (button == delete_place) {
/*var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.YES_NO,
dgettext(null, "Are you sure you wish to delete this tour?"));
msg.response.connect ((response_id) => {
switch (response_id) {
case Gtk.ResponseType.YES:
try {
Gtk.TreeModel model;
Gtk.TreeIter iter;
Tour tour;
if (selection.get_selected (out model, out iter)) {
model.get (iter,
Column.TOUR, out tour);
Tour.delete_tour (conn, tour);
}
}
catch (DBError e) {
if (e.code == 1) {
var msg2 = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
dgettext(null, "Error: Could not delete tour because there are still associated arrival and departure dates and times!"));
msg2.response.connect ((response_id) => {
msg2.destroy ();
});
msg2.set_title (_ ("Error"));
msg2.show ();
}
else {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
break;
}
msg.destroy ();
});
msg.set_title (_ ("Error"));
msg.show ();*/
}
else if (button == close_place) {
this.close ();
}
}
/**
* This callback is run when the user clicks on a column to reorder the rows
* @param column The column that was clicked
*/
[GtkCallback]
private void on_clicked_column (Gtk.TreeViewColumn column) {
edit_place.sensitive = false;
delete_place.sensitive = false;
if (column == place_name) {
if (!place_name.sort_indicator) {
reset_columns ();
place_name.sort_indicator = true;
}
if (place_name.sort_order == Gtk.SortType.ASCENDING) {
place_name.sort_order = Gtk.SortType.DESCENDING;
}
else {
place_name.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (place_name.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.lugar.nombre_lugar, b.lugar.nombre_lugar);
}
else {
return strcmp (b.lugar.nombre_lugar, a.lugar.nombre_lugar);
}
});
}
else if (column == ticket_price) {
if (!ticket_price.sort_indicator) {
reset_columns ();
ticket_price.sort_indicator = true;
}
if (ticket_price.sort_order == Gtk.SortType.ASCENDING) {
ticket_price.sort_order = Gtk.SortType.DESCENDING;
}
else {
ticket_price.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (ticket_price.sort_order == Gtk.SortType.ASCENDING) {
if (a.lugar.valor_entrada < b.lugar.valor_entrada) {
return -1;
}
else if (a.lugar.valor_entrada == b.lugar.valor_entrada) {
return 0;
}
else {
return 1;
}
}
else {
if (a.lugar.valor_entrada < b.lugar.valor_entrada) {
return 1;
}
else if (a.lugar.valor_entrada == b.lugar.valor_entrada) {
return 0;
}
else {
return -1;
}
}
});
}
else if (column == difficulty) {
if (!difficulty.sort_indicator) {
reset_columns ();
difficulty.sort_indicator = true;
}
if (difficulty.sort_order == Gtk.SortType.ASCENDING) {
difficulty.sort_order = Gtk.SortType.DESCENDING;
}
else {
difficulty.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (difficulty.sort_order == Gtk.SortType.ASCENDING) {
if (a.lugar.nivel < b.lugar.nivel) {
return -1;
}
else if (a.lugar.nivel == b.lugar.nivel) {
return 0;
}
else {
return 1;
}
}
else {
if (a.lugar.nivel < b.lugar.nivel) {
return 1;
}
else if (a.lugar.nivel == b.lugar.nivel) {
return 0;
}
else {
return -1;
}
}
});
}
else if (column == arrival_date) {
if (!arrival_date.sort_indicator) {
reset_columns ();
arrival_date.sort_indicator = true;
}
if (arrival_date.sort_order == Gtk.SortType.ASCENDING) {
arrival_date.sort_order = Gtk.SortType.DESCENDING;
}
else {
arrival_date.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (arrival_date.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.fecha_llegada, b.fecha_llegada);
}
else {
return strcmp (b.fecha_llegada, a.fecha_llegada);
}
});
}
else if (column == arrival_time) {
if (!arrival_time.sort_indicator) {
reset_columns ();
arrival_time.sort_indicator = true;
}
if (arrival_time.sort_order == Gtk.SortType.ASCENDING) {
arrival_time.sort_order = Gtk.SortType.DESCENDING;
}
else {
arrival_time.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (arrival_time.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.hora_llegada, b.hora_llegada);
}
else {
return strcmp (b.hora_llegada, a.hora_llegada);
}
});
}
else if (column == departure_date) {
if (!departure_date.sort_indicator) {
reset_columns ();
departure_date.sort_indicator = true;
}
if (departure_date.sort_order == Gtk.SortType.ASCENDING) {
departure_date.sort_order = Gtk.SortType.DESCENDING;
}
else {
departure_date.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (departure_date.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.fecha_salida, b.fecha_salida);
}
else {
return strcmp (b.fecha_salida, a.fecha_salida);
}
});
}
else if (column == departure_time) {
if (!departure_time.sort_indicator) {
reset_columns ();
departure_time.sort_indicator = true;
}
if (departure_time.sort_order == Gtk.SortType.ASCENDING) {
departure_time.sort_order = Gtk.SortType.DESCENDING;
}
else {
departure_time.sort_order = Gtk.SortType.ASCENDING;
}
associated_list.sort_with_data ((a, b) => {
if (departure_time.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.hora_salida, b.hora_salida);
}
else {
return strcmp (b.hora_salida, a.hora_salida);
}
});
}
list_store.clear ();
associated_list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Column.PLACE_NAME, entry.lugar.nombre_lugar,
Column.TICKET_PRICE, Money.format_uint (entry.lugar.valor_entrada),
Column.DIFFICULTY, entry.lugar.nivel,
Column.ARRIVAL_DATE, entry.fecha_llegada,
Column.ARRIVAL_TIME, entry.hora_llegada,
Column.DEPARTURE_DATE, entry.fecha_salida,
Column.DEPARTURE_TIME, entry.hora_salida,
Column.ASSOCIATED, entry);
});
}
/**
* Reset the sort indicator and order of all the columns
*/
private void reset_columns () {
place_name.sort_indicator = false;
place_name.sort_order = Gtk.SortType.DESCENDING;
ticket_price.sort_indicator = false;
ticket_price.sort_order = Gtk.SortType.DESCENDING;
difficulty.sort_indicator = false;
difficulty.sort_order = Gtk.SortType.DESCENDING;
arrival_date.sort_indicator = false;
arrival_date.sort_order = Gtk.SortType.DESCENDING;
arrival_time.sort_indicator = false;
arrival_time.sort_order = Gtk.SortType.DESCENDING;
departure_date.sort_indicator = false;
departure_date.sort_order = Gtk.SortType.DESCENDING;
departure_time.sort_indicator = false;
departure_time.sort_order = Gtk.SortType.DESCENDING;
}
/**
* Update the list store with the data from the database
*/
private void update_list_store () {
try {
associated_list = Asociado.get_all_asociados_by_tour (conn, tour);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
associated_list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Column.PLACE_NAME, entry.lugar.nombre_lugar,
Column.TICKET_PRICE, Money.format_uint (entry.lugar.valor_entrada),
Column.DIFFICULTY, entry.lugar.nivel,
Column.ARRIVAL_DATE, entry.fecha_llegada,
Column.ARRIVAL_TIME, entry.hora_llegada,
Column.DEPARTURE_DATE, entry.fecha_salida,
Column.DEPARTURE_TIME, entry.hora_salida,
Column.ASSOCIATED, entry);
});
}
/**
* Initialize the tour list class
* @param application The application used to make the GLib object
* @param conn The database connection to use
* @param tour The tour which is the parent of this window
*/
public TourPlaces (Gtk.Application application, Connection conn, Tour tour) {
Object (application: application);
this.conn = conn;
this.tour = tour;
this.set_visible (true); // This fixes: Gtk-CRITICAL **: 23:58:22.139: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
}
/**
* Initialize what is needed for this window
*/
public void initialize () {
list_store = new Gtk.ListStore (Column.N_COLUMNS,
typeof (string),
typeof (string),
typeof (uint),
typeof (string),
typeof (string),
typeof (string),
typeof (string),
typeof (Asociado));
update_list_store ();
place_tree.set_model (list_store);
}
}
}