OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
gradient_list_model.py
Go to the documentation of this file.
1 from ost import gui
2 from ost import gfx
3 import os
4 import ost
5 from PyQt4 import QtCore, QtGui
6 from immutable_gradient_info_handler import ImmutableGradientInfoHandler
7 from gradient_info_handler import GradientInfoHandler
8 
9 class GradientListModel(QtCore.QAbstractListModel):
10 
11  IMMUTABLE_GRADIENTS_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
12  "gradients.xml")
13  MUTABLE_GRADIENTS_PATH = "user_gradients.xml"
14 
15  def __init__(self, parent=None, *args):
16  QtCore.QAbstractListModel.__init__(self, parent, *args)
17 
18  self.data_ = list()
19 
20  #Info Handler
21  self.immutable_infoh_ = ImmutableGradientInfoHandler(GradientListModel.IMMUTABLE_GRADIENTS_PATH)
22  self.infoh_ = GradientInfoHandler(GradientListModel.MUTABLE_GRADIENTS_PATH)
24 
25  def AddItem(self, name, gradient, row, editable, save):
26  if self.NameIsValid(name):
27  icon = self.GetIcon(gradient)
28  self.insertRow(row, QtCore.QModelIndex())
29  self.data_[row] = [name, gradient, icon, editable]
30  model_index = self.createIndex(row,0)
31  index = self.index(row)
32  end_index = self.createIndex(self.rowCount(),0)
33  if save:
34  self.AddGradientToInfo(gradient,name)
35  self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
36  return True
37  return False
38 
39  def IsEditable(self, row):
40  return self.data_[row][3]
41 
42  def RemoveItem(self, row):
43  if self.IsEditable(row):
44  name = self.data_[row][0]
45  self.removeRow(row, QtCore.QModelIndex())
46  model_index = self.createIndex(row,0)
47  self.infoh_.RemoveGradient(name)
48  self.emit(QtCore.SIGNAL("dataChanged"),model_index, model_index)
49  return True
50  return False
51 
52  def AddGradientToInfo(self,gradient,name):
53  self.infoh_.StoreQGradient(gradient,str(name))
54 
56  if self.immutable_infoh_:
57  qgradients = self.immutable_infoh_.GetQGradients()
58  for k, v in qgradients.iteritems():
59  self.AddItem(k, v, self.GetLastRow(), False, False)
60 
61  qgradients = self.infoh_.GetQGradients()
62  for k, v in qgradients.iteritems():
63  self.AddItem(k, v, self.GetLastRow(), True, False)
64 
65  def GetGradient(self, model_index):
66  if model_index.isValid():
67  return self.data_[model_index.row()][1]
68 
69  def GetLastRow(self):
70  return self.rowCount()
71 
72  #Helper
73  def GetIcon(self, gradient):
74  pixmap = QtGui.QPixmap(64, 64)
75  pixmap.fill(QtCore.Qt.transparent)
76  painter = QtGui.QPainter()
77  if painter.begin(pixmap):
78  gradient.setStart(QtCore.QPointF(0, 0))
79  gradient.setFinalStop(QtCore.QPointF(64, 0))
80  brush = QtGui.QBrush(gradient)
81  painter.setBrush(brush)
82  painter.drawRect(0, 0, 64, 64)
83  painter.end()
84 
85  return QtGui.QIcon(pixmap)
86 
87  def NameIsValid(self, string):
88  if len(string)==0:
89  return False
90  for values in self.data_:
91  if string == values[0]:
92  return False
93  return True
94 
95  #Overwritten Methods
96  def rowCount(self, parent=QtCore.QModelIndex()):
97  return len(self.data_)
98 
99  def data(self, index, role):
100  if index.isValid() and index.row()< self.rowCount():
101  data = self.data_[index.row()]
102  if role == QtCore.Qt.DisplayRole:
103  return QtCore.QVariant(data[0])
104  elif role == QtCore.Qt.DecorationRole:
105  return QtCore.QVariant(data[2])
106  return QtCore.QVariant()
107 
108  def setData(self, index, value, role):
109  if index.isValid():
110  row = index.row()
111  if not self.data_[row]:
112  self.data_[row] = list()
113  if role == QtCore.Qt.EditRole and self.NameIsValid(value.toString()):
114  old_name = str(self.data_[row][0])
115  new_name = value.toString()
116  self.data_[row][0] = new_name
117  self.infoh_.RenameGradient(old_name,str(new_name))
118  self.emit(QtCore.SIGNAL("dataChanged"),index, index)
119  return True
120  elif role == QtCore.Qt.DisplayRole:
121  self.data_[row][0] = value.toString()
122  elif role == QtCore.Qt.DecorationRole:
123  self.dat_[row][2] = value.toPyObject()
124  return False
125 
126  def flags(self, index):
127  if index.isValid():
128  flags = QtCore.QAbstractItemModel.flags(self,index)
129  if self.IsEditable(index.row()):
130  return flags | QtCore.Qt.ItemIsEditable
131  else:
132  return flags
133  return QtCore.Qt.ItemIsEnabled
134 
135  def insertRow(self, position, index):
136  self.beginInsertRows(index, position, position)
137  self.data_.insert(position,list())
138  self.endInsertRows()
139  return True
140 
141  def removeRow(self, position, index):
142  self.beginRemoveRows(index, position, position)
143  del self.data_[position]
144  self.endRemoveRows()
145  return True
146 
147 
148 
149 
String DLLEXPORT_OST_BASE GetSharedDataPath()