| | 175 | |
| | 176 | def beforeAddIndicatorToGroupShown(self): |
| | 177 | print "AddIndicatorToGroup about to be shown" |
| | 178 | |
| | 179 | domDocument = self.xmlTreeObject.parent.toolboxStuff.doc |
| | 180 | node_list = elementsByAttributeValue(domDocument = domDocument, |
| | 181 | attribute = 'type', |
| | 182 | value = 'indicator') |
| | 183 | |
| | 184 | self.indicator_group_menu.clear() |
| | 185 | for element, node in node_list: |
| | 186 | indicator = QString(element.nodeName()) |
| | 187 | act_indicator = QAction(self.acceptIcon, |
| | 188 | element.nodeName(), |
| | 189 | self.indicator_group_menu) |
| | 190 | callback = lambda indicator=indicator: self.addIndicatorToGroup(indicator) |
| | 191 | QObject.connect(act_indicator, SIGNAL("triggered()"), callback) |
| | 192 | self.indicator_group_menu.addAction(act_indicator) |
| | 194 | def addIndicatorToGroup(self, indicator): |
| | 195 | print "adding indicator to group..." |
| | 196 | print "addIndicatorToGroup pressed with column = %s and item = %s" % \ |
| | 197 | (self.currentColumn, self.currentIndex.internalPointer().node().toElement().tagName()) |
| | 198 | model = self.currentIndex.model() |
| | 199 | document = model.domDocument |
| | 200 | |
| | 201 | newNode = model.create_node(document = document, |
| | 202 | name = indicator, |
| | 203 | type = 'indicator_group_member', |
| | 204 | value = '') |
| | 205 | model.insertRow(0, |
| | 206 | self.currentIndex, |
| | 207 | newNode) |
| | 208 | |
| | 209 | general_node = document.elementsByTagName(QString('general')).item(0) |
| | 210 | available_datasets = get_child_values(parent = general_node, |
| | 211 | child_names = ['available_datasets']) |
| | 212 | |
| | 213 | datasets = '|'.join(str(available_datasets['available_datasets'])[1:-1].split("','")) |
| | 214 | |
| | 215 | visualizations = [ |
| | 216 | 'Map (per indicator per year)', |
| | 217 | 'Chart (per indicator, spans years)', |
| | 218 | 'Table (per indicator, spans years)', |
| | 219 | 'Table (per year, spans indicators)'] |
| | 220 | visualizations = '|'.join(visualizations) |
| | 221 | |
| | 222 | visualization_node = model.create_node(document = document, |
| | 223 | name = 'visualization_type', |
| | 224 | type = 'string', |
| | 225 | value = '', |
| | 226 | choices = visualizations) |
| | 227 | |
| | 228 | dataset_node = model.create_node(document = document, |
| | 229 | name = 'dataset_name', |
| | 230 | type = 'string', |
| | 231 | value = '', |
| | 232 | choices = datasets) |
| | 233 | |
| | 234 | parent = self.currentIndex.parent() |
| | 235 | child_index = model.findElementIndexByName(indicator, parent)[0] |
| | 236 | if child_index.isValid(): |
| | 237 | for node in [dataset_node, visualization_node]: |
| | 238 | model.insertRow(0, |
| | 239 | child_index, |
| | 240 | node) |
| | 241 | else: |
| | 242 | print "No valid node was found..." |
| | 243 | |
| | 244 | self.currentIndex.model().markAsDirty() |
| | 245 | model.emit(SIGNAL("layoutChanged()")) |
| | 246 | |
| | 247 | def beforeRunIndicatorGroupShown(self): |
| | 248 | print "AddIndicatorToGroup about to be shown" |
| | 249 | |
| | 250 | domDocument = self.xmlTreeObject.parent.toolboxStuff.doc |
| | 251 | node_list = elementsByAttributeValue(domDocument = domDocument, |
| | 252 | attribute = 'type', |
| | 253 | value = 'source_data') |
| | 254 | |
| | 255 | self.run_indicator_group_menu.clear() |
| | 256 | for element, node in node_list: |
| | 257 | simulation_run = QString(element.nodeName()) |
| | 258 | act_simulation_run = QAction(self.acceptIcon, |
| | 259 | element.nodeName(), |
| | 260 | self.run_indicator_group_menu) |
| | 261 | callback = lambda simulation_run=simulation_run: self.indicatorGroupRun(simulation_run) |
| | 262 | QObject.connect(act_simulation_run, SIGNAL("triggered()"), callback) |
| | 263 | self.run_indicator_group_menu.addAction(act_simulation_run) |
| | 264 | |
| | 265 | def indicatorGroupRun(self, simulation_run): |
| | 266 | print "indicatorGroupRun pressed with column = %s and item = %s" % \ |
| | 267 | (self.currentColumn, self.currentIndex.internalPointer().node().toElement().tagName()) |
| | 268 | if not self.xmlTreeObject.model.dirty: |
| | 269 | self.xmlTreeObject.parent.resultManagerStuff.addRunIndicatorGroupForm( |
| | 270 | selected_item = self.currentIndex.internalPointer().node().toElement().tagName(), |
| | 271 | simulation_run = simulation_run) |
| | 272 | else: |
| | 273 | # Prompt the user to save... |
| | 274 | QMessageBox.warning(self.xmlTreeObject.parent, |
| | 275 | "Warning", |
| | 276 | "Please save changes to project before generating results") |
| | 277 | |
| 332 | | |
| | 438 | def _build_indicator_group_menu(self): |
| | 439 | #needs to be called when indicator_group right clicked on... |
| | 440 | self.indicator_group_menu = QMenu(self.xmlTreeObject.parent) |
| | 441 | self.indicator_group_menu.setTitle(QString("Add indicator to group...")) |
| | 442 | QObject.connect(self.indicator_group_menu, SIGNAL('aboutToShow()'), self.beforeAddIndicatorToGroupShown) |
| | 443 | self.menu.addMenu(self.indicator_group_menu) |
| | 444 | |
| | 445 | |
| | 446 | self.run_indicator_group_menu = QMenu(self.xmlTreeObject.parent) |
| | 447 | self.run_indicator_group_menu.setTitle(QString('Run indicator group on...')) |
| | 448 | QObject.connect(self.run_indicator_group_menu, SIGNAL('aboutToShow()'), self.beforeRunIndicatorGroupShown) |
| | 449 | |
| | 450 | self.menu.addMenu(self.run_indicator_group_menu) |