File indexing completed on 2024-12-22 04:18:18

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "datarange.h"
0014 #include "dialogdefaults.h"
0015 
0016 namespace Kst {
0017 
0018 DataRange::DataRange(QWidget *parent)
0019   : QWidget(parent) {
0020   setupUi(this);
0021 
0022   connect(_countFromEnd, SIGNAL(toggled(bool)), this, SLOT(countFromEndChanged()));
0023   connect(_readToEnd, SIGNAL(toggled(bool)), this, SLOT(readToEndChanged()));
0024   connect(_doSkip, SIGNAL(toggled(bool)), this, SLOT(doSkipChanged()));
0025 
0026   connect(_start, SIGNAL(textEdited(QString)), this, SLOT(startChanged()));
0027   connect(_range, SIGNAL(textEdited(QString)), this, SLOT(rangeChanged()));
0028   connect(_last, SIGNAL(textEdited(QString)), this, SLOT(lastChanged()));
0029   connect(_skip, SIGNAL(valueChanged(int)), this, SIGNAL(modified()));
0030   connect(_doFilter, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0031   connect(_countFromEnd, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0032   connect(_readToEnd, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0033   connect(_doSkip, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0034   connect(_startUnits, SIGNAL(currentIndexChanged(int)), this, SLOT(unitsChanged()));
0035   connect(_rangeUnits, SIGNAL(currentIndexChanged(int)), this, SLOT(unitsChanged()));
0036 
0037   _controlField0 = Range;
0038   _controlField1 = Start;
0039 }
0040 
0041 
0042 DataRange::~DataRange() {
0043 }
0044 
0045 
0046 void DataRange::clearValues() {
0047   _start->clear();
0048   _range->clear();
0049   _skip->clear();
0050   _doFilter->setCheckState(Qt::PartiallyChecked);
0051   _readToEnd->setCheckState(Qt::PartiallyChecked);
0052   _doSkip->setCheckState(Qt::PartiallyChecked);
0053 }
0054 
0055 
0056 qreal DataRange::start() const {
0057   return _start->text().toDouble();
0058 }
0059 
0060 
0061 bool DataRange::startDirty() const {
0062   return !_start->text().isEmpty();
0063 }
0064 
0065 
0066 void DataRange::setStart(qreal start, bool callUpdateFields) {
0067   _start->setText(QString::number(start, 'g', 12));
0068   if (callUpdateFields) {
0069     updateFields(None);
0070   }
0071 }
0072 
0073 qreal DataRange::last() const {
0074   return _last->text().toDouble();
0075 }
0076 
0077 
0078 bool DataRange::lastDirty() const {
0079   return !_last->text().isEmpty();
0080 }
0081 
0082 
0083 void DataRange::setLast(qreal last, bool callUpdateFields) {
0084   _last->setText(QString::number(last, 'g', 12));
0085   if (callUpdateFields) {
0086     updateFields(None);
0087   }
0088 }
0089 
0090 void DataRange::clearIndexList() {
0091   _startUnits->clear();
0092   _rangeUnits->clear();
0093 }
0094 
0095 
0096 void DataRange::updateIndexList(const QStringList &indexFields) {
0097   _startUnits->clear();
0098   _startUnits->addItems(indexFields);
0099   setStartUnits(_requestedStartUnits);
0100   _rangeUnits->clear();
0101   _rangeUnits->addItems(indexFields);
0102   setRangeUnits(_requestedRangeUnits);
0103 }
0104 
0105 
0106 QString DataRange::startUnits() const {
0107   return _startUnits->currentText();
0108 }
0109 
0110 
0111 int DataRange::startUnitsIndex() const {
0112   return _startUnits->currentIndex();
0113 }
0114 
0115 
0116 void DataRange::setStartUnits(const QString &startUnits) {
0117   _requestedStartUnits = startUnits;
0118   int i = _startUnits->findText(startUnits);
0119   if (i>=0) {
0120     _startUnits->setCurrentIndex(i);
0121   }
0122 }
0123 
0124 qreal DataRange::range() const {
0125   return _range->text().toDouble();
0126 }
0127 
0128 
0129 int DataRange::rangeUnitsIndex()  const {
0130   return _rangeUnits->currentIndex();
0131 }
0132 
0133 
0134 bool DataRange::rangeDirty() const {
0135   return !_range->text().isEmpty();
0136 }
0137 
0138 
0139 void DataRange::setRange(qreal range, bool callUpdateFields) {
0140   _range->setText(QString::number(range));
0141   if (callUpdateFields) {
0142     updateFields(None);
0143   }
0144 }
0145 
0146 
0147 QString DataRange::rangeUnits() const {
0148   return _rangeUnits->currentText();
0149 }
0150 
0151 
0152 void DataRange::setRangeUnits(const QString &rangeUnits) {
0153   _requestedRangeUnits = rangeUnits;
0154   int i = _rangeUnits->findText(rangeUnits);
0155   if (i>=0) {
0156     _rangeUnits->setCurrentIndex(i);
0157   } else {
0158     _rangeUnits->setCurrentIndex(0);
0159   }
0160 }
0161 
0162 
0163 int DataRange::skip() const {
0164   return _skip->value();
0165 }
0166 
0167 
0168 bool DataRange::skipDirty() const {
0169   return !_skip->text().isEmpty();
0170 }
0171 
0172 
0173 void DataRange::setSkip(int skip) {
0174   _skip->setValue(skip);
0175 }
0176 
0177 
0178 bool DataRange::countFromEnd() const {
0179   return _countFromEnd->isChecked();
0180 }
0181 
0182 
0183 void DataRange::setCountFromEnd(bool countFromEnd) {
0184   _countFromEnd->setChecked(countFromEnd);
0185   updateFields(None);
0186 }
0187 
0188 
0189 bool DataRange::countFromEndDirty() const {
0190   return _readToEnd->checkState() == Qt::PartiallyChecked;
0191 }
0192 
0193 
0194 bool DataRange::readToEnd() const {
0195   return _readToEnd->isChecked();
0196 }
0197 
0198 
0199 bool DataRange::readToEndDirty() const {
0200   return _readToEnd->checkState() == Qt::PartiallyChecked;
0201 }
0202 
0203 
0204 void DataRange::setReadToEnd(bool readToEnd) {
0205   _readToEnd->setChecked(readToEnd);
0206   updateFields(None);
0207 }
0208 
0209 
0210 bool DataRange::doSkip() const {
0211   return _doSkip->isChecked();
0212 }
0213 
0214 
0215 bool DataRange::doSkipDirty() const {
0216   return _doSkip->checkState() == Qt::PartiallyChecked;
0217 }
0218 
0219 
0220 void DataRange::setDoSkip(bool doSkip) {
0221   _doSkip->setChecked(doSkip);
0222 }
0223 
0224 
0225 bool DataRange::doFilter() const {
0226   return _doFilter->isChecked();
0227 }
0228 
0229 
0230 bool DataRange::doFilterDirty() const {
0231   return _doFilter->checkState() == Qt::PartiallyChecked;
0232 }
0233 
0234 
0235 void DataRange::setDoFilter(bool doFilter) {
0236   _doFilter->setChecked(doFilter);
0237 }
0238 
0239 
0240 void DataRange::countFromEndChanged() {
0241   if (countFromEnd()) {
0242     setReadToEnd(false);
0243   }
0244 
0245   updateFields(None);
0246 
0247 }
0248 
0249 
0250 void DataRange::readToEndChanged() {
0251   if (readToEnd()) {
0252     setCountFromEnd(false);
0253   }
0254 
0255   updateFields(None);
0256 
0257 }
0258 
0259 
0260 void DataRange::unitsChanged() {
0261   updateFields(None);
0262 }
0263 
0264 
0265 void DataRange::doSkipChanged() {
0266   _skip->setEnabled(doSkip());
0267   _doFilter->setEnabled(doSkip());
0268 }
0269 
0270 // control field logic:
0271 // the last one changed, other than this one, should be the control field
0272 // do we need a history?
0273 // F0 R -> L
0274 // F0 L -> R
0275 // R L -> F0
0276 // R F0 -> L
0277 
0278 void DataRange::startChanged() {
0279   updateFields(Start);
0280   emit modified();
0281 }
0282 
0283 
0284 void DataRange::lastChanged() {
0285   updateFields(Last);
0286   emit modified();
0287 }
0288 
0289 
0290 void DataRange::rangeChanged() {
0291   updateFields(Range);
0292   emit modified();
0293 }
0294 
0295 
0296 void DataRange::updateFields(ControlField cf) {
0297 
0298   bool enable_last = (_rangeUnits->currentIndex() == _startUnits->currentIndex());
0299   enable_last &= !readToEnd();
0300   enable_last &= !countFromEnd();
0301 
0302 
0303   _last->setEnabled(enable_last);
0304   _lastLabel->setEnabled(enable_last);
0305 
0306   _start->setEnabled(!countFromEnd());
0307   _startLabel->setEnabled(!countFromEnd());
0308   _startUnits->setEnabled(!countFromEnd());
0309   _range->setEnabled(!readToEnd());
0310   _rangeLabel->setEnabled(!readToEnd());
0311   _rangeUnits->setEnabled(!readToEnd());
0312 
0313   if ((cf!=None) && (cf != _controlField1)) {
0314     _controlField0 = _controlField1;
0315     _controlField1 = cf;
0316   }
0317 
0318   // don't do anything if it wouldn't make sense to.
0319   if (readToEnd() || countFromEnd()) {
0320     return;
0321   } 
0322 
0323   if (startUnits() != (rangeUnits())) {
0324     return;
0325   }
0326   
0327   if ((_controlField0 != Start) && (_controlField1 != Start)) {
0328     _start->setText(QString::number(last() - range() + 1, 'g', 12));
0329   } else if ((_controlField0 != Last) && (_controlField1 != Last)) {
0330     _last->setText(QString::number(start() + range() - 1, 'g', 12));
0331   } else if ((_controlField0 != Range) && (_controlField1 != Range)) {
0332     _range->setText(QString::number(last() - start() + 1, 'g', 12));
0333   }
0334 }
0335 
0336 
0337 void DataRange::setWidgetDefaults() {
0338   //FIXME Do we need a V->readLock() here?
0339   dialogDefaults().setValue("vector/range", range());
0340   dialogDefaults().setValue("vector/start", start());
0341   dialogDefaults().setValue("vector/countFromEnd", countFromEnd());
0342   dialogDefaults().setValue("vector/readToEnd", readToEnd());
0343   dialogDefaults().setValue("vector/skip", skip());
0344   dialogDefaults().setValue("vector/doSkip", doSkip());
0345   dialogDefaults().setValue("vector/doAve", doFilter());
0346   dialogDefaults().setValue("vector/rangeUnits", rangeUnits());
0347   dialogDefaults().setValue("vector/startUnits", rangeUnits());
0348 }
0349 
0350 void DataRange::loadWidgetDefaults() {
0351   setRange(dialogDefaults().value("vector/range", 1).toInt());
0352   setStart(dialogDefaults().value("vector/start", 0).toInt());
0353   setCountFromEnd(dialogDefaults().value("vector/countFromEnd",false).toBool());
0354   setReadToEnd(dialogDefaults().value("vector/readToEnd",true).toBool());
0355   setSkip(dialogDefaults().value("vector/skip", 0).toInt());
0356   setDoSkip(dialogDefaults().value("vector/doSkip", false).toBool());
0357   setDoFilter(dialogDefaults().value("vector/doAve",false).toBool());
0358   setRangeUnits(dialogDefaults().value("vector/rangeUnits",tr("frames")).toString());
0359   setStartUnits(dialogDefaults().value("vector/startUnits",tr("frames")).toString());
0360 }
0361 
0362 bool DataRange::rangeIsValid() {
0363   if (readToEnd()) {
0364     return true;
0365   } else {
0366     return (range()>1);
0367   }
0368 }
0369 
0370 }
0371 
0372 // vim: ts=2 sw=2 et