OpenStructure
__init__.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 #------------------------------------------------------------------------------
20 from ._ost_gui import *
21 
22 
24 def _close_event_override_(event):
25  print("close event")
26 def _set_data_override_(data):
27  print("set data")
28 
29 def CreateDataViewer(ih,flag=False):
30  viewer=GostyApp.Instance().CreateDataViewer(ih)
31  if flag:
32  viewer.image_=ih
33  sip_viewer=viewer.qobject
34  sip_viewer.closeEvent=_close_event_override_
35  sip_viewer.setData=_set_data_override_
36  return viewer
37 
39  gosty=GostyApp.Instance()
40  gosty.message_widget.Clear()
41 
42 
43 from PyQt5.QtGui import *
44 from PyQt5.QtWidgets import *
45 from PyQt5.QtCore import *
46 from ost import gfx
47 
48 def PickColor(default=gfx.WHITE):
49  """
50  Pops up a color chooser that lets' the user pick a color and returns the
51  selected color. If the user cancels the color chooser, None is returned.
52 
53  :rtype: :class:`~ost.gfx.Color`
54  """
55  dialog=QColorDialog()
56  qt_color=QColor(int(min(255,default.Red()*256)),
57  int(min(255,default.Green()*256)),
58  int(min(255,default.Blue()*256)))
59  qt_color=dialog.getColor(qt_color)
60  if not qt_color.isValid():
61  return None
62  return gfx.RGBb(qt_color.red(), qt_color.green(),qt_color.blue())
63 
64 def GetMenu(menu_name, create=False):
65  persp=GostyApp.Instance().perspective
66  if isinstance(menu_name[0], QAction):
67  return menu_name[0]
68  else:
69  node=persp.GetMenu(menu_name[0])
70  for name in menu_name[1:]:
71  found=False
72  for action in node.actions():
73  if str(action.text())==str(name):
74  found=True
75  node=action
76  break
77  if not found:
78  if create:
79  node=node.addMenu(name)
80  else:
81  raise ValueError("Menu '%s' doesn't exist" % ', '.join(menu_name))
82  return node
83 
84 
85 
86 def AddMenuAction(*args, **kwargs):
87  """
88  Add menu action to main menu.
89 
90  This function lets you conveniently add actions to the main menu. To add a new
91  new action "Background Color" to the "Scene" menu, simply use
92 
93  .. code-block:: python
94 
95  def SetBackgroundColor():
96  scene.bg=gfx.PickColor(scene.bg)
97 
98  AddMenuAction('Scene', "Background Color", SetBackgroundColor)
99 
100  This will add the menu "Scene" if it does not exist yet, register the action
101  "Background Color" and execute the function SetBackgroundColor whenever the
102  action is triggered.
103 
104  To assign a keyboard shortcut to the action, you can use the shortcut
105  argument:
106 
107  .. code-block:: python
108 
109  AddMenuAction('Scene', 'Background Color', SetBackgroundColor,
110  shortcut='Ctrl+B')
111 
112  Whenever you press Ctrl+B (Cmd+B on macOS), the action will be executed.
113 
114  Very often menu actions are coupled to the current selected objects in the
115  scene menu. These menu actions are either enabled or disabled depending on the
116  type of the selected objects. To easily support this scenario, the "enable"
117  state of the menu action can be tightly coupled to the scene menu by providing
118  a callable to the enabled argument. As an example, the following menu action
119  is only enabled when exactly one gfx.Entity is selected.
120 
121  .. code-block:: python
122 
123  AddMenuAction('Scene', 'PrintAtomCount', PrintAtomCount,
124  enabled=OneOf(gfx.Entity))
125 
126  OneOf is a simple function object that takes any number of classes and returns
127  true when the selected object is an instance. Alterantively, you might want to
128  use ManyOf or supply a custom function that evaluates the state of the menu
129  action to suit your needs.
130  """
131  class MenuActionEnabler(QObject):
132  def __init__(self, predicate, action):
133  QObject.__init__(self, action)
134  self.predicate=predicate
135  self.action=action
136  app=GostyApp.Instance()
137  app.scene_win.qobject.ActiveNodesChanged.connect(self.TestEnable)
138  self.TestEnable()
139 
140  def TestEnable(self):
141  self.action.setEnabled(self.predicate())
142  persp=GostyApp.Instance().perspective
143  menu_name=args[:-1]
144  function=args[-1]
145  if isinstance(menu_name[0], QMenu):
146  node=menu_name[0]
147  else:
148  node=persp.GetMenu(args[0])
149  for name in menu_name[1:-1]:
150  found=False
151  for action in node.actions():
152  if str(action.text())==str(name):
153  node=action
154  break
155  if not found:
156  node=node.addMenu(name)
157  action=node.addAction(str(menu_name[-1]))
158  if 'shortcut' in kwargs:
159  action.setShortcut(QKeySequence(kwargs['shortcut']))
160  if 'checkable' in kwargs:
161  action.setCheckable(kwargs['checkable'])
162  if 'checked' in kwargs:
163  action.setChecked(kwargs['checked'])
164  if 'enabled' in kwargs:
165  if callable(kwargs['enabled']):
166  enabler=MenuActionEnabler(kwargs['enabled'], action)
167  else:
168  action.setEnabled(kwargs['enabled'])
169  action.triggered.connect(function)
170  return action
171 
172 
173 class OneOf:
174  def __init__(self, *classes):
175  self.classesclasses=classes
176  def __call__(self):
177  sel=SceneSelection.Instance()
178  if sel.GetActiveNodeCount()!=1:
179  return False
180  node=sel.GetActiveNode(0)
181  for cl in self.classesclasses:
182  if isinstance(node, cl):
183  return True
184  return False
185 
186 class TwoOf:
187  def __init__(self, *classes):
188  self.classesclasses=classes
189  def __call__(self):
190  sel=SceneSelection.Instance()
191  act_count=sel.GetActiveNodeCount()
192  if act_count<2:
193  return False
194  found=0
195  for i in range(0, act_count):
196  node=sel.GetActiveNode(i)
197  for cl in self.classesclasses:
198  if isinstance(node, cl):
199  found += 1
200  if found > 2:
201  return False
202  if found == 2:
203  return True
204  return False
205 
206 class ManyOf:
207  def __init__(self, *classes):
208  self.classesclasses=classes
209  def __call__(self):
210  sel=SceneSelection.Instance()
211  if sel.GetActiveNodeCount()==0:
212  return False
213  for i in range(sel.GetActiveNodeCount()):
214  node=sel.GetActiveNode(i)
215  found=False
216  for cl in self.classesclasses:
217  if isinstance(node, cl):
218  found=True
219  break
220  if not found:
221  return False
222  return True
223 
224 from ost import PushVerbosityLevel as _PushVerbosityLevel
225 from ost import PopVerbosityLevel as _PopVerbosityLevel
226 from ost import GetVerbosityLevel as _GetVerbosityLevel
227 
228 
230  GostyApp.Instance().perspective.ChangeVerbositySlider(value)
231 
233  _PopVerbosityLevel()
234  GostyApp.Instance().perspective.ChangeVerbositySlider(_GetVerbosityLevel())
235  _PopVerbosityLevel() # the slider change pushes a new level :-(
def __call__(self)
Definition: __init__.py:209
def __init__(self, *classes)
Definition: __init__.py:207
def __call__(self)
Definition: __init__.py:176
def __init__(self, *classes)
Definition: __init__.py:174
def __call__(self)
Definition: __init__.py:189
def __init__(self, *classes)
Definition: __init__.py:187
def PopVerbosityLevel()
Definition: __init__.py:232
def PickColor(default=gfx.WHITE)
Definition: __init__.py:48
def CreateDataViewer(ih, flag=False)
Definition: __init__.py:29
def GetMenu(menu_name, create=False)
Definition: __init__.py:64
def PushVerbosityLevel(value)
Definition: __init__.py:229
def AddMenuAction(*args, **kwargs)
Definition: __init__.py:86
def ClearMessageWidget()
Definition: __init__.py:38