Skip to content

Instantly share code, notes, and snippets.

@sanfx
Created April 4, 2013 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanfx/5307788 to your computer and use it in GitHub Desktop.
Save sanfx/5307788 to your computer and use it in GitHub Desktop.
from PyQt4 import QtCore,QtGui
from tableModel import TableModel
#import tableModel.TableModel as PaletteTableModel
import sys
class PaletteListModel(QtCore.QAbstractListModel):
def __init__(self, colors = [], parent = None):
super(PaletteListModel,self).__init__(parent)
self.__colors = colors
def headerData(self, section, orientation, role):
#the method that allows us to display something on the header of the view
# orientation tells if it is the row part or at the column part
# section shows which index on that part
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return QtCore.QString("Pallete")
else:
return QtCore.QString("Color %1").arg(section)
def rowCount(self, parent):
# the method that allows the views to how many items it needs to display
return len(self.__colors)
def flags(self, index):
#the method which allows the view to know how it should treat each other i.e whether it shoudl be editable or selectable
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role = QtCore.Qt.EditRole):
# the method that views use to submit data thats when we type in or edit a column cell/item
if role == QtCore.Qt.EditRole:
row = index.row()
color = QtGui.QColor(value)
if color.isValid():
self.__colors[row] = color
self.dataChanged.emit(index, index)
return True
return False
#=================================================#
#INSERTING & REMOVING
#=================================================#
def insertRows(self, position, rows, parent = QtCore.QModelIndex()):
self.beginInsertRows(parent, position, position + rows - 1)
#Do inserting here
for i in range(rows):
self.__colors.insert(position,QtGui.QColor("#000000"))
self.endInsertRows()
return True
def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
self.beginRemoveRows(parent, position, position + rows - 1)
#Do removing here
for i in range(rows):
value = self.__colors[position]
self.__colors.remove(value)
self.endRemoveRows()
return True
def data(self,index,role):
# the method that views use when it wants to display something
# role is used to return different types of data
# for example below we will use DisplayRole to return strings to display
# and decorationRole that we can use to display icons
# and toolTipRole to display tool tip text
row = index.row()
if role == QtCore.Qt.ToolTipRole:
return "Hex Code: " + self.__colors[index.row()].name()
if role == QtCore.Qt.DecorationRole:
value = self.__colors[row]
pixmap = QtGui.QPixmap(26,26)
pixmap.fill(value)
icon = QtGui.QIcon(pixmap)
return icon
if role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole):
value = self.__colors[row]
return value.name()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
red = QtGui.QColor(255,0,0)
green = QtGui.QColor(0,255,0)
blue = QtGui.QColor(0,0,255)
rowCount = 4
columnCount = 6
headers = ['Pallete0', 'Colors', 'Brushes', 'Omg', 'Technical', 'Artist']
# return this list ================= for each row ====================
tableData0 = [[QtGui.QColor("#FFFF00") for i in range(columnCount)] for j in range(rowCount)]
model =TableModel(tableData0,headers)
#model = PaletteListModel([red,green,blue])
#treeView = QtGui.QTreeView()
#treeView.show()
#listView = QtGui.QListView()
#listView.show()
tableView = QtGui.QTableView()
tableView.show()
#listView.setModel(model)
tableView.setModel(model)
#treeView.setModel(model)
#model.insertRows(0,5)
model.insertColumns(0,5)
model.removeColumns(0,1)
#model.removeRows(0,5)
sys.exit(app.exec_())
from PyQt4 import QtCore,QtGui
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, colors = [[]], headers = [], parent = None):
super(TableModel,self).__init__(parent)
self.__colors = colors
self.__headers = headers
def rowCount(self, parent):
# the method that allows the views to how many items it needs to display
return len(self.__colors)
def columnCount(self, parent):
# 0th element is the internal list
return len(self.__colors[0])
def setData(self, index, value, role = QtCore.Qt.EditRole):
# the method that views use to submit data thats when we type in or edit a column cell/item
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
color = QtGui.QColor(value)
if color.isValid():
self.__colors[row][column] = color
self.dataChanged.emit(index, index)
return True
return False
def flags(self, index):
#the method which allows the view to know how it should treat each other i.e whether it shoudl be editable or selectable
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def headerData(self, section, orientation, role):
#the method that allows us to display something on the header of the view
# orientation tells if it is the row part or at the column part
# section shows which index on that part
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
if section < len(self.__headers):
return self.__headers[section]
else:
return "TEMP"
else:
return QtCore.QString("Color %1").arg(section)
def insertRows(self, position, rows, parent = QtCore.QModelIndex()):
self.beginInsertRows(parent, position, position + rows - 1)
#Do inserting here
for i in range(rows):
defaultValues = [QtGui.QColor("#000000") for i in range(self.columnCount(None))]
self.__colors.insert(position,defaultValues)
self.endInsertRows()
return True
def insertColumns(self, position, columns, parent = QtCore.QModelIndex()):
self.beginInsertColumns(parent, position, position + columns - 1)
rowCount = len(self.__colors)
for i in range(columns):
for j in range(rowCount):# inner loop for every row
self.__colors[j].insert(position,QtGui.QColor("#000000"))
self.endInsertColumns()
return True
def removeColumns(self, position, columns, parent = QtCore.QModelIndex()):
self.beginRemoveColumns(parent, position, position + columns - 1)
#Do removing here
rowCount = len(self.__colors)
for i in range(position, columns):
for j in range(rowCount):
print value
value = self.__colors[i][j]
self.__colors.remove(value)
self.endRemoveColumns()
return True
def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
self.beginRemoveRows(parent, position, position + rows - 1)
#Do removing here
for i in range(rows):
value = self.__colors[position]
self.__colors.remove(value)
self.endRemoveRows()
return True
def data(self, index, role):
# the method that views use when it wants to display something
# role is used to return different types of data
# for example below we will use DisplayRole to return strings to display
# and decorationRole that we can use to display icons
# and toolTipRole to display tool tip text
row = index.row()
column = index.column()
if role == QtCore.Qt.ToolTipRole:
return "Hex Code: " + self.__colors[row][column].name()
if role == QtCore.Qt.DecorationRole:
value = self.__colors[row][column]
pixmap = QtGui.QPixmap(26,26)
pixmap.fill(value)
icon = QtGui.QIcon(pixmap)
return icon
if role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole):
value = self.__colors[row][column]
return value.name()
tbValues = [[1, 2, 3],
[5, 2, 7],
[5, 2, 7, 8],
[5, 2, 7],
]
#tbValues = getProperValues(tbValues)
"""
tbValues = [[1, 2, 3, None],
[5, 2, 7, None],
[5, 2, 7, 8],
[5, 2, 7, None],
]
"""
#tableModel = TableModel(tbValues)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment