mrv.maya.ui.dialog
Covered: 47 lines
Missed: 62 lines
Skipped 43 lines
Percent: 43 %
  2
"""
  3
Contains some default dialogs as well as layouts suitable for layout dialogs
  4
"""
  5
__docformat__ = "restructuredtext"
  7
import base as uibase
  8
import maya.cmds as cmds
  9
import maya.utils as mutils
 10
import mrv.util as util
 11
from mrv.interface import iPrompt, iChoiceDialog, iProgressIndicator
 13
import logging
 14
log = logging.getLogger("mrv.maya.ui.dialog")
 17
class Dialog( uibase.BaseUI ):
 18
	""" Base for all dialog classes """
 21
class PromptDialog( Dialog ):
 22
	""" Wrapper class for maya prompt dialog"""
 24
	def __init__( self, title, message, okText, cancelText, **kwargs ):
 25
		""" Create a prompt dialog and allow to query the result
 26
		:note: return default text in batch mode, given with 'text' key"""
 27
		if cmds.about( batch=1 ):
 28
			return kwargs.get( 'text', kwargs.get( 't', '' ) )
 30
		ret = cmds.promptDialog( t = title, m = message, b = [okText,cancelText],
 31
									db = okText, cb = cancelText,**kwargs )
 32
		self._text = None
 33
		if ret == okText:
 34
			self._text = cmds.promptDialog( q=1, text = 1 )
 36
	def text( self ):
 37
		""":return: the entered text or None if the box has been aborted"""
 38
		return self._text
 41
class Prompt( iPrompt ):
 42
	"""Implements the prompt interface using a prompt dialog"""
 44
	def prompt( self ):
 45
		"""Aquire the information using a prompt dialog
 47
		:return: prompted value if input was confirmed using confirmToken, or the cancelValue
 48
			if cancelToken was pressed
 49
		:note: tokens correspond to buttons
 50
		:note: handles batch mode correctly"""
 51
		if cmds.about( batch = 1 ):
 52
			return super( Prompt, self ).prompt( )
 54
		default_text = ( self.confirmDefault is not None and self.confirmDefault ) or ""
 56
		tokens = [ self.confirmToken ]
 57
		token_kwargs = { "db" : self.confirmToken }
 58
		if self.cancelToken is not None:
 59
			tokens.append( self.cancelToken )
 60
			token_kwargs[ "cb" ] = self.cancelToken
 62
		token_kwargs.update( self._kwargs )
 64
		ret = cmds.promptDialog( t="Prompt", m = self.msg, b = tokens, text = default_text, **token_kwargs )
 66
		if ret == self.cancelToken:
 67
			return self.cancelDefault
 69
		if ret == self.confirmToken:
 70
			return cmds.promptDialog( q=1, text = 1 )
 72
		return self.confirmDefault
 75
class ChoiceDialog( iChoiceDialog ):
 76
	"""Maya implementation of the generic choice dialog interface"""
 78
	def choice( self ):
 79
		"""Return the choice made by the user"""
 81
		if cmds.about( b=1 ):
 82
			return self.default_choice
 84
		return cmds.confirmDialog( 	t = self.title,
 85
								  	m = self.message,
 86
									b = [ str( c ) for c in self.choices ],
 87
									db = self.default_choice,
 88
									cb = self.cancel_choice,
 89
									ds = self.cancel_choice )
 92
class ProgressWindow( iProgressIndicator ):
 93
	"""Simple progress window wrapping the default maya progress window"""
 94
	def __init__( self, **kwargs ):
 95
		"""Everything that iProgress indicator and Maya Progress Window support"""
 96
		min = kwargs.pop( "min", kwargs.pop( "minValue" , 0 ) )
 97
		max = kwargs.pop( "max", kwargs.pop( "maxValue", 100 ) )
 99
		relative = kwargs.pop( "is_relative", 1 )
100
		super( ProgressWindow, self ).__init__( min = min, max = max, is_relative = relative )
103
		kwargs.pop( "s", kwargs.pop( "step", 0 ) )
104
		kwargs.pop( "pr", kwargs.pop( "progress", 0 ) )
105
		kwargs.pop( "ep", kwargs.pop( "endProgress", 0 ) )
106
		kwargs.pop( "ic", kwargs.pop( "isCancelled", 0 ) )
107
		kwargs.pop( "e", kwargs.pop( "edit", 0 ) )
109
		self.kwargs = kwargs  			# store for begin
111
	def refresh( self, message = None ):
112
		"""Finally show the progress window"""
113
		mn,mx = ( self.isRelative() and ( 0,100) ) or self.range()
114
		p = self.get()
116
		myargs = dict()
117
		myargs[ "e" ] = 1
118
		myargs[ "min" ] = mn
119
		myargs[ "max" ] = mx
120
		myargs[ "pr" ] = p
121
		myargs[ "status" ] = message or ( "Progress %s" % ( "." * ( int(p) % 4 ) ) )
123
		try:
124
			cmds.progressWindow( **myargs )
125
		except RuntimeError,e:
126
			log.warn(str( e ))
127
			pass 		# don't know yet why that happens
130
	def begin( self ):
131
		"""Show our window"""
132
		super( ProgressWindow, self ).begin( )
133
		cmds.progressWindow( **self.kwargs )
135
	def end( self ):
136
		"""Close the progress window"""
138
		super( ProgressWindow, self ).end( )
139
		mutils.executeDeferred( cmds.progressWindow, ep=1 )
141
	def isCancelRequested( self ):
142
		""":return: True if the action should be cancelled, False otherwise"""
143
		return cmds.progressWindow( q=1, ic=1 )
145
	def isAbortable( self ):
146
		""":return : true if the process can be aborted"""
147
		return cmds.progressWindow( q=1, ii=1 )
149
	def setAbortable( self, state ):
150
		cmds.progressWindow( e=1, ii=state )
151
		return super( ProgressWindow, self ).setAbortable( state )