OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
gradient_preset_widget.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 # -*- coding: utf-8 -*-
20 
21 import os
22 import ost
23 from ost import gui
24 from ost import gfx
25 from ost import info
26 from datetime import datetime
27 
28 from datetime import datetime
29 from PyQt4 import QtCore, QtGui
30 from gradient_info_handler import GradientInfoHandler
31 from gradient_list_model import GradientListModel
32 
33 
34 #Gradient Preset Widget
35 class GradientPresetWidget(QtGui.QWidget):
36  ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
37  def __init__(self, gradient_edit, parent=None):
38  QtGui.QWidget.__init__(self, parent)
39 
40  #Title
41  self.text_ = "Gradient Presets"
42 
43  #Refrences
44  self.gradient_edit_ = gradient_edit
45 
46  #Create Ui elements
47  self.list_view_ = QtGui.QListView()
48 
49  #Create Model
50  self.list_model_ = GradientListModel(self)
51  self.list_view_.setModel(self.list_model_)
52  self.list_view_.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
53 
54  preset_label = QtGui.QLabel(self.text_)
55  font = preset_label.font()
56  font.setBold(True)
57 
58  self.add_action = QtGui.QAction("+",self)
59  self.add_action.setIcon(QtGui.QIcon(GradientPresetWidget.ICONS_DIR+"add_icon.png"))
60 
61  QtCore.QObject.connect(self.add_action, QtCore.SIGNAL("triggered()"), self.Add)
62 
63  self.add_button_ = QtGui.QToolButton(self)
64  self.add_button_.setIconSize(QtCore.QSize(20,20))
65  self.add_button_.setDefaultAction(self.add_action)
66 
67  grid = QtGui.QGridLayout()
68  grid.setContentsMargins(0,5,0,0)
69  grid.addWidget(preset_label, 0, 0, 1, 1)
70  qhbox = QtGui.QHBoxLayout()
71  grid.addWidget(self.list_view_,1,0,3,3)
72  grid.addWidget(self.add_button_,4,0,1,1)
73  self.setLayout(grid)
74 
75  self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
76  QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("customContextMenuRequested(const QPoint)"), self.contextMenuEvent)
78  self.CreateContextMenu()
79 
80  QtCore.QObject.connect(self.list_view_, QtCore.SIGNAL("doubleClicked(const QModelIndex)"), self.Load)
81 
83  self.immucontextMenu_ = QtGui.QMenu("Context menu", self)
84  self.load_ = QtGui.QAction("Load", self.list_view_)
85  self.immucontextMenu_.addAction(self.load_)
86  #Connect Signal with Slot
87  QtCore.QObject.connect(self.load_, QtCore.SIGNAL("triggered()"), self.LoadCurrentIndex)
88 
89  def CreateContextMenu(self):
90  self.contextMenu_ = QtGui.QMenu("Context menu", self)
91  self.remove_ = QtGui.QAction("Remove", self.list_view_)
92  self.rename_ = QtGui.QAction("Rename", self.list_view_)
93  self.contextMenu_.addAction(self.load_)
94  self.contextMenu_.addAction(self.remove_)
95  self.contextMenu_.addAction(self.rename_)
96  #Connect Signals with Slots
97  QtCore.QObject.connect(self.remove_, QtCore.SIGNAL("triggered()"), self.Remove)
98  QtCore.QObject.connect(self.rename_, QtCore.SIGNAL("triggered()"), self.Rename)
99 
100  def contextMenuEvent(self, pos):
101  #ContextMenu
102  index = self.list_view_.indexAt(pos)
103  if index.isValid():
104  if self.list_model_.IsEditable(index.row()):
105  self.contextMenu_.popup(QtGui.QCursor.pos())
106  else:
107  self.immucontextMenu_.popup(QtGui.QCursor.pos())
108 
109  def Add(self):
110  if(self.list_view_.currentIndex().isValid()):
111  self.list_view_.closePersistentEditor(self.list_view_.currentIndex())
112  row = self.list_model_.GetLastRow()
113  if self.list_model_.AddItem(datetime.now().isoformat(' '), self.gradient_edit_.GetGradient(), row, True, True):
114  index = self.list_model_.index(row)
115  self.list_view_.setCurrentIndex(index)
116  self.Rename()
117  else:
118  QtGui.QMessageBox.information(self, "Gradient not added", "The gradient could not be added!")
119 
120  def Remove(self):
121  if(self.list_view_.currentIndex().isValid()):
122  ret = QtGui.QMessageBox.warning(self, "Delete Gradient",
123  "Delete Gradient?",
124  QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
125  if ret == QtGui.QMessageBox.Yes:
126  self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
127 
128  def LoadCurrentIndex(self):
129  if(self.list_view_.currentIndex().isValid()):
130  self.Load(self.list_view_.currentIndex())
131 
132  def Load(self, index):
133  if(index.isValid()):
134  self.emit(QtCore.SIGNAL("gradientSelected"),self.list_model_.GetGradient(index))
135 
136  def Rename(self):
137  if(self.list_view_.currentIndex().isValid()):
138  self.list_view_.edit(self.list_view_.currentIndex())
139 
140  def GetText(self):
141  return self.text_
142 
String DLLEXPORT_OST_BASE GetSharedDataPath()