#include "settings.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "utils.h" void SetLength(QLineEdit *lineEdit, int min, int max) { lineEdit->setMaxLength(max); int w0 = lineEdit->fontMetrics().width('0'); int w = lineEdit->frameWidth() * 2 + 6; lineEdit->setMinimumWidth(w + w0 * min); lineEdit->setMaximumWidth(w + w0 * max); } Settings::Settings(QWidget* parent) : QWidget(parent) { QHBoxLayout *buttonLayout; QVBoxLayout *vbox; QString str; time_t now; struct tm date; vbox = new QVBoxLayout(this, 0, 0, "vBox"); setName("Settings"); table = new QScrollView(this, "table"); form = new QWidget(table->viewport(), "form"); formVL = new QVBoxLayout(form, 0, 0, "formVL"); formLayout = new QGridLayout(0, 3, 3, 6, 3); formVL->addLayout(formLayout); formVL->addStretch(1); /* formBox = new QHBox(table->viewport(), "formBox"); form = new QGrid(4, formBox, "form"); */ table->addChild(form); table->setHScrollBarMode(QScrollView::AlwaysOff); buttonLayout = new QHBoxLayout(0, 3, 3, "buttonLayout"); newButton = new QPushButton(this, "newButton"); newButton->setText("New Curve"); buttonLayout->addWidget(newButton); /* autoButton = new QPushButton(this, "autoButton"); autoButton->setText("Auto"); buttonLayout->addWidget(autoButton); */ cancelButton = new QPushButton(this, "cancelButton"); cancelButton->setText("Cancel"); buttonLayout->addWidget(cancelButton); okButton = new QPushButton(this, "okButton"); okButton->setText("OK"); buttonLayout->addWidget(okButton); okButton->setDefault(true); buttonLayout->addStretch(1); time(&now); date = *localtime(&now); buttonLayout->addWidget(new QLabel("# Days", this)); rLE = new QLineEdit(this); SetLength(rLE, 2, 2); rLE->setText(str); rLE->setValidator(new QIntValidator(0, 99, this)); buttonLayout->addWidget(rLE); buttonLayout->addWidget(new QLabel("starting from", this)); dLE = new QLineEdit(this); SetLength(dLE, 2, 2); str.sprintf("%d", date.tm_mday); dLE->setText(str); dLE->setValidator(new QIntValidator(0, 31, this)); buttonLayout->addWidget(dLE); buttonLayout->addWidget(new QLabel(".", this)); mLE = new QLineEdit(this); SetLength(mLE, 2, 2); str.sprintf("%d", date.tm_mon+1); mLE->setText(str); mLE->setValidator(new QIntValidator(1, 12, this)); buttonLayout->addWidget(mLE); buttonLayout->addWidget(new QLabel(".", this)); yLE = new QLineEdit(this); SetLength(yLE, 4, 4); str.sprintf("%d", date.tm_year+1900); yLE->setText(str); yLE->setValidator(new QIntValidator(2001, date.tm_year+1900, this)); buttonLayout->addWidget(yLE); hrLE = new QLineEdit(this); SetLength(hrLE, 5, 5); hrLE->setText("00:00"); buttonLayout->addWidget(hrLE); vbox->addLayout(buttonLayout); vbox->addWidget(table, 1); //vbox->addStretch(1); formLayout->addWidget(new QLabel("Curve", form), 0, 0); formLayout->addWidget(new QLabel("Label", form), 0, 1); formLayout->addWidget(new QLabel("Unit", form), 0, 2); formLayout->addWidget(new QLabel("Color", form), 0, 3); /* new QLabel("Curve", form); new QLabel("Label", form); new QLabel("Plot", form); new QLabel("Color", form); */ row = 1; connect(newButton, SIGNAL(clicked()), this, SLOT(addEmpty())); connect(okButton, SIGNAL(clicked()), this, SLOT(ok())); /* connect(autoButton, SIGNAL(clicked()), this, SLOT(autoCurves())); */ connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); connect(rLE, SIGNAL(textChanged(const QString &)), this, SLOT(checkRange(const QString &))); /* connect(dLE, SIGNAL(textChanged(const QString &)), this, SLOT(calcDate(const QString &))); connect(mLE, SIGNAL(textChanged(const QString &)), this, SLOT(calcDate(const QString &))); connect(yLE, SIGNAL(textChanged(const QString &)), this, SLOT(calcDate(const QString &))); */ clearWState(WState_Polished); } void Settings::updateHeight() { form->setMinimumHeight(table->visibleHeight()); } void Settings::resizeEvent(QResizeEvent *e) { updateHeight(); form->setFixedWidth(table->visibleWidth()); QWidget::resizeEvent(e); } void Settings::showIt(bool yes) { if (yes) { clear(); putCurves(&set->dataList); show(); } else { hide(); } } #define YEAR (24*3600*360) void Settings::checkRange(const QString &str0) { if (str0.compare("") == 0) { dLE->setEnabled(false); mLE->setEnabled(false); yLE->setEnabled(false); hrLE->setEnabled(false); } else { dLE->setEnabled(true); mLE->setEnabled(true); yLE->setEnabled(true); hrLE->setEnabled(true); } } void Settings::putCurve(const QString &name, const QString &label, const QString &plotName, long colorIndex) { QLineEdit *n, *p; ColorMenu *cm; unsigned int idx; QString colortext; idx = row - 1; if (idx < names.count()) { n = names.at(idx); } else { n = new QLineEdit(form); SetLength(n, 8, 64); formLayout->addWidget(n, row, 0); names.append(n); } n->setText(name); n->show(); if (idx < labels.count()) { p = labels.at(idx); } else { p = new QLineEdit(form); SetLength(p, 8, 64); formLayout->addWidget(p, row, 1); labels.append(p); } p->setText(label); p->show(); if (idx < plots.count()) { p = plots.at(idx); } else { p = new QLineEdit(form); SetLength(p, 4, 8); formLayout->addWidget(p, row, 2); plots.append(p); } p->setText(plotName); p->show(); Convert2ColorName(colorIndex, colortext); if (idx < colors.count()) { cm = colors.at(idx); cm->setCurrentItem(0); cm->setCurrentText(colortext); } else { cm = new ColorMenu(colortext, form); cm->setLength(12, 32); formLayout->addWidget(cm, row, 3); colors.append(cm); } cm->show(); row++; } void Settings::putCurves(QPtrList *dataList) { SeaData *c; for (c = dataList->first(); c != 0; c = dataList->next()) { putCurve(c->name, c->label, c->plotName, c->style); } updateHeight(); } void Settings::addEmpty() { putCurve("","","",-1); updateHeight(); /* names.last()->show(); labels.last()->show(); plots.last()->show(); colors.last()->show(); */ } void Settings::apply() { int i, n; long days; time_t t; struct tm date; QStringList hr_min; if (rLE->text().compare("") != 0) { days = rLE->text().toLong(); time(&t); date = *localtime(&t); date.tm_mday = dLE->text().toInt(); date.tm_mon = mLE->text().toInt() - 1; date.tm_year = yLE->text().toInt() - 1900; hr_min = QStringList::split(":", hrLE->text()); date.tm_hour = (int)hr_min[0].toFloat(); if (hr_min.size() > 1) { date.tm_min = (int)hr_min[1].toFloat(); } else { date.tm_min = 0; } date.tm_sec = 0; date.tm_isdst = -1; set->setTimeRange(mktime(&date), 24*3600*QMAX(1, days)); set->autoCurves(); } else { set->clrDataList(); n = names.count(); for (i = 0; i < n; i++) { //printf("%s\n", names.at(i)->text().latin1()); set->insertData(names.at(i)->text(), labels.at(i)->text(), plots.at(i)->text(), colors.at(i)->getColorIndex()); } set->finishDataList(); } } void Settings::ok() { apply(); leaveSettings(); } void Settings::cancel() { leaveSettings(); } void Settings::clear() { int i, n; n = names.count(); for (i = 0; i < n; i++) { names.at(i)->setText(""); plots.at(i)->setText(""); labels.at(i)->setText(""); colors.at(i)->setValue(0); } row = 1; rLE->setText(""); } void Settings::autoCurves() { clear(); set->autoCurves(); putCurves(&set->dataList); }