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-2020 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 PyQt5 import QtCore, QtWidgets, QtGui
30 from .gradient_info_handler import GradientInfoHandler
31 from .gradient_list_model import GradientListModel
32 
33 
34 #Gradient Preset Widget
35 class GradientPresetWidget(QtWidgets.QWidget):
36  ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
37 
38  gradientSelected = QtCore.pyqtSignal(object, name="gradientSelected")
39 
40  def __init__(self, gradient_edit, parent=None):
41  QtWidgets.QWidget.__init__(self, parent)
42 
43  #Title
44  self.text_ = "Gradient Presets"
45 
46  #Refrences
47  self.gradient_edit_ = gradient_edit
48 
49  #Create Ui elements
50  self.list_view_ = QtWidgets.QListView()
51 
52  #Create Model
53  self.list_model_ = GradientListModel(self)
54  self.list_view_.setModel(self.list_model_)
55  self.list_view_.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
56 
57  preset_label = QtWidgets.QLabel(self.text_)
58  font = preset_label.font()
59  font.setBold(True)
60 
61  self.add_action = QtWidgets.QAction("+",self)
62  self.add_action.setIcon(QtGui.QIcon(GradientPresetWidget.ICONS_DIR+"add_icon.png"))
63 
64  self.add_action.triggered.connect(self.Add)
65 
66  self.add_button_ = QtWidgets.QToolButton(self)
67  self.add_button_.setIconSize(QtCore.QSize(20,20))
68  self.add_button_.setDefaultAction(self.add_action)
69 
70  grid = QtWidgets.QGridLayout()
71  grid.setContentsMargins(0,5,0,0)
72  grid.addWidget(preset_label, 0, 0, 1, 1)
73  qhbox = QtWidgets.QHBoxLayout()
74  grid.addWidget(self.list_view_,1,0,3,3)
75  grid.addWidget(self.add_button_,4,0,1,1)
76  self.setLayout(grid)
77 
78  self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
79  self.list_view_.customContextMenuRequested.connect(self.contextMenuEvent)
81  self.CreateContextMenu()
82 
83  self.list_view_.doubleClicked.connect(self.Load)
84 
86  self.immucontextMenu_ = QtWidgets.QMenu("Context menu", self)
87  self.load_ = QtWidgets.QAction("Load", self.list_view_)
88  self.immucontextMenu_.addAction(self.load_)
89  #Connect Signal with Slot
90  self.load_.triggered.connect(self.LoadCurrentIndex)
91 
92  def CreateContextMenu(self):
93  self.contextMenu_ = QtWidgets.QMenu("Context menu", self)
94  self.remove_ = QtWidgets.QAction("Remove", self.list_view_)
95  self.rename_ = QtWidgets.QAction("Rename", self.list_view_)
96  self.contextMenu_.addAction(self.load_)
97  self.contextMenu_.addAction(self.remove_)
98  self.contextMenu_.addAction(self.rename_)
99  #Connect Signals with Slots
100  self.remove_.triggered.connect(self.Remove)
101  self.rename_.triggered.connect(self.Rename)
102 
103  def contextMenuEvent(self, pos):
104  #ContextMenu
105  index = self.list_view_.indexAt(pos)
106  if index.isValid():
107  if self.list_model_.IsEditable(index.row()):
108  self.contextMenu_.popup(QtWidgets.QCursor.pos())
109  else:
110  self.immucontextMenu_.popup(QtWidgets.QCursor.pos())
111 
112  def Add(self):
113  if(self.list_view_.currentIndex().isValid()):
114  self.list_view_.closePersistentEditor(self.list_view_.currentIndex())
115  row = self.list_model_.GetLastRow()
116  if self.list_model_.AddItem(datetime.now().isoformat(' '), self.gradient_edit_.GetGradient(), row, True, True):
117  index = self.list_model_.index(row)
118  self.list_view_.setCurrentIndex(index)
119  self.Rename()
120  else:
121  QtWidgets.QMessageBox.information(self, "Gradient not added", "The gradient could not be added!")
122 
123  def Remove(self):
124  if(self.list_view_.currentIndex().isValid()):
125  ret = QtWidgets.QMessageBox.warning(self, "Delete Gradient",
126  "Delete Gradient?",
127  QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
128  if ret == QtWidgets.QMessageBox.Yes:
129  self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
130 
131  def LoadCurrentIndex(self):
132  if(self.list_view_.currentIndex().isValid()):
133  self.Load(self.list_view_.currentIndex())
134 
135  def Load(self, index):
136  if(index.isValid()):
137  self.gradientSelected.emit(self.list_model_.GetGradient(index))
138 
139  def Rename(self):
140  if(self.list_view_.currentIndex().isValid()):
141  self.list_view_.edit(self.list_view_.currentIndex())
142 
143  def GetText(self):
144  return self.text_
145 
String DLLEXPORT_OST_BASE GetSharedDataPath()