Skip to content

Commit 417d9ef

Browse files
committed
Added remove and modify functionality in the first indivs tab
* #48
1 parent 1b2157b commit 417d9ef

File tree

6 files changed

+117
-53
lines changed

6 files changed

+117
-53
lines changed

MLC/GUI/Autogenerated/autogenerated.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,6 @@ def setupUi(self, ExperimentWindow):
731731
self.first_add_indiv_from_textfile = QtWidgets.QPushButton(self.groupBox_11)
732732
self.first_add_indiv_from_textfile.setObjectName("first_add_indiv_from_textfile")
733733
self.verticalLayout_10.addWidget(self.first_add_indiv_from_textfile)
734-
self.first_modifiy_indiv = QtWidgets.QPushButton(self.groupBox_11)
735-
self.first_modifiy_indiv.setObjectName("first_modifiy_indiv")
736-
self.verticalLayout_10.addWidget(self.first_modifiy_indiv)
737734
self.first_remove_indiv = QtWidgets.QPushButton(self.groupBox_11)
738735
self.first_remove_indiv.setObjectName("first_remove_indiv")
739736
self.verticalLayout_10.addWidget(self.first_remove_indiv)
@@ -746,7 +743,7 @@ def setupUi(self, ExperimentWindow):
746743
ExperimentWindow.setCentralWidget(self.centralWidget)
747744

748745
self.retranslateUi(ExperimentWindow)
749-
self.tabWidget.setCurrentIndex(1)
746+
self.tabWidget.setCurrentIndex(0)
750747
self.next_gen_button.clicked.connect(ExperimentWindow.on_next_gen_button_clicked)
751748
self.save_config_button.clicked.connect(ExperimentWindow.on_save_config_button_clicked)
752749
self.dimension_check.clicked.connect(ExperimentWindow.on_dimension_check_clicked)
@@ -776,7 +773,6 @@ def setupUi(self, ExperimentWindow):
776773
self.export_config_button.clicked.connect(ExperimentWindow.on_export_config_button_clicked)
777774
self.first_add_indiv.clicked.connect(ExperimentWindow.on_first_add_indiv_button_clicked)
778775
self.first_add_indiv_from_textfile.clicked.connect(ExperimentWindow.on_first_add_indiv_from_textfile_button_clicked)
779-
self.first_modifiy_indiv.clicked.connect(ExperimentWindow.on_first_modify_indiv_button_clicked)
780776
self.first_remove_indiv.clicked.connect(ExperimentWindow.on_first_remove_indiv_button_clicked)
781777
# QtCore.QMetaObject.connectSlotsByName(ExperimentWindow)
782778

@@ -835,7 +831,6 @@ def retranslateUi(self, ExperimentWindow):
835831
self.groupBox_11.setTitle(_translate("ExperimentWindow", "Management"))
836832
self.first_add_indiv.setText(_translate("ExperimentWindow", "Add Individual"))
837833
self.first_add_indiv_from_textfile.setText(_translate("ExperimentWindow", "Add Individual From Text File"))
838-
self.first_modifiy_indiv.setText(_translate("ExperimentWindow", "Modify Individual"))
839834
self.first_remove_indiv.setText(_translate("ExperimentWindow", "Remove Individual"))
840835
self.tabWidget.setTabText(self.tabWidget.indexOf(self.first_indivs_tab), _translate("ExperimentWindow", "Individuals"))
841836

MLC/GUI/Autogenerated/mlc_qtcreator/experiment.ui

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<enum>QTabWidget::North</enum>
2525
</property>
2626
<property name="currentIndex">
27-
<number>1</number>
27+
<number>0</number>
2828
</property>
2929
<widget class="QWidget" name="experiment_tab">
3030
<property name="layoutDirection">
@@ -724,13 +724,6 @@
724724
</property>
725725
</widget>
726726
</item>
727-
<item>
728-
<widget class="QPushButton" name="first_modifiy_indiv">
729-
<property name="text">
730-
<string>Modify Individual</string>
731-
</property>
732-
</widget>
733-
</item>
734727
<item>
735728
<widget class="QPushButton" name="first_remove_indiv">
736729
<property name="text">
@@ -1230,22 +1223,6 @@
12301223
</hint>
12311224
</hints>
12321225
</connection>
1233-
<connection>
1234-
<sender>first_modifiy_indiv</sender>
1235-
<signal>clicked()</signal>
1236-
<receiver>ExperimentWindow</receiver>
1237-
<slot>on_first_modify_indiv_button_clicked()</slot>
1238-
<hints>
1239-
<hint type="sourcelabel">
1240-
<x>826</x>
1241-
<y>149</y>
1242-
</hint>
1243-
<hint type="destinationlabel">
1244-
<x>480</x>
1245-
<y>283</y>
1246-
</hint>
1247-
</hints>
1248-
</connection>
12491226
<connection>
12501227
<sender>first_remove_indiv</sender>
12511228
<signal>clicked()</signal>
@@ -1287,7 +1264,6 @@
12871264
<slot>on_board_config_button_clicked()</slot>
12881265
<slot>on_edit_config_button_clicked()</slot>
12891266
<slot>on_first_add_indiv_button_clicked()</slot>
1290-
<slot>on_first_modify_indiv_button_clicked()</slot>
12911267
<slot>on_first_remove_indiv_button_clicked()</slot>
12921268
<slot>on_first_add_indiv_from_textfile_button_clicked()</slot>
12931269
</slots>

MLC/GUI/Experiment/ExperimentWindow.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,25 +393,18 @@ def on_edit_config_button_clicked(self):
393393
QDesktopServices.openUrl(QUrl(self._experiment_conf_path))
394394

395395
def on_first_add_indiv_button_clicked(self):
396-
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '
397-
'Executing on_first_add_indiv_button_clicked function'
398-
.format(self._experiment_name))
399396
self._first_indivs_manager.add_individual()
400397

401398
def on_first_add_indiv_from_textfile_button_clicked(self):
402399
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '
403400
'Executing on_first_add_indiv_from_textfile_button_clicked function'
404401
.format(self._experiment_name))
405402

406-
def on_first_modify_indiv_button_clicked(self):
407-
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '
408-
'Executing on_first_modify_indiv_button_clicked function'
409-
.format(self._experiment_name))
410-
411403
def on_first_remove_indiv_button_clicked(self):
412404
logger.debug('[EXPERIMENT {0}] [BOARD_CONFIG_BUTTON] - '
413405
'Executing on_first_remove_indiv_button_clicked function'
414406
.format(self._experiment_name))
407+
self._first_indivs_manager.remove_individual()
415408

416409
def _config_table_edited(self, left, right):
417410
config_table = self._autogenerated_object.config_table

MLC/GUI/Experiment/FirstIndividualsManager.py

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from MLC.GUI.Tables.ConfigTableModel import ConfigTableModel
2-
from MLC.GUI.util import test_individual_value
2+
from MLC.GUI.util import check_individual_value
33
from MLC.Log.log import get_gui_logger
44
from MLC.individual.Individual import Individual
55
from MLC.mlc_parameters.mlc_parameters import Config
@@ -22,6 +22,10 @@ def __init__(self, parent, experiment_name, autogenerated_object, mlc_local):
2222
self._mlc_local = mlc_local
2323

2424
def add_individual(self):
25+
logger.debug('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
26+
'Executing add_individual function'
27+
.format(self._experiment_name))
28+
2529
indiv = QInputDialog.getText(self._parent, "Add Individual",
2630
"Insert the value of the individual to be added.")
2731

@@ -37,11 +41,10 @@ def add_individual(self):
3741
return
3842

3943
indiv_value = indiv[0]
40-
if test_individual_value(parent=self._parent,
41-
experiment_name=self._experiment_name,
42-
log_prefix="[FIRST_INDIVS_MANAGER]",
43-
indiv_value=indiv_value,
44-
config=Config.get_instance()):
44+
if check_individual_value(parent=self._parent,
45+
experiment_name=self._experiment_name,
46+
log_prefix="[FIRST_INDIVS_MANAGER]",
47+
indiv_value=indiv_value):
4548
self._individuals.append(indiv_value)
4649
self._load_table()
4750

@@ -53,13 +56,80 @@ def add_individual(self):
5356
.format(self._experiment_name, indiv_value))
5457

