class ContextEditWidget(QtGui.QWidget): def __init__(self,parent=None,ctxID=None): self.ctxID = ctxID QtGui.QWidget.__init__(self,None) self.ui = ContextEditUi() self.ui.setupUi(self) if ctxID is not None: self.fillForm(ctxID) def fillForm(self,cid): self.snapapi = SnapAPI() self.ctx = self.snapapi.get_context(cid) try: self.ctx.get_attributes(update=True) except: self.ctx.attributes = {} self.ctx.get_attributes()#update=True may not be updated in the last bliss package? self.ui.line_name.setText(self.ctx.name) self.ui.line_author.setText(self.ctx.author) self.ui.line_reason.setText(self.ctx.reason) self.ui.line_description.setText(self.ctx.description) attributes = self.ctx.get_attributes()#get_attributes_data self.ui.attch.updateList([v['full_name'] for v in attributes.values()]) def onCreatePressed(self): alert = "" if len(str(self.ui.line_name.text()).lstrip()) == 0: alert += "Field <b>name</b> is empty. Please fill it out.<br>" if len(str(self.ui.line_author.text()).lstrip()) == 0: alert += "Field <b>author</b> is empty. Please fill it out.<br>" if len(str(self.ui.line_reason.text()).lstrip()) == 0: alert += "Field <b>reason</b> is empty. Please fill it out.<br>" if len(str(self.ui.line_description.text()).lstrip()) == 0: alert += "Field <b>description</b> is empty. Please fill it out.<br>" if self.ui.final_List.count() == 0: alert += "No <b>attributes</b> have been checked. Please select at least one.<br>" if len(alert) != 0: Qt.QMessageBox.warning(self, "Error", alert) return if self.ctxID: self.modifyContext(self.ctxID) else: self.createNewContext() def createNewContext(self): try: self.snapapi = SnapAPI() attributes=[] for i in range(self.ui.final_List.count()): attributes.append(str(self.ui.final_List.item(i).text())) self.snapapi.create_context(str(self.ui.line_author.text()),str(self.ui.line_name.text()),str(self.ui.line_reason.text()),str(self.ui.line_description.text()),attributes) except Exception: print traceback.format_exc() Qt.QMessageBox.critical(self,"Tango Archiving Problem", "Could not create new context.<br>" + \ "Was not possible to talk with SnapManager DS.<br>" + \ "Please check if DS is running.") return self.newID = self.snapapi.contexts.keys()[len(self.snapapi.contexts.items())-1] print('un contexte nou: %d' %self.newID) self.emit(QtCore.SIGNAL("NewContextCreated(int)"), self.newID) self.onCancelPressed() Qt.QMessageBox.information(self,"Context","Context created succesfully!") def modifyContext(self, cid): print 'In modifyContext(%s)'%cid try: attributes=[] for i in range(self.ui.final_List.count()): attributes.append(str(self.ui.final_List.item(i).text())) #self.snapapi = SnapAPI() self.ctx.name=str(self.ui.line_name.text()) self.ctx.author=str(self.ui.line_author.text()) self.ctx.reason=str(self.ui.line_reason.text()) self.ctx.description=str(self.ui.line_description.text()) #self.snapapi.db.update_context(self.ctx) #self.snapapi.db.update_context_attributes(cid, attributes) self.snapapi.modify_context(cid,self.ctx.author,self.ctx.name, self.ctx.reason,self.ctx.description,attributes) except Exception: print traceback.format_exc() Qt.QMessageBox.critical(self,"Tango Archiving Problem", "Could not modify context.<br>" + \ "Was not possible to talk with SnapManager DS.<br>" + \ "Please check if DS is running.") return Qt.QMessageBox.information(self,"Context","Context modified succesfully!") self.emit(QtCore.SIGNAL("ContextModified(int)"), cid) self.onCancelPressed() def onCancelPressed(self): self.close()
class SnapButton(QtGui.QPushButton): def __init__(self,useInputForComment=False,useWizardForContext=False,parent=None): QtGui.QPushButton .__init__(self,parent) self.setIconSize(Qt.QSize(30,30)) self.setIcon(Qt.QIcon(":/devices/camera-photo.svg")) self.snapapi=SnapAPI() self.contextid = None self.model="" self.author = self.getUserName() self.name = "AutoSnap_" + time.strftime("%Y_%m_%d_%H%M%S") self.reason = "Always a good reason to create an AutoSnap" self.description = "Snap for all attributes of " + self.model self.comment = "AutoSnap" self.useInputForComment = useInputForComment self.useWizardForContext = useWizardForContext QtCore.QObject.connect(self, QtCore.SIGNAL("clicked()"), self.snap) self.setToolTip("Snapshot for device " + self.model) def setModel(self, model): self.model = model self.name = self.model + "_" + time.strftime("%Y_%m_%d_%H%M%S") self.description = "Snap for all attributes of " + self.model self.setToolTip("Snapshot for device " + self.model) def setName(self, name): self.name = name def setAuthor(self, author): self.author = author def setReason(self, reason): self.reason = reason def setDescription(self, description): self.description = description def getUserName(self): for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): user = os.environ.get(name) if user: return user else: return "Unknown User" def getUserInput(self, val, title): input, ok=QtGui.QInputDialog.getText(self, 'Input dialog', 'Do you wan to change the value of ' + title + " ?", QtGui.QLineEdit.Normal, val) if ok: return str(input) else: return str(val) def snap(self): if self.contextid == None: try: attributes = [] device = PyTango.DeviceProxy(self.model) attr_infos = device.attribute_list_query() for attr_info in attr_infos: attributes.append(self.model + '/' + attr_info.name) if self.useWizardForContext: self.author = self.getUserInput(self.author, "Author") self.name = self.getUserInput(self.name, "Name") self.reason = self.getUserInput(self.reason, "Reason") self.description = self.getUserInput(self.description, "Description") self.snapapi.create_context(self.author,self.name,self.reason,self.description,attributes) print "context ok" self.contextid = self.snapapi.contexts.keys()[len(self.snapapi.contexts.items())-1] except Exception: fandango.qt.QExceptionMessage() return try: if self.useInputForComment == True: self.comment = self.getUserInput(self.comment, "Comment") ctx=self.snapapi.get_context(self.contextid) ctx.take_snapshot(str(self.comment)) except Exception: fandango.qt.QExceptionMessage() return