File indexing completed on 2024-12-22 04:17:37

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 "markerstab.h"
0014 
0015 #include "plotdefines.h"
0016 
0017 #include <QMessageBox>
0018 
0019 namespace Kst {
0020 
0021 MarkersTab::MarkersTab(QWidget *parent)
0022   : DialogTab(parent) {
0023 
0024   setupUi(this);
0025   setTabTitle(tr("Markers"));
0026 
0027   _markerLineStyle->addItem(tr("Solid Line", "Pen type"), (int)Qt::SolidLine);
0028   _markerLineStyle->addItem(tr("Dash Line", "Pen type"), (int)Qt::DashLine);
0029   _markerLineStyle->addItem(tr("Dot Line", "Pen type"), (int)Qt::DotLine);
0030   _markerLineStyle->addItem(tr("Dash Dot Line", "Pen type"), (int)Qt::DashDotLine);
0031   _markerLineStyle->addItem(tr("Dash Dot Dot Line", "Pen type"), (int)Qt::DashDotDotLine);
0032   _markerLineStyle->addItem(tr("Custom Dash Line", "Pen type"), (int)Qt::CustomDashLine);
0033 
0034   connect(_curveMarkers, SIGNAL(stateChanged(int)), this, SLOT(update()));
0035   connect(_vectorMarkers, SIGNAL(stateChanged(int)), this, SLOT(update()));
0036   connect(_currentMarkersList, SIGNAL(itemSelectionChanged()), this, SLOT(update()));
0037   connect(_newMarker, SIGNAL(textChanged(QString)), this, SLOT(update()));
0038 
0039   connect(_markerLineStyle, SIGNAL(currentIndexChanged(int)), this, SIGNAL(modified()));
0040   connect(_markerLineColor, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
0041   connect(_curveMarkers, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
0042   connect(_vectorMarkers, SIGNAL(stateChanged(int)), this, SIGNAL(modified()));
0043   connect(_vector, SIGNAL(selectionChanged(QString)), this, SIGNAL(modified()));
0044   connect(_curve, SIGNAL(selectionChanged(QString)), this, SIGNAL(modified()));
0045   connect(_risingEdge, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0046   connect(_fallingEdge, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0047   connect(_both, SIGNAL(toggled(bool)), this, SIGNAL(modified()));
0048   connect(_markerLineWidth, SIGNAL(valueChanged(double)), this, SIGNAL(modified()));
0049 
0050   connect(_addMarker, SIGNAL(clicked()), this, SLOT(add()));
0051   connect(_removeMarker, SIGNAL(clicked()), this, SLOT(remove()));
0052   connect(_clearMarkers, SIGNAL(clicked()), this, SLOT(clear()));
0053 
0054   connect(this, SIGNAL(modified()), this, SLOT(setDirty()));
0055 
0056   update();
0057 
0058 }
0059 
0060 
0061 MarkersTab::~MarkersTab() {
0062 }
0063 
0064 
0065 void MarkersTab::setObjectStore(ObjectStore *store) {
0066   _vector->setObjectStore(store);
0067   _curve->setObjectStore(store);
0068 }
0069 
0070 
0071 void MarkersTab::update() {
0072   _curve->setEnabled(_curveMarkers->isChecked());
0073   _risingEdge->setEnabled(_curveMarkers->isChecked());
0074   _fallingEdge->setEnabled(_curveMarkers->isChecked());
0075   _both->setEnabled(_curveMarkers->isChecked());
0076   _vector->setEnabled(_vectorMarkers->isChecked());
0077 
0078   _removeMarker->setEnabled(_currentMarkersList->selectedItems().count() > 0);
0079   _clearMarkers->setEnabled(_currentMarkersList->count() > 0);
0080   _addMarker->setEnabled(!_newMarker->text().isEmpty());
0081 }
0082 
0083 
0084 Qt::PenStyle MarkersTab::lineStyle() const {
0085   return Qt::PenStyle(_markerLineStyle->itemData(_markerLineStyle->currentIndex()).toInt());
0086 }
0087 
0088 
0089 void MarkersTab::setLineStyle(Qt::PenStyle style) {
0090   _markerLineStyle->setCurrentIndex(_markerLineStyle->findData(QVariant((int)style)));
0091 }
0092 
0093 
0094 QColor MarkersTab::lineColor() const {
0095   return _markerLineColor->color();
0096 }
0097 
0098 
0099 void MarkersTab::setLineColor(const QColor &color) {
0100   _markerLineColor->setColor(color);
0101 }
0102 
0103 
0104 double MarkersTab::lineWidth() const {
0105   return _markerLineWidth->value();
0106 }
0107 
0108 
0109 void MarkersTab::setLineWidth(const double width) {
0110   _markerLineWidth->setValue(width);
0111 }
0112 
0113 
0114 bool MarkersTab::isCurveSource() const {
0115   return _curveMarkers->isChecked();
0116 }
0117 
0118 
0119 void MarkersTab::setCurveSource(const bool enabled) {
0120   _curveMarkers->setChecked(enabled);
0121 }
0122 
0123 
0124 bool MarkersTab::isVectorSource() const {
0125   return _vectorMarkers->isChecked();
0126 }
0127 
0128 
0129 void MarkersTab::setVectorSource(const bool enabled) {
0130   _vectorMarkers->setChecked(enabled);
0131 }
0132 
0133 
0134 VectorPtr MarkersTab::vector() const {
0135   return _vector->selectedVector();
0136 }
0137 
0138 
0139 void MarkersTab::setVector(const VectorPtr vector) {
0140   _vector->setSelectedVector(vector);
0141 }
0142 
0143 
0144 PlotMarkers::CurveMarkerMode MarkersTab::curveMarkerMode() const {
0145   if (_risingEdge->isChecked()) {
0146     return PlotMarkers::RisingEdge;
0147   } else if (_fallingEdge->isChecked()) {
0148     return PlotMarkers::FallingEdge;
0149   } else {
0150     return PlotMarkers::BothEdges;
0151   }
0152 }
0153 
0154 
0155 void MarkersTab::setCurveMarkerMode(const PlotMarkers::CurveMarkerMode mode) {
0156   if (mode == PlotMarkers::RisingEdge) {
0157     _risingEdge->setChecked(true);
0158   } else if (mode == PlotMarkers::FallingEdge) {
0159     _fallingEdge->setChecked(true);
0160   } else {
0161     _both->setChecked(true);
0162   }
0163 }
0164 
0165 
0166 QList<double> MarkersTab::markers() const {
0167   QList<double> markerList;
0168   foreach(QListWidgetItem *item, _currentMarkersList->findItems("*", Qt::MatchWildcard)) {
0169     markerList << item->text().toDouble();
0170   }
0171   return markerList;
0172 }
0173 
0174 
0175 void MarkersTab::setMarkers(const QList<double> &markers) {
0176   QStringList markerList;
0177   foreach(double marker, markers) {
0178     markerList << QString::number(marker, 'g', MARKER_LABEL_PRECISION);
0179   }
0180   _currentMarkersList->clear();
0181   _currentMarkersList->addItems(markerList);
0182 }
0183 
0184 
0185 CurvePtr MarkersTab::curve() const {
0186   return _curve->selectedCurve();
0187 }
0188 
0189 
0190 void MarkersTab::setCurve(CurvePtr curve) {
0191   _curve->setSelectedCurve(curve);
0192 }
0193 
0194 
0195 void MarkersTab::add() {
0196   if (!_newMarker->text().isEmpty()) {
0197     bool ok;
0198     double newMarkerValue = _newMarker->text().toDouble(&ok);
0199 
0200     if (ok) {
0201       int i = 0;
0202       QString newMarkerString;
0203 
0204       newMarkerString.setNum(newMarkerValue, 'g', MARKER_LABEL_PRECISION);
0205       while (i < _currentMarkersList->count() && _currentMarkersList->item(i)->text().toDouble() < newMarkerValue) {
0206         i++;
0207       }
0208       if (i == _currentMarkersList->count()) {
0209         _currentMarkersList->addItem(newMarkerString);
0210         _newMarker->clear();
0211         update();
0212         emit modified();
0213       } else if (newMarkerValue != _currentMarkersList->item(i)->text().toDouble()) {
0214         _currentMarkersList->insertItem(i, newMarkerString);
0215         _newMarker->clear();
0216         update();
0217         emit modified();
0218       } else {
0219         QMessageBox::warning(this, tr("Kst"), tr("A plot marker with equal (or very close) value already exists."));
0220       }
0221     } else {
0222         QMessageBox::warning(this, tr("Kst"), tr("The text you have entered is not a valid number."));
0223     }
0224   }
0225 }
0226 
0227 
0228 void MarkersTab::remove() {
0229   foreach(QListWidgetItem *item, _currentMarkersList->selectedItems()) {
0230     _currentMarkersList->takeItem(_currentMarkersList->row(item));
0231   }
0232   emit modified();
0233 }
0234 
0235 
0236 void MarkersTab::clear() {
0237   _currentMarkersList->clear();
0238   emit modified();
0239 }
0240 
0241 
0242 PlotMarkers MarkersTab::plotMarkers() const {
0243   PlotMarkers plotMarker = _plotMarkers;
0244   plotMarker.setManualMarkers(markers());
0245   plotMarker.setLineStyle(lineStyle());
0246   plotMarker.setLineColor(lineColor());
0247   plotMarker.setLineWidth(lineWidth());
0248   if (isVectorSource()) {
0249     plotMarker.setVector(vector());
0250   } else {
0251     plotMarker.setVector(0);
0252   }
0253   if (isCurveSource()) {
0254     plotMarker.setCurve(curve());
0255   } else {
0256     plotMarker.setCurve(0);
0257   }
0258   plotMarker.setCurveMarkerMode(curveMarkerMode());
0259   return plotMarker;
0260 }
0261 
0262 
0263 void MarkersTab::setPlotMarkers(const PlotMarkers &plotMarkers) {
0264   _plotMarkers = plotMarkers;
0265   setMarkers(plotMarkers.manualMarkers());
0266   setLineStyle(plotMarkers.lineStyle());
0267   setLineColor(plotMarkers.lineColor());
0268   setLineWidth(plotMarkers.lineWidth());
0269   setCurveMarkerMode(plotMarkers.curveMarkerMode());
0270   if (plotMarkers.isVectorSource()) {
0271     setVector(plotMarkers.vector());
0272     setVectorSource(true);
0273   } else {
0274     setVectorSource(false);
0275   }
0276   if (plotMarkers.isCurveSource()) {
0277     setCurve(plotMarkers.curve());
0278     setCurveSource(true);
0279   } else {
0280     setCurveSource(false);
0281   }
0282   _dirty = false;
0283 }
0284 
0285 
0286 void MarkersTab::clearTabValues() {
0287   _currentMarkersList->clear();
0288   _hidden->setChecked(true);
0289   _markerLineStyle->setCurrentIndex(-1);
0290   _markerLineColor->clearSelection();
0291   _markerLineWidth->clear();
0292 
0293   _curve->clearSelection();
0294   _vector->clearSelection();
0295 
0296   _curveMarkers->setCheckState(Qt::PartiallyChecked);
0297   _vectorMarkers->setCheckState(Qt::PartiallyChecked);
0298 }
0299 
0300 
0301 void MarkersTab::enableSingleEditOptions(bool enabled) {
0302   if (enabled) {
0303     _curveMarkers->setTristate(false);
0304     _vectorMarkers->setTristate(false);
0305   }
0306 }
0307 
0308 }
0309 
0310 // vim: ts=2 sw=2 et