OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 PyQt4 import QtCore, QtGui
6 from immutable_preset_info_handler import ImmutablePresetInfoHandler
7 from preset_info_handler import PresetInfoHandler
8 from PyQt4.QtGui import QDesktopServices
9 class PresetListModel(QtCore.QAbstractListModel):
10 
11  IMMUTABLE_PRESET_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
12  "presets.xml")
13  MUTABLE_PRESET_PATH = "user_presets.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 
22  self.immutable_infoh_=ImmutablePresetInfoHandler(PresetListModel.IMMUTABLE_PRESET_PATH)
23  data_loc=QDesktopServices.storageLocation(QDesktopServices.DataLocation)
24  mutable_path=os.path.join(str(data_loc), 'config',
25  PresetListModel.MUTABLE_PRESET_PATH)
26  qdir=QtCore.QDir(data_loc)
27  if not qdir.exists():
28  qdir.mkpath("config")
29  self.infoh_=PresetInfoHandler(mutable_path)
30  self.LoadPresetsFromInfo()
31 
32  def AddItem(self, preset, row, editable, save):
33  if self.NameIsValid(preset.GetName()):
34  icon = self.GetIcon(preset)
35  self.insertRow(row, QtCore.QModelIndex())
36  self.data_[row] = [preset, icon, editable]
37  model_index = self.createIndex(row,0)
38  index = self.index(row)
39  end_index = self.createIndex(self.rowCount(),0)
40  if save:
41  self.AddPresetToInfo(preset)
42  self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
43  return True
44  return False
45 
46  def IsEditable(self, row):
47  return self.data_[row][2]
48 
49  def RemoveItem(self, row):
50  if self.IsEditable(row):
51  name = self.data_[row][0].GetName()
52  self.removeRow(row, QtCore.QModelIndex())
53  model_index = self.createIndex(row,0)
54  self.infoh_.RemovePreset(name)
55  self.emit(QtCore.SIGNAL("dataChanged"),model_index, model_index)
56  return True
57  return False
58 
59  def AddPresetToInfo(self,preset):
60  self.infoh_.StorePreset(preset)
61 
63  if self.immutable_infoh_:
64  presets = self.immutable_infoh_.GetPresets()
65  for k, v in presets.iteritems():
66  self.AddItem(v, self.GetLastRow(), False, False)
67 
68  presets = self.infoh_.GetPresets()
69  for k, v in presets.iteritems():
70  self.AddItem(v, self.GetLastRow(), True, False)
71 
72  def GetPreset(self, model_index):
73  if model_index.isValid():
74  return self.data_[model_index.row()][0]
75 
76  def GetLastRow(self):
77  return self.rowCount()
78 
79  #Helper
80  def GetIcon(self, preset):
81  pixmap = QtGui.QPixmap(64, 64)
82  pixmap.fill(QtCore.Qt.transparent)
83  return QtGui.QIcon(pixmap)
84 
85  def NameIsValid(self, string):
86  if len(string)==0:
87  return False
88  for values in self.data_:
89  if string == values[0].GetName():
90  return False
91  return True
92 
93  #Overwritten Methods
94  def rowCount(self, parent=QtCore.QModelIndex()):
95  return len(self.data_)
96 
97  def data(self, index, role):
98  if index.isValid() and index.row()< self.rowCount():
99  data = self.data_[index.row()]
100  if role == QtCore.Qt.DisplayRole:
101  return QtCore.QVariant(data[0].GetName())
102  elif role == QtCore.Qt.DecorationRole:
103  return QtCore.QVariant(data[1])
104  return QtCore.QVariant()
105 
106  def setData(self, index, value, role):
107  if index.isValid():
108  row = index.row()
109  if not self.data_[row]:
110  self.data_[row] = list()
111  if role == QtCore.Qt.EditRole and self.NameIsValid(value.toString()):
112  old_name = str(self.data_[row][0].GetName())
113  new_name = value.toString()
114  self.data_[row][0].SetName(str(new_name))
115  self.infoh_.RenamePreset(old_name,str(new_name))
116  self.emit(QtCore.SIGNAL("dataChanged"),index, index)
117  return True
118  elif role == QtCore.Qt.DisplayRole:
119  self.data_[row][0].SetName(value.toString())
120  elif role == QtCore.Qt.DecorationRole:
121  self.dat_[row][1] = value.toPyObject()
122  return False
123 
124  def flags(self, index):
125  if index.isValid():
126  flags = QtCore.QAbstractItemModel.flags(self,index)
127  if self.IsEditable(index.row()):
128  return flags | QtCore.Qt.ItemIsEditable
129  else:
130  return flags
131  return QtCore.Qt.ItemIsEnabled
132 
133  def insertRow(self, position, index):
134  self.beginInsertRows(index, position, position)
135  self.data_.insert(position,list())
136  self.endInsertRows()
137  return True
138 
139  def removeRow(self, position, index):
140  self.beginRemoveRows(index, position, position)
141  del self.data_[position]
142  self.endRemoveRows()
143  return True
144 
145 
146 
147 
String DLLEXPORT_OST_BASE GetSharedDataPath()