00001 from PyQt4.QtGui import * 00002 from PyQt4.QtCore import * 00003 00004 from ost import table 00005 __all__=('Table', ) 00006 00007 class TableModel(QAbstractTableModel): 00008 def __init__(self, table, parent=None): 00009 QAbstractTableModel.__init__(self, parent) 00010 self.table=table 00011 00012 def rowCount(self, index): 00013 return len(self.table.rows) 00014 00015 def headerData(self, section, orientation, role): 00016 if role!=Qt.DisplayRole or orientation!=Qt.Horizontal: 00017 return QVariant() 00018 return self.table.col_names[section] 00019 00020 def columnCount(self, index): 00021 return len(self.table.col_names) 00022 00023 def sort(self, column, order): 00024 o='+' 00025 if order!=Qt.AscendingOrder: 00026 o='-' 00027 self.table.Sort(by=self.table.col_names[column], order=o) 00028 self.reset() 00029 00030 def data(self, index, role): 00031 if not index.isValid() or role!=Qt.DisplayRole: 00032 return QVariant() 00033 row=self.table.rows[index.row()] 00034 return QVariant(row[index.column()]) 00035 00036 class Table(QTableView): 00037 def __init__(self, table): 00038 QTableView.__init__(self) 00039 self._model=TableModel(table) 00040 self.setFrameShape(QFrame.NoFrame) 00041 self.setAttribute(Qt.WA_MacSmallSize) 00042 self.setShowGrid(False) 00043 self.double_click = None 00044 self.horizontalHeader().setStretchLastSection(True) 00045 self.setContextMenuPolicy(Qt.CustomContextMenu) 00046 self.setSelectionBehavior(QAbstractItemView.SelectRows) 00047 self.setSizePolicy(QSizePolicy.MinimumExpanding, 00048 QSizePolicy.MinimumExpanding) 00049 self.setSortingEnabled(True) 00050 self.setModel(self._model) 00051 QObject.connect(self, SIGNAL('doubleClicked(QModelIndex)'), 00052 self.OnDoubleClick) 00053 def OnDoubleClick(self, model_index): 00054 print 'DOUBLE' 00055 if not self.double_click: 00056 return 00057 row = table.TableRow(self._model.table.rows[model_index.row()], 00058 self._model.table) 00059 self.double_click(row)