Practical scripting guide: Using dialogs

To create interactive components, first declare the variables that serve to receive the user input. The best is to declare the variables outside the function where the user input values are retrieved, so that these values are available to the rest of the script. In the figure it is shown that the variables are declared early in the script.

 

An example is to receive user input via LineEdit, ComboBox or TextEdit. The graphical components are shown on the dialog according to the order that they are declared in the script. In the current example, the two LineEdit components, called 'nameEdit' and 'expEdit', are the first ones appearing in the script, so also the first ones shown on the dialog.

 

 

The picture above shows a dialog with the LineEdit, ComboBox and TextEdit components.

 

Steps

Script

 

 

To achieve this, first an empty function has to be created:

function Get_user_input_via_dialog() {

// put all the code here

}

 

Then, the main dialog is created via:

var dialog = new Dialog;

dialog.title = "Data of experiment";

dialog.okButtonText = "Done";

dialog.cancelButtonText = "Cancel";

dialog.width = 350;

 

3a. The components are first declared:

  b. Then its properties can be set:

  c. Now the LineEdit is ready to be added to the main dialog:

 

The same is done for the other LineEdit, the ComboBox and the TextEdit. For the ComboBox, the items in the list can be added via an array:

var nameEdit = new LineEdit;

nameEdit.label = "Subject name: ";

dialog.add(nameEdit);

 

 

 

 

expTypeBox.itemList = ["fMRI", "PET", "EEG"];

4. To retrieve the user input, create variables to capture the input:

 

All variable types (string, integer, etc) are declared with the keyword var.

var nameInput;

var expInput;

var detailsInput;

var expTypeInput;

 

5a.Show the dialog to the user:

 

 

  b. and copy the data that the user enters    in the variables

if( dialog.exec() ) {

 }

 

if( dialog.exec() ) {

nameInput = nameEdit.text;

expInput = expEdit.text;

detailsInput = detailsEdit.text;

expTypeInput = expTypeBox.currentItem;

}

6. To check what the user has entered, a message box can be used.

 

First a string named 'displayInfo' is created that contains the information.

Note: In the message box also HTML code can be used; to start a new line, the <BR> and <P> commands are helpful.

 

Because the message is long, the "+=" operator is used to concatenate the string with more information.

 

Enter the command to show the message box with the user input.

 

 

 

var displayInfo = "EXPERIMENT DATA<P> Subject name: '" + nameInput + "'<BR> Experiment name: " + expInput;

 

 

 

 

displayInfo += "; <BR> Type of experiment: '" + expTypeInput + "'";

displayInfo += "; <P> Experiment details: '" + detailsInput + "'";

 

 

MessageBox.information(displayInfo);

 

 

 

The result is the following message box:

 

 

 

The full script is available here.

 

Another example

 

The user input can be retrieved with the graphical QSA components. In section How to create a graphical component is shown how to do this. When the dialog is active, via the command "if (paramDialog.exec() ) { ... } " the values that are entered in checkboxes, spinboxes and other components, can be requested by asking the components what value was entered. In the script a checkbox called "motionCorrBox" is defined. To ask whether the checkbox was checked, the checkbox property "checked" is used and transferred to the variabele "motionCorr". This property pops up in a list when typing the text (see figure below).

The following script fragment illustrates the different properties of the graphical components that hold the user value. To retrieve a checkbox value for example, the property 'checked' is used, for a combobox the property 'currentItem' and for a number edit the property 'value'. The value of the graphical elements on the dialog is requested while the dialog is active: "if (paramdialog.exec()) { ... "

The variable "motionCorr" contains now the boolean value whether the checkbox is checked or not. This value is used in a method to decide if the motion correction method should be performed.

for (j=0; j<fmrFilesArray.length; j++) {

var fmrFileName = fmrFilesArray.pop();

var newFMR;

if (motionCorr) {

var docFMR = BrainVoyagerQX.OpenDocument(fmrFileName);

docFMR.CorrectMotion(1);

newFMR = docFMR.FileNameOfPreprocessdFMR;

docFMR.Close();

}

}

 

This is all information needed to create interactive components. The example script of this section is also available here.

 

 

Next: 3e The finishing touch - use HTML and tooltips in your dialogs

Index