This is an example dialog:
'''
XRCDialog Example
by Patrick Ruoff in 2012
'''
from validators import *
from validator_mixins import *
from transfer import *
from xrc_dialog import *
import sys
import os.path as path
# Get the directory where we expect the xrc file
run_dir = path.split(path.abspath(sys.argv[0]))[0];
# Create and run the wx Application object
app = wx.App(False);
app.MainLoop();
# Define the dialog data, this is the data that you are interested in at the end of the day
dialog_data = {'some_int' : 4,
'some_float' : 42.0,
'some_choice' : 0,
};
'''
Define the validators, i.e. the mediators between the controls and the dialog data.
The format is
'name of the control in the xrc file' : Validator derived from CValidator
The CSingleItemValidator used here takes as first argument the name of the dialog item to modify
and as second optional argument a transfer function.
For the first validator, we want the control named 'm_int_sc' (a spincontrol, but we don't have to care about that)
to modify the data item 'some_int', using the transfer function CMinMaxTrans,
which defines a maximum and minimum value for 'some_int'.
The second validator, this time attached to the slider named 'm_int_slider', controls the same data item,
but this time with a transfer function that multiplies the value by 10, i.e. if the control's value is set to 42,
the data item 'some_int' will be set to 420.
The third validator is similar to the first one (this time, the valid range is 0-1000),
except that it mixes in a CTextFilterMixin, which filters out unwanted characters.
The 'm_float_slider' validator shows how to concatenate transfer functions with the >> operator:
Following the path of the data from the control to the dialog item, first an affine transfer function,
mapping the slider range (0, 100) to the range (0, 2) is executed, followed by an
exponential transfer that maps x to 10^x.
So the slider will set the data item in a range from 1 = 10^0 to 100 = 10^2 exponentially.
The last two validators show how to use validators for controls with items.
The control's value is interpreted as the current selection index in this case.
For the 'm_choice', this means that the selection index will be stored in 'some_choice',
whereas for the 'm_listbox', the selection index is transformed via i -> 1-i.
'''
validators = {'m_int_sc' : CSingleItemValidator( 'some_int', CMinMaxTrans(0, 100)),
'm_int_slider' : CSingleItemValidator( 'some_int', CAffineTrans(10,0) ),
'm_float_tc' : MixIn(CSingleItemValidator, CTextFilterMixin)(['some_float', CMinMaxTrans(0, 1000)], [ACCEPT_UFLOAT]),
'm_float_slider' : CSingleItemValidator( 'some_float', CAffineTrans((0.0,100.0),(0.0,2.0)) >> CExpTrans() ),
'm_choice' : CSingleItemValidator( 'some_choice' ),
'm_listbox' : CSingleItemValidator( 'some_choice', CAffineTrans(-1,1) ),
};
''' The update callback is used for live preview and is called whenever a value changes
and the dialogs update policy is set to UPDATE_ON_CHANGE '''
def OnUpdate(dialog_data):
''' as the dialog_data is shadowed in our example
(you don't want to see changes to the data if the user cancels the dialog),
we use the argument dialog_data and not the global
dialog_data, which is only updated when the user presses ok in our example'''
if dialog_data['some_choice'] == 0:
res = dialog_data['some_int'] + dialog_data['some_float'];
else:
res = dialog_data['some_int'] * dialog_data['some_float'];
# we directly set the value of the result textbox here
dlg.GetControl('m_result').SetValue(str(res));
''' Now its time to create the actual dialog
the arguments used here are:
path to the xrc file
the dialog's parent window
the dialog data
whether to shadow the dialog data or to modify it directly
the update callback
'''
dlg = CXrcDialog(path.join(run_dir,'test.xrc'), None, dialog_data, True, OnUpdate);
# We demonstrate how to manually initialize a control here
dlg.GetControl('m_listbox').Append('*');
dlg.GetControl('m_listbox').Append('+');
# we set the validators we defined earlier
dlg.SetValidators(validators);
''' This function registers a handler that changes the dialog's update policy
whenever a checkbox called 'm_preview' is checked
One can register custom event handlers with the dlg.AddEventHandlers method'''
RegisterPreviewCheckboxHandler(dlg);
# GO!
dlg.ShowModal();
print 'some_int is '+str(dialog_data['some_int'])