OpenStructure
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 PyQt5 import QtCore, QtWidgets, 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  dataChanged = QtCore.pyqtSignal(int, int, name="dataChanged")
16 
17  def __init__(self, parent=None, *args):
18  QtCore.QAbstractListModel.__init__(self, parent, *args)
19 
20  self.data_data_ = list()
21 
22  #Info Handler
23  self.immutable_infoh_immutable_infoh_ = ImmutableGradientInfoHandler(GradientListModel.IMMUTABLE_GRADIENTS_PATH)
24  self.infoh_infoh_ = GradientInfoHandler(GradientListModel.MUTABLE_GRADIENTS_PATH)
25  self.LoadGradientFromInfoLoadGradientFromInfo()
26 
27  def AddItem(self, name, gradient, row, editable, save):
28  if self.NameIsValidNameIsValid(name):
29  icon = self.GetIconGetIcon(gradient)
30  self.insertRowinsertRow(row, QtCore.QModelIndex())
31  self.data_data_[row] = [name, gradient, icon, editable]
32  model_index = self.createIndex(row,0)
33  index = self.index(row)
34  end_index = self.createIndex(self.rowCountrowCount(),0)
35  if save:
36  self.AddGradientToInfoAddGradientToInfo(gradient,name)
37  self.dataChangeddataChanged.emit(model_index, end_index)
38  return True
39  return False
40 
41  def IsEditable(self, row):
42  return self.data_data_[row][3]
43 
44  def RemoveItem(self, row):
45  if self.IsEditableIsEditable(row):
46  name = self.data_data_[row][0]
47  self.removeRowremoveRow(row, QtCore.QModelIndex())
48  model_index = self.createIndex(row,0)
49  self.infoh_infoh_.RemoveGradient(name)
50  self.dataChangeddataChanged.emit(model_index, model_index)
51  return True
52  return False
53 
54  def AddGradientToInfo(self,gradient,name):
55  self.infoh_infoh_.StoreQGradient(gradient,str(name))
56 
58  if self.immutable_infoh_immutable_infoh_:
59  qgradients = self.immutable_infoh_immutable_infoh_.GetQGradients()
60  for k, v in qgradients.items():
61  self.AddItemAddItem(k, v, self.GetLastRowGetLastRow(), False, False)
62 
63  qgradients = self.infoh_infoh_.GetQGradients()
64  for k, v in qgradients.items():
65  self.AddItemAddItem(k, v, self.GetLastRowGetLastRow(), True, False)
66 
67  def GetGradient(self, model_index):
68  if model_index.isValid():
69  return self.data_data_[model_index.row()][1]
70 
71  def GetLastRow(self):
72  return self.rowCountrowCount()
73 
74  #Helper
75  def GetIcon(self, gradient):
76  pixmap = QtGui.QPixmap(64, 64)
77  pixmap.fill(QtCore.Qt.transparent)
78  painter = QtGui.QPainter()
79  if painter.begin(pixmap):
80  gradient.setStart(QtCore.QPointF(0, 0))
81  gradient.setFinalStop(QtCore.QPointF(64, 0))
82  brush = QtGui.QBrush(gradient)
83  painter.setBrush(brush)
84  painter.drawRect(0, 0, 64, 64)
85  painter.end()
86 
87  return QtGui.QIcon(pixmap)
88 
89  def NameIsValid(self, string):
90  if len(string)==0:
91  return False
92  for values in self.data_data_:
93  if string == values[0]:
94  return False
95  return True
96 
97  #Overwritten Methods
98  def rowCount(self, parent=QtCore.QModelIndex()):
99  return len(self.data_data_)
100 
101  def data(self, index, role):
102  if index.isValid() and index.row()< self.rowCountrowCount():
103  data = self.data_data_[index.row()]
104  if role == QtCore.Qt.DisplayRole:
105  return QtCore.QVariant(data[0])
106  elif role == QtCore.Qt.DecorationRole:
107  return QtCore.QVariant(data[2])
108  return QtCore.QVariant()
109 
110  def setData(self, index, value, role):
111  if index.isValid():
112  row = index.row()
113  if not self.data_data_[row]:
114  self.data_data_[row] = list()
115  if role == QtCore.Qt.EditRole and self.NameIsValidNameIsValid(value.toString()):
116  old_name = str(self.data_data_[row][0])
117  new_name = value.toString()
118  self.data_data_[row][0] = new_name
119  self.infoh_infoh_.RenameGradient(old_name,str(new_name))
120  self.dataChangeddataChanged.emit(index, index)
121  return True
122  elif role == QtCore.Qt.DisplayRole:
123  self.data_data_[row][0] = value.toString()
124  elif role == QtCore.Qt.DecorationRole:
125  self.data_data_[row][2] = value.toPyObject()
126  return False
127 
128  def flags(self, index):
129  if index.isValid():
130  flags = QtCore.QAbstractItemModel.flags(self,index)
131  if self.IsEditableIsEditable(index.row()):
132  return flags | QtCore.Qt.ItemIsEditable
133  else:
134  return flags
135  return QtCore.Qt.ItemIsEnabled
136 
137  def insertRow(self, position, index):
138  self.beginInsertRows(index, position, position)
139  self.data_data_.insert(position,list())
140  self.endInsertRows()
141  return True
142 
143  def removeRow(self, position, index):
144  self.beginRemoveRows(index, position, position)
145  del self.data_data_[position]
146  self.endRemoveRows()
147  return True
148 
149 
150 
151 
def AddItem(self, name, gradient, row, editable, save)
def rowCount(self, parent=QtCore.QModelIndex())
String DLLEXPORT_OST_BASE GetSharedDataPath()