14 #include "xwavengraph.h"
16 #include "ui_graphnurlform.h"
17 #include "graphwidget.h"
20 #include <QPushButton>
23 #define OFSMODE (std::ios::out | std::ios::app | std::ios::ate)
27 XWaveNGraph::XWaveNGraph(
const char *name,
bool runtime,
FrmGraphNURL *item) :
28 XNode(name, runtime), m_btnDump(item->m_btnDump), m_graph(create<
XGraph> (
29 name, false)), m_dump(create<
XTouchableNode> (
"Dump", true)), m_filename(create<
31 item->m_graphwidget->setGraph(m_graph);
32 m_conFilename = xqcon_create<XFilePathConnector> (m_filename, item->m_edUrl, item->m_btnUrl,
33 "Data files (*.dat);;All files (*.*)",
true);
34 m_conDump = xqcon_create<XQButtonConnector> (m_dump, item->m_btnDump);
37 XWaveNGraph::XWaveNGraph(
const char *name,
bool runtime,
XQGraph *graphwidget,
38 QLineEdit *ed, QAbstractButton *btn, QPushButton *btndump) :
39 XNode(name, runtime), m_btnDump(btndump), m_graph(create<
XGraph> (name,
40 false)), m_dump(create<
XTouchableNode> (
"Dump", true)), m_filename(create<
43 m_conFilename = xqcon_create<XFilePathConnector> (m_filename, ed, btn,
44 "Data files (*.dat);;All files (*.*)",
true);
45 m_conDump = xqcon_create<XQButtonConnector> (m_dump, btndump);
48 void XWaveNGraph::init() {
50 m_lsnOnFilenameChanged = tr[ *filename()].onValueChanged().connectWeakly(
51 shared_from_this(), &XWaveNGraph::onFilenameChanged);
55 m_lsnOnIconChanged = tr[ *
this].onIconChanged().connectWeakly(
57 &XWaveNGraph::onIconChanged, XListener::FLAG_MAIN_THREAD_CALL
58 | XListener::FLAG_AVOID_DUP);
59 tr.mark(tr[ *
this].onIconChanged(),
false);
61 tr[ *dump()].setUIEnabled(
false);
62 tr[ *m_graph->persistence()] = 0.0;
63 tr[ *
this].clearPlots();
66 XWaveNGraph::~XWaveNGraph() {
71 XWaveNGraph::Payload::clearPoints() {
73 for(
int i = 0; i < numPlots(); ++i)
74 tr()[ *
plot(i)].points().clear();
76 shared_ptr<XGraph> graph(static_cast<XWaveNGraph*>( &
node())->graph());
77 tr().mark(tr()[ *graph].onUpdate(), graph.get());
80 XWaveNGraph::Payload::clearPlots() {
81 const auto &graph(static_cast<XWaveNGraph &>(node()).m_graph);
82 for(
auto it = m_plots.begin(); it != m_plots.end(); it++) {
83 graph->plots()->release(tr(), it->xyplot);
86 graph->axes()->release(tr(), m_axisw);
88 graph->axes()->release(tr(), m_axisz);
90 graph->axes()->release(tr(), m_axisy2);
93 tr()[ *m_axisx->label()] =
"";
95 tr()[ *m_axisy->label()] =
"";
103 XWaveNGraph::Payload::insertPlot(
const XString &label,
int x,
int y1,
int y2,
105 const auto &graph(static_cast<XWaveNGraph &>(node()).m_graph);
106 assert( (y1 < 0) || (y2 < 0) );
111 plot.colweight = weight;
115 if((m_colw >= 0) && (m_colw != weight))
122 tr()[ *graph->label()] = node().getLabel();
124 unsigned int plotnum = m_plots.size() + 1;
125 plot.xyplot = graph->plots()->create<
XXYPlot>(tr(), formatString(
"Plot%u",
126 plotnum).c_str(),
true, ref(tr()), graph);
128 tr()[ *plot.xyplot->label()] = label;
130 m_axisx = static_pointer_cast<
XAxis>(axes_list.at(0));
131 m_axisy = static_pointer_cast<
XAxis>(axes_list.at(1));
132 tr()[ *plot.xyplot->axisX()] = m_axisx;
133 tr()[ *m_axisx->label()] = m_labels[plot.colx];
134 if(plot.coly1 >= 0) {
135 tr()[ *plot.xyplot->axisY()] = m_axisy;
136 tr()[ *m_axisy->label()] = m_labels[plot.coly1];
138 tr()[ *plot.xyplot->maxCount()] = rowCount();
139 tr()[ *plot.xyplot->maxCount()] =
false;
140 tr()[ *plot.xyplot->clearPoints()].setUIEnabled(
false);
141 tr()[ *plot.xyplot->intensity()] = 1.0;
143 tr()[ *plot.xyplot->pointColor()] = clGreen;
144 tr()[ *plot.xyplot->lineColor()] = clGreen;
145 tr()[ *plot.xyplot->barColor()] = clGreen;
146 tr()[ *plot.xyplot->displayMajorGrid()] =
false;
151 m_axisz = graph->axes()->create<
XAxis>(tr(),
"Z Axis",
true,
152 XAxis::DirAxisZ,
true, ref(tr()), graph);
154 tr()[ *plot.xyplot->axisZ()] = m_axisz;
155 tr()[ *m_axisz->label()] = m_labels[plot.colz];
157 if(plot.colweight >= 0) {
159 m_axisw = graph->axes()->create<
XAxis>(tr(),
"Weight",
true,
160 XAxis::AxisWeight,
true, ref(tr()), graph);
162 tr()[ *m_axisw->autoScale()] =
false;
163 tr()[ *m_axisw->autoScale()].setUIEnabled(
false);
164 tr()[ *plot.xyplot->axisW()] = m_axisw;
165 tr()[ *m_axisw->label()] = m_labels[plot.colweight];
167 if(plot.coly2 >= 0) {
169 m_axisy2 = graph->axes()->create<
XAxis>(tr(),
"Y2 Axis",
true,
170 XAxis::DirAxisY,
true, ref(tr()), graph);
172 tr()[ *plot.xyplot->axisY()] = m_axisy2;
173 tr()[ *m_axisy2->label()] = m_labels[plot.coly2];
176 m_plots.push_back(plot);
180 XWaveNGraph::Payload::setColCount(
unsigned int n,
const char **labels) {
182 m_labels.resize(m_colcnt);
183 for(
unsigned int i = 0; i < n; i++) {
184 m_labels[i] = labels[i];
188 XWaveNGraph::Payload::setLabel(
unsigned int col,
const char *label) {
189 m_labels[col] = label;
192 XWaveNGraph::Payload::setRowCount(
unsigned int n) {
193 m_cols.resize(m_colcnt * n);
194 for(
auto it = m_plots.begin(); it != m_plots.end(); it++) {
195 tr()[ *it->xyplot->maxCount()] = n;
199 XWaveNGraph::Payload::cols(
unsigned int n) {
200 return rowCount() ? &(m_cols[rowCount() * n]) : 0;
203 XWaveNGraph::Payload::cols(
unsigned int n)
const {
204 return rowCount() ? &(m_cols[rowCount() * n]) : 0;
207 XWaveNGraph::Payload::weight()
const {
208 if(m_colw < 0)
return 0L;
213 XWaveNGraph::onIconChanged(
const Snapshot &shot,
bool v) {
214 if( !m_conDump->isAlive())
return;
216 m_btnDump->setIcon(QApplication::style()->
217 standardIcon(QStyle::SP_DialogSaveButton));
219 m_btnDump->setIcon(QApplication::style()->
220 standardIcon(QStyle::SP_BrowserReload));
227 if(m_stream.is_open())
231 (
const char*)QString(shot[ *filename()].to_str().c_str()).toLocal8Bit().data(),
235 if(m_stream.good()) {
236 m_lsnOnDumpTouched = tr[ *dump()].onTouch().connectWeakly(
237 shared_from_this(), &XWaveNGraph::onDumpTouched);
238 tr[ *dump()].setUIEnabled(
true);
241 m_lsnOnDumpTouched.reset();
242 tr[ *dump()].setUIEnabled(
false);
243 gErrPrint(i18n(
"Failed to open file."));
245 tr.mark(tr[ *
this].onIconChanged(),
false);
255 if( !m_stream.good())
return;
257 int rowcnt = shot[ *
this].rowCount();
258 int colcnt = shot[ *
this].colCount();
261 for(
unsigned int i = 0; i < colcnt; i++) {
262 m_stream << shot[ *
this].labels()[i] << KAME_DATAFILE_DELIMITER;
265 m_stream <<
"#at " << (XTime::now()).getTimeFmtStr(
266 "%Y/%m/%d %H:%M:%S") << std::endl;
268 auto &p(shot[ *
this]);
269 for(
unsigned int i = 0; i < rowcnt; i++) {
270 if( !p.weight() || (p.weight()[i] > 0)) {
271 for(
unsigned int j = 0; j < colcnt; j++) {
272 m_stream << p.cols(j)[i] << KAME_DATAFILE_DELIMITER;
274 m_stream << std::endl;
277 m_stream << std::endl;
283 tr.mark(tr[ *
this].onIconChanged(),
true);
288 for(
int i = 0; i < shot[ *
this].numPlots(); ++i) {
289 int rowcnt = shot[ *
this].rowCount();
290 double *colx = tr[ *
this].cols(shot[ *
this].colX(i));
292 if(shot[ *
this].colY1(i) >= 0)
293 coly = tr[ *
this].cols(shot[ *
this].colY1(i));
294 if(shot[ *
this].colY2(i) >= 0)
295 coly = tr[ *
this].cols(shot[ *
this].colY2(i));
296 double *colweight = (shot[ *
this].colWeight(i) >= 0) ? tr[ *
this].cols(shot[ *
this].colWeight(i)) : NULL;
297 double *colz = (shot[ *
this].colZ(i) >= 0) ? tr[ *
this].cols(shot[ *
this].colZ(i)) : NULL;
300 double weight_max = 0.0;
301 for(
int i = 0; i < rowcnt; i++)
302 weight_max = std::max(weight_max, colweight[i]);
303 tr[ *shot[ *
this].axisw()->maxValue()] = weight_max;
304 tr[ *shot[ *
this].axisw()->minValue()] = -0.4 * weight_max;
307 auto &points_plot(tr[ *shot[ *
this].plot(i)].points());
309 for(
int i = 0; i < rowcnt; ++i) {
316 coly[i], z, colweight[i]));
322 tr.mark(tr[ *m_graph].onUpdate(), m_graph.get());