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