Better persosProxyModel

This commit is contained in:
Olivier Keshavjee 2015-06-15 17:24:02 +02:00
parent ed95481ead
commit 9452b32f50
4 changed files with 56 additions and 66 deletions

View file

@ -2,6 +2,7 @@
#--!-- coding: utf8 --!-- #--!-- coding: utf8 --!--
from qt import * from qt import *
from random import *
def wordCount(text): def wordCount(text):
return len(text.strip().replace(" ", "\n").split("\n")) if text else 0 return len(text.strip().replace(" ", "\n").split("\n")) if text else 0
@ -67,4 +68,17 @@ def iconFromColor(color):
return QIcon(px) return QIcon(px)
def iconFromColorString(string): def iconFromColorString(string):
return iconFromColor(QColor(string)) return iconFromColor(QColor(string))
def randomColor(mix=None):
"Generates a random color. If mix (QColor) is given, mixes the random color and mix."
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
if mix:
r = (r + mix.red()) / 2
g = (g + mix.green()) / 2
b = (b + mix.blue()) / 2
return QColor(r, g, b)

View file

@ -95,7 +95,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
# Persos # Persos
self.mdlPersos = QStandardItemModel(0, 10) self.mdlPersos = QStandardItemModel(0, 10)
self.mdlPersosProxy = None #persosProxyModel() self.mdlPersosProxy = persosProxyModel() # None
#self.mdlPersoProxyFilter = QSortFilterProxyModel() #self.mdlPersoProxyFilter = QSortFilterProxyModel()
if self.mdlPersosProxy: if self.mdlPersosProxy:
self.mdlPersosProxy.setSourceModel(self.mdlPersos) self.mdlPersosProxy.setSourceModel(self.mdlPersos)
@ -113,7 +113,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.btnAddPerso.clicked.connect(self.createPerso) self.btnAddPerso.clicked.connect(self.createPerso)
self.btnRmPerso.clicked.connect(self.removePerso) self.btnRmPerso.clicked.connect(self.removePerso)
self.btnPersoColor.clicked.connect(self.setPersoColor) self.btnPersoColor.clicked.connect(self.chosePersoColor)
self.btnPersoAddInfo.clicked.connect(lambda: self.mdlPersosInfos.insertRow(self.mdlPersosInfos.rowCount())) self.btnPersoAddInfo.clicked.connect(lambda: self.mdlPersosInfos.insertRow(self.mdlPersosInfos.rowCount()))
self.mprPersos = QDataWidgetMapper() self.mprPersos = QDataWidgetMapper()
self.mprPersos.setModel(self.mdlPersos) self.mprPersos.setModel(self.mdlPersos)
@ -265,6 +265,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.mdlPersos.appendRow(p) self.mdlPersos.appendRow(p)
pid = self.getPersosID() pid = self.getPersosID()
self.checkPersosID() # Attributes a persoID (which is logically pid) self.checkPersosID() # Attributes a persoID (which is logically pid)
self.setPersoColor(self.mdlPersos.rowCount()-1, randomColor(QColor(Qt.white)))
# Add column in persos infos # Add column in persos infos
self.mdlPersosInfos.insertColumn(self.mdlPersosInfos.columnCount(), [QStandardItem(pid)]) self.mdlPersosInfos.insertColumn(self.mdlPersosInfos.columnCount(), [QStandardItem(pid)])
@ -319,7 +320,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.resizePersosInfos() self.resizePersosInfos()
def updatePersoColor(self): def updatePersoColor(self):
if self.mdlPersosProxy: if self.mdlPersosProxy:
idx = self.mdlPersosProxy.mapToSource(self.lstPersos.currentIndex()) idx = self.mdlPersosProxy.mapToSource(self.lstPersos.currentIndex())
else: else:
@ -343,7 +344,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.tblPersoInfos.setColumnWidth(current, w - w2) self.tblPersoInfos.setColumnWidth(current, w - w2)
def setPersoColor(self): def chosePersoColor(self):
if self.mdlPersosProxy: if self.mdlPersosProxy:
idx = self.mdlPersosProxy.mapToSource(self.lstPersos.currentIndex()) idx = self.mdlPersosProxy.mapToSource(self.lstPersos.currentIndex())
else: else:
@ -356,10 +357,13 @@ class MainWindow(QMainWindow, Ui_MainWindow):
color = Qt.white color = Qt.white
self.colorDialog = QColorDialog(color, self) self.colorDialog = QColorDialog(color, self)
color = self.colorDialog.getColor(color) color = self.colorDialog.getColor(color)
self.setPersoColor(idx.row(), color)
self.updatePersoColor()
def setPersoColor(self, row, color):
px = QPixmap(32, 32) px = QPixmap(32, 32)
px.fill(color) px.fill(color)
self.mdlPersos.item(idx.row(), Perso.name.value).setIcon(QIcon(px)) self.mdlPersos.item(row, Perso.name.value).setIcon(QIcon(px))
self.updatePersoColor()
#################################################################################################### ####################################################################################################

View file

