Ver código fonte

Add roast color edit widget

Neal Wilson 7 anos atrás
pai
commit
a7ba0a7ddb
2 arquivos alterados com 130 adições e 0 exclusões
  1. 128
    0
      src/roastcoloredit.w
  2. 2
    0
      src/typica.w

+ 128
- 0
src/roastcoloredit.w Ver arquivo

@@ -0,0 +1,128 @@
1
+@* Data entry for degree of roast data.
2
+
3
+\noindent Widgets for entering roast color data change appearance depending on
4
+the current configuration. It should always be possible to enter data manually,
5
+but if communications with the hardware producing these measurements is
6
+possible, there will also be a button available to run the samples directly
7
+from \pn{}.
8
+
9
+@<Class declarations@>=
10
+class RoastColorEdit : public QWidget@/
11
+{@/
12
+    @[Q_OBJECT@]@;
13
+    @[Q_PROPERTY(QString value READ value WRITE setValue)@]@;
14
+    public:@/
15
+        RoastColorEdit();
16
+        QString value();
17
+    @[public slots@]:@/
18
+        void setValue(const QString &color);
19
+    @[private slots@]:@/
20
+        void readColor();
21
+        void measureFinished();
22
+        void readFinished();
23
+    private:
24
+        QLineEdit *edit;
25
+        QNetworkReply *networkReply;
26
+};
27
+
28
+@ The constructor fills the widget with a |QLineEdit| or with that and a
29
+|QPushButton|, connecting the clicked signal as appropriate.
30
+
31
+@<RoastColorEdit implementation@>=
32
+RoastColorEdit::RoastColorEdit() : edit(new QLineEdit)
33
+{
34
+    QHBoxLayout *layout = new QHBoxLayout;
35
+    layout->setContentsMargins(0, 0, 0, 0);
36
+    layout->addWidget(edit);
37
+    QSettings settings;
38
+    if(settings.value("settings/color/javalytics/enable", false).toBool())
39
+    {
40
+        QPushButton *button = new QPushButton(tr("Measure"));
41
+        layout->addWidget(button);
42
+        connect(button, SIGNAL(clicked()), this, SLOT(readColor()));
43
+    }
44
+    setLayout(layout);
45
+}
46
+
47
+@ When the Measure button is clicked, two network operations are taken. First,
48
+an HTTP POST request is sent to request running a new sample.
49
+
50
+@<RoastColorEdit implementation@>=
51
+void RoastColorEdit::readColor()
52
+{
53
+    QSettings settings;
54
+    QUrl postData;
55
+    postData.addQueryItem("calscale", settings.value("settings/color/javalytics/scale", 1).toString());
56
+    postData.addQueryItem("webcontrol", "20");
57
+    QNetworkRequest request(QUrl("http://" + settings.value("settings/color/javalytics/address", "192.168.1.10").toString() + "/index.zhtml"));
58
+    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
59
+    networkReply = AppInstance->network->post(request, postData.encodedQuery());
60
+    connect(networkReply, SIGNAL(finished()), this, SLOT(measureFinished()));
61
+}
62
+
63
+@ When the device has responded to the POST request, it creates an HTTP GET
64
+request to obtain the latest data.
65
+
66
+@<RoastColorEdit implementation@>=
67
+void RoastColorEdit::measureFinished()
68
+{
69
+    QSettings settings;
70
+    networkReply->deleteLater();
71
+    networkReply = AppInstance->network->get(QNetworkRequest(QUrl("http://" + settings.value("settings/color/javalytics/address", "192.168.1.10").toString() + "/data.csv")));
72
+    connect(networkReply, SIGNAL(finished()), this, SLOT(readFinished()));
73
+}
74
+
75
+@ The GET request returns all of the data stored on the device as a CSV file,
76
+but for this control we only care about getting the most recent measurement, so
77
+the last line of the response is extracted. When split on commas the fields
78
+contain the following:
79
+
80
+\halign{\hfil # & # \hfil \cr
81
+Field Number & Description \cr
82
+0 & Date (MM/DD/YYYY) \cr
83
+1 & Time (hh:mm:ss AP) \cr
84
+2 & Result (the value of interest) \cr
85
+3 & Scale \cr
86
+4 & Custom field 1 \cr
87
+5 & Custom field 2 \cr
88
+6 & Custom field 3 \cr
89
+7 & Custom field 4 \cr
90
+8 & Batch number \cr
91
+9 & Samples averaged \cr}
92
+
93
+@<RoastColorEdit implementation@>=
94
+void RoastColorEdit::readFinished()
95
+{
96
+    QByteArray response = networkReply->readAll();
97
+    networkReply->deleteLater();
98
+    networkReply = 0;
99
+    edit->setText(response.split('\n').last().split(',').at(2));
100
+}
101
+
102
+@ Two methods provide access to the |QLineEdit|.
103
+
104
+@<RoastColorEdit implementation@>=
105
+void RoastColorEdit::setValue(const QString &color)
106
+{
107
+    edit->setText(color);
108
+}
109
+
110
+QString RoastColorEdit::value()
111
+{
112
+    return edit->text();
113
+}
114
+
115
+@ This control can be added to a box layout.
116
+
117
+@<Additional box layout elements@>=
118
+else if(currentElement.tagName() == "roastcoloredit")
119
+{
120
+    QBoxLayout *layout = qobject_cast<QBoxLayout *>(layoutStack->top());
121
+    RoastColorEdit *edit = new RoastColorEdit;
122
+    layout->addWidget(edit);
123
+}
124
+
125
+@ The implementation goes into typica.cpp.
126
+
127
+@<Class implementations@>=
128
+@<RoastColorEdit implementation@>

+ 2
- 0
src/typica.w Ver arquivo

@@ -20330,6 +20330,8 @@ app.registerDeviceConfigurationWidget("translation", TranslationConfWidget::stat
20330 20330
 
20331 20331
 @i user.w
20332 20332
 
20333
+@i roastcoloredit.w
20334
+
20333 20335
 @** Local changes.
20334 20336
 
20335 20337
 \noindent This is the end of \pn{} as distributed by its author. It is expected

Carregando…
Cancelar
Salvar