OpenStructure
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.tabletable=table
11 
12  def rowCount(self, index):
13  return len(self.tabletable.rows)
14 
15  def headerData(self, section, orientation, role):
16  if role!=Qt.DisplayRole or orientation!=Qt.Horizontal:
17  return QVariant()
18  return self.tabletable.col_names[section]
19 
20  def columnCount(self, index):
21  return len(self.tabletable.col_names)
22 
23  def sort(self, column, order):
24  o='+'
25  if order!=Qt.AscendingOrder:
26  o='-'
27  self.tabletable.Sort(by=self.tabletable.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.tabletable.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_model=TableModel(table)
40  self.setFrameShape(QFrame.NoFrame)
41  self.setAttribute(Qt.WA_MacSmallSize)
42  self.setShowGrid(False)
43  self.double_clickdouble_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_model)
51  QObject.connect(self, SIGNAL('doubleClicked(QModelIndex)'),
52  self.OnDoubleClickOnDoubleClick)
53  def OnDoubleClick(self, model_index):
54  print('DOUBLE')
55  if not self.double_clickdouble_click:
56  return
57  row = table.TableRow(self._model_model.table.rows[model_index.row()],
58  self._model_model.table)
59  self.double_clickdouble_click(row)
def OnDoubleClick(self, model_index)
Definition: table.py:53
def __init__(self, table)
Definition: table.py:37
def sort(self, column, order)
Definition: table.py:23
def columnCount(self, index)
Definition: table.py:20
def data(self, index, role)
Definition: table.py:30
def headerData(self, section, orientation, role)
Definition: table.py:15
def __init__(self, table, parent=None)
Definition: table.py:8
def rowCount(self, index)
Definition: table.py:12