#include "report.h" #include #include #include #include Report::Report(QWidget *parent, SicsConnection *conn) : QWidget(parent, "report") { QVBoxLayout *vbox = new QVBoxLayout(this, 0, 0, "report_vbox"); QHBoxLayout *hdr = new QHBoxLayout(0, 3, 3, "report_hdr"); this->conn = conn; editButton = new QPushButton("edit", this); hdr->addWidget(editButton); varEdit = new QLineEdit("tt.main", this); hdr->addWidget(varEdit); leaveButton = new QPushButton("close", this); hdr->addWidget(leaveButton); vbox->addLayout(hdr); textedit = new MyTextEdit(this, "report_edit"); textedit->setReadOnly(TRUE); textedit->setSelectionAttributes(1, QColor(255,255,128), FALSE); vbox->addWidget(textedit, 1); timer = new QTimer(this, "reportTimer"); connect(leaveButton, SIGNAL(clicked()), this, SIGNAL(leave())); connect(editButton, SIGNAL(clicked()), this, SLOT(editReport())); connect(timer, SIGNAL(timeout()), this, SLOT(updatePos())); } void Report::gotoTime(time_t from, time_t at, time_t to, bool silent) { char cmd[128]; int iret; QString line; time_t lastRead, t, dt; bool ok; QString message; QDateTime datim; int l; int p; if (isHidden()) { if (silent) return; openMe(); } if (variable != varEdit->text() || from != this->from || to != this->to) { this->to = to; this->from = from; times.clear(); variable = varEdit->text(); snprintf(cmd, sizeof cmd, "graph %ld %ld text np %ld %s", from, to, to - from, variable.latin1()); textedit->clear(); conn->command(cmd); iret = conn->getLine(line); if (iret <= 0) { message = ("can not get response 1"); goto err; } /* get returned absolute time */ lastRead = line.toLong(); iret = conn->getLine(line); if (iret <= 0) { message = "can not get response 2"; goto err; } if (!line.startsWith("*" + variable)) { message = "response '" + line + "' must start with '*" + variable + "'"; goto err; } t = 0; for (;;) { iret = conn->getLine(line); if (iret <= 0) { message = "can not get line"; goto err; } //printf("> %s\n", line.latin1()); if (line.startsWith("*")) { break; } dt = line.section(' ', 0, 0).toLong(&ok); if (!ok) { message = "bad timestep"; goto err; } t += dt; times.append(t); datim.setTime_t(t); textedit->appendHtml("" + datim.toString("hh:mm:ss ") + ""); textedit->appendText( line.section(' ', 1).replace("\\n","\n").replace("\\t","\t").replace("\\\\","\\") +"\n" ); } textedit->append("\n"); pos = -1; } l = times.count(); p = l - 1; for (int ipos = 0; ipos < l; ipos++) { if (times[ipos] >= at) { p = ipos - 1; break; } } if (p < 0) p = 0; if (pos < 0) { pos = l; } setCursorPos(p); timer->start(200); return; err: textedit->append("error: "+message); } void Report::setCursorPos(int newpos) { int index; textedit->setCursorPosition(newpos, 0); if (newpos > pos) { textedit->moveCursor(QTextEdit::MoveDown, false); textedit->moveCursor(QTextEdit::MoveDown, false); } else { textedit->moveCursor(QTextEdit::MoveUp, false); textedit->moveCursor(QTextEdit::MoveUp, false); } textedit->getCursorPosition(&posc, &index); pos = newpos; textedit->setSelection(pos, 0, pos+1, 0, 1); textedit->setCursorPosition(posc, 0); textedit->ensureCursorVisible(); } void Report::updatePos() { int para, index; textedit->getCursorPosition(¶, &index); if (para == pos) return; if (para == posc && index == 0) { // cursor repositioning pending posc = pos; textedit->setCursorPosition(posc, 0); textedit->ensureCursorVisible(); } else { setCursorPos(para); } if (pos < (int)times.count()) { setMarker(times[pos]); } } void Report::editReport() { QString text; if (textedit->isReadOnly()) { textedit->setReadOnly(FALSE); timer->stop(); setMarker(0); } else { printf("%s\n", textedit->text().latin1()); textedit->setText(textedit->text()); } }