def __init__(self, parent=None): super(ReferenceEditor, self).__init__(parent) self.editor = Ui_ReferenceEditor() self.editor.setupUi(self) self.extraction = None self.populating = False self.for_update = False # Connect signals to update the reference if changed self.editor.filePathLine.textChanged.connect(self._mark_for_update) self.editor.filePathLine.textChanged.connect(self.filePathChanged) self.editor.resultLine.textChanged.connect(self._mark_for_update) self.editor.queryLine.textChanged.connect(self._mark_for_update) self.editor.validitySpin.valueChanged.connect(self._mark_for_update) self.connect(self.editor.fields, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) self.connect(self.editor.authors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) self.connect(self.editor.editors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) # Add new row if last one is not empty self.connect(self.editor.fields, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_fields_last_row) self.connect(self.editor.authors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_authors_last_row) self.connect(self.editor.editors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_editors_last_row)
class ReferenceEditor(QtGui.QWidget): filePathChanged = QtCore.pyqtSignal(QtCore.QString) def __init__(self, parent=None): super(ReferenceEditor, self).__init__(parent) self.editor = Ui_ReferenceEditor() self.editor.setupUi(self) self.extraction = None self.populating = False self.for_update = False # Connect signals to update the reference if changed self.editor.filePathLine.textChanged.connect(self._mark_for_update) self.editor.filePathLine.textChanged.connect(self.filePathChanged) self.editor.resultLine.textChanged.connect(self._mark_for_update) self.editor.queryLine.textChanged.connect(self._mark_for_update) self.editor.validitySpin.valueChanged.connect(self._mark_for_update) self.connect(self.editor.fields, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) self.connect(self.editor.authors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) self.connect(self.editor.editors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self._mark_for_update) # Add new row if last one is not empty self.connect(self.editor.fields, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_fields_last_row) self.connect(self.editor.authors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_authors_last_row) self.connect(self.editor.editors, QtCore.SIGNAL("itemChanged(QTreeWidgetItem *,int)"), self.add_editors_last_row) def set_extraction(self, extraction): self.load(extraction) def clear(self): """ Clears all the information of the reference editor """ self.editor.filePathLine.setText('') self.editor.resultLine.setText('') self.editor.queryLine.setText('') self.editor.validitySpin.setValue(0.0) self.editor.fields.clear() self.editor.authors.clear() self.editor.editors.clear() def create_last_rows(self): """ Adds empty rows at the end of the lists for fields, authors and editors """ item = QtGui.QTreeWidgetItem(self.editor.fields) item.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled) item = QtGui.QTreeWidgetItem(self.editor.authors) item.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled) item = QtGui.QTreeWidgetItem(self.editor.editors) item.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled) def _mark_for_update(self): """ Marks the current reference for update """ if not self.populating: self.for_update = True log.debug("Reference marked for update") #@UndefinedVariable def update(self): """ If needed, it updates the current extraction with all the changes made by the user. Changes won't be flushed to the database until 'Save changes' is clicked. """ if not (self.extraction and self.for_update): return self.for_update = True self.extraction.file_path = unicode(self.editor.filePathLine.text()) self.extraction.query_string = unicode(self.editor.queryLine.text()) self.extraction.result_url = unicode(self.editor.resultLine.text()) if not self.extraction.references: self.extraction.add_reference() reference = self.extraction.references[0] reference.validity = float(self.editor.validitySpin.value()) self._update_fields(reference) self._update_people(self.editor.authors, reference.authors, reference.add_author_by_name) self._update_people(self.editor.editors, reference.editors, reference.add_editor_by_name) def _update_fields(self, reference): """ Updates the fields of a reference """ log.debug('Updating reference') #@UndefinedVariable for index in range(self.editor.fields.topLevelItemCount()): item = self.editor.fields.topLevelItem(index) # Remove empty items if ((len(reference.fields) > index) and not (item.text(0) and item.text(1) and (item.text(2)))): reference.fields.pop(index) # Skip non-empty items that have an invalid status if not ((item.text(2) == 'True' or item.text(2) == 'False')): continue log.debug('Index: %d Number of fields %d' % (index , len(reference.fields))) #@UndefinedVariable try: name = unicode(item.text(0)) value = unicode(item.text(1)) valid = True if str(item.text(2)) == "True" else False except TypeError, e: log.error('Type error when casting to store to database %s' % str(e)) #@UndefinedVariable continue if(len(reference.fields) > index): reference.fields[index].name = name reference.fields[index].value = value reference.fields[index].valid = valid else: reference.add_field(name, value, valid)