VivoQuant 6.5.0
Loading...
Searching...
No Matches
ScriptPanel Class Reference

Description

Dockable panel that scripts fill with fields and read back while they run.

The panel lives in the "Script Panel" dock of the main window. Obtain it with VQ.scriptPanel(), which returns null without a VivoScript license. There is one panel, and its contents stay between scripts, so a script that builds its own fields starts with clear().

Closing the dock only hides it: its fields and values are kept. It can be reopened from the Script > VivoScript Panel menu (enabled once a script has shown the panel) or from a script with showPanel().

See also VQ.scriptPanel() to obtain this object. The Script Panel Tutorial in the Workflow Assistant builds panels step by step, with snippets to run. Two examples show it at work: Customizable VQS UI (fields, sections, reading values) and Script Panel: Tabs, Repeatables and Validation (tabs, repeatables, the status line, reacting to edits and waiting for the user). For a script's settings, kept in a file and checked on a tab before the script runs, SettingsTab.vqs (installed with VivoQuant) builds the tab for you; see Script Settings with SettingsTab.vqs.

var panel = VQ.scriptPanel();
panel.clear();
panel.setTitle('QC Progress');
panel.setFields([
{ name: 'study', label: 'Study', type: 'label' },
{ name: 'status', label: 'Status', type: 'item', items: ['Pending', 'In progress', 'Done'] },
{ name: 'notes', label: 'Notes', type: 'textarea' }
]);
panel.showPanel();
for (var i = 0; i < studies.length; i++) {
panel.setValue('study', studies[i].PatientsName);
// ... work ...
panel.setValue('study' + i, 'done'); // unknown names create a text field
}
panel.wait('Submit'); // let the user edit, until they press Submit
var vals = panel.values(); // { study: ..., status: ..., notes: ..., study0: ... }
Fields, layout containers and data containers
A panel is described as a tree. Each entry is one of:
  • a field (text, textarea, int, double, item, bool, label): one editor with one value;
  • a layout container (section): arranges its children. It has no value of its own; its children's values pass straight through, so a field reads back by its own name however deeply it is nested in layout containers. Moving fields into or out of sections never changes values().
  • a data container (repeatable): its value is an array with one object per copy, and each copy is a new name scope.

Containers nest: a section or a repeatable copy can hold fields, sections and repeatables. Every container needs at least one child; a container with nothing to show is an error, so leave it out (or show a label saying there is nothing).

Names must be unique within their scope (the panel, or one copy of a repeatable), looking through layout containers. Sections don't need a name; an unnamed section can't be found with hasField(), removeField() or setReadOnly().

Tabs
The panel can show its entries on several tabs. Every call that places entries takes an optional tab argument, the name of the tab to place them on; without it they go on the default tab, whose name is ''. A tab that doesn't exist yet is created, at the end, by setFields(), addField(), setValue() and setColumns(); this is how a script adds a tab while it runs. The tab bar only appears once there are two tabs, so a panel that never names a tab looks like a plain panel.

All tabs share one name scope: a name can only be used once in the whole panel. So calls that look entries up by name (value(), values(), removeField(), hasField(), setReadOnly(), and setValue() for an existing entry) search every tab and need no tab, and values() returns one flat object holding the values of every tab.

A tab's name is also the text on its tab, except for the default tab, which shows "Main"; setTabLabel() changes the text. Each tab has its own columns and scrolls on its own.

To follow the tab the user is looking at, connect to the tab widget's currentChanged: VQ.getWidget('ScriptPanel.tabs').currentChanged.connect(...). A call that adds or removes tabs, clear() included, reports a change of the current tab once, after it has finished; a handler can then safely call any panel function, tab calls included.

panel.clear();
panel.setTabLabel('', 'Overview');
panel.setFields([{ name: 'study', label: 'Study', type: 'label' }]);
for (var i = 0; i < organs.length; i++) {
panel.setColumns(2, organs[i]);
panel.setFields([
{ name: organs[i] + 'Uptake', label: 'Uptake', type: 'double' },
{ name: organs[i] + 'Note', label: 'Note', type: 'text' }
], organs[i]);
}
panel.setCurrentTab(organs[0]);
Strict checking
Descriptions are checked completely before anything changes, and any problem is a script error: an unknown type, an unknown property or one the type doesn't use, a property of the wrong kind, a missing or duplicate name, an empty children array, or an invalid value. This keeps typos from being silently ignored, and guarantees that properties added in future versions can't change how existing scripts behave.

