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 PyQt4.QtGui import *
46 from PyQt4.QtCore import *
47 from ost import gfx
48 
49 def PickColor(default=gfx.WHITE):
50  """
51  Pops up a color chooser that lets' the user pick a color and returns the
52  selected color. If the user cancels the color chooser, None is returned.
53 
54  :rtype: :class:`~ost.gfx.Color`
55  """
56  dialog=QColorDialog()
57  qt_color=QColor(int(min(255,default.Red()*256)),
58  int(min(255,default.Green()*256)),
59  int(min(255,default.Blue()*256)))
60  qt_color=dialog.getColor(qt_color)
61  if not qt_color.isValid():
62  return None
63  return gfx.RGBb(qt_color.red(), qt_color.green(),qt_color.blue())
64 
65 def GetMenu(menu_name, create=False):
66  persp=GostyApp.Instance().perspective
67  if isinstance(menu_name[0], QAction):
68  return menu_name[0]
69  else:
70  node=persp.GetMenu(menu_name[0])
71  for name in menu_name[1:]:
72  found=False
73  for action in node.actions():
74  if str(action.text())==str(name):
75  found=True
76  node=action
77  break
78  if not found:
79  if create:
80  node=node.addMenu(name)
81  else:
82  raise ValueError("Menu '%s' doesn't exist" % ', '.join(menu_name))
83  return node
84 
85 
86 
87 def AddMenuAction(*args, **kwargs):
88  """
89  Add menu action to main menu.
90 
91  This function lets you conveniently add actions to the main menu. To add a new
92  new action "Background Color" to the "Scene" menu, simply use
93 
94  .. code-block:: python
95 
96  def SetBackgroundColor():
97  scene.bg=gfx.PickColor(scene.bg)
98 
99  AddMenuAction('Scene', "Background Color", SetBackgroundColor)
100 
101  This will add the menu "Scene" if it does not exist yet, register the action
102  "Background Color" and execute the function SetBackgroundColor whenever the
103  action is triggered.
104 
105  To assign a keyboard shortcut to the action, you can use the shortcut
106  argument:
107 
108  .. code-block:: python
109 
110  AddMenuAction('Scene', 'Background Color', SetBackgroundColor,
111  shortcut='Ctrl+B')
112 
113  Whenever you press Ctrl+B (Cmd+B on MacOS X), the action will be executed.
114 
115  Very often menu actions are coupled to the current selected objects in the
116  scene menu. These menu actions are either enabled or disabled depending on the
117  type of the selected objects. To easily support this scenario, the "enable"
118  state of the menu action can be tightly coupled to the scene menu by providing
119  a callable to the enabled argument. As an example, the following menu action
120  is only enabled when exactly one gfx.Entity is selected.
121 
122  .. code-block:: python
123 
124  AddMenuAction('Scene', 'PrintAtomCount', PrintAtomCount,
125  enabled=OneOf(gfx.Entity))
126 
127  OneOf is a simple function object that takes any number of classes and returns
128  true when the selected object is an instance. Alterantively, you might want to
129  use ManyOf or supply a custom function that evaluates the state of the menu
130  action to suit your needs.
131  """
132  class MenuActionEnabler(QObject):
133  def __init__(self, predicate, action):
134  QObject.__init__(self, action)
135  self.predicate=predicate
136  self.action=action
137  app=GostyApp.Instance()
138  QObject.connect(app.scene_win.qobject, SIGNAL('ActiveNodesChanged()'),
139  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  QObject.connect(action, SIGNAL('triggered()'), 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:49
def PopVerbosityLevel
Definition: __init__.py:234
def PushVerbosityLevel
Definition: __init__.py:231
def AddMenuAction
Definition: __init__.py:87
def ClearMessageWidget
Definition: __init__.py:40
def GetMenu
Definition: __init__.py:65