00001 from ost import gui
00002 from ost import gfx
00003 import os
00004 import ost
00005 from PyQt4 import QtCore, QtGui
00006 from immutable_preset_info_handler import ImmutablePresetInfoHandler
00007 from preset_info_handler import PresetInfoHandler
00008 from PyQt4.QtGui import QDesktopServices
00009 class PresetListModel(QtCore.QAbstractListModel):
00010
00011 IMMUTABLE_PRESET_PATH = os.path.join(ost.GetSharedDataPath(),"scene",
00012 "presets.xml")
00013 MUTABLE_PRESET_PATH = "user_presets.xml"
00014
00015 def __init__(self, parent=None, *args):
00016 QtCore.QAbstractListModel.__init__(self, parent, *args)
00017
00018 self.data_ = list()
00019
00020
00021
00022 self.immutable_infoh_=ImmutablePresetInfoHandler(PresetListModel.IMMUTABLE_PRESET_PATH)
00023 data_loc=QDesktopServices.storageLocation(QDesktopServices.DataLocation)
00024 mutable_path=os.path.join(str(data_loc), 'config',
00025 PresetListModel.MUTABLE_PRESET_PATH)
00026 qdir=QtCore.QDir(data_loc)
00027 if not qdir.exists():
00028 qdir.mkpath("config")
00029 self.infoh_=PresetInfoHandler(mutable_path)
00030 self.LoadPresetsFromInfo()
00031
00032 def AddItem(self, preset, row, editable, save):
00033 if self.NameIsValid(preset.GetName()):
00034 icon = self.GetIcon(preset)
00035 self.insertRow(row, QtCore.QModelIndex())
00036 self.data_[row] = [preset, icon, editable]
00037 model_index = self.createIndex(row,0)
00038 index = self.index(row)
00039 end_index = self.createIndex(self.rowCount(),0)
00040 if save:
00041 self.AddPresetToInfo(preset)
00042 self.emit(QtCore.SIGNAL("dataChanged"),model_index, end_index)
00043 return True
00044 return False
00045
00046 def IsEditable(self, row):
00047 return self.data_[row][2]
00048
00049 def RemoveItem(self, row):
00050 if self.IsEditable(row):
00051 name = self.data_[row][0].GetName()
00052 self.removeRow(row, QtCore.QModelIndex())
00053 model_index = self.createIndex(row,0)
00054 self.infoh_.RemovePreset(name)
00055 self.emit(QtCore.SIGNAL("dataChanged"),model_index, model_index)
00056 return True
00057 return False
00058
00059 def AddPresetToInfo(self,preset):
00060 self.infoh_.StorePreset(preset)
00061
00062 def LoadPresetsFromInfo(self):
00063 if self.immutable_infoh_:
00064 presets = self.immutable_infoh_.GetPresets()
00065 for k, v in presets.iteritems():
00066 self.AddItem(v, self.GetLastRow(), False, False)
00067
00068 presets = self.infoh_.GetPresets()
00069 for k, v in presets.iteritems():
00070 self.AddItem(v, self.GetLastRow(), True, False)
00071
00072 def GetPreset(self, model_index):
00073 if model_index.isValid():
00074 return self.data_[model_index.row()][0]
00075
00076 def GetLastRow(self):
00077 return self.rowCount()
00078
00079
00080 def GetIcon(self, preset):
00081 pixmap = QtGui.QPixmap(64, 64)
00082 pixmap.fill(QtCore.Qt.transparent)
00083 return QtGui.QIcon(pixmap)
00084
00085 def NameIsValid(self, string):
00086 if len(string)==0:
00087 return False
00088 for values in self.data_:
00089 if string == values[0].GetName():
00090 return False
00091 return True
00092
00093
00094 def rowCount(self, parent=QtCore.QModelIndex()):
00095 return len(self.data_)
00096
00097 def data(self, index, role):
00098 if index.isValid() and index.row()< self.rowCount():
00099 data = self.data_[index.row()]
00100 if role == QtCore.Qt.DisplayRole:
00101 return QtCore.QVariant(data[0].GetName())
00102 elif role == QtCore.Qt.DecorationRole:
00103 return QtCore.QVariant(data[1])
00104 return QtCore.QVariant()
00105
00106 def setData(self, index, value, role):
00107 if index.isValid():
00108 row = index.row()
00109 if not self.data_[row]:
00110 self.data_[row] = list()
00111 if role == QtCore.Qt.EditRole and self.NameIsValid(value.toString()):
00112 old_name = str(self.data_[row][0].GetName())
00113 new_name = value.toString()
00114 self.data_[row][0].SetName(str(new_name))
00115 self.infoh_.RenamePreset(old_name,str(new_name))
00116 self.emit(QtCore.SIGNAL("dataChanged"),index, index)
00117 return True
00118 elif role == QtCore.Qt.DisplayRole:
00119 self.data_[row][0].SetName(value.toString())
00120 elif role == QtCore.Qt.DecorationRole:
00121 self.dat_[row][1] = value.toPyObject()
00122 return False
00123
00124 def flags(self, index):
00125 if index.isValid():
00126 flags = QtCore.QAbstractItemModel.flags(self,index)
00127 if self.IsEditable(index.row()):
00128 return flags | QtCore.Qt.ItemIsEditable
00129 else:
00130 return flags
00131 return QtCore.Qt.ItemIsEnabled
00132
00133 def insertRow(self, position, index):
00134 self.beginInsertRows(index, position, position)
00135 self.data_.insert(position,list())
00136 self.endInsertRows()
00137 return True
00138
00139 def removeRow(self, position, index):
00140 self.beginRemoveRows(index, position, position)
00141 del self.data_[position]
00142 self.endRemoveRows()
00143 return True
00144
00145
00146
00147