00001 from ost import gui
00002 from ost import gfx
00003 import os
00004 import ost
00005 from PyQt4 import QtCore, QtGui
00006 from immutable_gradient_info_handler import ImmutableGradientInfoHandler
00007 from gradient_info_handler import GradientInfoHandler
00008
00009 class GradientListModel(QtCore.QAbstractListModel):
00010
00011 IMMUTABLE_GRADIENTS_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
00012 "gradients.xml")
00013 MUTABLE_GRADIENTS_PATH = "user_gradients.xml"
00014
00015 def __init__(self, parent=None, *args):
00016 QtCore.QAbstractListModel.__init__(self, parent, *args)
00017
00018 self.data_ = list()
00019
00020
00021 self.immutable_infoh_ = ImmutableGradientInfoHandler(GradientListModel.IMMUTABLE_GRADIENTS_PATH)
00022 self.infoh_ = GradientInfoHandler(GradientListModel.MUTABLE_GRADIENTS_PATH)
00023 self.LoadGradientFromInfo()
00024
00025 def AddItem(self, name, gradient, row, editable, save):
00026 if self.NameIsValid(name):
00027 icon = self.GetIcon(gradient)
00028 self.insertRow(row, QtCore.QModelIndex())
00029 self.data_[row] = [name, gradient, icon, editable]
00030 model_index = self.createIndex(row,0)
00031 index = self.index(row)
00032 end_index = self.createIndex(self.rowCount(),0)
00033 if save:
00034 self.AddGradientToInfo(gradient,name)
00035 self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
00036 return True
00037 return False
00038
00039 def IsEditable(self, row):
00040 return self.data_[row][3]
00041
00042 def RemoveItem(self, row):
00043 if self.IsEditable(row):
00044 name = self.data_[row][0]
00045 self.removeRow(row, QtCore.QModelIndex())
00046 model_index = self.createIndex(row,0)
00047 self.infoh_.RemoveGradient(name)
00048 self.emit(QtCore.SIGNAL("dataChanged"),model_index, model_index)
00049 return True
00050 return False
00051
00052 def AddGradientToInfo(self,gradient,name):
00053 self.infoh_.StoreQGradient(gradient,str(name))
00054
00055 def LoadGradientFromInfo(self):
00056 if self.immutable_infoh_:
00057 qgradients = self.immutable_infoh_.GetQGradients()
00058 for k, v in qgradients.iteritems():
00059 self.AddItem(k, v, self.GetLastRow(), False, False)
00060
00061 qgradients = self.infoh_.GetQGradients()
00062 for k, v in qgradients.iteritems():
00063 self.AddItem(k, v, self.GetLastRow(), True, False)
00064
00065 def GetGradient(self, model_index):
00066 if model_index.isValid():
00067 return self.data_[model_index.row()][1]
00068
00069 def GetLastRow(self):
00070 return self.rowCount()
00071
00072
00073 def GetIcon(self, gradient):
00074 pixmap = QtGui.QPixmap(64, 64)
00075 pixmap.fill(QtCore.Qt.transparent)
00076 painter = QtGui.QPainter()
00077 if painter.begin(pixmap):
00078 gradient.setStart(QtCore.QPointF(0, 0))
00079 gradient.setFinalStop(QtCore.QPointF(64, 0))
00080 brush = QtGui.QBrush(gradient)
00081 painter.setBrush(brush)
00082 painter.drawRect(0, 0, 64, 64)
00083 painter.end()
00084
00085 return QtGui.QIcon(pixmap)
00086
00087 def NameIsValid(self, string):
00088 if len(string)==0:
00089 return False
00090 for values in self.data_:
00091 if string == values[0]:
00092 return False
00093 return True
00094
00095
00096 def rowCount(self, parent=QtCore.QModelIndex()):
00097 return len(self.data_)
00098
00099 def data(self, index, role):
00100 if index.isValid() and index.row()< self.rowCount():
00101 data = self.data_[index.row()]
00102 if role == QtCore.Qt.DisplayRole:
00103 return QtCore.QVariant(data[0])
00104 elif role == QtCore.Qt.DecorationRole:
00105 return QtCore.QVariant(data[2])
00106 return QtCore.QVariant()
00107
00108 def setData(self, index, value, role):
00109 if index.isValid():
00110 row = index.row()
00111 if not self.data_[row]:
00112 self.data_[row] = list()
00113 if role == QtCore.Qt.EditRole and self.NameIsValid(value.toString()):
00114 old_name = str(self.data_[row][0])
00115 new_name = value.toString()
00116 self.data_[row][0] = new_name
00117 self.infoh_.RenameGradient(old_name,str(new_name))
00118 self.emit(QtCore.SIGNAL("dataChanged"),index, index)
00119 return True
00120 elif role == QtCore.Qt.DisplayRole:
00121 self.data_[row][0] = value.toString()
00122 elif role == QtCore.Qt.DecorationRole:
00123 self.dat_[row][2] = value.toPyObject()
00124 return False
00125
00126 def flags(self, index):
00127 if index.isValid():
00128 flags = QtCore.QAbstractItemModel.flags(self,index)
00129 if self.IsEditable(index.row()):
00130 return flags | QtCore.Qt.ItemIsEditable
00131 else:
00132 return flags
00133 return QtCore.Qt.ItemIsEnabled
00134
00135 def insertRow(self, position, index):
00136 self.beginInsertRows(index, position, position)
00137 self.data_.insert(position,list())
00138 self.endInsertRows()
00139 return True
00140
00141 def removeRow(self, position, index):
00142 self.beginRemoveRows(index, position, position)
00143 del self.data_[position]
00144 self.endRemoveRows()
00145 return True
00146
00147
00148
00149