Errors say which call failed, the tab if one was given, and where the entry is: its position, counted from 0, and its name, or for an unnamed entry its type and label. Entries inside a container add their own position. For example:

ScriptPanel.setFields, item 7 (section 'ROI sets'): 'children' must be a non-empty array ...
ScriptPanel.setFields, tab 'Liver', item 6 (section 'Notes'), item 0 ('note1'): 'span' must be a whole number of at least 1
void setFields(Object &children, string tab=string())
Replaces the contents of tab with children, an array of descriptions.
Text
Labels, titles and label fields show text exactly as given; it is never interpreted as HTML.
Widget names
Widgets can be found with VQ.getWidget(). Their names are paths:
  • ScriptPanel_<name> for entries in the panel's scope (sections included);
  • ScriptPanel_<repeatable>[<copy>].<name> inside repeatables, e.g. ScriptPanel_roiNotes[0].roi;
  • ScriptPanel_<repeatable>.add and ScriptPanel_<repeatable>.remove[<copy>] for a repeatable's buttons;
  • ScriptPanel.title, ScriptPanel.status and ScriptPanel.continue for the title, the status line and the wait() button;
  • ScriptPanel.tabs for the tab widget, and ScriptPanel.tab[<tab>] for each tab's page (ScriptPanel.tab[] for the default tab).

Entry names don't include the tab, since names are unique across tabs.

Copies are renumbered when one is removed. Entry names can't contain '.', '[' or ']', so these names never clash.

Responsiveness
Scripts run on the GUI thread, so the user can only type into the panel while the script is waiting: in wait(), or in any other dialog or suspend(). Values the script sets with setValue() are shown immediately. After adding or removing fields during long-running work, call VQ.processEvents() so the layout is updated.
Reacting to edits
Scripts can connect to valueChanged; the handler runs while the script is waiting. It may change the panel, including removing the entry that was edited. If a handler throws, the error is logged as a warning and the script carries on: the error doesn't reach the script, and a running wait() continues. Catch errors inside the handler if the script needs to react to them.
panel.valueChanged.connect(function (name, value) {
if (name === 'status') {
panel.setReadOnly('notes', value === 'Done');
}
});
Variant value(string name)
Current value of name, or undefined if there is no such entry or it has no value.

Public Slots

void cancelWait ()
 Ends a running wait(), which then returns false. Called when a script is aborted.

Signals

void valueChanged (string name, Variant &value)

Public Member Functions

void clear ()
void setColumns (int columns, string tab=string())
 Lays out a tab's top-level entries in columns columns (default 1).
int columns (string tab=string())
 Returns the number of columns set with setColumns() for tab (default: the default tab). Throws a script error if there is no such tab.
void setTitle (string title)
 Text shown above the fields (and the tabs). Empty hides it.
void setStatus (string text, string kind=stringLiteral("info"))
 Shows text in the status line, just above the wait() button.
string status ()
 Text of the status line set with setStatus(); empty if hidden.
string statusKind ()
 Kind of the status line set with setStatus(): 'info', 'warning' or 'error'.
void setFields (Object &children, string tab=string())
 Replaces the contents of tab with children, an array of descriptions.
void addField (Object &entry, string tab=string())
bool removeField (string name)
bool hasField (string name)
Array< string > fieldNames ()
void setValue (string name, Variant &value)
 Sets the value of name, and shows it immediately.
void setValue (string name, Variant &value, string tab)
 Same as setValue(name, value), but a new text field goes on tab, which is created if it doesn't exist.
Variant value (string name)
 Current value of name, or undefined if there is no such entry or it has no value.
Object values ()
 Snapshot of all current values, as an object from name to value.
void setReadOnly (string name, bool readOnly)
Array< string > tabNames ()
void setCurrentTab (string tab)
 Brings tab to the front. Throws a script error if there is no such tab.
