Make GlyphDBEdit columns pluggable.
This commit is contained in:
parent
bcc0b86001
commit
2379c1e98c
1 changed files with 49 additions and 26 deletions
|
|
@ -32,7 +32,44 @@ from ..glyphdb import GlyphDB
|
||||||
from ..image import Image
|
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):
|
class GlyphDBModel(QAbstractTableModel):
|
||||||
|
COLUMNS = [
|
||||||
|
TextColumn(),
|
||||||
|
ElevationColumn(),
|
||||||
|
]
|
||||||
|
|
||||||
def __init__(self, glyphdb, parent=None):
|
def __init__(self, glyphdb, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.glyphdb = glyphdb
|
self.glyphdb = glyphdb
|
||||||
|
|
@ -43,26 +80,19 @@ class GlyphDBModel(QAbstractTableModel):
|
||||||
return len(self.values)
|
return len(self.values)
|
||||||
|
|
||||||
def columnCount(self, parent):
|
def columnCount(self, parent):
|
||||||
return 2
|
return len(self.COLUMNS)
|
||||||
|
|
||||||
def headerData(self, section, orientation, role):
|
def headerData(self, section, orientation, role):
|
||||||
if orientation != Qt.Horizontal or role != Qt.DisplayRole:
|
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
|
||||||
return None
|
return self.COLUMNS[section].header
|
||||||
if section == 0:
|
|
||||||
return 'Text'
|
|
||||||
elif section == 1:
|
|
||||||
return 'Elevation'
|
|
||||||
|
|
||||||
def data(self, index, role):
|
def data(self, index, role):
|
||||||
|
column = self.COLUMNS[index.column()]
|
||||||
data = self.values[index.row()]
|
data = self.values[index.row()]
|
||||||
if index.column() == 0:
|
|
||||||
if role == Qt.DisplayRole:
|
if role == Qt.DisplayRole:
|
||||||
return data.text
|
return column.data(data)
|
||||||
elif role == Qt.DecorationRole:
|
elif role == Qt.DecorationRole:
|
||||||
return data.image.qimage
|
return column.image(data)
|
||||||
elif index.column() == 1:
|
|
||||||
if role == Qt.DisplayRole:
|
|
||||||
return str(data.elevation)
|
|
||||||
|
|
||||||
def removeRows(self, row, count, parent=None):
|
def removeRows(self, row, count, parent=None):
|
||||||
self.beginRemoveRows(parent, row, row + count - 1)
|
self.beginRemoveRows(parent, row, row + count - 1)
|
||||||
|
|
@ -75,18 +105,11 @@ class GlyphDBModel(QAbstractTableModel):
|
||||||
self.endRemoveRows()
|
self.endRemoveRows()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def sort(self, column, order):
|
def sort(self, column_index, order):
|
||||||
key_func = None
|
column = self.COLUMNS[column_index]
|
||||||
if column == 0:
|
if column.sortKey is not None:
|
||||||
def key_func(value):
|
|
||||||
return value.text
|
|
||||||
elif column == 1:
|
|
||||||
def key_func(value):
|
|
||||||
return value.elevation
|
|
||||||
|
|
||||||
if key_func:
|
|
||||||
self.layoutAboutToBeChanged.emit()
|
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()
|
self.layoutChanged.emit()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue