[PyQt] FetchMore Example - Table Edition

2009-05-11 Thread Darryl Wallace

Hello Everyone,

The last time I wrote in I had presented the FetchMore example.  I've 
extended this example to use a simple table model.  The input is a 2d 
numpy array.  I couldn't find an example with lazy table population 
anywhere, so here's what I've got.


Feel free to use it as you wish.  Hope that someone finds it useful.

Regards,
Darryl

--
__
Darryl Wallace: Project Leader
ProSensus Inc.
McMaster Innovation Park
175 Longwood Road South
Hamilton, Ontario, L8P 0A1
Canada(GMT -05:00) 


Tel:   1-905-528-9136
Fax:   1-905-546-1372

Web site:  http://www.prosensus.ca/
__

# Fetch More Example - Lazy Table Edition
# Ported to PyQt4 by Darryl Wallace, 2009 - walla...@gmail.com

import sys
from PyQt4 import QtGui, QtCore
import numpy

class LazyTableModel(QtCore.QAbstractTableModel):

def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.numRows=0
self.numColumns=0
self._data=data
#__init__

def rowCount(self, parent):

parent=QModelIndex

return self.numRows
#rowCount

def columnCount(self, parent):

parent=QModelIndex

return self.numColumns
#columnCount

def data(self, index, role=QtCore.Qt.DisplayRole):

index=QModelIndex

if not index.isValid():
return QtCore.QVariant()

if index.row()=self.numRows or index.row()0 or 
index.column()=self.numColumns or index.column()0:
return QtCore.QVariant()

if role==QtCore.Qt.DisplayRole:
return QtCore.QVariant(self._data[index.row(), index.column()])
elif role==QtCore.Qt.BackgroundRole:
return QtCore.QVariant(QtGui.qApp.palette().base())

return QtCore.QVariant()
#data

def canFetchMore(self, index):

index=QModelIndex

if self.numRowsself._data.shape[0] or 
self.numColumnsself._data.shape[1]:
return True
else:
return False
#canFetchMore

def fetchMore(self, index):

Index=QModelIndex

maxFetch=10 #maximum number of rows/columns to grab at a time.

remainderRows=self._data.shape[0]-self.numRows
rowsToFetch=min(maxFetch, remainderRows)

if rowsToFetch0:
self.beginInsertRows(QtCore.QModelIndex(), self.numRows, 
self.numRows+rowsToFetch-1)
self.endInsertRows()
self.numRows+=rowsToFetch

remainderColumns=self._data.shape[1]-self.numColumns
columnsToFetch=min(maxFetch, remainderColumns)
if columnsToFetch0:
self.beginInsertColumns(QtCore.QModelIndex(), self.numColumns, 
self.numColumns+columnsToFetch-1)
self.endInsertColumns()
self.numColumns+=columnsToFetch

self.emit(QtCore.SIGNAL(numberPopulated), rowsToFetch, columnsToFetch)
#fetchMore
#LazyTableModel

class Window(QtGui.QWidget):

def __init__(self, data, parent=None):

Data is any 2-d numpy array

QtGui.QWidget.__init__(self, parent)

self.model = LazyTableModel(data, parent=self)

view=QtGui.QTableView()
view.setModel(self.model)

self.logViewer=QtGui.QTextBrowser()

self.logViewer.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, 
QtGui.QSizePolicy.Preferred))

self.connect(self.model, QtCore.SIGNAL(numberPopulated), 
self.updateLog)

layout=QtGui.QGridLayout()
layout.addWidget(view, 0, 0, 1, 2)
layout.addWidget(self.logViewer, 1, 0, 1, 2)

self.setLayout(layout)

self.setWindowTitle(self.tr(Fetch More Example - Table Edition))
self.resize(400, 600)
#__init__

def updateLog(self, rows, columns):
self.logViewer.append(self.tr(%1 rows added.  %2 columns 
added).arg(rows).arg(columns))
#updateLog
#Window

if __name__=='__main__':
qApp=QtGui.QApplication(sys.argv)
data=numpy.random.normal(size=(117, 53))
fetchMoreWindow=Window(data)
fetchMoreWindow.show()
qApp.exec_()
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[PyQt] FetchMore Example

2009-05-08 Thread Darryl Wallace

Hello Everyone,

Since I was interested in the FetchMore example (included in Qt4.5) and 
extending it for myself, I just decided to port the FetchMore example to 
PyQt4 since I noticed it was not included in the ItemViews examples in 
the latest snapshot. 


