5 from PyQt5
import QtCore, QtWidgets, QtGui
6 from .immutable_gradient_info_handler
import ImmutableGradientInfoHandler
7 from .gradient_info_handler
import GradientInfoHandler
13 MUTABLE_GRADIENTS_PATH =
"user_gradients.xml"
15 dataChanged = QtCore.pyqtSignal(int, int, name=
"dataChanged")
18 QtCore.QAbstractListModel.__init__(self, parent, *args)
23 self.
immutable_infoh_ = ImmutableGradientInfoHandler(GradientListModel.IMMUTABLE_GRADIENTS_PATH)
24 self.
infoh_ = GradientInfoHandler(GradientListModel.MUTABLE_GRADIENTS_PATH)
27 def AddItem(self, name, gradient, row, editable, save):
31 self.
data_[row] = [name, gradient, icon, editable]
32 model_index = self.createIndex(row,0)
33 index = self.index(row)
34 end_index = self.createIndex(self.
rowCount(),0)
37 self.dataChanged.emit(model_index, end_index)
42 return self.
data_[row][3]
46 name = self.
data_[row][0]
48 model_index = self.createIndex(row,0)
49 self.infoh_.RemoveGradient(name)
50 self.dataChanged.emit(model_index, model_index)
55 self.infoh_.StoreQGradient(gradient,str(name))
59 qgradients = self.immutable_infoh_.GetQGradients()
60 for k, v
in qgradients.items():
63 qgradients = self.infoh_.GetQGradients()
64 for k, v
in qgradients.items():
68 if model_index.isValid():
69 return self.
data_[model_index.row()][1]
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)
87 return QtGui.QIcon(pixmap)
92 for values
in self.
data_:
93 if string == values[0]:
98 def rowCount(self, parent=QtCore.QModelIndex()):
99 return len(self.
data_)
102 if index.isValid()
and index.row()< self.
rowCount():
103 data = self.
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()
113 if not self.
data_[row]:
114 self.
data_[row] = list()
115 if role == QtCore.Qt.EditRole
and self.
NameIsValid(value.toString()):
116 old_name = str(self.
data_[row][0])
117 new_name = value.toString()
118 self.
data_[row][0] = new_name
119 self.infoh_.RenameGradient(old_name,str(new_name))
120 self.dataChanged.emit(index, index)
122 elif role == QtCore.Qt.DisplayRole:
123 self.
data_[row][0] = value.toString()
124 elif role == QtCore.Qt.DecorationRole:
125 self.
data_[row][2] = value.toPyObject()
130 flags = QtCore.QAbstractItemModel.flags(self,index)
132 return flags | QtCore.Qt.ItemIsEditable
135 return QtCore.Qt.ItemIsEnabled
138 self.beginInsertRows(index, position, position)
139 self.data_.insert(position,list())
144 self.beginRemoveRows(index, position, position)
145 del self.
data_[position]
String DLLEXPORT_OST_BASE GetSharedDataPath()