-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextDisplay.cpp
More file actions
140 lines (122 loc) · 3.83 KB
/
TextDisplay.cpp
File metadata and controls
140 lines (122 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2014-2017 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <QLabel>
#include <complex>
/***********************************************************************
* |PothosDoc Text Display
*
* The text display widget display's a formatted string.
* The display value can be set through one of the set*Value() slots.
* Display values can be strings, floating point, and integers.
*
* For arbitrary value types, use the generic setValue() slot.
* This slot uses the capability of Pothos::Object() to stringify.
*
* |category /Widgets
* |keywords text display label
*
* |param title The name of the value displayed by this widget
* |default "My Text Value"
* |widget StringEntry()
*
* |param formatStr [Formatter] The format string with a %1 to sub in the value.
* |default "%1"
* |widget StringEntry()
* |preview disable
*
* |param base [Base] The base used for integer formatting.
* |default 10
* |option [Binary] 2
* |option [Octal] 8
* |option [Decimal] 10
* |option [Hex] 16
* |preview disable
* |widget ComboBox(editable=true)
*
* |mode graphWidget
* |factory /widgets/text_display()
* |setter setTitle(title)
* |setter setFormatStr(formatStr)
* |setter setBase(base)
**********************************************************************/
class TextDisplay : public QLabel, public Pothos::Block
{
Q_OBJECT
public:
static Block *make(void)
{
return new TextDisplay();
}
TextDisplay(void):
_base(10)
{
this->setFormatStr("%1");
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, widget));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setTitle));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setFormatStr));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setBase));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setStringValue));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setFloatValue));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setComplexValue));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setIntValue));
this->registerCall(this, POTHOS_FCN_TUPLE(TextDisplay, setValue));
}
QWidget *widget(void)
{
return this;
}
void setTitle(const QString &title)
{
_title = title;
this->update();
}
void setFormatStr(const QString &formatStr)
{
if (not formatStr.contains("%1")) throw Pothos::Exception("Format string missing %1");
_formatStr = formatStr;
this->update();
}
void setBase(const size_t base)
{
_base = base;
}
void setStringValue(const QString &value)
{
_valueStr = _formatStr.arg(value);
this->update();
}
void setFloatValue(const double value)
{
_valueStr = _formatStr.arg(value);
this->update();
}
void setComplexValue(const std::complex<double> value)
{
_valueStr = QString("%1+%2j").arg(_formatStr.arg(value.real())).arg(_formatStr.arg(value.imag()));
this->update();
}
void setIntValue(const int value)
{
_valueStr = _formatStr.arg(value, 0, _base);
this->update();
}
void setValue(const Pothos::Object &obj)
{
_valueStr = _formatStr.arg(QString::fromStdString(obj.toString()));
this->update();
}
private:
void update(void)
{
const auto text = QString("<b>%1:</b> %2").arg(_title.toHtmlEscaped()).arg(_valueStr.toHtmlEscaped());
QMetaObject::invokeMethod(this, "setText", Qt::QueuedConnection, Q_ARG(QString, text));
}
size_t _base;
QString _title;
QString _valueStr;
QString _formatStr;
};
static Pothos::BlockRegistry registerTextDisplay(
"/widgets/text_display", &TextDisplay::make);
#include "TextDisplay.moc"