view python/qtpropertyviewer.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents ad14c7c52589
children
line wrap: on
line source

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class QtPropertyModel(QAbstractTableModel):
   def __init__(self):
      super().__init__()
      self.InspectedWidget = None
   def getInspectedWidget(self):
      return self.inspectedWidget
   def setInspectedWidget(self, w):
      self.inspectedWidget = w
      if isinstance(self.inspectedWidget, QObject):
         mo = self.inspectedWidget.metaObject()
         self.metaprops = [mo.property(i) for i in range(mo.propertyCount())]
         self.metaprops = sorted(self.metaprops, key=lambda mp: mp.name())
      else:
         self.metaprops = []
      self.modelReset.emit()
   InspectedWidget = property(getInspectedWidget, setInspectedWidget)
   def rowCount(self, parent):
      return len(self.metaprops)
   def columnCount(self, parent):
      return 2
   def headerData(self, section, orientation, role):
      if orientation == Qt.Horizontal and role == Qt.DisplayRole:
         if section == 0:
            return 'Property'
         elif section == 1:
            return 'Value'
   def data(self, index, role):
      if index.isValid():
         row = index.row()
         col = index.column()
         metaprop = self.metaprops[row]
         if role == Qt.DisplayRole:
            propname = metaprop.name()
            if col == 0:
               return propname
            elif col == 1:
               if metaprop.isReadable():
                  propval = metaprop.read(self.inspectedWidget)
                  return propval
               return 'NOT READABLE'
            return '{0}, {1}'.format(row, col)
         elif role == Qt.BackgroundRole:
            if col == 1:
               if metaprop.isWritable():
                  return QBrush(Qt.green)
               else:
                  return QBrush(Qt.red)
         elif role == Qt.CheckStateRole:
            if col == 1:
               val = metaprop.read(self.inspectedWidget)
               if type(val) is bool:
                  val = Qt.Checked if val else Qt.Unchecked
                  return val

   def flags(self, index):
      if index.isValid():
         row = index.row()
         col = index.column()
         metaprop = self.metaprops[row]
         if col == 1 and metaprop.isWritable():
            val = metaprop.read(self.inspectedWidget)
            if type(val) == bool:
               return super().flags(index) | Qt.ItemIsEditable | Qt.ItemIsUserCheckable
            else:
               return super().flags(index) | Qt.ItemIsEditable
         else:
            return super().flags(index)
   def setData(self, index, value, role):
      if index.isValid():
         row = index.row()
         col = index.column()
         metaprop = self.metaprops[row]
         if role == Qt.EditRole:
            metaprop.write(self.inspectedWidget, value)
            return True
         elif role == Qt.CheckStateRole:
            value = True if value == Qt.Checked else False
            metaprop.write(self.inspectedWidget, value)
            return True
         else:
            print('unknown role:', role)
            return False
      return False

class QtPropertyViewer(QWidget):
   """
      The binviewer consists of a hex view, a ascii view
      and perhaps others..
   """
   def __init__(self):
      super().__init__()
      tableView = QTableView()
      l = QVBoxLayout(self)
      l.addWidget(tableView)
      header = tableView.horizontalHeader()
      header.setResizeMode(QHeaderView.ResizeToContents)
      self.propertyModel = QtPropertyModel()
      tableView.setModel(self.propertyModel)