<commit_before>##########################################################################
#
#  Copyright (c) 2011-2012, <NAME>. All rights reserved.
#  Copyright (c) 2011-2013, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#      * Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      * Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided with
#        the distribution.
#
#      * Neither the name of <NAME> nor the names of
#        any other contributors to this software may be used to endorse or
#        promote products derived from this software without specific prior
#        written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import imath

import IECore
import IECoreGL

import Gaffer
import GafferUI

import OpenGL.GL as GL

from Qt import QtCore
from Qt import QtWidgets

## The GadgetWidget class provides a means of
# hosting a Gadget within a Widget based interface.
class GadgetWidget( GafferUI.GLWidget ) :

	## The gadget may either be a ViewportGadget in which case it will be used in a call
	# to setViewportGadget, otherwise a suitable viewport will be created and the gadget will
	# be placed within it.
	def __init__( self, gadget=None, bufferOptions=set(), **kw ) :

		GafferUI.GLWidget.__init__( self, bufferOptions, **kw )

		self._qtWidget().setFocusPolicy( QtCore.Qt.ClickFocus )

		self.__requestedDepthBuffer = self.BufferOptions.Depth in bufferOptions

		self.__enterConnection = self.enterSignal().connect( Gaffer.WeakMethod( self.__enter ) )
		self.__leaveConnection = self.leaveSignal().connect( Gaffer.WeakMethod( self.__leave ) )
		self.__keyPressConnection = self.keyPressSignal().connect( Gaffer.WeakMethod( self.__keyPress ) )
		self.__keyReleaseConnection = self.keyReleaseSignal().connect( Gaffer.WeakMethod( self.__keyRelease ) )
		self.__buttonPressConnection = self.buttonPressSignal().connect( Gaffer.WeakMethod( self.__buttonPress ) )
		self.__buttonReleaseConnection = self.buttonReleaseSignal().connect( Gaffer.WeakMethod( self.__buttonRelease ) )
		self.__buttonDoubleClickConnection = self.buttonDoubleClickSignal().connect( Gaffer.WeakMethod( self.__buttonDoubleClick ) )
		self.__mouseMoveConnection = self.mouseMoveSignal().connect( Gaffer.WeakMethod( self.__mouseMove ) )
		self.__dragBeginConnection = self.dragBeginSignal().connect( Gaffer.WeakMethod( self.__dragBegin ) )
		self.__dragEnterConnection = self.dragEnterSignal().connect( Gaffer.WeakMethod( self.__dragEnter ) )
		self.__dragMoveConnection = self.dragMoveSignal().connect( Gaffer.WeakMethod( self.__dragMove ) )
		self.__dragLeaveConnection = self.dragLeaveSignal().connect( Gaffer.WeakMethod( self.__dragLeave ) )
		self.__dropConnection = self.dropSignal().connect( Gaffer.WeakMethod( self.__drop ) )
		self.__dragEndConnection = self.dragEndSignal().connect( Gaffer.WeakMethod( self.__dragEnd ) )

		self.__wheelConnection = self.wheelSignal().connect( Gaffer.WeakMethod( self.__wheel ) )

		self.__visibilityChangedConnection = self.visibilityChangedSignal().connect( Gaffer.WeakMethod( self.__visibilityChanged ) )

		self.__viewportGadget = None
		if isinstance( gadget, GafferUI.ViewportGadget ) :
			self.setViewportGadget( gadget )
		else :
			self.setViewportGadget( GafferUI.ViewportGadget( gadget ) )

		self._qtWidget().installEventFilter( _eventFilter )

	## Returns the ViewportGadget used to render this Widget. You can
	# modify this freely to change the Gadgets being rendered.
	def getViewportGadget( self ) :

		return self.__viewportGadget

	## Sets the ViewportGadget used to render this Widget.
	def setViewportGadget( self, viewportGadget ) :

		assert( isinstance( viewportGadget, GafferUI.ViewportGadget ) )

		if viewportGadget.isSame( self.__viewportGadget ) :
			return

		if self.__viewportGadget is not None :
			self.__viewportGadget.setVisible( False )

		self.__viewportGadget = viewportGadget
		self.__renderRequestConnection = self.__viewportGadget.renderRequestSignal().connect( Gaffer.WeakMethod( self.__renderRequest ) )
		size = self.size()
		if size.x and size.y :
			self.__viewportGadget.setViewport( size )

		self.__viewportGadget.setVisible( self.visible() )

		self._redraw()

	def _resize( self, size ) :

		GafferUI.GLWidget._resize( self, size )
		if size.x and size.y :
			# avoid resizing if resolution has hit 0, as then
			# the reframing maths breaks down
			self.__viewportGadget.setViewport( size )

	def _draw( self ) :

		self.__viewportGadget.render()

	def __enter( self, widget ) :

		if not isinstance( QtWidgets.QApplication.focusWidget(), ( QtWidgets.QLineEdit, QtWidgets.QPlainTextEdit ) ) :
			self._qtWidget().setFocus()

		## \todo Widget.enterSignal() should be providing this
		# event itself.
		p = self.mousePosition( relativeTo = self )
		event = GafferUI.ButtonEvent(
			GafferUI.ButtonEvent.Buttons.None,
			GafferUI.ButtonEvent.Buttons.None,
			IECore.LineSegment3f(
				imath.V3f( p.x, p.y, 1 ),
				imath.V3f( p.x, p.y, 0 )
			)
		)

		self.__viewportGadget.enterSignal()( self.__viewportGadget, event )

	def __leave( self, widget ) :

		self._qtWidget().clearFocus()

		p = self.mousePosition( relativeTo = self )
		event = GafferUI.ButtonEvent(
			GafferUI.ButtonEvent.Buttons.None,
			GafferUI.ButtonEvent.Buttons.None,
			IECore.LineSegment3f(
				imath.V3f( p.x, p.y, 1 ),
				imath.V3f( p.x, p.y, 0 )
			)
		)

		self.__viewportGadget.leaveSignal()( self.__viewportGadget, event )

	def __renderRequest( self, gadget ) :

		self._redraw()

	def __buttonPress( self, widget, event ) :

		# we get given button presses before they're given to the overlay items,
		# so we must ignore them so they can be used by the overlay.
		if self._qtWidget().itemAt( event.line.p0.x, event.line.p0.y ) is not None :
			return False

		# but if we're outside the overlay item then we should take the
		# keyboard focus back from the overlay.
		focusItem = self._qtWidget().scene().focusItem()
		if focusItem is not None :
			self._qtWidget().scene().clearFocus()
			if focusItem.widget().focusWidget() is not None :
				focusItem.widget().focusWidget().clearFocus()

		if not self._makeCurrent() :
			return False

		return self.__viewportGadget.buttonPressSignal()( self.__viewportGadget, event )

	def __buttonRelease( self, widget, event ) :

		if not self._makeCurrent() :<commit_msg>GadgetWidget : Fix GL context handling for enter/leave events<commit_after>##########################################################################
#
#  Copyright (c) 2011-2012, <NAME>. All rights reserved.
#  Copyright (c) 2011-2013, Image Engine Design Inc. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#      * Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      * Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials provided with
#        the distribution.
#
#      * Neither the name of <NAME> nor the names of
#        any other contributors to this software may be used to endorse or
#        promote products derived from this software without specific prior
#        written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import imath

import IECore
import IECoreGL

import Gaffer
import GafferUI

import OpenGL.GL as GL

from Qt import QtCore
from Qt import QtWidgets

## The GadgetWidget class provides a means of
# hosting a Gadget within a Widget based interface.
class GadgetWidget( GafferUI.GLWidget ) :

	## The gadget may either be a ViewportGadget in which case it will be used in a call
	# to setViewportGadget, otherwise a suitable viewport will be created and the gadget will
	# be placed within it.
	def __init__( self, gadget=None, bufferOptions=set(), **kw ) :

		GafferUI.GLWidget.__init__( self, bufferOptions, **kw )

		self._qtWidget().setFocusPolicy( QtCore.Qt.ClickFocus )

		self.__requestedDepthBuffer = self.BufferOptions.Depth in bufferOptions

		self.__enterConnection = self.enterSignal().connect( Gaffer.WeakMethod( self.__enter ) )
		self.__leaveConnection = self.leaveSignal().connect( Gaffer.WeakMethod( self.__leave ) )
		self.__keyPressConnection = self.keyPressSignal().connect( Gaffer.WeakMethod( self.__keyPress ) )
		self.__keyReleaseConnection = self.keyReleaseSignal().connect( Gaffer.WeakMethod( self.__keyRelease ) )
		self.__buttonPressConnection = self.buttonPressSignal().connect( Gaffer.WeakMethod( self.__buttonPress ) )
		self.__buttonReleaseConnection = self.buttonReleaseSignal().connect( Gaffer.WeakMethod( self.__buttonRelease ) )
		self.__buttonDoubleClickConnection = self.buttonDoubleClickSignal().connect( Gaffer.WeakMethod( self.__buttonDoubleClick ) )
		self.__mouseMoveConnection = self.mouseMoveSignal().connect( Gaffer.WeakMethod( self.__mouseMove ) )
		self.__dragBeginConnection = self.dragBeginSignal().connect( Gaffer.WeakMethod( self.__dragBegin ) )
		self.__dragEnterConnection = self.dragEnterSignal().connect( Gaffer.WeakMethod( self.__dragEnter ) )
		self.__dragMoveConnection = self.dragMoveSignal().connect( Gaffer.WeakMethod( self.__dragMove ) )
		self.__dragLeaveConnection = self.dragLeaveSignal().connect( Gaffer.WeakMethod( self.__dragLeave ) )
		self.__dropConnection = self.dropSignal().connect( Gaffer.WeakMethod( self.__drop ) )
		self.__dragEndConnection = self.dragEndSignal().connect( Gaffer.WeakMethod( self.__dragEnd ) )

		self.__wheelConnection = self.wheelSignal().connect( Gaffer.WeakMethod( self.__wheel ) )

		self.__visibilityChangedConnection = self.visibilityChangedSignal().connect( Gaffer.WeakMethod( self.__visibilityChanged ) )

		self.__viewportGadget = None
		if isinstance( gadget, GafferUI.ViewportGadget ) :
			self.setViewportGadget( gadget )
		else :
			self.setViewportGadget( GafferUI.ViewportGadget( gadget ) )

		self._qtWidget().installEventFilter( _eventFilter )

	## Returns the ViewportGadget used to render this Widget. You can
	# modify this freely to change the Gadgets being rendered.
	def getViewportGadget( self ) :

		return self.__viewportGadget

	## Sets the ViewportGadget used to render this Widget.
	def setViewportGadget( self, viewportGadget ) :

		assert( isinstance( viewportGadget, GafferUI.ViewportGadget ) )

		if viewportGadget.isSame( self.__viewportGadget ) :
			return

		if self.__viewportGadget is not None :
			self.__viewportGadget.setVisible( False )

		self.__viewportGadget = viewportGadget
		self.__renderRequestConnection = self.__viewportGadget.renderRequestSignal().connect( Gaffer.WeakMethod( self.__renderRequest ) )
		size = self.size()
		if size.x and size.y :
			self.__viewportGadget.setViewport( size )

		self.__viewportGadget.setVisible( self.visible() )

		self._redraw()

	def _resize( self, size ) :

		GafferUI.GLWidget._resize( self, size )
		if size.x and size.y :
			# avoid resizing if resolution has hit 0, as then
			# the reframing maths breaks down
			self.__viewportGadget.setViewport( size )

	def _draw( self ) :

		self.__viewportGadget.render()

	def __enter( self, widget ) :

		if not isinstance( QtWidgets.QApplication.focusWidget(), ( QtWidgets.QLineEdit, QtWidgets.QPlainTextEdit ) ) :
			self._qtWidget().setFocus()

		## \todo Widget.enterSignal() should be providing this
		# event itself.
		p = self.mousePosition( relativeTo = self )
		event = GafferUI.ButtonEvent(
			GafferUI.ButtonEvent.Buttons.None,
			GafferUI.ButtonEvent.Buttons.None,
			IECore.LineSegment3f(
				imath.V3f( p.x, p.y, 1 ),
				imath.V3f( p.x, p.y, 0 )
			)
		)

		if not self._makeCurrent() :
			return False

		self.__viewportGadget.enterSignal()( self.__viewportGadget, event )

	def __leave( self, widget ) :

		self._qtWidget().clearFocus()

		p = self.mousePosition( relativeTo = self )
		event = GafferUI.ButtonEvent(
			GafferUI.ButtonEvent.Buttons.None,
			GafferUI.ButtonEvent.Buttons.None,
			IECore.LineSegment3f(
				imath.V3f( p.x, p.y, 1 ),
				imath.V3f( p.x, p.y, 0 )
			)
		)

		if not self._makeCurrent() :
			return False

		self.__viewportGadget.leaveSignal()( self.__viewportGadget, event )

	def __renderRequest( self, gadget ) :

		self._redraw()

	def __buttonPress( self, widget, event ) :

		# we get given button presses before they're given to the overlay items,
		# so we must ignore them so they can be used by the overlay.
		if self._qtWidget().itemAt( event.line.p0.x, event.line.p0.y ) is not None :
			return False

		# but if we're outside the overlay item then we should take the
		# keyboard focus back from the overlay.
		focusItem = self._qtWidget().scene().focusItem()
		if focusItem is not None :
			self._qtWidget().scene().clearFocus()
			if focusItem.widget().focusWidget() is not None :
				focusItem.widget().focusWidget().clearFocus()

		if not self._makeCurrent() :
			return False

		return self.__viewportGadget.buttonPressSignal()( self.__viewportGadget, event )

	def __buttonRelease( self, widget, event ) :

		if not self._makeCurrent() :
{'fraction_non_alphanumeric': 0.1080368906455863, 'fraction_numerical': 0.0038208168642951254, 'mean_word_length': 4.326666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7534501', 'n_tokens_mistral': 4861, 'n_tokens_neox': 4459, 'n_words': 1331}