def __init__(self, parent=None, selectMode=True): QDialog.__init__(self, parent) self.setupUi(self) self.notifBar = NotificationBar(self.vlNotification) if selectMode: self.buttonBox.setVisible(True) self.manageButtonBox.setVisible(False) currHeight = self.size().height() self.resize(200, currHeight) else: self.buttonBox.setVisible(False) self.manageButtonBox.setVisible(True) self.setWindowTitle( QApplication.translate("TemplateDocumentSelector", "Template Manager")) #Configure manage buttons btnEdit = self.manageButtonBox.button(QDialogButtonBox.Ok) btnEdit.setText( QApplication.translate("TemplateDocumentSelector", "Edit...")) btnEdit.setIcon(QIcon(":/plugins/stdm/images/icons/edit.png")) btnDelete = self.manageButtonBox.button(QDialogButtonBox.Save) btnDelete.setText( QApplication.translate("TemplateDocumentSelector", "Delete")) btnDelete.setIcon(QIcon(":/plugins/stdm/images/icons/delete.png")) #Connect signals self.buttonBox.accepted.connect(self.onAccept) btnEdit.clicked.connect(self.onEditTemplate) btnDelete.clicked.connect(self.onDeleteTemplate) #Get saved document templates then add to the model templates = documentTemplates() self._docItemModel = QStandardItemModel(parent) self._docItemModel.setColumnCount(2) for name, path in templates.iteritems(): docNameItem = self._createDocNameItem(name) filePathItem = QStandardItem(path) self._docItemModel.appendRow([docNameItem, filePathItem]) self.lstDocs.setModel(self._docItemModel)
def __init__(self,parent = None,selectMode=True): QDialog.__init__(self,parent) self.setupUi(self) self.notifBar = NotificationBar(self.vlNotification) if selectMode: self.buttonBox.setVisible(True) self.manageButtonBox.setVisible(False) currHeight = self.size().height() self.resize(200,currHeight) else: self.buttonBox.setVisible(False) self.manageButtonBox.setVisible(True) self.setWindowTitle(QApplication.translate("TemplateDocumentSelector","Template Manager")) #Configure manage buttons btnEdit = self.manageButtonBox.button(QDialogButtonBox.Ok) btnEdit.setText(QApplication.translate("TemplateDocumentSelector","Edit...")) btnEdit.setIcon(QIcon(":/plugins/stdm/images/icons/edit.png")) btnDelete = self.manageButtonBox.button(QDialogButtonBox.Save) btnDelete.setText(QApplication.translate("TemplateDocumentSelector","Delete")) btnDelete.setIcon(QIcon(":/plugins/stdm/images/icons/delete.png")) #Connect signals self.buttonBox.accepted.connect(self.onAccept) btnEdit.clicked.connect(self.onEditTemplate) btnDelete.clicked.connect(self.onDeleteTemplate) #Get saved document templates then add to the model templates = documentTemplates() self._docItemModel = QStandardItemModel(parent) self._docItemModel.setColumnCount(2) for name,path in templates.iteritems(): docNameItem = self._createDocNameItem(name) filePathItem = QStandardItem(path) self._docItemModel.appendRow([docNameItem,filePathItem]) self.lstDocs.setModel(self._docItemModel)
def saveTemplate(self): """ Creates and saves a new document template. """ #Validate if the user has specified the data source if not self.selectedDataSource(): QMessageBox.critical(self.composerView(), QApplication.translate("ComposerWrapper","Error"), QApplication.translate("ComposerWrapper","Please specify the " "data source name for the document composition.")) return #If it is a new unsaved document template then prompt for the document name. docFile = self.documentFile() if docFile is None: docName,ok = QInputDialog.getText(self.composerView(), QApplication.translate("ComposerWrapper","Template Name"), QApplication.translate("ComposerWrapper","Please enter the template name below"), ) if ok and docName: templateDir = self._composerTemplatesPath() if templateDir is None: QMessageBox.critical(self.composerView(), QApplication.translate("ComposerWrapper","Error"), QApplication.translate("ComposerWrapper", "Directory for document templates cannot not be found.")) return absPath = templateDir + "/" + docName + ".sdt" #Check if there is an existing document with the same name caseInsenDic = CaseInsensitiveDict(documentTemplates()) if docName in caseInsenDic: result = QMessageBox.warning(self.composerView(), QApplication.translate("ComposerWrapper", "Existing Template"), u"'{0}' {1}.\nDo you want to replace the " "existing template?".format(docName, QApplication.translate("ComposerWrapper", "already exists")), QMessageBox.Yes|QMessageBox.No) if result == QMessageBox.Yes: #Delete the existing template delFile = QFile(absPath) remStatus = delFile.remove() if not remStatus: QMessageBox.critical(self.composerView(), QApplication.translate("ComposerWrapper", "Delete Error"), "'{0}' {1}.".format(docName, QApplication.translate("ComposerWrapper", "template could not be removed by the system," " please remove it manually from the document templates directory."))) return else: return docFile= QFile(absPath) else: return docFileInfo = QFileInfo(docFile) if not docFile.open(QIODevice.WriteOnly): QMessageBox.critical(self.composerView(), QApplication.translate("ComposerWrapper", "Save Operation Error"), "{0}\n{1}".format(QApplication.translate("ComposerWrapper", "Could not save template file."), docFile.errorString() )) return templateDoc = QDomDocument() template_name = docFileInfo.completeBaseName() self._writeXML(templateDoc, template_name) if docFile.write(templateDoc.toByteArray()) == -1: QMessageBox.critical(self.composerView(), QApplication.translate("ComposerWrapper","Save Error"), QApplication.translate("ComposerWrapper","Could not save template file.")) return else: self.mainWindow().setWindowTitle(template_name) docFile.close()