add asociado list and editor

This commit is contained in:
Chris Cromer 2019-07-20 17:24:55 -04:00
parent 84c077f941
commit a46b95437f
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
11 changed files with 1082 additions and 2 deletions

156
data/ui/asociado.editor.ui Normal file
View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1
Copyright 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.
-->
<interface domain="sernatur">
<requires lib="gtk+" version="3.20"/>
<template class="ColegioAsociadoEditor" parent="GtkApplicationWindow">
<property name="can_focus">False</property>
<property name="type">popup</property>
<property name="title" translatable="yes">Curso Editor</property>
<property name="modal">True</property>
<property name="window_position">center-on-parent</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<property name="skip_taskbar_hint">True</property>
<property name="skip_pager_hint">True</property>
<property name="show_menubar">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Curso</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">end</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="label" translatable="yes">Asignatura</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="spacing">25</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkButton" id="cancel">
<property name="label" translatable="yes">Cerrar</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="save">
<property name="label" translatable="yes">Guardar</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">2</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="asignatura">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="curso">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 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.
-->
<gresources>
<gresource prefix="/cl/cromer/ubb/colegio">
<file preprocess="xml-stripblanks">asociado.list.ui</file>
<file preprocess="xml-stripblanks">asociado.editor.ui</file>
</gresource>
</gresources>

175
data/ui/asociado.list.ui Normal file
View File

@ -0,0 +1,175 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1
Copyright 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.
-->
<interface domain="sernatur">
<requires lib="gtk+" version="3.20"/>
<object class="GtkListStore" id="liststore1"/>
<template class="ColegioAsociadoList" parent="GtkApplicationWindow">
<property name="can_focus">False</property>
<property name="type">popup</property>
<property name="title">Curso</property>
<property name="modal">True</property>
<property name="window_position">center-on-parent</property>
<property name="default_height">240</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<property name="skip_taskbar_hint">True</property>
<property name="skip_pager_hint">True</property>
<property name="show_menubar">False</property>
<child type="titlebar">
<placeholder/>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="hscrollbar_policy">never</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="asociado_tree">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="hscroll_policy">natural</property>
<property name="vscroll_policy">natural</property>
<property name="model">liststore1</property>
<property name="fixed_height_mode">True</property>
<property name="enable_grid_lines">both</property>
<property name="enable_tree_lines">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="selection">
<property name="mode">multiple</property>
<signal name="changed" handler="on_changed_selection" swapped="no"/>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="curso">
<property name="sizing">fixed</property>
<property name="title" translatable="yes">Curso</property>
<property name="clickable">True</property>
<signal name="clicked" handler="on_clicked_column" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="markup">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="asignatura">
<property name="sizing">fixed</property>
<property name="title" translatable="yes">Asignatura</property>
<property name="clickable">True</property>
<signal name="clicked" handler="on_clicked_column" swapped="no"/>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="markup">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="margin_top">5</property>
<property name="margin_bottom">5</property>
<property name="hexpand">True</property>
<property name="column_spacing">25</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkButton" id="edit_asociado">
<property name="label" translatable="yes">Modificar</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="new_asociado">
<property name="label" translatable="yes">Nuevo</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="delete_asociado">
<property name="label" translatable="yes">Eliminar</property>
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="close_asociado">
<property name="label" translatable="yes">Cerrar</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked_button" swapped="no"/>
</object>
<packing>
<property name="left_attach">3</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
</child>
</template>
</interface>

View File

