Package mrv :: Package maya :: Package ui :: Module dialog
[hide private]
[frames] | no frames]

Source Code for Module mrv.maya.ui.dialog

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  Contains some default dialogs as well as layouts suitable for layout dialogs 
  4  """ 
  5  __docformat__ = "restructuredtext" 
  6   
  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 
 12   
 13  import logging 
 14  log = logging.getLogger("mrv.maya.ui.dialog") 
 15   
 16   
17 -class Dialog( uibase.BaseUI ):
18 """ Base for all dialog classes """
19 20
21 -class PromptDialog( Dialog ):
22 """ Wrapper class for maya prompt dialog""" 23
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', '' ) ) 29 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 )
35
36 - def text( self ):
37 """:return: the entered text or None if the box has been aborted""" 38 return self._text
39 40
41 -class Prompt( iPrompt ):
42 """Implements the prompt interface using a prompt dialog""" 43
44 - def prompt( self ):
45 """Aquire the information using a prompt dialog 46 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( ) 53 54 default_text = ( self.confirmDefault is not None and self.confirmDefault ) or "" 55 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 61 # END token preparation 62 token_kwargs.update( self._kwargs ) 63 64 ret = cmds.promptDialog( t="Prompt", m = self.msg, b = tokens, text = default_text, **token_kwargs ) 65 66 if ret == self.cancelToken: 67 return self.cancelDefault 68 69 if ret == self.confirmToken: 70 return cmds.promptDialog( q=1, text = 1 ) 71 72 return self.confirmDefault
73 74
75 -class ChoiceDialog( iChoiceDialog ):
76 """Maya implementation of the generic choice dialog interface""" 77
78 - def choice( self ):
79 """Return the choice made by the user""" 80 # don't do anything inbatch mode 81 if cmds.about( b=1 ): 82 return self.default_choice 83 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 )
90 91
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 ) ) 98 99 relative = kwargs.pop( "is_relative", 1 ) 100 super( ProgressWindow, self ).__init__( min = min, max = max, is_relative = relative ) 101 102 # remove invalid args 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 ) ) 108 109 self.kwargs = kwargs # store for begin
110
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() 115 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 ) ) ) 122 123 try: 124 cmds.progressWindow( **myargs ) 125 except RuntimeError,e: 126 log.warn(str( e )) 127 pass # don't know yet why that happens
128 # END handle progress window errors 129
130 - def begin( self ):
131 """Show our window""" 132 super( ProgressWindow, self ).begin( ) 133 cmds.progressWindow( **self.kwargs )
134
135 - def end( self ):
136 """Close the progress window""" 137 # damn, has to be deferred to actually work 138 super( ProgressWindow, self ).end( ) 139 mutils.executeDeferred( cmds.progressWindow, ep=1 )
140
141 - def isCancelRequested( self ):
142 """:return: True if the action should be cancelled, False otherwise""" 143 return cmds.progressWindow( q=1, ic=1 )
144
145 - def isAbortable( self ):
146 """:return : true if the process can be aborted""" 147 return cmds.progressWindow( q=1, ii=1 )
148
149 - def setAbortable( self, state ):
150 cmds.progressWindow( e=1, ii=state ) 151 return super( ProgressWindow, self ).setAbortable( state )
152