00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 from ost import gui
00021 from ost import gfx
00022 import ost
00023 import os
00024 from datetime import datetime
00025 from PyQt4 import QtCore, QtGui
00026 from scene_selection_helper import SelHelper
00027 from preset_list_model import PresetListModel
00028 from preset_editor_widget import PresetEditor
00029 from preset import Preset
00030
00031 class PresetWidget(QtGui.QWidget):
00032 PRESET_XML_FILE = os.path.join(ost.GetSharedDataPath(), "scene", "presets.xml")
00033 ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
00034 def __init__(self, parent=None):
00035 QtGui.QWidget.__init__(self, parent)
00036
00037 self.text_ = "Presets"
00038
00039
00040 self.list_view_ = QtGui.QListView()
00041
00042
00043 self.list_model_ = PresetListModel(self)
00044 self.list_view_.setModel(self.list_model_)
00045 self.list_view_.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
00046
00047 self.add_action = QtGui.QAction("+",self)
00048 self.add_action.setIcon(QtGui.QIcon(PresetWidget.ICONS_DIR+"add_icon.png"))
00049
00050 QtCore.QObject.connect(self.add_action, QtCore.SIGNAL("triggered()"), self.Add)
00051
00052 self.add_button_ = QtGui.QToolButton(self)
00053 self.add_button_.setIconSize(QtCore.QSize(20,20))
00054 self.add_button_.setDefaultAction(self.add_action)
00055
00056 self.preset_editor_ = PresetEditor(self)
00057
00058 grid = QtGui.QGridLayout()
00059 grid.setContentsMargins(0,5,0,0)
00060 grid.addWidget(self.list_view_,0,0,3,3)
00061 grid.addWidget(self.add_button_,3,0,1,1)
00062 self.setLayout(grid)
00063
00064 self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
00065 QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("customContextMenuRequested(const QPoint)"), self.contextMenuEvent)
00066 self.CreateImmutableContextMenu()
00067 self.CreateContextMenu()
00068
00069 QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("doubleClicked(const QModelIndex)"), self.Load)
00070
00071 self.setMinimumSize(250,200)
00072
00073 def CreateImmutableContextMenu(self):
00074 self.immucontext_menu_ = QtGui.QMenu("Context menu", self)
00075 self.load_ = QtGui.QAction("Load", self.list_view_)
00076 self.immucontext_menu_.addAction(self.load_)
00077
00078 QtCore.QObject.connect(self.load_, QtCore.SIGNAL("triggered()"), self.LoadCurrentIndex)
00079
00080 def CreateContextMenu(self):
00081 self.context_menu_ = QtGui.QMenu("Context menu", self)
00082 self.remove_ = QtGui.QAction("Remove", self.list_view_)
00083 self.rename_ = QtGui.QAction("Rename", self.list_view_)
00084 self.edit_ = QtGui.QAction("Edit", self.list_view_)
00085 self.context_menu_.addAction(self.load_)
00086 self.context_menu_.addAction(self.remove_)
00087 self.context_menu_.addAction(self.rename_)
00088 self.context_menu_.addAction(self.edit_)
00089
00090 QtCore.QObject.connect(self.remove_, QtCore.SIGNAL("triggered()"), self.Remove)
00091 QtCore.QObject.connect(self.rename_, QtCore.SIGNAL("triggered()"), self.Rename)
00092 QtCore.QObject.connect(self.edit_, QtCore.SIGNAL("triggered()"), self.Edit)
00093
00094 def contextMenuEvent(self, pos):
00095
00096 index = self.list_view_.indexAt(pos)
00097 if index.isValid():
00098 if self.list_model_.IsEditable(index.row()):
00099 self.context_menu_.popup(QtGui.QCursor.pos())
00100 else:
00101 self.immucontext_menu_.popup(QtGui.QCursor.pos())
00102
00103 def Add(self):
00104 row = self.list_model_.GetLastRow()
00105 preset = Preset(datetime.now().isoformat(' '))
00106 self.preset_editor_.SetPreset(preset)
00107 if(self.preset_editor_.exec_()):
00108 if self.list_model_.AddItem(preset, row, True, True):
00109 index = self.list_model_.index(row)
00110 self.list_view_.setCurrentIndex(index)
00111 self.Rename()
00112
00113 def Remove(self):
00114 if(self.list_view_.currentIndex().isValid()):
00115 ret = QtGui.QMessageBox.warning(self, "Delete Preset",
00116 "Delete Preset?",
00117 QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
00118 if ret == QtGui.QMessageBox.Yes:
00119 self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
00120
00121 def Edit(self):
00122 if(self.list_view_.currentIndex().isValid()):
00123 preset = self.list_model_.GetPreset(self.list_view_.currentIndex())
00124 self.preset_editor_.SetPreset(preset)
00125 if(self.preset_editor_.exec_()):
00126 row = self.list_view_.currentIndex().row()
00127 self.list_model_.RemoveItem(row)
00128 self.list_model_.AddItem(preset, row, True, True)
00129
00130 def LoadCurrentIndex(self):
00131 if(self.list_view_.currentIndex().isValid()):
00132 self.Load(self.list_view_.currentIndex())
00133
00134 def Load(self, index):
00135 if(index.isValid()):
00136 scene_selection = gui.SceneSelection.Instance()
00137 preset=self.list_model_.GetPreset(index)
00138 for i in range(0,scene_selection.GetActiveNodeCount()):
00139 node = scene_selection.GetActiveNode(i)
00140 if isinstance(node, gfx.Entity):
00141 node.CleanColorOps()
00142 preset.ApplyOn(node)
00143
00144 def ChangeColor(self,node):
00145 self.LoadCurrentIndex()
00146
00147 def Update(self):
00148 self.setEnabled(True)
00149 if SelHelper().CheckAllFlags(SelHelper.NO_SELECTION):
00150 self.setEnabled(False)
00151 return
00152
00153 if SelHelper().CheckNotFlags(SelHelper.HAS_ENTITY | SelHelper.IS_ONE_TYPE):
00154 self.setEnabled(False)
00155 return
00156
00157 def Rename(self):
00158 if(self.list_view_.currentIndex().isValid()):
00159 self.list_view_.edit(self.list_view_.currentIndex())
00160
00161 def GetText(self):
00162 return self.text_