OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
loader_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_loader_info_handler import ImmutableLoaderInfoHandler
7 from loader_info_handler import LoaderInfoHandler
8 from ost.gui import LoaderManager
9 
10 class LoaderListModel(QtCore.QAbstractListModel):
11 
12  IMMUTABLE_LOADERS_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
13  "loaders.xml")
14  MUTABLE_LOADERS_PATH = "user_loaders.xml"
15 
16  def __init__(self, parent=None, *args):
17  QtCore.QAbstractListModel.__init__(self, parent, *args)
18 
19  self.data_ = list()
20 
21  #Info Handler
22  self.immutable_infoh_ = ImmutableLoaderInfoHandler(LoaderListModel.IMMUTABLE_LOADERS_PATH)
23  self.infoh_ = LoaderInfoHandler(LoaderListModel.MUTABLE_LOADERS_PATH)
24  self.LoadLoaderFromInfo()
25 
26  self.loader_manager_ = gui.FileLoader.GetLoaderManager()
27 
28  def AddItem(self, name, loader, row, editable, save):
29  if self.NameIsValid(name):
30  self.insertRow(row, QtCore.QModelIndex())
31  self.data_[row] = [name, loader, editable]
32  model_index = self.createIndex(row,0)
33  index = self.index(row)
34  end_index = self.createIndex(self.rowCount(),0)
35  if save:
36  self.AddLoaderToInfo(name, loader)
37  self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
38  gui.FileLoader.GetLoaderManager().AddRemoteSiteLoader(name, loader)
39  if(loader.IsDefault()):
40  gui.FileLoader.GetLoaderManager().SetDefaultRemoteSiteIdent(name)
41  return True
42  return False
43 
44  def IsEditable(self, row):
45  return self.data_[row][2]
46 
47  def RemoveItem(self, row):
48  if self.IsEditable(row):
49  name = self.data_[row][0]
50  self.removeRow(row, QtCore.QModelIndex())
51  model_index = self.createIndex(row,0)
52  self.infoh_.RemoveLoader(name)
53  self.emit(QtCore.SIGNAL("dataChanged"),model_index, model_index)
54  self.loader_manager_.RemoveRemoteSiteLoader(name)
55  return True
56  return False
57 
58  def AddLoaderToInfo(self, name, loader):
59  self.infoh_.StoreLoader(name, loader)
60 
61  def RemoveLoaderFromInfo(self, name):
62  self.infoh_.RemoveLoader(name)
63 
64  def SetItem(self, model_index, loader):
65  row = model_index.row()
66  name = self.data_[row][0]
67  self.data_[row][1] = loader
68  model_index = self.createIndex(row,0)
69  index = self.index(row)
70  end_index = self.createIndex(self.rowCount(),0)
71  self.RemoveLoaderFromInfo(self.data_[row][0])
72  self.AddLoaderToInfo(self.data_[row][0], loader)
73  self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
74  self.loader_manager_.RemoveRemoteSiteLoader(name)
75  self.loader_manager_.AddRemoteSiteLoader(name, loader)
76  return True
77 
78  def LoadLoaderFromInfo(self):
79  if self.immutable_infoh_:
80  loaders = self.immutable_infoh_.GetLoaders()
81  for k, v in loaders.iteritems():
82  self.AddItem(k, v, self.GetLastRow(), False, False)
83 
84  loaders = self.infoh_.GetLoaders()
85  for k, v in loaders.iteritems():
86  self.AddItem(k, v, self.GetLastRow(), True, False)
87 
88  def GetLoader(self, model_index):
89  if model_index.isValid():
90  return self.data_[model_index.row()][1]
91 
92  def GetLastRow(self):
93  return self.rowCount()
94 
95  #Helper
96  def NameIsValid(self, string):
97  if len(string)==0:
98  return False
99  for values in self.data_:
100  if string == values[0]:
101  return False
102  return True
103 
104  #Overwritten Methods
105  def rowCount(self, parent=QtCore.QModelIndex()):
106  return len(self.data_)
107 
108  def data(self, index, role):
109  if index.isValid() and index.row()< self.rowCount():
110  data = self.data_[index.row()]
111  if role == QtCore.Qt.DisplayRole:
112  return QtCore.QVariant(data[0])
113  return QtCore.QVariant()
114 
115  def setData(self, index, value, role):
116  if index.isValid():
117  row = index.row()
118  if not self.data_[row]:
119  self.data_[row] = list()
120  if role == QtCore.Qt.EditRole and self.NameIsValid(value.toString()):
121  old_name = str(self.data_[row][0])
122  new_name = value.toString()
123  self.data_[row][0] = new_name
124  self.infoh_.RenameLoader(old_name,str(new_name))
125  self.emit(QtCore.SIGNAL("dataChanged"),index, index)
126  self.loader_manager_.RemoveRemoteSiteLoader(old_name)
127  self.loader_manager_.AddRemoteSiteLoader(str(new_name), self.data_[row][1])
128  return True
129  elif role == QtCore.Qt.DisplayRole:
130  self.data_[row][0] = value.toString()
131  return False
132 
133  def flags(self, index):
134  if index.isValid():
135  flags = QtCore.QAbstractItemModel.flags(self,index)
136  if self.IsEditable(index.row()):
137  return flags | QtCore.Qt.ItemIsEditable
138  else:
139  return flags
140  return QtCore.Qt.ItemIsEnabled
141 
142  def insertRow(self, position, index):
143  self.beginInsertRows(index, position, position)
144  self.data_.insert(position,list())
145  self.endInsertRows()
146  return True
147 
148  def removeRow(self, position, index):
149  self.beginRemoveRows(index, position, position)
150  del self.data_[position]
151  self.endRemoveRows()
152  return True
153 
154 
155 
156