Skip to content

Commit

Permalink
Merge 'issue-398' into 'dev-3.0' (#433)
Browse files Browse the repository at this point in the history
* #398 Refactor code

* #398 Remove all previous selections

* Close #398
  • Loading branch information
daviderill authored Mar 12, 2018
1 parent e8431ed commit d78fdad
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 86 deletions.
40 changes: 26 additions & 14 deletions actions/manage_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
sys.path.append(plugin_path)
import utils_giswater

from dao.event import Event
from dao.visit import Visit
from dao.om_visit_event import OmVisitEvent
from dao.om_visit import OmVisit
from dao.om_visit_x_arc import OmVisitXArc
from dao.om_visit_x_connec import OmVisitXConnec
from dao.om_visit_x_node import OmVisitXNode
Expand Down Expand Up @@ -80,7 +80,7 @@ def manage_visit(self, visit_id=None, geom_type=None, feature_id=None, single_to
self.locked_feature_id = feature_id

# Create the dialog and signals and related ORM Visit class
self.current_visit = Visit(self.controller)
self.current_visit = OmVisit(self.controller)
self.dlg = AddVisit()
self.load_settings(self.dlg)

Expand Down Expand Up @@ -249,6 +249,9 @@ def manage_accepted(self):
# notify that a new visit has been added
self.visit_added.emit(self.current_visit.id)

# Remove all previous selections
self.remove_selection()


def manage_rejected(self):
"""Do all action when closed the dialog with Cancel or X.
Expand All @@ -257,10 +260,12 @@ def manage_rejected(self):
# set the previous dialog
utils_giswater.setDialog(self.previous_dialog)

# removed current working visit
# this should cascade removing of all related records
# removed current working visit. This should cascade removing of all related records
if hasattr(self, 'it_is_new_visit') and self.it_is_new_visit:
self.current_visit.delete()

# Remove all previous selections
self.remove_selection()


def tab_index(self, tab_name):
Expand Down Expand Up @@ -533,7 +538,7 @@ def event_feature_type_selected(self):
self.disconnect_signal_selection_changed()


def edit_visit(self):
def edit_visit(self, geom_type=None, feature_id=None):
""" Button 65: Edit visit """

# Create the dialog
Expand All @@ -545,11 +550,18 @@ def edit_visit(self):
utils_giswater.setDialog(self.dlg_man)
utils_giswater.set_table_selection_behavior(self.dlg_man.tbl_visit)

# Set a model with selected filter. Attach that model to selected table
table_object = "om_visit"
self.fill_table_object(self.dlg_man.tbl_visit, self.schema_name + "." + table_object)
self.set_table_columns(self.dlg_man.tbl_visit, table_object)
self.set_configuration(self.dlg_man.tbl_visit, table_object)
if geom_type is None:
# Set a model with selected filter. Attach that model to selected table
table_object = "om_visit"
self.fill_table_object(self.dlg_man.tbl_visit, self.schema_name + "." + table_object)
self.set_table_columns(self.dlg_man.tbl_visit, table_object)
else:
# Set a model with selected filter. Attach that model to selected table
table_object = "v_ui_om_visit_x_" + str(geom_type)
expr_filter = geom_type + "_id = '" + feature_id + "'"
# Refresh model with selected filter
self.fill_table_object(self.dlg_man.tbl_visit, self.schema_name + "." + table_object, expr_filter)
self.set_table_columns(self.dlg_man.tbl_visit, table_object)

# manage save and rollback when closing the dialog
self.dlg_man.rejected.connect(self.manage_rejected)
Expand Down Expand Up @@ -789,7 +801,7 @@ def event_insert(self):
return

# create an empty Event
event = Event(self.controller)
event = OmVisitEvent(self.controller)
event.id = event.max_pk() + 1
event.parameter_id = parameter_id
event.visit_id = int(self.visit_id.text())
Expand Down Expand Up @@ -839,7 +851,7 @@ def event_update(self):
return

# fetch the record
event = Event(self.controller)
event = OmVisitEvent(self.controller)
event.id = selected_list[0].data()
if not event.fetch(commit=self.autocommit):
return
Expand Down Expand Up @@ -912,7 +924,7 @@ def event_delete(self):
return

# a fake event to get some ancyllary data
event = Event(self.controller)
event = OmVisitEvent(self.controller)

# Get selected rows
# TODO: use tbl_event.model().fieldIndex(event.pk()) to be pk name independent
Expand Down
29 changes: 17 additions & 12 deletions actions/parent_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@ def remove_selection(self, remove_groups=True):
if layer:
layer.removeSelection()

if remove_groups:
for layer in self.layers['arc']:
layer.removeSelection()
for layer in self.layers['node']:
layer.removeSelection()
for layer in self.layers['connec']:
layer.removeSelection()
for layer in self.layers['gully']:
layer.removeSelection()
for layer in self.layers['element']:
layer.removeSelection()
try:
if remove_groups:
for layer in self.layers['arc']:
layer.removeSelection()
for layer in self.layers['node']:
layer.removeSelection()
for layer in self.layers['connec']:
layer.removeSelection()
for layer in self.layers['gully']:
layer.removeSelection()
for layer in self.layers['element']:
layer.removeSelection()
except:
pass

self.canvas.refresh()

Expand Down Expand Up @@ -855,14 +858,16 @@ def disconnect_snapping(self):
pass


def fill_table_object(self, widget, table_name):
def fill_table_object(self, widget, table_name, expr_filter=None):
""" Set a model with selected filter. Attach that model to selected table """

# Set model
model = QSqlTableModel();
model.setTable(table_name)
model.setEditStrategy(QSqlTableModel.OnManualSubmit)
model.sort(0, 1)
if expr_filter:
model.setFilter(expr_filter)
model.select()

# Check for errors
Expand Down
13 changes: 5 additions & 8 deletions dao/visit.py → dao/om_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class Visit(Table):

"""Class table for Visit."""
class OmVisit(Table):
""" Class to serialize table 'om_visit' """

id = GenericDescriptor(-1)
visitcat_id = GenericDescriptor(None)
Expand All @@ -37,5 +33,6 @@ class Visit(Table):
is_done = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit', 'id')

13 changes: 5 additions & 8 deletions dao/event.py → dao/om_visit_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class Event(Table):

"""Class table for Events."""
class OmVisitEvent(Table):
""" Class to serialize table 'om_visit_event' """

id = GenericDescriptor(-1)
event_code = GenericDescriptor(None)
Expand All @@ -45,5 +41,6 @@ class Event(Table):
is_last = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_event', 'id')

11 changes: 4 additions & 7 deletions dao/om_visit_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class OmVisitParameter(Table):

"""Class table for om_visit_parameter."""
""" Class to serialize table 'om_visit_parameter' """

id = GenericDescriptor(None)
code = GenericDescriptor(None)
Expand All @@ -35,5 +31,6 @@ class OmVisitParameter(Table):
vdefault = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_parameter', 'id')

11 changes: 4 additions & 7 deletions dao/om_visit_x_arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class OmVisitXArc(Table):

"""Class table for Visit."""
""" Class to serialize table 'om_visit_x_arc' """

id = GenericDescriptor(-1)
visit_id = GenericDescriptor(None)
arc_id = GenericDescriptor(None)
is_last = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_x_arc', 'id')

11 changes: 4 additions & 7 deletions dao/om_visit_x_connec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class OmVisitXConnec(Table):

"""Class table for Visit."""
""" Class to serialize table 'om_visit_x_connec' """

id = GenericDescriptor(-1)
visit_id = GenericDescriptor(None)
connec_id = GenericDescriptor(None)
is_last = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_x_connec', 'id')

11 changes: 4 additions & 7 deletions dao/om_visit_x_gully.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class OmVisitXGully(Table):

"""Class table for Visit."""
""" Class to serialize table 'om_visit_x_gully' """

id = GenericDescriptor(-1)
visit_id = GenericDescriptor(None)
gully_id = GenericDescriptor(None)
is_last = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_x_gully', 'id')

11 changes: 4 additions & 7 deletions dao/om_visit_x_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@

__revision__ = '$Format:%H$'

from dao.table import (
Table,
GenericDescriptor
)
from dao.table import Table, GenericDescriptor


class OmVisitXNode(Table):

"""Class table for Visit."""
""" Class to serialize table 'om_visit_x_node' """

id = GenericDescriptor(-1)
visit_id = GenericDescriptor(None)
node_id = GenericDescriptor(None)
is_last = GenericDescriptor(None)

def __init__(self, controller):
"""constructor."""
""" Class constructor """
Table.__init__(self, controller, 'om_visit_x_node', 'id')

5 changes: 2 additions & 3 deletions dao/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ def delete(self, pks=[], all_records=False, where_clause='', commit=True):
"""Delete all listed records with specified pks.
If not ids are specified and not remove all => del current record."""

sql = "DELETE FROM {0}.{1}".format(
self.controller().schema_name, self.table_name())
sql = "DELETE FROM {0}.{1}".format(self.controller().schema_name, self.table_name())
if not all_records:
if not where_clause:
# if ampty list of ids => get the current id of the record
Expand All @@ -204,5 +203,5 @@ def delete(self, pks=[], all_records=False, where_clause='', commit=True):
else:
sql += " WHERE {}".format(where_clause)

return self.controller().execute_sql(sql, commit=commit)
return self.controller().execute_sql(sql, commit=commit, log_sql=True)

9 changes: 3 additions & 6 deletions parent_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,11 +887,10 @@ def check_expression(self, expr_filter, log_info=False):

def open_visit(self):
""" Call button 65: om_visit_management """
self.controller.log_info("open_visit")

manage_visit = ManageVisit(self.iface, self.settings, self.controller, self.plugin_dir)
manage_visit.visit_added.connect(self.update_visit_table)
manage_visit.edit_visit()
manage_visit.edit_visit(self.geom_type, self.id)


def new_visit(self):
Expand Down Expand Up @@ -1294,11 +1293,9 @@ def fill_tbl_event(self, widget, table_name, filter_):
btn_open_gallery.setEnabled(False)
btn_open_visit_doc.setEnabled(False)
btn_open_visit_event.setEnabled(False)

self.tbl_event = self.dialog.findChild(QTableView, "tbl_event_node")
self.tbl_event.clicked.connect(partial(self.tbl_event_clicked, table_name))


# Set signals
widget.clicked.connect(partial(self.tbl_event_clicked, table_name))
event_type.activated.connect(partial(self.set_filter_table_event, widget))
event_id.activated.connect(partial(self.set_filter_table_event2, widget))
self.date_event_to.dateChanged.connect(partial(self.set_filter_table_event, widget))
Expand Down

0 comments on commit d78fdad

Please sign in to comment.