OpenStructure
preset_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_preset_info_handler import ImmutablePresetInfoHandler
7 from .preset_info_handler import PresetInfoHandler
8 class PresetListModel(QtCore.QAbstractListModel):
9 
10  IMMUTABLE_PRESET_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
11  "presets.xml")
12  MUTABLE_PRESET_PATH = "user_presets.xml"
13 
14  dataChanged = QtCore.pyqtSignal(int, int, name="dataChanged")
15 
16  def __init__(self, parent=None, *args):
17  QtCore.QAbstractListModel.__init__(self, parent, *args)
18 
19  self.data_data_ = list()
20 
21  #Info Handler
22 
23  self.immutable_infoh_immutable_infoh_=ImmutablePresetInfoHandler(PresetListModel.IMMUTABLE_PRESET_PATH)
24  data_loc=str(QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.DataLocation))
25 
26  mutable_path=os.path.join(str(data_loc), 'config',
27  PresetListModel.MUTABLE_PRESET_PATH)
28  qdir=QtCore.QDir(data_loc)
29  if not qdir.exists():
30  qdir.mkpath("config")
31  self.infoh_infoh_=PresetInfoHandler(mutable_path)
32  self.LoadPresetsFromInfoLoadPresetsFromInfo()
33 
34  def AddItem(self, preset, row, editable, save):
35  if self.NameIsValidNameIsValid(preset.GetName()):
36  icon = self.GetIconGetIcon(preset)
37  self.insertRowinsertRow(row, QtCore.QModelIndex())
38  self.data_data_[row] = [preset, icon, editable]
39  model_index = self.createIndex(row,0)
40  index = self.index(row)
41  end_index = self.createIndex(self.rowCountrowCount(),0)
42  if save:
43  self.AddPresetToInfoAddPresetToInfo(preset)
44  self.dataChangeddataChanged.emit(model_index, end_index)
45  return True
46  return False
47 
48  def IsEditable(self, row):
49  return self.data_data_[row][2]
50 
51  def RemoveItem(self, row):
52  if self.IsEditableIsEditable(row):
53  name = self.data_data_[row][0].GetName()
54  self.removeRowremoveRow(row, QtCore.QModelIndex())
55  model_index = self.createIndex(row,0)
56  self.infoh_infoh_.RemovePreset(name)
57  self.dataChangeddataChanged.emit(model_index, model_index)
58  return True
59  return False
60 
61  def AddPresetToInfo(self,preset):
62  self.infoh_infoh_.StorePreset(preset)
63 
65  if self.immutable_infoh_immutable_infoh_:
66  presets = self.immutable_infoh_immutable_infoh_.GetPresets()
67  for k, v in presets.items():
68  self.AddItemAddItem(v, self.GetLastRowGetLastRow(), False, False)
69 
70  presets = self.infoh_infoh_.GetPresets()
71  for k, v in presets.items():
72  self.AddItemAddItem(v, self.GetLastRowGetLastRow(), True, False)
73 
74  def GetPreset(self, model_index):
75  if model_index.isValid():
76  return self.data_data_[model_index.row()][0]
77 
78  def GetLastRow(self):
79  return self.rowCountrowCount()
80 
81  #Helper
82  def GetIcon(self, preset):
83  pixmap = QtGui.QPixmap(64, 64)
84  pixmap.fill(QtCore.Qt.transparent)
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_data_:
91  if string == values[0].GetName():
92  return False
93  return True
94 
95  #Overwritten Methods
96  def rowCount(self, parent=QtCore.QModelIndex()):
97  return len(self.data_data_)
98 
99  def data(self, index, role):
100  if index.isValid() and index.row()< self.rowCountrowCount():
101  data = self.data_data_[index.row()]
102  if role == QtCore.Qt.DisplayRole:
103  return QtCore.QVariant(data[0].GetName())
104  elif role == QtCore.Qt.DecorationRole:
105  return QtCore.QVariant(data[1])
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_data_[row]:
112  self.data_data_[row] = list()
113  if role == QtCore.Qt.EditRole and self.NameIsValidNameIsValid(value.toString()):
114  old_name = str(self.data_data_[row][0].GetName())
115  new_name = value.toString()
116  self.data_data_[row][0].SetName(str(new_name))
117  self.infoh_infoh_.RenamePreset(old_name,str(new_name))
118  self.dataChangeddataChanged.emit(index, index)
119  return True
120  elif role == QtCore.Qt.DisplayRole:
121  self.data_data_[row][0].SetName(value.toString())
122  elif role == QtCore.Qt.DecorationRole:
123  self.dat_[row][1] = 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.IsEditableIsEditable(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_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_data_[position]
144  self.endRemoveRows()
145  return True
146 
147 
148 
149 
def AddItem(self, preset, row, editable, save)
def rowCount(self, parent=QtCore.QModelIndex())
String DLLEXPORT_OST_BASE GetSharedDataPath()