OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
20 from ost import gui
21 from ost import gfx
22 import ost
23 import os
24 from datetime import datetime
25 from PyQt5 import QtCore, QtWidgets, QtGui
26 from .scene_selection_helper import SelHelper
27 from .preset_list_model import PresetListModel
28 from .preset_editor_widget import PresetEditor
29 from .preset import Preset
30 
31 class PresetWidget(QtWidgets.QWidget):
32  PRESET_XML_FILE = os.path.join(ost.GetSharedDataPath(), "scene", "presets.xml")
33  ICONS_DIR = os.path.join(ost.GetSharedDataPath(), "gui", "icons/")
34  def __init__(self, parent=None):
35  QtWidgets.QWidget.__init__(self, parent)
36 
37  self.text_ = "Presets"
38 
39  #Create Ui elements
40  self.list_view_ = QtWidgets.QListView()
41 
42  #Create Model
43  self.list_model_ = PresetListModel(self)
44  self.list_view_.setModel(self.list_model_)
45  self.list_view_.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
46 
47  self.add_action = QtWidgets.QAction("+",self)
48  self.add_action.setIcon(QtGui.QIcon(PresetWidget.ICONS_DIR+"add_icon.png"))
49 
50  self.add_action.triggered.connect(self.Add)
51 
52  self.add_button_ = QtWidgets.QToolButton(self)
53  self.add_button_.setIconSize(QtCore.QSize(20,20))
54  self.add_button_.setDefaultAction(self.add_action)
55 
56  self.preset_editor_ = PresetEditor(self)
57 
58  grid = QtWidgets.QGridLayout()
59  grid.setContentsMargins(0,5,0,0)
60  grid.addWidget(self.list_view_,0,0,3,3)
61  grid.addWidget(self.add_button_,3,0,1,1)
62  self.setLayout(grid)
63 
64  self.list_view_.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
65  self.list_view_.customContextMenuRequested.connect(self.contextMenuEvent)
67  self.CreateContextMenu()
68 
69  self.list_view_.doubleClicked.connect(self.Load)
70 
71  self.setMinimumSize(250,200)
72 
74  self.immucontext_menu_ = QtWidgets.QMenu("Context menu", self)
75  self.load_ = QtWidgets.QAction("Load", self.list_view_)
76  self.immucontext_menu_.addAction(self.load_)
77  #Connect Signal with Slot
78  self.load_.triggered.connect(self.LoadCurrentIndex)
79 
80  def CreateContextMenu(self):
81  self.context_menu_ = QtWidgets.QMenu("Context menu", self)
82  self.remove_ = QtWidgets.QAction("Remove", self.list_view_)
83  self.rename_ = QtWidgets.QAction("Rename", self.list_view_)
84  self.edit_ = QtWidgets.QAction("Edit", self.list_view_)
85  self.context_menu_.addAction(self.load_)
86  self.context_menu_.addAction(self.remove_)
87  self.context_menu_.addAction(self.rename_)
88  self.context_menu_.addAction(self.edit_)
89  #Connect Signals with Slots
90  self.remove_.triggered.connect(self.Remove)
91  self.rename_.triggered.connect(self.Rename)
92  self.edit_.triggered.connect(self.Edit)
93 
94  def contextMenuEvent(self, pos):
95  #ContextMenu
96  index = self.list_view_.indexAt(pos)
97  if index.isValid():
98  if self.list_model_.IsEditable(index.row()):
99  self.context_menu_.popup(QtWidgets.QCursor.pos())
100  else:
101  self.immucontext_menu_.popup(QtWidgets.QCursor.pos())
102 
103  def Add(self):
104  row = self.list_model_.GetLastRow()
105  preset = Preset(datetime.now().isoformat(' '))
106  self.preset_editor_.SetPreset(preset)
107  if(self.preset_editor_.exec_()):
108  if self.list_model_.AddItem(preset, row, True, True):
109  index = self.list_model_.index(row)
110  self.list_view_.setCurrentIndex(index)
111  self.Rename()
112 
113  def Remove(self):
114  if(self.list_view_.currentIndex().isValid()):
115  ret = QtWidgets.QMessageBox.warning(self, "Delete Preset",
116  "Delete Preset?",
117  QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
118  if ret == QtWidgets.QMessageBox.Yes:
119  self.list_model_.RemoveItem(self.list_view_.currentIndex().row())
120 
121  def Edit(self):
122  if(self.list_view_.currentIndex().isValid()):
123  preset = self.list_model_.GetPreset(self.list_view_.currentIndex())
124  self.preset_editor_.SetPreset(preset)
125  if(self.preset_editor_.exec_()):
126  row = self.list_view_.currentIndex().row()
127  self.list_model_.RemoveItem(row)
128  self.list_model_.AddItem(preset, row, True, True)
129 
130  def LoadCurrentIndex(self):
131  if(self.list_view_.currentIndex().isValid()):
132  self.Load(self.list_view_.currentIndex())
133 
134  def Load(self, index):
135  if(index.isValid()):
136  scene_selection = gui.SceneSelection.Instance()
137  preset=self.list_model_.GetPreset(index)
138  for i in range(0,scene_selection.GetActiveNodeCount()):
139  node = scene_selection.GetActiveNode(i)
140  if isinstance(node, gfx.Entity):
141  node.CleanColorOps()
142  preset.ApplyOn(node)
143 
144  def ChangeColor(self,node):
145  self.LoadCurrentIndex()
146 
147  def Update(self):
148  self.setEnabled(True)
149  if SelHelper().CheckAllFlags(SelHelper.NO_SELECTION):
150  self.setEnabled(False)
151  return
152 
153  if SelHelper().CheckNotFlags(SelHelper.HAS_ENTITY | SelHelper.IS_ONE_TYPE):
154  self.setEnabled(False)
155  return
156 
157  def Rename(self):
158  if(self.list_view_.currentIndex().isValid()):
159  self.list_view_.edit(self.list_view_.currentIndex())
160 
161  def GetText(self):
162  return self.text_
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:60
String DLLEXPORT_OST_BASE GetSharedDataPath()