import PySide.QtCore as QtCore import PySide.QtGui as QtGui import PySide.QtUiTools as QtUiTools import os #============================================================================== # CLASS ListModel #============================================================================== class ListModel(QtCore.QAbstractListModel): """Wraps a Python list so that it can be used as the model driving a Qt item view widget (list, column, tree, etc.). See the Qt documentation about Model/View programming. """ def __init__(self, mutt, attr, header, parent): super(ListModel, self).__init__(parent) self._mutt = mutt self._attr = attr self._header = header @property def list(self): return getattr(self._mutt, self._attr) def rowCount(self, parent = QtCore.QModelIndex()): return len(self.list) def data(self, index, role = QtCore.Qt.DisplayRole): if role == QtCore.Qt.DisplayRole: return self.list[index.row()] else: return None def headerData(self, section, orientation, role): if role == QtCore.Qt.DisplayRole: return self._header else: return None class Mutt(object): #--------------------------------------------------------------------------- # Initialization #--------------------------------------------------------------------------- def __init__(self, category=None, parent=None, standalone=False): # Empty test data while initializing. self._categories = [] self._subCategories = [] # UI state self.doExpand = True self.isRunning = False # Load Qt Designer UI loader = QtUiTools.QUiLoader() file = QtCore.QFile(os.path.join('c:/mutt','test.ui')) file.open(QtCore.QFile.ReadOnly) self.ui = loader.load(file) file.close() # Initialize widgets self.initTreeViews() def initTreeViews(self): """Initialize the tree views displaying the list of categories, sub-categories and tests.""" ui = self.ui # Helper functions def setupTreeView(treeView): """Setup common to all 3 tree views.""" treeView.setAllColumnsShowFocus(True) treeView.setAlternatingRowColors(True) treeView.setHeaderHidden(False) treeView.setRootIsDecorated( False ) treeView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) treeView.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) treeView.setSortingEnabled( True ) def setupCategoryView(catView, attr, header, selectionChanged, filter): """Common setup for the categories and sub-categories views.""" setupTreeView(catView) catView.setIndentation(0) model = ListModel(self, attr, header, catView) proxyModel = QtGui.QSortFilterProxyModel(catView) proxyModel.setSourceModel(model) catView.setModel(proxyModel) catView.selectionModel().selectionChanged.connect(selectionChanged) proxyModel.setDynamicSortFilter(True) proxyModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) proxyModel.setSortCaseSensitivity(QtCore.Qt.CaseInsensitive) def onFilterChanged(text): proxyModel.setFilterWildcard(text) if not catView.selectionModel().hasSelection(): catView.selectionModel().select( catView.model().index(0,0), QtGui.QItemSelectionModel.Select) filter.textChanged.connect(onFilterChanged) # Confgure the categories and sub-categories views. setupCategoryView(ui.categories, '_categories', "Category", self.onCatSelectionChanged, ui.catFilter) # Confgure the test view. setupTreeView(ui.tests) def onCatSelectionChanged(self, selected, deselected): pass def show(self): """Show and raise the Mutt window.""" self.ui.show() self.ui.raise_() w = Mutt("") w.show()