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 PyQt5.QtGui import *
2 from PyQt5.QtCore import *
3 
4 from ost import table
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.double_click = None
44  self.horizontalHeader().setStretchLastSection(True)
45  self.setContextMenuPolicy(Qt.CustomContextMenu)
46  self.setSelectionBehavior(QAbstractItemView.SelectRows)
47  self.setSizePolicy(QSizePolicy.MinimumExpanding,
48  QSizePolicy.MinimumExpanding)
49  self.setSortingEnabled(True)
50  self.setModel(self._model)
51  QObject.connect(self, SIGNAL('doubleClicked(QModelIndex)'),
52  self.OnDoubleClick)
53  def OnDoubleClick(self, model_index):
54  print('DOUBLE')
55  if not self.double_click:
56  return
57  row = table.TableRow(self._model.table.rows[model_index.row()],
58  self._model.table)
59  self.double_click(row)