string currentTab ()
 Name of the tab in front: '' for the default tab, or when there are no tabs.
void setTabLabel (string tab, string label)
 Sets the text shown on tab; an empty label goes back to the default (the tab's name, or "Main" for the default tab).
bool removeTab (string tab)
void showPanel ()
 Shows the dock and brings it to the front.
void hidePanel ()
 Hides the dock. Fields and values are kept.
bool isOpen ()
 True if the dock is showing (false after the user closes it, or after hidePanel()).
bool wait (string buttonText=stringLiteral("Continue"))
 Waits until the user presses the buttonText button on the panel.

Member Function Documentation

◆ addField()

void addField ( Object & entry,
string tab = string() )

Appends one entry to tab, which is created if it doesn't exist (default: the default tab). See setFields() for the description format.

◆ clear()

void clear ( )

Removes all tabs, fields, the title and the status, leaving just the default tab with one column and its default label.

◆ fieldNames()

Array< string > fieldNames ( )

Names of the entries with a value (fields and repeatables), in display order: tab by tab, in tab order.

◆ hasField()

bool hasField ( string name)

True if an entry (field, section or repeatable) has this name in the panel's scope, on any tab. Always false for an empty name.

◆ removeField()

bool removeField ( string name)

Removes the named entry, wherever it is in the panel's scope, on any tab (sections included). Returns false if there is no such entry.

◆ removeTab()

bool removeTab ( string tab)

Removes tab and everything on it. Returns false if there is no such tab. Removing the default tab is allowed; placing something on it again recreates it.

◆ setColumns()

void setColumns ( int columns,
string tab = string() )

Lays out a tab's top-level entries in columns columns (default 1).

Each column holds one label and its field. Entries fill the columns left to right, then top to bottom, in the order they were added. An entry with span takes that many columns; containers take the whole row unless they set span. Sections and repeatables have their own columns. setFields() keeps the current number of columns; clear() resets it to 1. Each tab has its own columns; tab names the tab, which is created if it doesn't exist (default: the default tab).

◆ setFields()

void setFields ( Object & children,
string tab = string() )

Replaces the contents of tab with children, an array of descriptions.

tab names the tab, which is created if it doesn't exist; without it, the default tab. Other tabs are left as they are. Names must not be used on other tabs.

Properties every type accepts:

  • type: see below; defaults to 'text'
  • name: key used by setValue(), value(), values() and the other calls. Required for fields and repeatables, optional for sections. Must not be empty, contain '.', '[' or ']', or be '__proto__'.
  • label: text shown with the entry; defaults to name. For a field, an empty label ('') means no label: the field also takes the label's cell.
  • tooltip: tooltip text, shown on the field and its label
  • readOnly: true to stop the user editing it; for a container, everything in it
  • span: columns taken in the parent's layout; default 1 for fields, the whole row for containers. A span larger than the parent's columns takes the whole row.

Field types and their extra properties:

  • 'text': single-line text. value: string (a number is shown as text).
  • 'textarea': multi-line text, 4 lines visible; Enter adds a line, Tab moves on. value: string (a number is shown as text).
  • 'int': whole number. value, min, max (default: full 32-bit range), step (default 1); min, max and step must be whole numbers in the 32-bit range. Values outside the range are clamped.
  • 'double': decimal number. value, min, max (default -1e9 to 1e9), decimals (default 2), step (default 0.1). NaN and infinity are rejected.
  • 'item': drop-down. items (array of strings), value (one of the items, default the first), editable (true lets the user type other text).
  • 'bool': check box. value: true or false.
  • 'label': read-only text the script sets. value: string (a number is shown as text).

Container types. Both require children, a non-empty array of entries (fields or other containers); to show nothing, leave the container out.

  • 'section': a framed box with label as its title, holding children in columns columns (default 1). A layout container: no value.
  • 'repeatable': a section the user can duplicate. children are the contents of each copy, laid out in columns columns. Add appends a blank copy and each copy has a Remove button; there is always at least one copy. A data container: its value is an array with one object per copy, mapping each value name in the copy to its value. value: such an array, to start with.
panel.setFields([
{ type: 'section', label: 'Acquisition', columns: 2, children: [
{ name: 'scanner', label: 'Scanner', type: 'item', items: ['Scanner A', 'Scanner B'] },
{ name: 'slices', label: 'Slices', type: 'int', min: 1 }
] },
{ name: 'roiNotes', label: 'ROI notes', type: 'repeatable', columns: 2, children: [
{ name: 'roi', label: 'ROI', type: 'item', items: roiNames },
{ name: 'note', label: 'Note', type: 'text' }
] }
]);
// values(): { scanner: 'Scanner A', slices: 1, roiNotes: [{ roi: 'Liver', note: '' }] }
int columns(string tab=string())
Returns the number of columns set with setColumns() for tab (default: the default tab)....

See "Widget names" above for finding the widgets with VQ.getWidget().

Throws a script error if the description is invalid (see Strict checking); the panel is then left unchanged, and no tab is created.

◆ setReadOnly()

void setReadOnly ( string name,
bool readOnly )

Makes the named entry read-only or editable, on any tab; for a container, everything in it. Throws a script error if there is no such entry.

◆ setStatus()

void setStatus ( string text,
string kind = stringLiteral("info") )

Shows text in the status line, just above the wait() button.

The status line is outside the tabs, so changing it never moves the entries; use it for messages that change often, such as what is wrong with the current values. It shows whether or not the panel is waiting. Empty text hides it; clear() empties it.

kind sets the text colour: 'info' (default, the normal colour), 'warning' (orange) or 'error' (red). Throws a script error for any other kind.

panel.setStatus("Can't run yet: 'Studies to' is not a date as YYYYMMDD", 'error');

◆ setTabLabel()

void setTabLabel ( string tab,
string label )

Sets the text shown on tab; an empty label goes back to the default (the tab's name, or "Main" for the default tab).

The tab is still addressed by its name. Throws a script error if there is no such tab.

◆ setValue() [1/2]

void setValue ( string name,
Variant & value )

Sets the value of name, and shows it immediately.

If there is no entry name, a text field labelled name is appended, so the panel can be used as a simple key/value log: panel.setValue('study3', 'done'). It goes on the default tab. Only the panel's own scope is searched (on every tab): a field inside a repeatable is not found by its name, so setValue('roi', ...) creates a new top-level field rather than changing the repeatable's 'roi' fields. Set the repeatable's whole array instead. Throws a script error if value isn't valid for the entry (see setFields()), or if name is a section.

◆ setValue() [2/2]

void setValue ( string name,
Variant & value,
string tab )

Same as setValue(name, value), but a new text field goes on tab, which is created if it doesn't exist.

Throws a script error if name already exists on another tab: panel.setValue('x', 1, 'Kidney') when 'x' is on 'Liver'. Use '' for the default tab.

◆ tabNames()

Array< string > tabNames ( )

Names of the tabs, in display order. The default tab is ''; it isn't listed after removeTab('') until something is placed on it again.

◆ valueChanged

void valueChanged ( string name,
Variant & value )
signal

Emitted when the user edits a value (not when the script calls setValue()). name is the top-level entry: for an edit inside a repeatable, the repeatable's own name (never the name of the field inside it), and value is that repeatable's value, one object per copy, as value() returns it. Which copy changed isn't passed; compare with the previous array.

◆ values()

Object values ( )

Snapshot of all current values, as an object from name to value.

Holds the values of every tab, in display order, tab by tab; tabs and sections add no properties of their own. Text, textarea, item and label fields give strings, int and double fields numbers, bool fields true or false, and repeatables arrays of objects. Values stay readable after the user closes the dock.

◆ wait()

bool wait ( string buttonText = stringLiteral("Continue"))

Waits until the user presses the buttonText button on the panel.

Shows the panel and a button at its bottom, and lets the user work in VivoQuant and edit the panel until the button is pressed. An empty buttonText shows "Continue".

Closing the dock doesn't affect the wait or the script: it only hides the panel, which can be reopened from the Script > VivoScript Panel menu to press the button. Stopping the script ends the wait.

Only one wait() can run at a time: calling it while the panel is already waiting, for example from a valueChanged handler, is a script error, and the running wait carries on.

Returns
true if the button was pressed; false if the wait was cancelled because the script was stopped or the application is closing.