From c4476b41d8b196cec8aa43c05802d016eb594aab Mon Sep 17 00:00:00 2001 From: Martin Baeuml Date: Wed, 8 Dec 2010 17:33:26 +0100 Subject: [PATCH] add new annotation items via mouse interaction and iteminserters --- annotationmodel.py | 22 +++++++++++++++++----- annotationscene.py | 15 +++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/annotationmodel.py b/annotationmodel.py index 634f1a1..881f1d3 100644 --- a/annotationmodel.py +++ b/annotationmodel.py @@ -63,6 +63,15 @@ class ImageFileModelItem(FileModelItem): ami = AnnotationModelItem(ann, self) self.children_.append(ami) + def addAnnotation(self, ann): + self.file_['annotations'].append(ann) + ami = AnnotationModelItem(ann, self) + self.children_.append(ami) + + def removeAnnotation(self, pos): + del self.file_['annotations'][pos] + del self.children_[pos] + def data(self, index, role): if role == ImageRole: return okapy.loadImage(self.filename()) @@ -328,22 +337,25 @@ class AnnotationModel(QAbstractItemModel): return True return False - def addAnnotation(self, frameidx, ann={}, **kwargs): + def addAnnotation(self, imageidx, ann={}, **kwargs): ann.update(kwargs) print "addAnnotation", ann - frameidx = QModelIndex(frameidx) # explicitly convert from QPersistentModelIndex - item = self.itemFromIndex(frameidx) - assert isinstance(item, FrameModelItem) + imageidx = QModelIndex(imageidx) # explicitly convert from QPersistentModelIndex + item = self.itemFromIndex(imageidx) + assert isinstance(item, FrameModelItem) or isinstance(item, ImageFileModelItem) next = len(item.children()) - self.beginInsertRows(frameidx, next, next) + self.beginInsertRows(imageidx, next, next) item.addAnnotation(ann) self.endInsertRows() self.setDirty(True) + self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"), imageidx, imageidx) + return True def removeAnnotation(self, annidx): + # TODO fix to work with ImageFileModelItem annidx = QModelIndex(annidx) # explicitly convert from QPersistentModelIndex item = self.itemFromIndex(annidx) assert isinstance(item, AnnotationModelItem) diff --git a/annotationscene.py b/annotationscene.py index c41f4a2..23acd5b 100644 --- a/annotationscene.py +++ b/annotationscene.py @@ -7,6 +7,7 @@ import okapy from okapy.guiqt.utilities import toQImage class ItemInserter: + #TODO remove model member def __init__(self, scene, model=None): self.scene_ = scene self.model_ = model @@ -53,12 +54,10 @@ class RectItemInserter(ItemInserter): def mousePressEvent(self, event, index): pos = event.scenePos() - # TODO create it in the model instead item = QGraphicsRectItem(QRectF(pos.x(), pos.y(), 0, 0)) self.current_item_ = item self.init_pos_ = pos self.scene().addItem(item) - event.accept() def mouseMoveEvent(self, event, index): @@ -77,6 +76,12 @@ class RectItemInserter(ItemInserter): self.current_item_.rect().height() > 1: # TODO commit to the model print "added rect:", self.current_item_ + rect = self.current_item_.rect() + ann = {'type': 'rect', + 'x': rect.x(), 'y': rect.y(), + 'width': rect.width(), 'height': rect.height()} + index.model().addAnnotation(index, ann) + self.scene().removeItem(self.current_item_) self.current_item_ = None self.init_pos_ = None @@ -132,12 +137,10 @@ class AnnotationScene(QGraphicsScene): self.addItemInserter('polygon', PolygonItemInserter(self)) #self.setMode({'type': 'point'}) - #self.setMode({'type': 'rect'}) - self.setMode({'type': 'poly'}) + self.setMode({'type': 'rect'}) + #self.setMode({'type': 'poly'}) self.reset() - self.setSceneRect(0,0, 640, 480) - self.addRect(QRectF(0, 0, 640, 480), brush=Qt.white) # # getters/setters