def openTemplate(self): if not Rpc.session.logged(): if not self.login(): return dialog = OpenTemplateDialog(self) if dialog.exec_() == QDialog.Rejected: return model = dialog.group[dialog.id] self._template = Template(model.value('name')) self._template.id = model.id fields = Rpc.session.execute('/object', 'execute', 'nan.template.box', 'fields_get') model.value('boxes').addFields(fields) for x in model.value('boxes'): box = TemplateBox() box.rect = QRectF(x.value('x'), x.value('y'), x.value('width'), x.value('height')) box.featureRect = QRectF(x.value('feature_x'), x.value('feature_y'), x.value('feature_width'), x.value('feature_height')) box.name = x.value('name') box.text = x.value('text') box.recognizer = x.value('recognizer') box.type = x.value('type') box.filter = x.value('filter') self._template.addBox(box) self.scene.setTemplate(self._template) self.updateTitle()
def newTemplate(self): answer = QMessageBox.question( self, _('New Template'), _('Do you want to save changes to the current template?'), QMessageBox.Save | QMessageBox.No | QMessageBox.Cancel) if answer == QMessageBox.Cancel: return elif answer == QMessageBox.Save: if not self.saveTemplate(): return self._template = Template(self.Unnamed) self.scene.setTemplate(self._template) self.updateTitle()
def getTemplateFromData(self, cr, uid, data, context=None): template = Template( data['name'] ) template.id = data['id'] template.analysisFunction = data['analysis_function'] ids = self.pool.get('nan.template.box').search( cr, uid, [('template_id','=',data['id'])], context=context ) boxes = self.pool.get('nan.template.box').read(cr, uid, ids, context=context) for y in boxes: box = TemplateBox() box.id = y['id'] box.rect = QRectF( y['x'], y['y'], y['width'], y['height'] ) box.name = y['name'] box.text = y['text'] box.recognizer = y['recognizer'] box.type = y['type'] box.filter = y['filter'] # Important step: ensure box.text is unicode! if isinstance( box.text, str ): box.text = str( box.text, 'latin-1' ) template.addBox( box ) return template
def getTemplateFromData(self, cr, uid, data): template = Template(data['name']) template.id = data['id'] ids = self.pool.get('nan.template.box').search( cr, uid, [('template', '=', data['id'])]) boxes = self.pool.get('nan.template.box').read(cr, uid, ids) for y in boxes: box = TemplateBox() box.id = y['id'] box.rect = QRectF(y['x'], y['y'], y['width'], y['height']) box.name = y['name'] box.text = y['text'] box.recognizer = y['recognizer'] box.type = y['type'] box.filter = y['filter'] # Important step: ensure box.text is unicode! if isinstance(box.text, str): box.text = unicode(box.text, 'latin-1') template.addBox(box) print "GETTING TEMPLATE: %s WITH %d BOXES" % (data['name'], len(boxes)) return template
def loadAll(): fields = ['name', 'boxes'] templateFields = Rpc.session.execute('/object', 'execute', 'nan.template', 'fields_get', fields) fields = [ 'x', 'y', 'width', 'height', 'feature_x', 'feature_y', 'feature_width', 'feature_height', 'name', 'text', 'recognizer', 'type', 'filter' ] boxFields = Rpc.session.execute('/object', 'execute', 'nan.template.box', 'fields_get', fields) ids = Rpc.session.execute('/object', 'execute', 'nan.template', 'search', []) group = RecordGroup('nan.template', templateFields, ids) templates = [] for record in group: template = Template(record.value('name')) template.id = record.id record.value('boxes').addFields(boxFields) for boxRecord in record.value('boxes'): box = TemplateBox() box.rect = QRectF(boxRecord.value('x'), boxRecord.value('y'), boxRecord.value('width'), boxRecord.value('height')) box.featureRect = QRectF(boxRecord.value('feature_x'), boxRecord.value('feature_y'), boxRecord.value('feature_width'), boxRecord.value('feature_height')) box.name = boxRecord.value('name') box.text = boxRecord.value('text') box.recognizer = boxRecord.value('recognizer') box.type = boxRecord.value('type') box.filter = boxRecord.value('filter') template.addBox(box) templates.append(template) return templates
def __init__(self, parent=None): QMainWindow.__init__(self, parent) loadUi('mainwindow.ui', self) self.scene = DocumentScene() self.uiView.setScene(self.scene) self.uiView.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform) self.uiView.setCacheMode(QGraphicsView.CacheBackground) self._template = Template(self.Unnamed) self.scene.setTemplate(self._template) self.uiTool = ToolWidget(self.uiToolDock) self.undoGroup = QUndoGroup(self) stack = QUndoStack(self.undoGroup) self.undoGroup.setActiveStack(stack) # Let default Qt Undo and Redo Actions handle the Undo/Redo actions # And put them at the very beggining of the Edit menu undoAction = self.undoGroup.createUndoAction(self.menuEdit) undoAction.setShortcut("Ctrl+Z") redoAction = self.undoGroup.createRedoAction(self.menuEdit) redoAction.setShortcut("Ctrl+Shift+Z") if self.menuEdit.actions(): firstAction = self.menuEdit.actions()[0] else: firstAction = None self.menuEdit.insertAction(firstAction, undoAction) self.menuEdit.insertAction(firstAction, redoAction) self.connect(self.scene, SIGNAL('newTemplateBox(QRectF)'), self.newTemplateBox) self.connect( self.scene, SIGNAL('currentTemplateBoxChanged(PyQt_PyObject,PyQt_PyObject)'), self.currentTemplateBoxChanged) self.connect(self.scene, SIGNAL('mouseMoved'), self.sceneMouseMoved) self.connect(self.actionExit, SIGNAL('triggered()'), self.close) self.connect(self.actionOpenImage, SIGNAL('triggered()'), self.openImage) self.connect(self.actionOpenTemplate, SIGNAL('triggered()'), self.openTemplate) self.connect(self.actionToggleImageBoxes, SIGNAL('triggered()'), self.toggleImageBoxes) self.connect(self.actionToggleTemplateBoxes, SIGNAL('triggered()'), self.toggleTemplateBoxes) self.connect(self.actionToggleFeatureBoxes, SIGNAL('triggered()'), self.toggleFeatureBoxes) self.connect(self.actionToggleBinarized, SIGNAL('triggered()'), self.toggleBinarized) self.connect(self.actionLogin, SIGNAL('triggered()'), self.login) self.connect(self.actionSaveTemplate, SIGNAL('triggered()'), self.saveTemplate) self.connect(self.actionSaveTemplateAs, SIGNAL('triggered()'), self.saveTemplateAs) self.connect(self.actionNewTemplate, SIGNAL('triggered()'), self.newTemplate) self.connect(self.actionDelete, SIGNAL('triggered()'), self.removeTemplateBox) self.connect(self.actionZoom, SIGNAL('triggered()'), self.zoom) self.connect(self.actionUnzoom, SIGNAL('triggered()'), self.unzoom) self.connect(self.actionFindMatchingTemplateByOffset, SIGNAL('triggered()'), self.findMatchingTemplateByOffset) self.connect(self.actionFindMatchingTemplateByText, SIGNAL('triggered()'), self.findMatchingTemplateByText) self.connect(self.actionRecognizeInvoice, SIGNAL('triggered()'), self.recognizeInvoice) self.toggleImageBoxes() QTimer.singleShot(1000, self.setup) self.updateTitle() self.updateActions() # Login defaults LoginDialog.defaultHost = 'localhost' LoginDialog.defaultPort = 8070 LoginDialog.defaultProtocol = 'socket://' LoginDialog.defaultUserName = '******' self.recognizer = Recognizer() self.connect(self.recognizer, SIGNAL('finished()'), self.recognized)