id: variable_qtgui_label
label: QT GUI Label
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: [eng_notation.str_to_num, int, str, bool, eval]
        str: [eng_notation.num_to_str, str, str, str, repr]
    hide: part
-   id: value
    label: Default Value
    dtype: ${ type }
    default: '0'
-   id: formatter
    label: Formatter
    dtype: raw
    default: None
    hide: part
-   id: gui_hint
    label: GUI Hint
    dtype: gui_hint
    hide: part
value: ${ value }

templates:
    imports: |-
        from gnuradio import eng_notation
    var_make: self.${id} = ${id} = ${value}
    callbacks:
    - self.set_${id}(${value})
    - Qt.QMetaObject.invokeMethod(self._${id}_label, "setText", Qt.Q_ARG("QString",
        str(self._${id}_formatter(${id}))))
    make: |-
        <%
            win = 'self._%s_tool_bar'%id
        %>\
        ${win} = Qt.QToolBar(self)

        if ${formatter}:
            self._${id}_formatter = ${formatter}
        else:
            self._${id}_formatter = lambda x: ${type.str}(x)

        ${win}.addWidget(Qt.QLabel("${no_quotes(label,repr(id))}"))
        self._${id}_label = Qt.QLabel(str(self._${id}_formatter(self.${id})))
        self._${id}_tool_bar.addWidget(self._${id}_label)
        ${gui_hint() % win}

cpp_templates:
    includes: [ '#include <QLabel>','#include <QMetaObject>', '#include <QString>' ]
    declarations: QLabel *_${id}_label;
    var_make: ${id} = ${value};
    callbacks:
    - set_${id}(${value})
    - QMetaObject::invokeMethod(this->_${id}_label, "setText", Q_ARG(QString, QString("${no_quotes(label)}\%1").arg(${id})))
    link: ['gnuradio::gnuradio-qtgui']
    make: |-
        <%
            win = 'this->_%s_label'%id
        %>\
        // Todo: use formatter
        this->_${id}_label = new QLabel(QString("${no_quotes(label,repr(id))}") +QString(": ") + QString("%1").arg(${value}));

        ${gui_hint() % win}

documentation: |-
    This block creates a variable with a label widget for text. Leave the label blank to use the variable id as the label.

    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