Feel free to use it as you wish.

Phil feel free to included it in your list of examples should it be 
deemed acceptable.  The only thing that I have done differently is that 
I force the QStringList to be a Python list.  I wrote it using PyQt4.4.2 
in Linux (Ubuntu).


See the attached file.

Regards,
Darryl
# Fetch More Example
# Ported to PyQt4 by Darryl Wallace, 2009 - walla...@gmail.com

import sys
from PyQt4 import QtGui, QtCore

class FileListModel(QtCore.QAbstractListModel):

def __init__(self, parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self.fileCount=0
self.fileList=[]#initialize the file list as a Python list.
#__init__

def rowCount(self, parent):

parent=QModelIndex

return self.fileCount
#rowCount

def data(self, index, role=QtCore.Qt.DisplayRole):

index=QModelIndex

if not index.isValid():
return QtCore.QVariant()

if index.row()=len(self.fileList) or index.row()0:
return QtCore.QVariant()

if role==QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.fileList[index.row()])
elif role==QtCore.Qt.BackgroundRole:
batch=(index.row()/100)%2
if batch==0:
return QtCore.QVariant(QtGui.qApp.palette().base())
else:
return QtCore.QVariant(QtGui.qApp.palette().alternateBase())
return QtCore.QVariant()
#data

def canFetchMore(self, index):

index=QModelIndex

if self.fileCountlen(self.fileList):
return True
else:
return False
#canFetchMore

def fetchMore(self, index):

Index=QModelIndex


remainder=len(self.fileList)-self.fileCount
itemsToFetch=min(100, remainder)

self.beginInsertRows(QtCore.QModelIndex(), self.fileCount, 
self.fileCount+itemsToFetch)

self.fileCount+=itemsToFetch

self.endInsertRows()

self.emit(QtCore.SIGNAL(numberPopulated), itemsToFetch)
#fetchMore

def setDirPath(self, path):
dir=QtCore.QDir(path)

self.fileList=list(dir.entryList()) # force Python list.
self.fileCount=0
self.reset()
#setDirPath
#FileListModel

class Window(QtGui.QWidget):

def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)

self.model = FileListModel(self)

self.model.setDirPath(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath))

label = QtGui.QLabel(self.tr(Directory))
lineEdit=QtGui.QLineEdit()
label.setBuddy(lineEdit)

view=QtGui.QListView()
view.setModel(self.model)

self.logViewer=QtGui.QTextBrowser()

self.logViewer.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, 
QtGui.QSizePolicy.Preferred))

self.connect(lineEdit, QtCore.SIGNAL(textChanged(const QString )), 
self.model.setDirPath)
self.connect(lineEdit, QtCore.SIGNAL(textChanged(const QString )), 
 self.logViewer, QtCore.SLOT(clear()))
self.connect(self.model, QtCore.SIGNAL(numberPopulated), 
self.updateLog)

layout=QtGui.QGridLayout()
layout.addWidget(label, 0, 0)
layout.addWidget(lineEdit, 0, 1)
layout.addWidget(view, 1, 0, 1, 2)
layout.addWidget(self.logViewer, 2, 0, 1, 2)

self.setLayout(layout)

self.setWindowTitle(self.tr(Fetch More Example))
#__init__

def updateLog(self, number):
self.logViewer.append(self.tr(%1 items added.).arg(number))
#updateLog
#Window

if __name__=='__main__':
qApp=QtGui.QApplication(sys.argv)

fetchMoreWindow=Window()
fetchMoreWindow.show()
sys.exit(qApp.exec_())
___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Re: [PyQt] FetchMore Example

2009-05-08 Thread projetmbc
I really appreciate that kind of simple examples. This is the best way 
to learn how to use PyQt.


Thanks a lot.

Christophe

Darryl Wallace a écrit :

Hello Everyone,

Since I was interested in the FetchMore example (included in Qt4.5) 
and extending it for myself, I just decided to port the FetchMore 
example to PyQt4 since I noticed it was not included in the ItemViews 
examples in the latest snapshot.

Feel free to use it as you wish.

Phil feel free to included it in your list of examples should it be 
deemed acceptable.  The only thing that I have done differently is 
that I force the QStringList to be a Python list.  I wrote it using 
PyQt4.4.2 in Linux (Ubuntu).


See the attached file.

Regards,
Darryl


___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt



___
PyQt mailing listPyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt