id: variable_qtgui_check_box
label: QT GUI Check Box
flags: [ show_id, python, cpp ]

parameters:
-   id: label
    label: Label
    dtype: string
    hide: ${ ('none' if label else 'part') }
-   id: type
    label: Type
    dtype: enum
    default: int
    options: [real, int, string, bool, raw]
    option_labels: [Float, Integer, String, Boolean, Any]
    option_attributes:
        conv: [float, int, str, bool, eval]
        cpp_opts: [double,int,"std::string",double,double]
    hide: part
-   id: value
    label: Default Value
    dtype: ${ type }
    default: 'True'
-   id: 'true'
    label: 'True'
    dtype: ${ type }
    default: 'True'
-   id: 'false'
    label: 'False'
    dtype: ${ type }
    default: 'False'
-   id: gui_hint
    label: GUI Hint
    dtype: gui_hint
    hide: part
value: ${ value }

asserts:
- ${value in (true, false)}

templates:
    var_make: self.${id} = ${id} = ${value}
    callbacks:
    - self.set_${id}(${value})
    - self._${id}_callback(${id})
    make: |-
        <%
            win = '_%s_check_box'%id
        %>
        ${win} = Qt.QCheckBox("${no_quotes(label,repr(id))}")
        self._${id}_choices = {True: ${true}, False: ${false}}
        self._${id}_choices_inv = dict((v,k) for k,v in self._${id}_choices.items())
        self._${id}_callback = lambda i: Qt.QMetaObject.invokeMethod(${win}, "setChecked", Qt.Q_ARG("bool", self._${id}_choices_inv[i]))
        self._${id}_callback(self.${id})
        ${win}.stateChanged.connect(lambda i: self.set_${id}(self._${id}_choices[bool(i)]))
        ${gui_hint() % win}

cpp_templates:
  includes: ['#include <QCheckBox>']
  declarations: |-
    QCheckBox *_${id};
    ${type.cpp_opts} _${id}_values[2] = {${false},${true}};
  var_make: ${id} = ${value};
  callbacks:
  - set_${id}(${value})

  make: |-
      <%
          win = '_%s'%id
      %>
      ${win} = new QCheckBox("${no_quotes(label,repr(id))}");
      if ( ${value} == ${true} ) {
          ${win}->setCheckState(Qt::Checked);
      }

      QObject::connect(${win},&QCheckBox::stateChanged, this, [this] () {this->set_${id}(_${id}_values[${win}->checkState()/2]);});

      ${gui_hint() % win}
documentation: |-
    This block creates a variable check box. Leave the label blank to use the variable id as the label.

    A check box selects between two values of similar type. The values do not necessarily need to be of boolean type.

    The GUI hint can be used to position the widget within the application. The hint is of the form [tab_id@tab_index]: [row, col, row_span, col_span]. Both the tab specification and the grid position are optional.

file_format: 1