@ -11,6 +11,12 @@ asignatura_gresource = gnome.compile_resources('asignatura_gresource',
c_name: meson.project_name() + '_resource_asignatura',
export: true,
install_header: true)
asociado_gresource = gnome.compile_resources('asociado_gresource',
'asociado.gresource.xml',
source_dir: '.',
c_name: meson.project_name() + '_resource_asociado',
export: true,
install_header: true)
horario_gresource = gnome.compile_resources('horario_gresource',
'horario.gresource.xml',
source_dir: '.',

351
src/asociado_editor.vala Normal file
View File

@ -0,0 +1,351 @@
/*
* Copyright 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 Colegio {
using Constants;
using Misc;
using DB;
using DB.Wrapper;
using Postgres;
[GtkTemplate (ui = "/cl/cromer/ubb/colegio/asociado.editor.ui")]
public class AsociadoEditor : Gtk.ApplicationWindow {
private Connection conn;
private Asociado asociado;
private Asociado? old_asociado = null;
private List<Asignatura> asignaturas;
private Gtk.ListStore asignatura_list_store;
private List<Curso> cursos;
private Gtk.ListStore curso_list_store;
public signal void save_asociado ();
private enum AsignaturaColumn {
NAME,
ASIGNATURA,
N_COLUMNS
}
private enum CursoColumn {
NAME,
CURSO,
N_COLUMNS
}
[GtkChild]
private Gtk.ComboBoxText asignatura;
[GtkChild]
private Gtk.ComboBoxText curso;
[GtkChild]
private Gtk.Button save;
[GtkChild]
private Gtk.Button cancel;
public AsociadoEditor (Gtk.Application application, Connection conn, Asociado? asociado) {
Object (application: application);
this.conn = conn;
this.asociado = asociado;
if (this.asociado != null) {
this.old_asociado = new Asociado ();
this.old_asociado.copy (asociado);
}
}
[GtkCallback]
public void on_clicked_button (Gtk.Button button) {
if (button == cancel) {
this.close ();
}
else if (button == save) {
update_instance ();
if (validate_data ()) {
if (old_asociado == null) {
try {
var res = conn.db.exec ("
INSERT INTO asociado
(id_asignatura, id_curso)
VALUES
(
'" + asociado.asignatura.id_asignatura.to_string () + "',
'" + conn.escape (asociado.curso.id_curso) + "'
)
");
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
catch (PostgresError e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
finally {
save_asociado (); // Signal the parent to update itself
this.close ();
}
}
else {
try {
var res = conn.db.exec ("
UPDATE asociado SET
id_asignatura = " + asociado.asignatura.id_asignatura.to_string () + ",
id_curso = '" + conn.escape (asociado.curso.id_curso) + "'
WHERE id_asignatura = " + old_asociado.asignatura.id_asignatura.to_string () + " AND
id_curso = '" + conn.escape (old_asociado.curso.id_curso) + "'"
);
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
catch (PostgresError e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
finally {
save_asociado (); // Signal the parent to update itself
this.close ();
}
}
}
}
}
private bool validate_data () {
var res = conn.db.exec ("
SELECT id_asignatura, id_curso
FROM asociado
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
return false;
#endif
}
else {
var wra = new ResultWrapper (res);
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
if (check_old_asociado (wra, i) &&
wra.get_int_n (i, "id_asignatura") == asociado.asignatura.id_asignatura &&
wra.get_string_n (i, "id_curso") == asociado.curso.id_curso) {
var msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
"Error: Un curso \"%s\" con la asignatura \"%s\" ya existe!",
asociado.curso.nombre, asociado.asignatura.nombre);
msg.response.connect ((response_id) => {
msg.destroy ();
});
msg.set_title ("Error");
msg.run ();
return false;
}
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
}
return true;
}
private bool check_old_asociado (ResultWrapper wra, int i) {
try {
if (old_asociado == null) {
// This is a new asociado
return true;
}
else if (old_asociado.asignatura.id_asignatura != wra.get_int_n (i, "id_asignatura") ||
old_asociado.curso.id_curso != wra.get_string_n (i, "id_curso")) {
return true;
}
else {
return false;
}
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
return false;
#endif
}
}
private void update_instance () {
Asignatura new_asignatura = new Asignatura ();
Gtk.TreeIter iter;
asignatura.get_active_iter (out iter);
if (asignatura_list_store.iter_is_valid (iter)) {
asignatura_list_store.get (iter,
AsignaturaColumn.ASIGNATURA, out new_asignatura);
}
asociado.asignatura = new_asignatura;
Curso new_curso = new Curso ();
curso.get_active_iter (out iter);
if (curso_list_store.iter_is_valid (iter)) {
curso_list_store.get (iter,
CursoColumn.CURSO, out new_curso);
}
asociado.curso = new_curso;
}
public void initialize () {
var res = conn.db.exec ("
SELECT id_asignatura, nombre
FROM asignatura
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
asignaturas = new List<Asignatura> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Asignatura (
wra.get_int_n (i, "id_asignatura"),
wra.get_string_n (i, "nombre")
);
asignaturas.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
}
res = conn.db.exec ("
SELECT id_curso, nombre
FROM curso
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
cursos = new List<Curso> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Curso (
wra.get_string_n (i, "id_curso"),
wra.get_string_n (i, "nombre")
);
cursos.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
}
asignaturas.sort_with_data ((a, b) => {
return strcmp (a.nombre, b.nombre);
});
cursos.sort_with_data ((a, b) => {
return strcmp (a.nombre, b.nombre);
});
asignatura_list_store = new Gtk.ListStore (AsignaturaColumn.N_COLUMNS,
typeof (string),
typeof (Asignatura));
curso_list_store = new Gtk.ListStore (CursoColumn.N_COLUMNS,
typeof (string),
typeof (Curso));
asignatura.set_model (asignatura_list_store);
curso.set_model (curso_list_store);
if (asociado == null) {
asociado = new Asociado ();
asociado.asignatura = new Asignatura ();
asociado.curso = new Curso ();
}
asignaturas.foreach ((entry) => {
Gtk.TreeIter iter;
asignatura_list_store.append (out iter);
asignatura_list_store.set (iter,
AsignaturaColumn.NAME, entry.nombre,
AsignaturaColumn.ASIGNATURA, entry);
if (entry.id_asignatura == asociado.asignatura.id_asignatura) {
asignatura.set_active_iter (iter);
}
});
cursos.foreach ((entry) => {
Gtk.TreeIter iter;
curso_list_store.append (out iter);
curso_list_store.set (iter,
CursoColumn.NAME, entry.id_curso,
CursoColumn.CURSO, entry);
if (entry.id_curso == asociado.curso.id_curso) {
curso.set_active_iter (iter);
}
});
if (asociado.asignatura.id_asignatura == 0 && asociado.curso.id_curso.strip () == "") {
asignatura.set_active (0);
curso.set_active (0);
}
}
}
}

315
src/asociado_list.vala Normal file
View File

@ -0,0 +1,315 @@
/*
* Copyright 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 Colegio {
using Constants;
using Misc;
using DB;
using DB.Wrapper;
using Postgres;
[GtkTemplate (ui = "/cl/cromer/ubb/colegio/asociado.list.ui")]
public class AsociadoList : Gtk.ApplicationWindow {
private Connection conn;
private enum Column {
CURSO,
ASIGNATURA,
ASOCIADO,
N_COLUMNS
}
private Gtk.ListStore list_store;
private List<Asociado> asociado_list;
[GtkChild]
private Gtk.TreeView asociado_tree;
[GtkChild]
private Gtk.Button new_asociado;
[GtkChild]
private Gtk.Button edit_asociado;
[GtkChild]
private Gtk.Button delete_asociado;
[GtkChild]
private Gtk.Button close_asociado;
[GtkChild]
private Gtk.TreeViewColumn curso;
[GtkChild]
private Gtk.TreeViewColumn asignatura;
[GtkChild]
private Gtk.TreeSelection selection;
public AsociadoList (Gtk.Application application, Connection conn) {
Object (application: application);
this.conn = conn;
this.set_visible (true); // This fixes: Gtk-CRITICAL **: 23:58:22.139: gtk_box_gadget_distribute: assertion 'size >= 0' failed in GtkScrollbar
}
[GtkCallback]
public void on_clicked_button (Gtk.Button button) {
if (button == close_asociado) {
this.close ();
}
if (button == new_asociado) {
var asociado_editor = new AsociadoEditor (application, conn, null);
asociado_editor.set_transient_for (this); // Set this window as the parent of the new window
asociado_editor.initialize ();
asociado_editor.show_all ();
asociado_editor.save_asociado.connect (on_save);
}
else if (button == edit_asociado) {
Gtk.TreeModel model;
var path = selection.get_selected_rows (out model);
path.foreach ((entry) => {
var tree_row_reference = new Gtk.TreeRowReference (model, entry);
Gtk.TreeIter iter;
list_store.get_iter (out iter, tree_row_reference.get_path ());
Asociado asociado;
model.get (iter,
Column.ASOCIADO, out asociado);
var asociado_editor = new AsociadoEditor (application, conn, asociado);
asociado_editor.set_transient_for (this); // Set this window as the parent of the new window
asociado_editor.initialize ();
asociado_editor.show_all ();
asociado_editor.save_asociado.connect (on_save);
});
}
else if (button == delete_asociado) {
Gtk.MessageDialog msg;
if (selection.count_selected_rows () == 1) {
msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.YES_NO,
"¿Está seguro que quiere eliminar este curso?");
}
else {
msg = new Gtk.MessageDialog (this,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.ERROR,
Gtk.ButtonsType.YES_NO,
"¿Está seguro que quiere eliminar estos cursos?");
}
msg.response.connect ((response_id) => {
switch (response_id) {
case Gtk.ResponseType.YES:
Gtk.TreeModel model;
var path = selection.get_selected_rows (out model);
path.foreach ((entry) => {
var tree_row_reference = new Gtk.TreeRowReference (model, entry);
Gtk.TreeIter iter;
list_store.get_iter (out iter, tree_row_reference.get_path ());
Asociado asociado;
model.get (iter,
Column.ASOCIADO, out asociado);
try {
var res = conn.db.exec ("
DELETE FROM asociado
WHERE id_curso = '" + conn.escape (asociado.curso.id_curso) + "' AND
id_asignatura = " + asociado.asignatura.id_asignatura.to_string ());
if (res.get_status () != ExecStatus.COMMAND_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
}
catch (PostgresError e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
});
edit_asociado.sensitive = false;
delete_asociado.sensitive = false;
reset_columns ();
list_store.clear ();
update_list_store ();
break;
}
msg.destroy ();
});
msg.show ();
}
}
[GtkCallback]
private void on_changed_selection(Gtk.TreeSelection selection) {
if (selection.count_selected_rows () == 1) {
edit_asociado.sensitive = true;
delete_asociado.sensitive =true;
}
else if (selection.count_selected_rows () > 1) {
edit_asociado.sensitive = false;
delete_asociado.sensitive = true;
}
else {
edit_asociado.sensitive = false;
delete_asociado.sensitive = false;
}
}
[GtkCallback]
private void on_clicked_column (Gtk.TreeViewColumn column) {
edit_asociado.sensitive = false;
delete_asociado.sensitive = false;
if (column == asignatura) {
if (!asignatura.sort_indicator) {
reset_columns ();
asignatura.sort_indicator = true;
}
if (asignatura.sort_order == Gtk.SortType.ASCENDING) {
asignatura.sort_order = Gtk.SortType.DESCENDING;
}
else {
asignatura.sort_order = Gtk.SortType.ASCENDING;
}
asociado_list.sort_with_data ((a, b) => {
if (asignatura.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.asignatura.nombre, b.asignatura.nombre);
}
else {
return strcmp (b.asignatura.nombre, a.asignatura.nombre);
}
});
}
else if (column == curso) {
if (!curso.sort_indicator) {
reset_columns ();
curso.sort_indicator = true;
}
if (curso.sort_order == Gtk.SortType.ASCENDING) {
curso.sort_order = Gtk.SortType.DESCENDING;
}
else {
curso.sort_order = Gtk.SortType.ASCENDING;
}
asociado_list.sort_with_data ((a, b) => {
if (curso.sort_order == Gtk.SortType.ASCENDING) {
return strcmp (a.curso.nombre, b.curso.nombre);
}
else {
return strcmp (b.curso.nombre, a.curso.nombre);
}
});
}
list_store.clear ();
asociado_list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Column.CURSO, entry.curso.nombre,
Column.ASIGNATURA, entry.asignatura.nombre,
Column.ASOCIADO, entry);
});
}
private void reset_columns () {
curso.sort_indicator = false;
curso.sort_order = Gtk.SortType.DESCENDING;
asignatura.sort_indicator = false;
asignatura.sort_order = Gtk.SortType.DESCENDING;
}
public void on_save(AsociadoEditor asociado_editor) {
edit_asociado.sensitive = false;
delete_asociado.sensitive = false;
reset_columns ();
list_store.clear ();
update_list_store ();
}
private void update_list_store () {
var res = conn.db.exec ("
SELECT C.id_curso, C.nombre AS nombre_curso,
A2.id_asignatura, A2.nombre AS nombre_asignatura
FROM asociado A
JOIN curso C on (C.id_curso = A.id_curso)
JOIN asignatura A2 on (A2.id_asignatura = A.id_asignatura)
");
if (res.get_status () != ExecStatus.TUPLES_OK) {
#if DEBUG
error (conn.db.get_error_message ());
#else
warning (conn.db.get_error_message ());
#endif
}
else {
var wra = new ResultWrapper (res);
asociado_list = new List<Asociado> ();
int n = res.get_n_tuples ();
for (int i = 0; i < n; i++) {
try {
var result = new Asociado (
new Curso (
wra.get_string_n (i, "id_curso"),
wra.get_string_n (i, "nombre_curso")
),
new Asignatura (
wra.get_int_n (i, "id_asignatura"),
wra.get_string_n (i, "nombre_asignatura")
)
);
asociado_list.append (result);
}
catch (Error e) {
#if DEBUG
error (e.message);
#else
warning (e.message);
#endif
}
}
asociado_list.foreach ((entry) => {
Gtk.TreeIter iter;
list_store.append (out iter);
list_store.set (iter,
Column.CURSO, entry.curso.id_curso,
Column.ASIGNATURA, entry.asignatura.nombre,
Column.ASOCIADO, entry);
});
}
}
public void initialize () {
list_store = new Gtk.ListStore (Column.N_COLUMNS,
typeof (string),
typeof (string),
typeof (Asociado));
update_list_store ();
asociado_tree.set_model (list_store);
}
}
}

20
src/db/asociado.vala Normal file
View File

@ -0,0 +1,20 @@
namespace Colegio {
namespace DB {
public class Asociado : Object {
public Curso curso { get; set; default = new Curso (); }
public Asignatura asignatura { get; set; default = new Asignatura (); }
public Asociado (Curso curso = new Curso (), Asignatura asignatura = new Asignatura ()) {
this.curso = curso;
this.asignatura = asignatura;
}
public void copy (Asociado asociado) {
this.curso = new Curso ();
this.curso.copy (asociado.curso);
this.asignatura = new Asignatura ();
this.asignatura.copy (asociado.asignatura);
}
}
}
}

23
src/db/curso.vala Normal file
View File

@ -0,0 +1,23 @@
namespace Colegio {
namespace DB {
public class Curso : Object {
public string id_curso { get; set; default = ""; }
public string nombre { get; set; default = ""; }
public string anyo { get; set; default = ""; }
public Profesor profesor { get; set; default = new Profesor (); }
public Curso (string id_curso = "", string nombre = "", string anyo = "", Profesor profesor = new Profesor ()) {
this.id_curso = id_curso;
this.nombre = nombre;
this.anyo = anyo;
this.profesor = profesor;
}
public void copy (Curso curso) {
this.id_curso = curso.id_curso;
this.nombre = curso.nombre;
this.anyo = curso.anyo;
}
}
}
}

View File

@ -123,7 +123,7 @@ namespace Colegio {
Horario horario;
model.get (iter,
Column.HORARIO, out horario);
var res = conn.db.exec ("
var res = conn.db.exec ("
DELETE FROM horario
WHERE id_asignatura = " + horario.asignatura.id_asignatura.to_string () + " AND
id_bloque = " + horario.bloque.id_bloque.to_string ());

View File

@ -30,6 +30,9 @@ namespace Colegio {
[GtkChild]
private Gtk.MenuItem horarios;
[GtkChild]
private Gtk.MenuItem curso;
[GtkChild]
private Gtk.MenuItem q1;
@ -70,6 +73,12 @@ namespace Colegio {
horarios_window.initialize ();
horarios_window.show_all ();
}
else if (menu_item == curso) {
var asociado_window = new AsociadoList (application, conn);
asociado_window.set_transient_for (this); // Set this window as the parent of the new window
asociado_window.initialize ();
asociado_window.show_all ();
}
else if (menu_item == q1) {
var query_window = new QueryWindow (application, conn, QueryWindow.Query.Q1);
query_window.set_transient_for (this); // Set this window as the parent of the new window

View File

@ -16,6 +16,8 @@ config_data_file = configure_file(input: 'config.vala.in',
vala_sources = files(
'asignatura_editor.vala',
'asignatura_list.vala',
'asociado_editor.vala',
'asociado_list.vala',
'colegio.vala',
'db.vala',
'dbwrapper.vala',
@ -26,8 +28,10 @@ vala_sources = files(
'queries.vala',
'query_window.vala',
'db/asignatura.vala',
'db/asociado.vala',
'db/bloque.vala',
'db/ciudad.vala',
'db/curso.vala',
'db/horario.vala',
'db/profesor.vala',
'db/q1.vala',
@ -41,6 +45,7 @@ vala_sources = files(
sources = vala_sources
sources += main_gresource
sources += asignatura_gresource
sources += asociado_gresource
sources += horario_gresource
sources += query_gresource
sources += config_data_file
@ -52,7 +57,7 @@ inc = include_directories('./')
exe = executable(meson.project_name(),
sources,
vala_args: vala_args,
include_directories : inc,
include_directories: inc,
gui_app: true,
dependencies: [glib_dep, gobject_dep, gtk_dep, gmodule_dep, pq_dep],
install: true)