File indexing completed on 2024-06-02 04:35:58

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 "vectorselector.h"
0014 
0015 #include "dialoglauncher.h"
0016 #include "datacollection.h"
0017 #include "objectstore.h"
0018 #include "dialogdefaults.h"
0019 #include "curve.h"
0020 #include "updateserver.h"
0021 #include "geticon.h"
0022 
0023 namespace Kst {
0024 
0025 VectorSelector::VectorSelector(QWidget *parent, ObjectStore *store)
0026   : QWidget(parent), _allowEmptySelection(false), _isX(false), _store(store)  {
0027 
0028   setupUi(this);
0029 
0030   //int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
0031 
0032   _newVector->setIcon(KstGetIcon("kst_vectornew"));
0033   _editVector->setIcon(KstGetIcon("kst_vectoredit"));
0034 
0035   _newVector->setFixedSize(iconWidth(), iconWidth());
0036   _editVector->setFixedSize(iconWidth(), iconWidth());
0037 
0038   fillVectors();
0039   connect(_newVector, SIGNAL(pressed()), this, SLOT(newVector()));
0040   connect(_editVector, SIGNAL(pressed()), this, SLOT(editVector()));
0041   connect(_vector, SIGNAL(activated(int)), this, SLOT(emitSelectionChanged()));
0042   connect(_vector, SIGNAL(currentIndexChanged(int)), this, SLOT(updateDescriptionTip()));
0043   connect(UpdateServer::self(), SIGNAL(objectListsChanged()), this, SLOT(fillVectors()));
0044 }
0045 
0046 
0047 VectorSelector::~VectorSelector() {
0048 }
0049 
0050 
0051 int VectorSelector::iconWidth() const {
0052   return fontMetrics().lineSpacing()*4/3;
0053 }
0054 
0055 QSize VectorSelector::minimumSizeHint() const {
0056   return QSize(15*fontMetrics().width("m")+ 2 * iconWidth(), iconWidth());
0057 }
0058 
0059 
0060 void VectorSelector::setObjectStore(ObjectStore *store) {
0061   _store = store;
0062   fillVectors();
0063 }
0064 
0065 
0066 void VectorSelector::updateDescriptionTip() {
0067   if (selectedVector()) {
0068     setToolTip(selectedVector()->descriptionTip());
0069   } else {
0070     setToolTip(QString());
0071   }
0072 }
0073 
0074 
0075 void VectorSelector::emitSelectionChanged() {
0076   if (_allowEmptySelection && (_vector->count()>0)) {
0077       _editVector->setDisabled(_vector->currentIndex()==0);
0078   }
0079   emit selectionChanged(_vector->currentText());
0080 }
0081 
0082 
0083 VectorPtr VectorSelector::selectedVector() const {
0084     QString shortName;
0085     QRegExp rx("(\\(|^)([A-Z]\\d+)(\\)$|$)");
0086     rx.indexIn(_vector->currentText());
0087     shortName = '('+rx.cap(2)+')';
0088 
0089 
0090     for(int i=0;i<_vector->count();i++) {
0091         if(_vector->itemText(i).contains(shortName)) {
0092             return _vector->itemData(i).value<Vector*>();
0093         }
0094     }
0095     return _vector->itemData(_vector->currentIndex()).value<Vector*>();
0096 }
0097 
0098 
0099 bool VectorSelector::vectorSelected() const {
0100   return (_vector->currentIndex()>=0);
0101 }
0102 
0103 bool VectorSelector::selectedVectorDirty() const {
0104   return _vector->currentIndex() != -1;
0105 }
0106 
0107 
0108 void VectorSelector::setSelectedVector(VectorPtr selectedVector) {
0109   if (!selectedVector) {
0110     return;
0111   }
0112   // "findData can't work here" says the trolls... so we do it 'manually'.
0113   //int i = _vector->findData(qVariantFromValue(selectedVector.data()));
0114   int i=-1;
0115   for (int j=0; j<_vector->count() ; ++j) {
0116     if (selectedVector.data() == _vector->itemData(j).value<Vector*>()) {
0117       i=j;
0118       break;
0119     }
0120   }
0121   Q_ASSERT(i != -1);
0122   _vector->setCurrentIndex(i);
0123 }
0124 
0125 
0126 void VectorSelector::clearSelection() {
0127   _vector->setCurrentIndex(-1);
0128 }
0129 
0130 
0131 bool VectorSelector::allowEmptySelection() const {
0132   return _allowEmptySelection;
0133 }
0134 
0135 
0136 void VectorSelector::setAllowEmptySelection(bool allowEmptySelection) {
0137   _allowEmptySelection = allowEmptySelection;
0138 
0139   int i = _vector->findText(tr("<None>"));
0140   if (i != -1)
0141     _vector->removeItem(i);
0142 
0143   if (_allowEmptySelection) {
0144     _vector->insertItem(0, tr("<None>"), qVariantFromValue(0));
0145     _vector->setCurrentIndex(0);
0146     _editVector->setEnabled(false);
0147   }
0148 }
0149 
0150 
0151 void VectorSelector::newVector() {
0152   QString newName;
0153   if (_isX) {
0154     newName = dialogDefaults().value("curve/xvectorfield","INDEX").toString();
0155   }
0156   DialogLauncher::self()->showVectorDialog(newName, 0, true);
0157   fillVectors();
0158   VectorPtr vector = kst_cast<Vector>(_store->retrieveObject(newName));
0159 
0160   if (vector) {
0161     setSelectedVector(vector);
0162     emitSelectionChanged();
0163     emit contentChanged();
0164   }
0165 }
0166 
0167 
0168 void VectorSelector::editVector() {
0169   if (!selectedVector()) {
0170     return; // Don't crash when the user clicks the Edit vector button and there is no selected vector
0171   }
0172   else if (selectedVector()->provider()) {
0173     DialogLauncher::self()->showObjectDialog(selectedVector()->provider());
0174   } else {
0175     QString vectorname;
0176     DialogLauncher::self()->showVectorDialog(vectorname, ObjectPtr(selectedVector()), true);
0177   }
0178   fillVectors(); // we might have just changed the name, so refill the combo.
0179 
0180   emit contentChanged();
0181 }
0182 
0183 
0184 void VectorSelector::setToLastX(QString field) {
0185   if (!_store) {
0186     return;
0187   }
0188   int match = -1;
0189   VectorList vectors = _store->getObjects<Vector>();
0190   int size = vectors.size();
0191   for (int i = 0; i < size; ++i) {
0192     if (vectors.at(i)->descriptiveName() == field) {
0193       match = i;
0194     }
0195   }
0196 
0197   if (match >-1) {
0198     setSelectedVector(vectors.at(match));
0199   } else {
0200     CurveList objects = _store->getObjects<Curve>();
0201     if (objects.count()>0) {
0202       setSelectedVector(objects.at(objects.count()-1)->xVector());
0203     }
0204   }
0205 }
0206 
0207 
0208 void VectorSelector::fillVectors() {
0209   if (!_store) {
0210     return;
0211   }
0212   int current_index = _vector->currentIndex();
0213   VectorPtr current = selectedVector();
0214 
0215   QHash<QString, VectorPtr> vectors;
0216 
0217   VectorList vectorList = _store->getObjects<Vector>();
0218 
0219   VectorList::ConstIterator it = vectorList.constBegin();
0220   for (; it != vectorList.constEnd(); ++it) {
0221     VectorPtr vector = (*it);
0222     if (vector->isScalarList())
0223       continue;
0224 
0225     vector->readLock();
0226     vectors.insert(vector->CleanedName(), vector);
0227     vector->unlock();
0228   }
0229 
0230   QStringList list = vectors.keys();
0231 
0232   qSort(list);
0233 
0234   _vector->clear();
0235   foreach (const QString &string, list) {
0236     VectorPtr v = vectors.value(string);
0237     _vector->addItem(string, qVariantFromValue(v.data()));
0238   }
0239   _editVector->setEnabled(_vector->count() > 0);
0240 
0241   if (_allowEmptySelection) //reset the <None>
0242     setAllowEmptySelection(true);
0243 
0244   if (current_index==-1) {
0245     _vector->setCurrentIndex(current_index);
0246   } else if (current) {
0247     setSelectedVector(current);
0248   }
0249 }
0250 
0251 }
0252 
0253 // vim: ts=2 sw=2 et