mirror of
https://github.com/wassname/sloth.git
synced 2026-07-29 11:27:41 +08:00
Removed old code fragments
This commit is contained in:
@@ -108,367 +108,6 @@ class KeyValueModelItem(ModelItem):
|
||||
return QVariant()
|
||||
|
||||
|
||||
class AnnotationModelItemOld(object):
|
||||
def __init__(self, data, model, parent=None):
|
||||
self.children_ = []
|
||||
self.type_ = type
|
||||
self.data_ = data
|
||||
self.parent_ = parent
|
||||
self.model_ = model
|
||||
self.visible_ = True
|
||||
|
||||
def parent(self):
|
||||
return self.parent_
|
||||
|
||||
def model(self):
|
||||
return self.model_
|
||||
|
||||
def children(self):
|
||||
return self.children_
|
||||
|
||||
def rowOfChild(self, item):
|
||||
for row, child in enumerate(self.children_):
|
||||
if child is item:
|
||||
return row
|
||||
return -1
|
||||
|
||||
def data(self, index, role):
|
||||
return QVariant()
|
||||
|
||||
def flags(self, index):
|
||||
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
|
||||
|
||||
def removeRows(self, position, rows):
|
||||
return False
|
||||
|
||||
def visible(self): return self.visible_
|
||||
def set_visible(self, visible): self.visible_ = visible
|
||||
|
||||
def isCheckable(self, column): return False
|
||||
|
||||
class PropertyDelegate(QItemDelegate):
|
||||
def __init__(self, model, parent=None):
|
||||
super(PropertyDelegate, self).__init__(parent)
|
||||
self.model_ = model
|
||||
|
||||
def createEditor(self, parent, option, index):
|
||||
if index.column() == 0:
|
||||
return None
|
||||
item = self.model_.itemFromIndex(index)
|
||||
return item.createEditor(parent, option, index)
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
item = self.model.getItem(index)
|
||||
item.setEditorData(editor, index)
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
item = self.model.getItem(index)
|
||||
item.setModelData(editor, model, index)
|
||||
|
||||
# def updateEditorGeometry(self, editor, option, index):
|
||||
# editor.setGeometry(option.rect)
|
||||
|
||||
def paint(self, painter, option, index):
|
||||
item = self.model.getItem(index)
|
||||
if isinstance(item, ColorProperty) and index.column() == 1:
|
||||
item.paint(painter, option, index)
|
||||
return
|
||||
QtGui.QItemDelegate.paint(self, painter, option, index)
|
||||
|
||||
class Property(AnnotationModelItem):
|
||||
def __init__(self, key, data, model, parent=None):
|
||||
super(Property, self).__init__(data, model, parent)
|
||||
self.key_ = key
|
||||
|
||||
def data(self, index, role):
|
||||
if index.column() == 0:
|
||||
return QVariant(self.key_)
|
||||
elif index.column() == 1:
|
||||
return QVariant(str(self.data_[self.key_]))
|
||||
return QVariant("")
|
||||
|
||||
def flags(self, index):
|
||||
if index.column() == 1:
|
||||
return super(Property, self).flags(index) | Qt.ItemIsEditable
|
||||
return super(Property, self).flags(index)
|
||||
|
||||
def setData(self, index, value, role):
|
||||
if role == Qt.EditRole:
|
||||
return self.parent().setData(index, value, role)
|
||||
return False
|
||||
|
||||
def createEditor(self, parent, option, index):
|
||||
return None
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
return
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
return
|
||||
|
||||
class StringProperty(Property):
|
||||
def __init__(self, key, data, model, parent):
|
||||
super(StringProperty, self).__init__(key, data, model, parent)
|
||||
|
||||
def createEditor(self, parent, option, index):
|
||||
editor = QLineEdit(parent)
|
||||
return editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
editor.setText(str(self.data_[self.key_]))
|
||||
|
||||
def setModelData(self, editor, model, index):
|
||||
self.data_[self.key_] = str(editor.text())
|
||||
|
||||
class RootAnnotationModelItem(AnnotationModelItem):
|
||||
def __init__(self, data, model):
|
||||
super(RootAnnotationModelItem, self).__init__(data, model)
|
||||
for row, file in enumerate(self.data_.files):
|
||||
self.children_.append(FileAnnotationModelItem(file, model, self))
|
||||
|
||||
def removeRows(self, position, rows):
|
||||
self.data_.files = self.data_.files[:position] + self.data_.files[position+rows:]
|
||||
self.children_ = self.children_[:position] + self.children_[position+rows:]
|
||||
return True
|
||||
|
||||
class FileAnnotationModelItem(AnnotationModelItem):
|
||||
def __init__(self, data, model, parent):
|
||||
super(FileAnnotationModelItem, self).__init__(data, model, parent)
|
||||
for row, annotation in enumerate(self.data_.annotations):
|
||||
if annotation.type == 'rect':
|
||||
self.children_.append(RectAnnotationModelItem(annotation, model, self))
|
||||
elif annotation.type == 'mask':
|
||||
self.children_.append(MaskAnnotationModelItem(annotation, model, self))
|
||||
elif annotation.type == 'point':
|
||||
self.children_.append(PointAnnotationModelItem(annotation, model, self))
|
||||
|
||||
def data(self, index, role):
|
||||
if role == DataRole:
|
||||
return QVariant(self.data_.filename)
|
||||
|
||||
if index.column() == 0:
|
||||
return QVariant(os.path.split(self.data_.filename)[1])
|
||||
else:
|
||||
return QVariant("")
|
||||
|
||||
def removeRows(self, position, rows):
|
||||
self.data_.annotations = self.data_.annotations[:position] + self.data_.annotations[position+rows:]
|
||||
self.children_ = self.children_[:position] + self.children_[position+rows:]
|
||||
return True
|
||||
|
||||
class RectAnnotationModelItem(AnnotationModelItem):
|
||||
def __init__(self, data, model, parent):
|
||||
super(RectAnnotationModelItem, self).__init__(data, model, parent)
|
||||
self.children_.append(StringProperty('id', data, model, self))
|
||||
self.children_.append(StringProperty('class', data, model, self))
|
||||
self.children_.append(StringProperty('x', data, model, self))
|
||||
self.children_.append(StringProperty('y', data, model, self))
|
||||
self.children_.append(StringProperty('width', data, model, self))
|
||||
self.children_.append(StringProperty('height', data, model, self))
|
||||
|
||||
def data(self, index, role):
|
||||
if role == GraphicsItemRole:
|
||||
return QVariant(AnnotationGraphicsRectItem(self, index))
|
||||
elif role == DataRole:
|
||||
return QVariant(QRectF(self.data_.x, self.data_.y, self.data_.width, self.data_.height))
|
||||
elif role == Qt.DisplayRole:
|
||||
if index.column() == 0:
|
||||
return QVariant("Rect")
|
||||
elif index.column() == 1:
|
||||
s = "id: '%s', class: '%s', (%d, %d, %d, %d)" % \
|
||||
(self.data_['id'], self.data_['class'], int(self.data_['x']), int(self.data_['y']),
|
||||
int(self.data_['width']), int(self.data_['height']))
|
||||
return QVariant(s)
|
||||
assert False
|
||||
|
||||
def setData(self, index, value, role):
|
||||
if role == Qt.EditRole:
|
||||
assert index.model().itemFromIndex(index.parent()) == self
|
||||
check_pairs = [('id', value.toString), ('class', value.toString),
|
||||
('x', value.toString), ('y', value.toString),
|
||||
('height', value.toString), ('width', value.toString)]
|
||||
key, f = check_pairs[index.row()]
|
||||
if self.data_[key] != f():
|
||||
self.data_[key] = str(f())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index.parent(), index.parent())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index, index)
|
||||
return True
|
||||
return False
|
||||
|
||||
if role == DataRole:
|
||||
rect = value.toRectF().toRect()
|
||||
modified = False
|
||||
check_pairs = [('x', rect.topLeft().x), ('y', rect.topLeft().y),
|
||||
('width', rect.size().width), ('height', rect.size().height)]
|
||||
for key, f in check_pairs:
|
||||
if self.data_[key] != f():
|
||||
self.data_[key] = f()
|
||||
modified = True
|
||||
|
||||
return modified
|
||||
return False
|
||||
|
||||
def flags(self, index):
|
||||
if index.column() == 0:
|
||||
return Qt.ItemIsUserCheckable | super(RectAnnotationModelItem, self).flags(index)
|
||||
return super(RectAnnotationModelItem, self).flags(index)
|
||||
|
||||
def isCheckable(self, column):
|
||||
if column == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
class MaskAnnotationModelItem(AnnotationModelItem):
|
||||
def __init__(self, data, model, parent):
|
||||
super(MaskAnnotationModelItem, self).__init__(data, model, parent)
|
||||
self.children_.append(StringProperty('id', data, model, self))
|
||||
self.children_.append(StringProperty('class', data, model, self))
|
||||
self.children_.append(StringProperty('filename', data, model, self))
|
||||
self.image_ = None
|
||||
self.shape_ = None
|
||||
self.dirty_ = False
|
||||
|
||||
def set_dirty(self, dirty=True): self.dirty_ = dirty
|
||||
def dirty(self): return self.dirty_
|
||||
|
||||
def data(self, index, role):
|
||||
if role == GraphicsItemRole:
|
||||
return QVariant(AnnotationGraphicsMaskItem(self, index))
|
||||
elif role == DataRole:
|
||||
return QVariant(self.image())
|
||||
elif role == Qt.DisplayRole:
|
||||
if index.column() == 0:
|
||||
return QVariant("Mask")
|
||||
elif index.column() == 1:
|
||||
s = "id: '%s', class: '%s'" % (self.data_['id'], self.data_['class'])
|
||||
return QVariant(s)
|
||||
|
||||
def setData(self, index, value, role):
|
||||
if role == Qt.EditRole:
|
||||
assert index.model().itemFromIndex(index.parent()) == self
|
||||
check_pairs = [('id', value.toString), ('class', value.toString)]
|
||||
key, f = check_pairs[index.row()]
|
||||
if self.data_[key] != f():
|
||||
self.data_[key] = str(f())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index.parent(), index.parent())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index, index)
|
||||
return True
|
||||
return False
|
||||
|
||||
if role == DataRole:
|
||||
assert value.type() == QVariant.Image
|
||||
self.image_ = QImage(value)
|
||||
self.set_dirty()
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index, index)
|
||||
# emit dataChanged (?) shouldn't be necessary
|
||||
return True
|
||||
return False
|
||||
|
||||
def flags(self, index):
|
||||
if index.column() == 0:
|
||||
return Qt.ItemIsUserCheckable | super(MaskAnnotationModelItem, self).flags(index)
|
||||
return super(MaskAnnotationModelItem, self).flags(index)
|
||||
|
||||
def isCheckable(self, column):
|
||||
if column == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def filename(self):
|
||||
if os.path.isabs(self.data_.filename):
|
||||
return self.data_.filename
|
||||
else:
|
||||
return os.path.join(self.model().baseDir(), self.data_.filename)
|
||||
|
||||
def image(self):
|
||||
if self.image_ is None:
|
||||
if os.path.exists(self.filename()):
|
||||
self.image_ = QImage(self.filename()).convertToFormat(QImage.Format_MonoLSB)
|
||||
else:
|
||||
# find parent image
|
||||
parent = self.parent()
|
||||
while (parent is not None):
|
||||
if isinstance(parent, FileAnnotationModelItem):
|
||||
break
|
||||
if parent is None:
|
||||
print >>sys.stderr, "Error: mask annotation item does not have a File parent"
|
||||
else:
|
||||
print 'Creating new mask for', self.filename()
|
||||
im = QImage(parent.data_.filename)
|
||||
self.image_ = QImage(im.width(), im.height(), QImage.Format_MonoLSB)
|
||||
self.image_.fill(0)
|
||||
self.set_dirty()
|
||||
|
||||
# strange qt convention:
|
||||
# white == Qt.color0 == 0 is background
|
||||
# black == Qt.color1 == 1 is foreground
|
||||
# thus we switch the pixel values and reassign the colors
|
||||
self.image_.invertPixels()
|
||||
return self.image_
|
||||
|
||||
def writeback(self):
|
||||
assert self.filename() is not None
|
||||
if not self.dirty(): return
|
||||
tmpimage = QImage(self.image()) # important to wrap this in a QImage constructor, otherwise no real copy made
|
||||
tmpimage.invertPixels()
|
||||
tmpimage.save(self.filename(), os.path.splitext(self.filename())[1][1:])
|
||||
|
||||
class PointAnnotationModelItem(AnnotationModelItem):
|
||||
def __init__(self, data, model, parent):
|
||||
super(PointAnnotationModelItem, self).__init__(data, model, parent)
|
||||
self.children_.append(StringProperty('id', data, model, self))
|
||||
self.children_.append(StringProperty('class', data, model, self))
|
||||
self.children_.append(StringProperty('x', data, model, self))
|
||||
self.children_.append(StringProperty('y', data, model, self))
|
||||
|
||||
def data(self, index, role):
|
||||
if role == GraphicsItemRole:
|
||||
return QVariant(AnnotationGraphicsPointItem(self, index))
|
||||
elif role == DataRole:
|
||||
return QVariant(QPointF(self.data_.x, self.data_.y))
|
||||
elif role == Qt.DisplayRole:
|
||||
if index.column() == 0:
|
||||
return QVariant("Point")
|
||||
elif index.column() == 1:
|
||||
s = "id: '%s', class: '%s', (%d, %d)" % \
|
||||
(self.data_['id'], self.data_['class'], int(self.data_['x']), int(self.data_['y']))
|
||||
return QVariant(s)
|
||||
return QVariant()
|
||||
|
||||
def setData(self, index, value, role):
|
||||
if role == Qt.EditRole:
|
||||
assert index.model().itemFromIndex(index.parent()) == self
|
||||
check_pairs = [('id', value.toString), ('class', value.toString),
|
||||
('x', value.toString), ('y', value.toString)]
|
||||
key, f = check_pairs[index.row()]
|
||||
if self.data_[key] != f():
|
||||
self.data_[key] = str(f())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index.parent(), index.parent())
|
||||
index.model().emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), index, index)
|
||||
return True
|
||||
return False
|
||||
|
||||
if role == DataRole:
|
||||
point = value.toPointF().toPoint()
|
||||
modified = False
|
||||
check_pairs = [('x', point.x), ('y', point.y)]
|
||||
for key, f in check_pairs:
|
||||
if self.data_[key] != f():
|
||||
self.data_[key] = f()
|
||||
modified = True
|
||||
return modified
|
||||
|
||||
return False
|
||||
|
||||
def flags(self, index):
|
||||
if index.column() == 0:
|
||||
return Qt.ItemIsUserCheckable | super(PointAnnotationModelItem, self).flags(index)
|
||||
return super(PointAnnotationModelItem, self).flags(index)
|
||||
|
||||
def isCheckable(self, column):
|
||||
if column == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
class AnnotationModel(QAbstractItemModel):
|
||||
def __init__(self, annotations, parent=None):
|
||||
|
||||
Reference in New Issue
Block a user