Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

roastcoloredit.w 4.6KB

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