import os
import time
import math
import director.vtkAll as vtk
import PythonQt
from PythonQt import QtCore
from PythonQt import QtGui
from director import getDRCBaseDir as getDRCBase
from director import botspy
from director import openscope
import functools
_mainWindow = None
_defaultRenderView = None
[docs]def getMainWindow():
global _mainWindow
if _mainWindow is None:
for widget in QtGui.QApplication.topLevelWidgets():
if isinstance(widget, PythonQt.QtGui.QMainWindow):
_mainWindow = widget
break
return _mainWindow
[docs]def quit():
QtGui.QApplication.instance().quit()
[docs]def getViewManager():
return getMainWindow().viewManager()
[docs]def getDRCView():
return _defaultRenderView or getMainWindow().viewManager().findView('DRC View')
[docs]def getSpreadsheetView():
return getMainWindow().viewManager().findView('Spreadsheet View')
[docs]def getCurrentView():
return _defaultRenderView or getMainWindow().viewManager().currentView()
[docs]def getCurrentRenderView():
view = getCurrentView()
if hasattr(view, 'camera'):
return view
[docs]def getOutputConsole():
return getMainWindow().outputConsole()
[docs]def getPythonConsole():
return PythonQt.dd._pythonManager.consoleWidget()
[docs]def showPythonConsole():
getPythonConsole().show()
_exclusiveDockWidgets = {}
[docs]def resetCamera(viewDirection=None, view=None):
view = view or getCurrentRenderView()
assert(view)
if viewDirection is not None:
camera = view.camera()
camera.SetPosition([0, 0, 0])
camera.SetFocalPoint(viewDirection)
camera.SetViewUp([0,0,1])
view.resetCamera()
view.render()
[docs]def setBackgroundColor(color, color2=None, view=None):
view = view or getCurrentRenderView()
assert(view)
if color2 is None:
color2 = color
ren = view.backgroundRenderer()
ren.SetBackground(color)
ren.SetBackground2(color2)
[docs]def displaySnoptInfo(info):
if getMainWindow() is not None:
getMainWindow().statusBar().showMessage('Info: %d' % info)
[docs]def toggleStereoRender():
view = getCurrentRenderView()
assert(view)
renderWindow = view.renderWindow()
renderWindow.SetStereoRender(not renderWindow.GetStereoRender())
view.render()
[docs]def getCameraTerrainModeEnabled(view):
return isinstance(view.renderWindow().GetInteractor().GetInteractorStyle(), vtk.vtkInteractorStyleTerrain2)
[docs]def setCameraTerrainModeEnabled(view, enabled):
if getCameraTerrainModeEnabled(view) == enabled:
return
if enabled:
view.renderWindow().GetInteractor().SetInteractorStyle(vtk.vtkInteractorStyleTerrain2())
view.camera().SetViewUp(0,0,1)
else:
view.renderWindow().GetInteractor().SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
view.render()
[docs]def toggleCameraTerrainMode(view = None):
view = view or getCurrentRenderView()
assert view
setCameraTerrainModeEnabled(view, not getCameraTerrainModeEnabled(view))
updateToggleTerrainAction(view)
[docs]def getActionsDict(actions):
actionsDict = {}
for action in actions:
if action.name:
actionsDict[action.name] = action
return actionsDict
[docs]def updateToggleTerrainAction(view):
if not getMainWindow():
return
isTerrainMode = False
if hasattr(view, 'renderWindow'):
isTerrainMode = isinstance(view.renderWindow().GetInteractor().GetInteractorStyle(), vtk.vtkInteractorStyleTerrain2)
getToolBarActions()['ActionToggleCameraTerrainMode'].checked = isTerrainMode
[docs]class ActionToggleHelper(object):
'''
This class manages a checkable action and forwards checked events
to user selected callbacks.
'''
def __init__(self, action, getEnabledFunc, setEnabledFunc):
self.getEnabled = getEnabledFunc
self.setEnabled = setEnabledFunc
self.action = action
self.action.setCheckable(True)
self.action.checked = getEnabledFunc()
self.action.connect('triggered()', self.onActionChanged)
[docs] def updateAction(self):
self.action.checked = self.getEnabled()
[docs] def onActionChanged(self):
if self.action.checked:
self.setEnabled(True)
else:
self.setEnabled(False)
self.updateAction()
[docs]def onCurrentViewChanged(previousView, currentView):
updateToggleTerrainAction(currentView)
[docs]def addShortcut(widget, keySequence, func):
shortcut = QtGui.QShortcut(QtGui.QKeySequence(keySequence), widget)
shortcut.connect('activated()', func)
shortcut.connect('activatedAmbiguously()', func)
return shortcut
[docs]def setupActions():
botApyAction = getToolsMenuActions()['ActionBotSpy']
botApyAction.connect(botApyAction, 'triggered()', botspy.startBotSpy)
scopeAction = getToolsMenuActions()['ActionSignalScope']
scopeAction.connect(scopeAction, 'triggered()', openscope.startSignalScope)
[docs]def showErrorMessage(message, title='Error'):
QtGui.QMessageBox.warning(getMainWindow(), title, message)
[docs]def showInfoMessage(message, title='Info'):
QtGui.QMessageBox.information(getMainWindow(), title, message)
[docs]def setupViewManager():
vm = getViewManager()
vm.connect('currentViewChanged(ddViewBase*, ddViewBase*)', onCurrentViewChanged)
tabBar = vm.findChildren('QTabBar')[0]
tabBar.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
tabBar.connect('customContextMenuRequested(const QPoint &)', onTabWidgetContextMenu)
[docs]def startup(globals):
global _mainWindow
_mainWindow = globals['_mainWindow']
if 'DRC_BASE' not in os.environ:
showErrorMessage('DRC_BASE environment variable is not set')
return
if not os.path.isdir(getDRCBase()):
showErrorMessage('DRC_BASE directory does not exist: ' + getDRCBase())
return
_mainWindow.connect('resetCamera()', resetCamera)
_mainWindow.connect('toggleStereoRender()', toggleStereoRender)
_mainWindow.connect('toggleCameraTerrainMode()', toggleCameraTerrainMode)
setupActions()
setupViewManager()