view DisplayModel.py @ 147:b290e29a94b5

use couchdb's mapping API instead of manually coding around Rows - much leaner code :-)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 25 Aug 2011 11:05:05 +0200
parents 04a730f9d07d
children c5a427d46703
line wrap: on
line source


from PyQt4.QtCore import QAbstractListModel, QModelIndex, QVariant, Qt

class DisplayModel(QAbstractListModel):
    def __init__(self, parent=None, list=None, displayAttribute=None, **args):
        QAbstractListModel.__init__(self, parent, *args)
        self.list = list
        self.displayAttribute = displayAttribute

    def rowCount(self, parent=QModelIndex()):
        return len(self.list)

    def data(self, index, role):
        if index.isValid() and role == Qt.DisplayRole:
            row = index.row()
            object = self.list[row]
            displayString = self._stringToDisplay(object)
            return QVariant(displayString)
        else:
            return QVariant()

    def _stringToDisplay(self, object):
        if hasattr(object, self.displayAttribute):
            return getattr(object, self.displayAttribute)
        else:
            return "invalid display attribute: " + self.displayAttribute