Skip to content

Commit

Permalink
Every asset will get a default group. tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ak2ls2py committed Dec 27, 2016
1 parent a633f25 commit 055ced3
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 20 deletions.
80 changes: 61 additions & 19 deletions src/rrpam_wds/gui/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import logging
import math
import sys
import os
import sys

from guidata.configtools import add_image_module_path
from guidata.configtools import get_icon
from guiqwt.builder import make
from guiqwt.config import CONF
from guidata import userconfig
from guiqwt.plot import CurveDialog
from guiqwt.styles import style_generator
from guiqwt.styles import update_style_attr
Expand Down Expand Up @@ -55,8 +54,8 @@
add_image_module_path("rrpam_wds.gui", "images")
# this how we change an option

# if there is not .config directory in the home directory, create it.
configfile=os.path.join(c.HOMEDIR,'.config')
# if there is not .config directory in the home directory, create it.
configfile = os.path.join(c.HOMEDIR, '.config')
if (os.path.isfile(configfile) and (not os.path.isdir(configfile))):
os.unlink(configfile)
if (not os.path.isdir(configfile)):
Expand Down Expand Up @@ -89,11 +88,7 @@ def hide(self):

def _selected_change_color(self, select):
self.my_selected.setProperty("selected", False)
self.my_selected.setStyleSheet("""
/* other rules go here */
QCheckBox[selected="true"] {background-color: yellow};
QCheckBox[selected="false"] {background-color: palette(base)};
""")

if(select):
self.my_selected.setProperty('selected', "true")
else:
Expand All @@ -104,14 +99,14 @@ def _selected_change_color(self, select):

def selected(self):
if(hasattr(self, "my_selected")):
self.logger.info("returning my (%s) checkbox state" % self)
# self.logger.info("returning my (%s) checkbox state" % self)
return self.my_selected.isChecked()
self.logger.info("No checkbox in me (%s), so returning False for selected state." % self)
return False

def select(self, select=True):
if(hasattr(self, "my_selected")):
self.logger.info("making me (%s) selected=%s" % (self, select))
# self.logger.info("making me (%s) selected=%s" % (self, select))
self.my_selected.setChecked(select)
else:
self.logger.info("No checkbox in me (%s). So ignoring select request." % self)
Expand Down Expand Up @@ -154,6 +149,7 @@ class DataWindow(QDialog):
def __init__(self, mainwindow, parent=None):

super(DataWindow, self).__init__(parent=parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Data Editor")
self.setWindowIcon(get_icon("data.png"))
self.initialize_assetgroups()
Expand All @@ -169,7 +165,7 @@ def draw_network(self, links):
if(not links):
logger.info("Links sent was None. Ignoring request to draw!")
return
logger.info("Drawing links (%s)" % links)
logger.info("Creating assignment items for %d links " % len(links))

for link in links:
self.add_assign_asset_item(link.id)
Expand Down Expand Up @@ -231,14 +227,21 @@ def setup_ui(self):

def connect_signals(self):
self.ui.no_groups.valueChanged.connect(self._set_no_groups)
self.groups_changed.connect(self._update_grouptocopy)
self.groups_changed.connect(self._update_groupchoices)
self.myselectionchanged_signal.connect(self.selected_holder)

def _update_grouptocopy(self, listofgroups):
def _update_groupchoices(self, listofgroups):
logger = logging.getLogger()
logger.info("Updating grouptocopy choices.")
logger.info("Updating grouptocopy choices and my_group choices.")
self.ui.grouptocopy.clear()
self.ui.grouptocopy.addItems(listofgroups)
for myc in self.myplotitems.values():
t = myc.my_group.currentIndex()
myc.my_group.clear()
myc.my_group.addItems(listofgroups)
if(myc.my_group.count() - 1 < t):
t = myc.my_group.count() - 1
myc.my_group.setCurrentIndex(t)

def customize_ui(self):
self.spacerItem1 = QtWidgets.QSpacerItem(
Expand Down Expand Up @@ -362,10 +365,21 @@ def add_assign_asset_item(self, id_):
ag.my_id.setText(id_)
ag.label_9.setText(_translate("projectDataWidget", "Group"))

# customize style
ag.my_selected.setStyleSheet("""
/* other rules go here */
QCheckBox[selected="true"] {background-color: yellow};
QCheckBox[selected="false"] {background-color: palette(base)};
""")

# connect signal
ag.my_selected.stateChanged.connect(self.myselectionchanged)
ag.my_selected.stateChanged.connect(ag._selected_change_color)

# add existing values of grouptocopy to my_group
[ag.my_group.addItem(self.ui.grouptocopy.itemText(i))
for i in range(self.ui.grouptocopy.count())]

# add this to the record.
# 1. Add _id to aq
# 2. then add aq to the record
Expand Down Expand Up @@ -895,10 +909,22 @@ def __init__(self, parent=None):
self.mdi = QMdiArea()
self.arrange_properties_panel()
self.setMenu()
self._standard_windows()
self.connect_project_manager()
self._initialize_all_components()

self._manage_window_settings()

def _initialize_all_components(self):
self._delete_all_subwindows()
self._standard_windows()

def _delete_all_subwindows(self):
# let's close all existing subwindows.
self._remove_all_subwindows()
self.datawindow = None
self.networkmap = None
self.riskmatrix = None

def arrange_properties_panel(self):
self.qs = QSplitter(self)
self.frame = QFrame(parent=self)
Expand Down Expand Up @@ -962,6 +988,7 @@ def hide_log_window(self):
logmdi.setVisible(False)

def show_logwindow(self):
self._setup_logging()
if(not any([x for x in self.mdi.subWindowList() if isinstance(x.widget(), LogDialog)])):
self.addSubWindow(self.logdialog)
self.logdialog.setVisible(True)
Expand All @@ -971,6 +998,17 @@ def connect_project_manager(self):
self.projectgui._new_project_signal.connect(self.pm.new_project)
self.pm.heres_a_project_signal.connect(self.take_up_results)

def _remove_all_subwindows(self):
for subw in self.mdi.subWindowList():
widget = subw.widget()
subw.setAttribute(QtCore.Qt.WA_DeleteOnClose)
subw.close()
widget.close()
widget.setParent(None)
subw.setParent(None)
widget.deleteLater()
subw.deleteLater()

def _standard_windows(self):
self.add_datawindow()
self.add_networkmap()
Expand All @@ -983,11 +1021,16 @@ def selected_holder(self, widget):
logger = logging.getLogger()
logger.info("Got message of change from: %s" % widget)
if(self.update_selected_items):
logger.info("Responding...")
self.update_all_plots_with_selection(widget)
else:
logger.info("Ignoring...")

def update_all_plots_with_selection(self, widget):
logger = logging.getLogger()
logger.info("selection changed!")
# first switch off responding to selections
self.update_selected_items = False
try:
# firt get all subplots
subplots = [x.get_plot() for x in self.optimaltimegraphs.values()]
Expand All @@ -999,17 +1042,15 @@ def update_all_plots_with_selection(self, widget):
# now select the selections of 'widget' in them.
selected_ids = [x.id_ for x in widget.get_selected_items()]
for p in subplots:
# first switch off responding to selections
self.update_selected_items = False
# find corressponding items
targets = [x for x in p.get_items() if getattr(x, 'id_', None) in selected_ids]
# now update
p.select_some_items(targets)
# don't forget to reset

except Exception as e:
logger = logging.getLogger()
logger.exception("non selectable item! (%s)" % e)
# don't forget to reset
self.update_selected_items = True

def add_optimaltimegraph(self):
Expand Down Expand Up @@ -1101,6 +1142,7 @@ def take_up_results(self, results):
self._display_project(results)

def _display_project(self, results=None):
self._initialize_all_components()
if (not results):
results = self.projectgui.projectproperties.dataset.get_network()
nodes = getattr(results, "nodes", None)
Expand Down
39 changes: 39 additions & 0 deletions src/rrpam_wds/tests/test_datawindow2.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,45 @@ def test_selection_and_deselection_change_color_of_assign_assert_widgets(self):
cnew = p.my_selected.property("selected")
self.assertNotEqual(cdefault, cnew)

def test_my_group_initially_has_first_choice_value_selected(self):
self.create_a_new_project()
for item in self.aw.datawindow.myplotitems.values():
self.assertGreater(item.my_group.count(), 0)
self.assertEqual(item.my_group.currentIndex(), 0)
self.assertEqual(item.my_group.currentText(), self.aw.datawindow._getgroupname(0))

def test_if_my_groups_choice_lost_it_will_select_last_available_value(self):
self.create_a_new_project()
l=list(self.aw.datawindow.myplotitems.values())
it0 = l[0].my_group
it1 = l[1].my_group
it2 = l[2].my_group
it3 = l[5].my_group
self.aw.datawindow.ui.no_groups.setValue(8) # now we have 8 groups 0 to 7
it0.setCurrentIndex(7)
it1.setCurrentIndex(6)
it2.setCurrentIndex(5)
it3.setCurrentIndex(4)
# now reduce choice
self.aw.datawindow.ui.no_groups.setValue(7)
self.assertTrue(
it0.currentIndex() ==
it1.currentIndex() ==
it2.currentIndex() + 1 ==
it3.currentIndex() + 2)
self.aw.datawindow.ui.no_groups.setValue(6)
self.assertTrue(
it0.currentIndex() ==
it1.currentIndex() ==
it2.currentIndex() ==
it3.currentIndex() + 1)
self.aw.datawindow.ui.no_groups.setValue(5)
self.assertTrue(
it0.currentIndex() ==
it1.currentIndex() ==
it2.currentIndex() ==
it3.currentIndex())

def create_a_new_project(self):
import tempfile
with mock.patch.object(self.aw.projectgui, '_getSaveFileName', autospec=True) as mock__getSaveFileName:
Expand Down
37 changes: 36 additions & 1 deletion src/rrpam_wds/tests/test_main_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from rrpam_wds.gui import set_pyqt_api # isort:skip # NOQA

import logging

import mock
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMdiArea

Expand All @@ -11,6 +14,7 @@


class test_main_window(Test_Parent):
logger = logging.getLogger()

def test_mainwindow_is_derived_from_QMainWindow(self):
"""optimalTimeGraph should be derived from CurveDialogWithClosable class"""
Expand All @@ -33,7 +37,7 @@ def test_main_window_will_not_add_more_than_one_network_map_or_risk_matrix(self)
list2 = self.aw.mdi.subWindowList()
list1 = [x for x in list1 if not isinstance(x.widget(), optimalTimeGraph)]
list2 = [x for x in list2 if not isinstance(x.widget(), optimalTimeGraph)]
self.assertEqual(list1, list2)
self.assertEqual(len(list1), len(list2))

def test_attempting_to_close_will_minimize_network_map_and_risk_matrix_and_the_last_optimal_time_graph(
self):
Expand Down Expand Up @@ -68,6 +72,37 @@ def test_last_remaining_optimal_time_graph_will_not_be_deleted_but_minimized(sel
for w in self.aw.mdi.subWindowList():
self.assertTrue(w.isMinimized())

def test__draw_network_will_always_call__initialize_all_components(self):
with mock.patch.object(self.aw, "_initialize_all_components", autospec=True) as mock__initialize_all_components:
try:
self.aw._display_project(None)
except Exception as e:
self.logger.info("Exception %s" % e)
self.assertTrue(mock__initialize_all_components.called)

def test__initialize_all_components_will_always_call____remove_all_subwindows(self):
with mock.patch.object(self.aw, "_remove_all_subwindows", autospec=True) as mock__remove_all_subwindows:
try:
self.aw._initialize_all_components()
except Exception as e:
self.logger.info("Exception %s" % e)

self.assertTrue(mock__remove_all_subwindows.called)

def test_calling__delete_all_subwindows_will_remove_all_subwindows(self):
self.assertNotEqual(self.aw.mdi.subWindowList(), [])
self.aw._delete_all_subwindows()
self.assertEqual(self.aw.mdi.subWindowList(), [])

def test__initialize_all_components_will_create_new_subwindows(self):
l1 = self.aw.mdi.subWindowList()
self.aw._initialize_all_components()
l2 = self.aw.mdi.subWindowList()
self.assertGreater(len(l1), 3)
self.assertGreater(len(l2), 3)
for item in l1:
self.assertNotIn(item, l2)

def test_display_project_will_not_raise_exceptions_for_any_object_passed_to_it(self):
dummy = object()
self.aw.take_up_results(dummy)
Expand Down

0 comments on commit 055ced3

Please sign in to comment.