00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 import os
00022 import ost
00023 from ost import gui
00024 from ost import gfx
00025 from ost import info
00026 from datetime import datetime
00027
00028 from datetime import datetime
00029 from PyQt4 import QtCore, QtGui
00030 from gradient_info_handler import GradientInfoHandler
00031 from gradient_list_model import GradientListModel
00032
00033
00034
00035 class GradientPresetWidget(QtGui.QWidget):
00036 ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
00037 def __init__(self, gradient_edit, parent=None):
00038 QtGui.QWidget.__init__(self, parent)
00039
00040
00041 self.text_ = "Gradient Presets"
00042
00043
00044 self.gradient_edit_ = gradient_edit
00045
00046
00047 self.list_view_ = QtGui.QListView()
00048
00049
00050 self.list_model_ = GradientListModel(self)
00051 self.list_view_.setModel(self.list_model_)
00052 self.list_view_.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
00053
00054 preset_label = QtGui.QLabel(self.text_)
00055 font = preset_label.font()
00056 font.setBold(True)
00057
00058 self.add_action = QtGui.QAction("+",self)
00059 self.add_action.setIcon(QtGui.QIcon(GradientPresetWidget.ICONS_DIR+"add_icon.png"))
00060
00061 QtCore.QObject.connect(self.add_action, QtCore.SIGNAL("triggered()"), self.Add)
00062
00063 self.add_button_ = QtGui.QToolButton(self)
00064 self.add_button_.setIconSize(QtCore.QSize(20,20))
00065 self.add_button_.setDefaultAction(self.add_action)
00066
00067 grid = QtGui.QGridLayout()
00068 grid.setContentsMargins(0,5,0,0)
00069 grid.addWidget(preset_label, 0, 0, 1, 1)
00070 qhbox = QtGui.QHBoxLayout()
00071 grid.addWidget(self.list_view_,1,0,3,3)
00072 grid.addWidget(self.add_button_,4,0,1,1)
00073 self.setLayout(grid)
00074
00075 self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
00076 QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("customContextMenuRequested(const QPoint)"), self.contextMenuEvent)
00077 self.CreateImmutableContextMenu()
00078 self.CreateContextMenu()
00079
00080 QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("doubleClicked(const QModelIndex)"), self.Load)
00081
00082 def CreateImmutableContextMenu(self):
00083 self.immucontextMenu_ = QtGui.QMenu("Context menu", self)
00084 self.load_ = QtGui.QAction("Load", self.list_view_)
00085 self.immucontextMenu_.addAction(self.load_)
00086
00087 QtCore.QObject.connect(self.load_, QtCore.SIGNAL("triggered()"), self.LoadCurrentIndex)
00088
00089 def CreateContextMenu(self):
00090 self.contextMenu_ = QtGui.QMenu("Context menu", self)
00091 self.remove_ = QtGui.QAction("Remove", self.list_view_)
00092 self.rename_ = QtGui.QAction("Rename", self.list_view_)
00093 self.contextMenu_.addAction(self.load_)
00094 self.contextMenu_.addAction(self.remove_)
00095 self.contextMenu_.addAction(self.rename_)
00096
00097 QtCore.QObject.connect(self.remove_, QtCore.SIGNAL("triggered()"), self.Remove)
00098 QtCore.QObject.connect(self.rename_, QtCore.SIGNAL("triggered()"), self.Rename)
00099
00100 def contextMenuEvent(self, pos):
00101
00102 index = self.list_view_.indexAt(pos)
00103 if index.isValid():
00104 if self.list_model_.IsEditable(index.row()):
00105 self.contextMenu_.popup(QtGui.QCursor.pos())
00106 else:
00107 self.immucontextMenu_.popup(QtGui.QCursor.pos())
00108
00109 def Add(self):
00110 if(self.list_view_.currentIndex().isValid()):
00111 self.list_view_.closePersistentEditor(self.list_view_.currentIndex())
00112 row = self.list_model_.GetLastRow()
00113 if self.list_model_.AddItem(datetime.now().isoformat(' '), self.gradient_edit_.GetGradient(), row, True, True):
00114 index = self.list_model_.index(row)
00115 self.list_view_.setCurrentIndex(index)
00116 self.Rename()
00117 else:
00118 QtGui.QMessageBox.information(self, "Gradient not added", "The gradient could not be added!")
00119
00120 def Remove(self):
00121 if(self.list_view_.currentIndex().isValid()):
00122 ret = QtGui.QMessageBox.warning(self, "Delete Gradient",
00123 "Delete Gradient?",
00124 QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
00125 if ret == QtGui.QMessageBox.Yes:
00126 self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
00127
00128 def LoadCurrentIndex(self):
00129 if(self.list_view_.currentIndex().isValid()):
00130 self.Load(self.list_view_.currentIndex())
00131
00132 def Load(self, index):
00133 if(index.isValid()):
00134 self.emit(QtCore.SIGNAL("gradientSelected"),self.list_model_.GetGradient(index))
00135
00136 def Rename(self):
00137 if(self.list_view_.currentIndex().isValid()):
00138 self.list_view_.edit(self.list_view_.currentIndex())
00139
00140 def GetText(self):
00141 return self.text_
00142