Make GlyphDBEdit columns pluggable.

This commit is contained in:
Andrey Golovizin 2014-09-01 18:41:58 +02:00
parent bcc0b86001
commit 2379c1e98c

View file

@ -32,7 +32,44 @@ from ..glyphdb import GlyphDB
from ..image import Image
class Column(object):
header = None
def data(self, value):
return None
def image(self, value):
return None
def sortKey(self, value):
return self.data(value)
class TextColumn(Column):
header = 'Text'
def data(self, value):
return value.text
def image(self, value):
return value.image.qimage
class ElevationColumn(Column):
header = 'Elevation'
def data(self, value):
return str(value.elevation)
def sortKey(self, value):
return value.elevation
class GlyphDBModel(QAbstractTableModel):
COLUMNS = [
TextColumn(),
ElevationColumn(),
]
def __init__(self, glyphdb, parent=None):
super().__init__(parent)
self.glyphdb = glyphdb
@ -43,26 +80,19 @@ class GlyphDBModel(QAbstractTableModel):
return len(self.values)
def columnCount(self, parent):
return 2
return len(self.COLUMNS)
def headerData(self, section, orientation, role):
if orientation != Qt.Horizontal or role != Qt.DisplayRole:
return None
if section == 0:
return 'Text'
elif section == 1:
return 'Elevation'
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.COLUMNS[section].header
def data(self, index, role):
column = self.COLUMNS[index.column()]
data = self.values[index.row()]
if index.column() == 0:
if role == Qt.DisplayRole:
return data.text
elif role == Qt.DecorationRole:
return data.image.qimage
elif index.column() == 1:
if role == Qt.DisplayRole:
return str(data.elevation)
if role == Qt.DisplayRole:
return column.data(data)
elif role == Qt.DecorationRole:
return column.image(data)
def removeRows(self, row, count, parent=None):
self.beginRemoveRows(parent, row, row + count - 1)
@ -75,18 +105,11 @@ class GlyphDBModel(QAbstractTableModel):
self.endRemoveRows()
return True
def sort(self, column, order):
key_func = None
if column == 0:
def key_func(value):
return value.text
elif column == 1:
def key_func(value):
return value.elevation
if key_func:
def sort(self, column_index, order):
column = self.COLUMNS[column_index]
if column.sortKey is not None:
self.layoutAboutToBeChanged.emit()
self.values.sort(key=key_func, reverse = (order == Qt.DescendingOrder))
self.values.sort(key=column.sortKey, reverse = (order == Qt.DescendingOrder))
self.layoutChanged.emit()