caltable.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 //---------------------------------------------------------------------
15 #include "caltable.h"
16 #include "measure.h"
17 #include <QPushButton>
18 #include <fstream>
19 #include "graph.h"
20 #include "graphwidget.h"
21 #include "xwavengraph.h"
22 #include "ui_caltableform.h"
23 #include "ui_graphnurlform.h"
24 
25 //---------------------------------------------------------------------
26 
27 XConCalTable::XConCalTable
28 (const shared_ptr<XThermometerList> &list, FrmCalTable *form)
29  : XQConnector(list, form),
30  m_list(list),
31  m_display(XNode::createOrphan<XTouchableNode>("display") ),
32  m_temp(XNode::createOrphan<XDoubleNode>("temp") ),
33  m_value(XNode::createOrphan<XDoubleNode>("value") ),
34  m_pForm(form),
35  m_waveform(new FrmGraphNURL(g_pFrmMain, Qt::Window)),
36  m_wave(XNode::createOrphan<XWaveNGraph>("Waveform", true, m_waveform.get())) {
37 
38  list->iterate_commit([=](Transaction &tr){
39  m_thermometer = XNode::createOrphan<XItemNode<XThermometerList, XThermometer> >(
40  "thermometer", false, ref(tr), list, true);
41  });
42 
43  m_conThermo = xqcon_create<XQComboBoxConnector> (m_thermometer,
44  (QComboBox *) form->cmbThermometer, Snapshot( *list));
45  m_conTemp = xqcon_create<XQLineEditConnector> (m_temp, form->edTemp, false);
46  m_conValue = xqcon_create<XQLineEditConnector> (m_value, form->edValue, false);
47  m_conDisplay = xqcon_create<XQButtonConnector> (m_display, form->btnDisplay);
48 
49  temp()->iterate_commit([=](Transaction &tr){
50  m_lsnTemp = tr[ *temp()].onValueChanged().connectWeakly(
51  shared_from_this(),
52  &XConCalTable::onTempChanged);
53  });
54  value()->iterate_commit([=](Transaction &tr){
55  m_lsnValue = tr[ *value()].onValueChanged().connectWeakly(
56  shared_from_this(),
57  &XConCalTable::onValueChanged);
58  });
59  display()->iterate_commit([=](Transaction &tr){
60  m_lsnDisplay = tr[ *display()].onTouch().connectWeakly(
61  shared_from_this(),
62  &XConCalTable::onDisplayTouched, XListener::FLAG_MAIN_THREAD_CALL);
63  });
64 
65  m_waveform->setWindowTitle(i18n("Thermometer Calibration"));
66  m_wave->iterate_commit([=](Transaction &tr){
67  const char *labels[] = {"Temp. [K]", "Value", "T(v(T))-T [K]"};
68  tr[ *m_wave].setColCount(3, labels);
69  tr[ *m_wave].insertPlot(labels[1], 0, 1);
70  tr[ *m_wave].insertPlot(labels[2], 0, -1, 2);
71  tr[ *tr[ *m_wave].plot(0)->label()] = i18n("Curve");
72  tr[ *tr[ *m_wave].plot(0)->drawPoints()] = false;
73  tr[ *tr[ *m_wave].plot(1)->label()] = i18n("Error");
74  tr[ *tr[ *m_wave].plot(1)->drawPoints()] = false;
75  shared_ptr<XAxis> axisx = tr[ *m_wave].axisx();
76  tr[ *axisx->logScale()] = true;
77  shared_ptr<XAxis> axisy = tr[ *m_wave].axisy();
78  tr[ *axisy->logScale()] = true;
79  m_wave->drawGraph(tr);
80  tr[ *m_wave].clearPoints();
81  });
82 }
83 
84 void
85 XConCalTable::onTempChanged(const Snapshot &shot, XValueNodeBase *) {
86  shared_ptr<XThermometer> thermo = ***thermometer();
87  if( !thermo) return;
88  double ret = thermo->getRawValue(shot[ *temp()]);
89  value()->iterate_commit([=](Transaction &tr){
90  tr[ *value()] = ret;
91  tr.unmark(m_lsnValue);
92  });
93 }
94 void
95 XConCalTable::onValueChanged(const Snapshot &shot, XValueNodeBase *) {
96  shared_ptr<XThermometer> thermo = ***thermometer();
97  if( !thermo) return;
98  double ret = thermo->getTemp(shot[ *value()]);
99  temp()->iterate_commit([=](Transaction &tr){
100  tr[ *temp()] = ret;
101  tr.unmark(m_lsnTemp);
102  });
103 }
104 void
105 XConCalTable::onDisplayTouched(const Snapshot &shot, XTouchableNode *) {
106  shared_ptr<XThermometer> thermo = ***thermometer();
107  if( !thermo) {
108  m_wave->iterate_commit([=](Transaction &tr){
109  tr[ *m_wave].clearPoints();
110  });
111  return;
112  }
113  const int length = 1000;
114  Snapshot shot_th( *thermo);
115  double step = (log(shot_th[ *thermo->tempMax()]) - log(shot_th[ *thermo->tempMin()])) / length;
116  m_wave->iterate_commit([=](Transaction &tr){
117  tr[ *m_wave].setRowCount(length);
118  double lt = log(shot_th[ *thermo->tempMin()]);
119  for(int i = 0; i < length; ++i) {
120  double t = exp(lt);
121  double r = thermo->getRawValue(t);
122  tr[ *m_wave].cols(0)[i] = t;
123  tr[ *m_wave].cols(1)[i] = r;
124  tr[ *m_wave].cols(2)[i] = thermo->getTemp(r) - t;
125  lt += step;
126  }
127  m_wave->drawGraph(tr);
128  });
129  m_waveform->showNormal();
130  m_waveform->raise();
131 }

Generated for KAME4 by  doxygen 1.8.3