import sys
import os
from PyQt4 import QtGui, QtCore
def __init__(self, datain, col ,parent=None, *args):
""" datain: a list where each item is a row
"""
self._listdata = datain
self._col = col
def colData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
else:
return QtCore.
QString(os.
path.
splitext(self._listdata
[section
])[-1][1:].
upper())
return len(self._listdata)
def columnCount(self, parent):
return self._col
def data(self, index, role):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
fileName = os.path.split(self._listdata[row][column])[-1]
return fileName
if role == QtCore.Qt.ToolTipRole:
row = index.row()
column = index.column()
fileName = os.path.split(self._listdata[row][column])[-1]
if index.isValid() and role == QtCore.Qt.DecorationRole:
row = index.row()
column = index.column()
value = self._listdata[row][column]
# pixmap.scaled(400,300, QtCore.Qt.KeepAspectRatio)
pixmap.load(value)
return QtGui.
QImage(pixmap
).
scaled(120,
100)
if index.isValid() and role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self._listdata[row][column]
fileName = os.path.split(value)[-1]
return os.path.splitext(fileName)[0]
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role=QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
newName = os.path.join(str(os.path.split(self._listdata[row][column])[0]), str(value.toString()))
self.__renameFile(self._listdata[row][column], newName)
self._listdata[row][column] = newName
self.dataChanged.emit(index, index)
return True
return False
def __renameFile(self, fileToRename, newName):
try:
os.rename(str(fileToRename), newName)
except Exception, err:
print err
"""docstring for MyTableView"""
def __init__(self):
super(MyTableView, self).__init__()
self.resize(sw, sh)
thumbWidth = 100
thumbheight = 120
col = sw/thumbWidth
self.setColumnWidth(thumbWidth, thumbheight)
crntDir = "/Users/krystosan/Pictures"
# create table
list_data = []
philes = os.listdir(crntDir)
for phile in philes:
if phile.endswith(".png") or phile.endswith("jpg"):
list_data.append(os.path.join(crntDir, phile))
_twoDLst = convertToTwoDList(list_data, col)
lm = MyListModel(_twoDLst, col, self)
self.setModel(lm)
self.show()
def convertToTwoDList(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
if __name__ == '__main__':
window = MyTableView()
window.show()
window.raise_()
sys.exit(app.exec_())
import sys
import os
from PyQt4 import QtGui, QtCore
class MyListModel(QtCore.QAbstractTableModel):
def __init__(self, datain, col ,parent=None, *args):
""" datain: a list where each item is a row
"""
QtCore.QAbstractListModel.__init__(self, parent, *args)
self._listdata = datain
self._col = col
def colData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return QtCore.QString("Images")
else:
return QtCore.QString(os.path.splitext(self._listdata[section])[-1][1:].upper())
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self._listdata)
def columnCount(self, parent):
return self._col
def data(self, index, role):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
fileName = os.path.split(self._listdata[row][column])[-1]
return fileName
if role == QtCore.Qt.ToolTipRole:
row = index.row()
column = index.column()
fileName = os.path.split(self._listdata[row][column])[-1]
return QtCore.QString(fileName)
if index.isValid() and role == QtCore.Qt.DecorationRole:
row = index.row()
column = index.column()
value = self._listdata[row][column]
pixmap = QtGui.QPixmap()
# pixmap.scaled(400,300, QtCore.Qt.KeepAspectRatio)
pixmap.load(value)
return QtGui.QImage(pixmap).scaled(120,100)
if index.isValid() and role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self._listdata[row][column]
fileName = os.path.split(value)[-1]
return os.path.splitext(fileName)[0]
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role=QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
newName = os.path.join(str(os.path.split(self._listdata[row][column])[0]), str(value.toString()))
self.__renameFile(self._listdata[row][column], newName)
self._listdata[row][column] = newName
self.dataChanged.emit(index, index)
return True
return False
def __renameFile(self, fileToRename, newName):
try:
os.rename(str(fileToRename), newName)
except Exception, err:
print err
class MyTableView(QtGui.QTableView):
"""docstring for MyTableView"""
def __init__(self):
super(MyTableView, self).__init__()
sw = QtGui.QDesktopWidget().screenGeometry(self).width()
sh = QtGui.QDesktopWidget().screenGeometry(self).height()
self.resize(sw, sh)
thumbWidth = 100
thumbheight = 120
col = sw/thumbWidth
self.setColumnWidth(thumbWidth, thumbheight)
crntDir = "/Users/krystosan/Pictures"
# create table
list_data = []
philes = os.listdir(crntDir)
for phile in philes:
if phile.endswith(".png") or phile.endswith("jpg"):
list_data.append(os.path.join(crntDir, phile))
_twoDLst = convertToTwoDList(list_data, col)
lm = MyListModel(_twoDLst, col, self)
self.setModel(lm)
self.show()
def convertToTwoDList(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyTableView()
window.show()
window.raise_()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks