User Tools

Site Tools


Action Accelerators

Summary

Below is an example demonstrating how you can list the keyboard accelerators (aka “shortcuts”) for all known actions, via script.

API Areas of Interest

Example

Action_Accelerators.dsa
// DAZ Studio version 4.6.2.118
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function(){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Array<String> : A function to get a list of action accelerators and names
	function getActionAccelerators( sFormat, bCustom )
	{
		// Define
		var aResults = [];
 
		// Get the action manager
		var oActionMgr = MainWindow.getActionMgr();
		// If we do not have an action manager
		if( !oActionMgr ){
			// We are done...
			return aResults;
		}
 
		// Define a regular expression for stripping accelerators from the label
		var regxAccelerator = new RegExp( "&(.)", "g" );
 
		// Get the number of actions
		var nActions = (bCustom != true ?
			oActionMgr.getNumActions() :
			oActionMgr.getNumCustomActions());
 
		// Declare working variables
		var vAction;
		var sAccel, sText;
 
		// Iterate over the actions
		for( var i = 0; i < nActions; i += 1 ){
			// Get the 'current' action
			vAction = (bCustom != true ?
				oActionMgr.getAction( i ) :
				oActionMgr.getCustomActionText( i ));
 
			// Get the accelerator
			sAccel = (bCustom != true ?
				vAction.shortcut.toString() :
				oActionMgr.getCustomActionShortcut( i ));
 
			// If the action doesn't have an accelerator
			if( sAccel.isEmpty() ){
				// Next!!
				continue;
			}
 
			// Get the text
			sText = (bCustom != true ?
				vAction.text.replace( regxAccelerator, "$1" ) :
				oActionMgr.getCustomActionText( i ).replace( regxAccelerator, "$1" ));
 
			// Collect the string
			aResults.push( String( sFormat )
				.arg( sAccel )
				.arg( sText.replace( regxAccelerator, "$1" ) ) );
		}
 
		// Return the results
		return aResults;
	};
 
	/*********************************************************************/
	// Array<String> : A function to get a list of viewport accelerators
	function getViewportAccelerators( sFormat )
	{
		// Define
		var aResults = [];
 
		// Get the viewport manager
		var oViewportMgr = MainWindow.getViewportMgr();
		// If we do not have a viewport manager
		if( !oViewportMgr ){
			// We are done...
			return aResults;
		}
 
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.OrbitCamera ) )
				.arg( text( "Orbit Camera" ) ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.RotateCamera ) )
				.arg( text( "Rotate Camera" ) ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.PanCamera ) )
				.arg( text( "Pan Camera") ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.DollyCamera ) )
				.arg( text( "Dolly Camera" ) ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.BankCamera ) )
				.arg( text( "Bank Camera" ) ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.ZoomDCamera ) )
				.arg( text( "Dolly Zoom Camera" ) ) );
		aResults.push( String( sFormat )
				.arg( oViewportMgr.getMouseModifierString( DzViewportMgr.ZoomFCamera ) )
				.arg( text( "Focal Zoom Camera" ) ) );
 
		// Return the results
		return aResults;
	};
 
	/*********************************************************************/
	// Get the current style
	var oStyle = App.getStyle();
	// Get the general margin
	var nMargin = oStyle.pixelMetric( "DZ_GeneralMargin" );
 
	// Create a new dialog
	var wDlg = new DzBasicDialog();
	wDlg.caption = text( "Accelerators" );
 
	// Create a group box
	var wViewportGB = new DzVGroupBox( wDlg );
	// Set the title
	wViewportGB.title = text( "Viewport :" );
	// Set the margin
	wViewportGB.insideMargin = nMargin;
	// Set the spacing
	wViewportGB.insideSpacing = nMargin;
	// Add it to the dialog
	wDlg.addWidget( wViewportGB, 0 );
 
	// Define the formatting
	var sStart = "<table>";
	var sLine = "<tr><td align='center'>%1</td><td>&nbsp;:&nbsp;</td><td>%2</td></tr>";
	var sEnd = "</table>";
 
	// Get whether or not we can use text interaction flag enumerations
	var bHasTextInteractionFlags = (App.version64 >= 0x000400090003005a);
	var nTextBrowser = (bHasTextInteractionFlags ? DzLabel.TextBrowserInteraction : 13);
 
	// Create a label
	var wLbl = new DzLabel( wViewportGB );
	// Populate it with viewport accelerators
	wLbl.text = sStart + getViewportAccelerators( sLine ).join( "\n" ) + sEnd;
	// Set text interaction flags
	wLbl.textInteractionFlags = nTextBrowser;
 
	// Create a group box
	var wActionsGB = new DzVGroupBox( wDlg );
	// Set the title
	wActionsGB.title = text( "Actions :" );
	// Set the margin
	wActionsGB.insideMargin = nMargin;
	// Set the spacing
	wActionsGB.insideSpacing = nMargin;
	// Add it to the dialog
	wDlg.addWidget( wActionsGB, 1 );
 
	// Create a scroll area
	var wScroll = new DzScrollArea( wActionsGB );
	// Hide the horizontal scroll bar
	wScroll.setHorizontalScrollbarDisplay( false );
	// Hide the frame
	wScroll.hideFrame();
 
	// Get the accelerators for actions
	var aAccelerators = getActionAccelerators( sLine, false );
	// Append the accelerators for custom actions
	aAccelerators = aAccelerators.concat( getActionAccelerators( sLine, true ) );
	// Sort the list
	aAccelerators.sort();
 
	// Create a label
	wLbl = new DzLabel( wScroll );
	// Populate it with the results
	wLbl.text = sStart + aAccelerators.join( "\n" ) + sEnd;
	// Set text interaction flags
	wLbl.textInteractionFlags = nTextBrowser;
 
	// Set the widget for the scroll area
	wScroll.setWidget( wLbl );
 
	// Change the label on the accept button
	wDlg.setAcceptButtonText( text( "&OK" ) );
	// Hide the cancel button
	wDlg.showCancelButton( false );
 
	// Get the wrapped widget
	var oWidget = wDlg.getWidget();
 
	// Strip the space for a settings key
	var sKey = wDlg.caption.replace( new RegExp(" ", "g"), "" ) + "Dlg";
 
	// Set a [unique] object name on the wrapped dialog widget;
	// this is used for recording position and size separately
	// from all other [uniquely named] DzBasicDialog instances
	oWidget.objectName = sKey;
 
	// Get the minimum size hint for the dialog
	var sizeHint = oWidget.minimumSizeHint;
 
	// Define the fixed width
	var nFixedWidth = 270;
	// Define the fixed height
	var nFixedHeight = Math.min( Math.max( sizeHint.height, 500 ), 600 );
 
	// Set the fixed size of the dialog
	wDlg.setFixedSize( nFixedWidth, nFixedHeight );
 
	// Show the dialog
	wDlg.exec();
 
// Finalize the function and invoke
})();