5558
def add_individuals_from_textfile(self):
56-
pass
57-
58-
def modify_individual(self, indiv_index):
59-
pass
60-
61-
def remove_individual(self, indiv_index):
62-
pass
59+
logger.debug('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
60+
'Executing add_individuals_from_textfile function'
61+
.format(self._experiment_name))
62+
63+
def modify_individual(self, left, right):
64+
logger.debug('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
65+
'Executing modify_individual function'
66+
.format(self._experiment_name))
67+
68+
if len(self._individuals) == 0:
69+
return
70+
71+
table_model = self._first_indivs_table.model()
72+
indiv_id = left.row()
73+
old_value = self._individuals[indiv_id]
74+
new_value = table_model.get_data(left.row(), left.column())
75+
76+
# Check if the value of the new individual is valid
77+
valid_indiv = check_individual_value(parent=self._parent,
78+
experiment_name=self._experiment_name,
79+
log_prefix="[FIRST_INDIVS_MANAGER]",
80+
indiv_value=new_value)
81+
if not valid_indiv:
82+
table_model.set_data(left.row(), left.column(), old_value)
83+
return
84+
85+
response = QMessageBox.information(self._parent,
86+
"Editing Individual",
87+
"Do you really want to change value of the individual?",
88+
QMessageBox.No | QMessageBox.Yes,
89+
QMessageBox.No)
90+
91+
if response == QMessageBox.No:
92+
# Restore the old value
93+
logger.info('[EXPERIMENT {0}] [FIRST_INDIVS] - '
94+
'Edition was canceled. Cell({1}, {2}) - Old value: {3}'
95+
.format(self._experiment_name, left.row(),
96+
left.column(), old_value))
97+
table_model.set_data(left.row(), left.column(), old_value)
98+
else:
99+
logger.info('[EXPERIMENT {0}] [FIRST_INDIVS] - '
100+
'Individual ({1}. {2}) was modified succesfully.'
101+
.format(self._experiment_name, left.row(), new_value))
102+
QMessageBox.information(self._parent,
103+
"Individual Edition",
104+
'Individual was modified succesfully.')
105+
106+
def remove_individual(self):
107+
logger.debug('[EXPERIMENT {0}] [FIRST_INDIVS_MANAGER] - '
108+
'Executing remove_individual function'
109+
.format(self._experiment_name))
110+
111+
if len(self._individuals) == 0:
112+
return
113+
114+
indiv_index = self._first_indivs_table.selectionModel().currentIndex().row()
115+
indiv_value = self._individuals[indiv_index]
116+
117+
response = QMessageBox.question(self._parent,
118+
"Remove Individual",
119+
"Do you really want to remove individual '{0}'"
120+
.format(indiv_value),
121+
QMessageBox.No | QMessageBox.Yes,
122+
QMessageBox.No)
123+
if response == QMessageBox.Yes:
124+
# Remove the individual
125+
logger.info("[FIRST_INDIVS_MANAGER] Experiment {0} - "
126+
"Individual {1} was succesfully removed"
127+
.format(self._experiment_name, indiv_value))
128+
QMessageBox.information(self._parent, "Individual Removed",
129+
"Individual {0} was succesfully removed"
130+
.format(indiv_value))
131+
del self._individuals[indiv_index]
132+
self._load_table()
63133

64134
def get_gen_creator(self):
65135
"""
@@ -101,6 +171,9 @@ def _load_table(self):
101171
self._first_indivs_table.resizeColumnsToContents()
102172
self._first_indivs_table.setVisible(True)
103173
self._first_indivs_table.setSortingEnabled(True)
174+
table_model.set_editable_columns([1])
175+
table_model.set_data_changed_callback(self.modify_individual)
176+
table_model.sort_by_col(0)
104177

105178
def _test_individual_value(self, indiv_value):
106179
"""

MLC/GUI/Tables/ConfigTableModel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def rowCount(self, parent):
2525
return len(self._data)
2626

2727
def columnCount(self, parent):
28-
return len(self._data[0])
28+
try:
29+
return len(self._data[0])
30+
except IndexError:
31+
return 0
2932

3033
def data(self, index, role):
3134
if not index.isValid():

MLC/GUI/util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,27 @@ def test_individual_value(parent, experiment_name, log_prefix, indiv_value, conf
4747
.format(log_prefix, experiment_name, err))
4848

4949
return None
50+
51+
52+
53+
def check_individual_value(parent, experiment_name, log_prefix, indiv_value):
54+
try:
55+
"""
56+
Evaluate an individual in order to check its correctness. Handle Exceptions
57+
"""
58+
LispTreeExpr.check_expression(indiv_value)
59+
return True
60+
except ExprException, err:
61+
# Print the error message returned in the exception,
62+
# removing the prefix ([EXPR_EXCEPTION]])
63+
QMessageBox.critical(parent,
64+
"Invalid Individsual",
65+
"Individual inserted is not well-formed. "
66+
"Error Msg: {0}"
67+
.format(err.message[err.message.find(']') + 2:]))
68+
logger.error("{0} Experiment {1} - "
69+
"Individual inserted is not well-formed. "
70+
"Error Msg: {2}"
71+
.format(log_prefix, experiment_name,
72+
err.message[err.message.find(']') + 2:]))
73+
return False

0 commit comments

Comments
 (0)