@ -19,11 +19,11 @@ class persosProxyModel(QAbstractProxyModel):
QAbstractProxyModel.__init__(self, parent) QAbstractProxyModel.__init__(self, parent)
self.rootItem = QStandardItem() self.rootItem = QStandardItem()
self.p1 = QStandardItem("Principaux") self.p1 = QStandardItem(self.tr("Main"))
self.p2 = QStandardItem("Secondaires") self.p2 = QStandardItem(self.tr("Secundary"))
self.p3 = QStandardItem("Mineurs") self.p3 = QStandardItem(self.tr("Minors"))
self.cats = [ self._cats = [
self.p1, self.p1,
self.p2, self.p2,
self.p3 self.p3
@ -34,16 +34,9 @@ class persosProxyModel(QAbstractProxyModel):
if not sourceIndex.isValid(): if not sourceIndex.isValid():
return QModelIndex() return QModelIndex()
row = sourceIndex.row() row = self._map.index(sourceIndex.row())
item = sourceIndex.internalPointer() item = sourceIndex.internalPointer()
if row in self._map[0]:
row = self._map[0].index(row) + 1
elif row in self._map[1]:
row = len(self._map[0]) + 1 + self._map[1].index(row) + 1
elif row in self._map[2]:
row = len(self._map[0]) + 1 + len(self._map[1]) + 1 + self._map[2].index(row) + 1
return self.createIndex(row, sourceIndex.column(), item) return self.createIndex(row, sourceIndex.column(), item)
def flags(self, index): def flags(self, index):
@ -56,23 +49,13 @@ class persosProxyModel(QAbstractProxyModel):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def mapToSource(self, proxyIndex): def mapToSource(self, proxyIndex):
if not proxyIndex.isValid(): #if not proxyIndex.isValid():
return QModelIndex() #return QModelIndex()
row = proxyIndex.row() row = self._map[proxyIndex.row()]
if row == 0: if type(row) != int:
return QModelIndex() return QModelIndex()
elif row < len(self._map[0]) + 1:
row = self._map[0][row-1]
elif row == len(self._map[0]) + 1:
return QModelIndex()
elif row < len(self._map[0]) + 1 + len(self._map[1]) + 1:
row = self._map[1][row - 2 - len(self._map[0])]
elif row == len(self._map[0]) + 1 + len(self._map[1]) + 1:
return QModelIndex()
else:
row = self._map[2][row - 3 - len(self._map[0]) - len(self._map[1])]
item = proxyIndex.internalPointer() item = proxyIndex.internalPointer()
@ -92,33 +75,31 @@ class persosProxyModel(QAbstractProxyModel):
self.beginResetModel() self.beginResetModel()
src = self.sourceModel() src = self.sourceModel()
self._map = [[], [], []] self._map = []
for i in range(src.rowCount()): for i in range(len(self._cats)):
item = src.item(i, Perso.importance.value) self._map.append(self._cats[i])
ID = src.item(i, Perso.ID.value)
if item: for p in range(src.rowCount()):
imp = int(item.text()) item = src.item(p, Perso.importance.value)
else:
imp = 0 if item:
imp = int(item.text())
self._map[2-imp].append(i) else:
imp = 0
if 2-imp == i:
self._map.append(p)
self.endResetModel() self.endResetModel()
def numberOfPersosByImportance(self):
return [len(i) for i in self._map]
def data(self, index, role=Qt.DisplayRole): def data(self, index, role=Qt.DisplayRole):
row = index.row() row = index.row()
if index.isValid() and not self.mapToSource(index).isValid(): if index.isValid() and not self.mapToSource(index).isValid():
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
n = self.numberOfPersosByImportance() return self._map[row].text()
p = 0 if row == 0 else \
1 if row == n[0] + 1 else \
2
return self.cats[p].text()
elif role == Qt.ForegroundRole: elif role == Qt.ForegroundRole:
return QBrush(Qt.darkBlue) return QBrush(Qt.darkBlue)
@ -137,30 +118,21 @@ class persosProxyModel(QAbstractProxyModel):
def index(self, row, column, parent): def index(self, row, column, parent):
n = self.numberOfPersosByImportance() i = self._map[row]
if row == 0 or row == n[0] + 1 or row == n[0]+n[1]+2: if type(i) != int:
p = 0 if row == 0 else \
1 if row == n[0] + 1 else \
2
return self.createIndex(row, column, self.cats[p]) return self.createIndex(row, column, i)
else: else:
if row < len(self._map[0]) + 1:
nrow = self._map[0][row - 1]
elif row < len(self._map[0]) + 1 + len(self._map[1]) + 1:
nrow = self._map[1][row - 2 - len(self._map[0])]
else:
nrow = self._map[2][row - 3 - len(self._map[0]) - len(self._map[1])]
return self.mapFromSource(self.sourceModel().index(nrow, column, QModelIndex())) return self.mapFromSource(self.sourceModel().index(i, column, QModelIndex()))
def parent(self, index=QModelIndex()): def parent(self, index=QModelIndex()):
return QModelIndex() return QModelIndex()
def rowCount(self, parent=QModelIndex()): def rowCount(self, parent=QModelIndex()):
return self.sourceModel().rowCount(QModelIndex())+3 return len(self._map)
def columnCount(self, parent=QModelIndex()): def columnCount(self, parent=QModelIndex()):
return self.sourceModel().columnCount(QModelIndex()) return self.sourceModel().columnCount(QModelIndex())

View file

@ -54,9 +54,9 @@ Pour voir.</col>
<col col="11"/> <col col="11"/>
</row> </row>
<row row="2"> <row row="2">
<col col="0" color="#ff0d05f0">Nouveau perso avec un nom très très long pour voir comment ça fait</col> <col col="0" color="#ff005500">Nouveau perso avec un nom très très long pour voir comment ça fait</col>
<col col="1">2</col> <col col="1">2</col>
<col col="2">1</col> <col col="2">0</col>
<col col="3"/> <col col="3"/>
<col col="4"/> <col col="4"/>
<col col="5"/> <col col="5"/>