OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
table.py
Go to the documentation of this file.
1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
3 
4 
5 __all__=('Table', )
6 
7 class TableModel(QAbstractTableModel):
8  def __init__(self, table, parent=None):
9  QAbstractTableModel.__init__(self, parent)
10  self.table=table
11 
12  def rowCount(self, index):
13  return len(self.table.rows)
14 
15  def headerData(self, section, orientation, role):
16  if role!=Qt.DisplayRole or orientation!=Qt.Horizontal:
17  return QVariant()
18  return self.table.col_names[section]
19 
20  def columnCount(self, index):
21  return len(self.table.col_names)
22 
23  def sort(self, column, order):
24  o='+'
25  if order!=Qt.AscendingOrder:
26  o='-'
27  self.table.Sort(by=self.table.col_names[column], order=o)
28  self.reset()
29 
30  def data(self, index, role):
31  if not index.isValid() or role!=Qt.DisplayRole:
32  return QVariant()
33  row=self.table.rows[index.row()]
34  return QVariant(row[index.column()])
35 
36 class Table(QTableView):
37  def __init__(self, table):
38  QTableView.__init__(self)
39  self._model=TableModel(table)
40  self.setFrameShape(QFrame.NoFrame)
41  self.setAttribute(Qt.WA_MacSmallSize)
42  self.setShowGrid(False)
43  #self.horizontalHeader().setStretchLastSection(True)
44  self.setContextMenuPolicy(Qt.CustomContextMenu)
45  self.setSelectionBehavior(QAbstractItemView.SelectRows)
46  self.setSizePolicy(QSizePolicy.MinimumExpanding,
47  QSizePolicy.MinimumExpanding)
48  self.setSortingEnabled(True)
49  self.setModel(self._model)