00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import ost.gui.trajectory_viewer
00020 from _ost_gui import *
00021 import sip
00022
00023
00024
00025
00026 def _close_event_override_(event):
00027 print "close event"
00028 def _set_data_override_(data):
00029 print "set data"
00030
00031 def CreateDataViewer(ih,flag=False):
00032 viewer=GostyApp.Instance().CreateDataViewer(ih)
00033 if flag:
00034 viewer.image_=ih
00035 sip_viewer=viewer.qobject
00036 sip_viewer.closeEvent=_close_event_override_
00037 sip_viewer.setData=_set_data_override_
00038 return viewer
00039
00040 def ClearMessageWidget():
00041 gosty=GostyApp.Instance()
00042 gosty.message_widget.Clear()
00043
00044
00045 from PyQt4.QtGui import *
00046 from PyQt4.QtCore import *
00047 from ost import gfx
00048
00049 def PickColor(default=gfx.WHITE):
00050 """
00051 Pops up a color chooser that lets' the user pick a color and returns the
00052 selected color. If the user cancels the color chooser, None is returned.
00053
00054 :rtype: :class:`~ost.gfx.Color`
00055 """
00056 dialog=QColorDialog()
00057 qt_color=QColor(int(min(255,default.Red()*256)),
00058 int(min(255,default.Green()*256)),
00059 int(min(255,default.Blue()*256)))
00060 qt_color=dialog.getColor(qt_color)
00061 if not qt_color.isValid():
00062 return None
00063 return gfx.RGBb(qt_color.red(), qt_color.green(),qt_color.blue())
00064
00065 def GetMenu(menu_name, create=False):
00066 persp=GostyApp.Instance().perspective
00067 if isinstance(menu_name[0], QAction):
00068 return menu_name[0]
00069 else:
00070 node=persp.GetMenu(menu_name[0])
00071 for name in menu_name[1:]:
00072 found=False
00073 for action in node.actions():
00074 if str(action.text())==str(name):
00075 found=True
00076 node=action
00077 break
00078 if not found:
00079 if create:
00080 node=node.addMenu(name)
00081 else:
00082 raise ValueError("Menu '%s' doesn't exist" % ', '.join(menu_name))
00083 return node
00084
00085
00086
00087 def AddMenuAction(*args, **kwargs):
00088 """
00089 Add menu action to main menu.
00090
00091 This function lets you conveniently add actions to the main menu. To add a new
00092 new action "Background Color" to the "Scene" menu, simply use
00093
00094 .. code-block:: python
00095
00096 def SetBackgroundColor():
00097 scene.bg=gfx.PickColor(scene.bg)
00098
00099 AddMenuAction('Scene', "Background Color", SetBackgroundColor)
00100
00101 This will add the menu "Scene" if it does not exist yet, register the action
00102 "Background Color" and execute the function SetBackgroundColor whenever the
00103 action is triggered.
00104
00105 To assign a keyboard shortcut to the action, you can use the shortcut
00106 argument:
00107
00108 .. code-block:: python
00109
00110 AddMenuAction('Scene', 'Background Color', SetBackgroundColor,
00111 shortcut='Ctrl+B')
00112
00113 Whenever you press Ctrl+B (Cmd+B on MacOS X), the action will be executed.
00114
00115 Very often menu actions are coupled to the current selected objects in the
00116 scene menu. These menu actions are either enabled or disabled depending on the
00117 type of the selected objects. To easily support this scenario, the "enable"
00118 state of the menu action can be tightly coupled to the scene menu by providing
00119 a callable to the enabled argument. As an example, the following menu action
00120 is only enabled when exactly one gfx.Entity is selected.
00121
00122 .. code-block:: python
00123
00124 AddMenuAction('Scene', 'PrintAtomCount', PrintAtomCount,
00125 enabled=OneOf(gfx.Entity))
00126
00127 OneOf is a simple function object that takes any number of classes and returns
00128 true when the selected object is an instance. Alterantively, you might want to
00129 use ManyOf or supply a custom function that evaluates the state of the menu
00130 action to suit your needs.
00131 """
00132 class MenuActionEnabler(QObject):
00133 def __init__(self, predicate, action):
00134 QObject.__init__(self, action)
00135 self.predicate=predicate
00136 self.action=action
00137 app=GostyApp.Instance()
00138 QObject.connect(app.scene_win.qobject, SIGNAL('ActiveNodesChanged()'),
00139 self.TestEnable)
00140 self.TestEnable()
00141
00142 def TestEnable(self):
00143 self.action.setEnabled(self.predicate())
00144 persp=GostyApp.Instance().perspective
00145 menu_name=args[:-1]
00146 function=args[-1]
00147 if isinstance(menu_name[0], QMenu):
00148 node=menu_name[0]
00149 else:
00150 node=persp.GetMenu(args[0])
00151 for name in menu_name[1:-1]:
00152 found=False
00153 for action in node.actions():
00154 if str(action.text())==str(name):
00155 node=action
00156 break
00157 if not found:
00158 node=node.addMenu(name)
00159 action=node.addAction(str(menu_name[-1]))
00160 if 'shortcut' in kwargs:
00161 action.setShortcut(QKeySequence(kwargs['shortcut']))
00162 if 'checkable' in kwargs:
00163 action.setCheckable(kwargs['checkable'])
00164 if 'checked' in kwargs:
00165 action.setChecked(kwargs['checked'])
00166 if 'enabled' in kwargs:
00167 if callable(kwargs['enabled']):
00168 enabler=MenuActionEnabler(kwargs['enabled'], action)
00169 else:
00170 action.setEnabled(kwargs['enabled'])
00171 QObject.connect(action, SIGNAL('triggered()'), function)
00172 return action
00173
00174
00175 class OneOf:
00176 def __init__(self, *classes):
00177 self.classes=classes
00178 def __call__(self):
00179 sel=SceneSelection.Instance()
00180 if sel.GetActiveNodeCount()!=1:
00181 return False
00182 node=sel.GetActiveNode(0)
00183 for cl in self.classes:
00184 if isinstance(node, cl):
00185 return True
00186 return False
00187
00188 class TwoOf:
00189 def __init__(self, *classes):
00190 self.classes=classes
00191 def __call__(self):
00192 sel=SceneSelection.Instance()
00193 act_count=sel.GetActiveNodeCount()
00194 if act_count<2:
00195 return False
00196 found=0
00197 for i in range(0, act_count):
00198 node=sel.GetActiveNode(i)
00199 for cl in self.classes:
00200 if isinstance(node, cl):
00201 found += 1
00202 if found > 2:
00203 return False
00204 if found == 2:
00205 return True
00206 return False
00207
00208 class ManyOf:
00209 def __init__(self, *classes):
00210 self.classes=classes
00211 def __call__(self):
00212 sel=SceneSelection.Instance()
00213 if sel.GetActiveNodeCount()==0:
00214 return False
00215 for i in range(sel.GetActiveNodeCount()):
00216 node=sel.GetActiveNode(i)
00217 found=False
00218 for cl in self.classes:
00219 if isinstance(node, cl):
00220 found=True
00221 break
00222 if not found:
00223 return False
00224 return True
00225
00226 from ost import PushVerbosityLevel as _PushVerbosityLevel
00227 from ost import PopVerbosityLevel as _PopVerbosityLevel
00228 from ost import GetVerbosityLevel as _GetVerbosityLevel
00229
00230
00231 def PushVerbosityLevel(value):
00232 GostyApp.Instance().perspective.ChangeVerbositySlider(value)
00233
00234 def PopVerbosityLevel():
00235 _PopVerbosityLevel()
00236 GostyApp.Instance().perspective.ChangeVerbositySlider(_GetVerbosityLevel())
00237 _PopVerbosityLevel()