void KstPluginDialogI::fillFieldsForEdit() {
    KstCPluginPtr pp = kst_cast<KstCPlugin>(_dp);
    if (!pp) {
        return;
    }
    pp->readLock();
    if (!pp->plugin()) { // plugin() can be null if the kst file is invalid
        pp->unlock();
        return;
    }
    const QString pluginName(pp->tagName());
    const QString pluginObjectName(pp->plugin()->data()._name);
    const int usage = pp->getUsage();
    KstSharedPtr<Plugin> plug = pp->plugin();
    pp->unlock();

    _tagName->setText(pluginName);
    updatePluginList();

    int i = _pluginList.findIndex(pluginObjectName);
    _w->PluginCombo->setCurrentItem(i);
    pluginChanged(_w->PluginCombo->currentItem());

    fillVectorScalarCombos(plug);
    _w->PluginCombo->setEnabled(usage < 3);

    fixupLayout();
}
Пример #2
0
bool KstPluginDialogI::editObject() {
  KstCPluginPtr pp = kst_cast<KstCPlugin>(_dp);
  if (!pp) { // something is dreadfully wrong - this should never happen
    return false;
  }

  pp->writeLock();
  if (_tagName->text() != pp->tagName() && KstData::self()->dataTagNameNotUnique(_tagName->text())) {
    _tagName->setFocus();
    pp->unlock();
    return false;
  }

  pp->setTagName(_tagName->text());

  int pitem = _w->PluginCombo->currentItem();
  KstSharedPtr<Plugin> pPtr = PluginCollection::self()->plugin(_pluginList[pitem]);

  // Must unlock before clear()
  for (KstVectorMap::Iterator i = pp->inputVectors().begin(); i != pp->inputVectors().end(); ++i) {
    (*i)->unlock();
  }
  for (KstScalarMap::Iterator i = pp->inputScalars().begin(); i != pp->inputScalars().end(); ++i) {
    (*i)->unlock();
  }
  for (KstStringMap::Iterator i = pp->inputStrings().begin(); i != pp->inputStrings().end(); ++i) {
    (*i)->unlock();
  }
  pp->inputVectors().clear();
  pp->inputScalars().clear();
  pp->inputStrings().clear();

  // Save the vectors and scalars
  if (!saveInputs(pp, pPtr)) {
    KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
    pp->unlock();
    return false;
  }

  if (pitem >= 0 && _w->PluginCombo->count() > 0) {
    pp->setPlugin(pPtr);
  }

  if (!saveOutputs(pp, pPtr)) {
    KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
    pp->unlock();
    return false;
  }

  if (!pp->isValid()) {
    KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
    pp->unlock();
    return false;
  }
  pp->setDirty();
  pp->unlock();

  emit modified();
  return true;
}
Пример #3
0
bool KstPluginDialogI::newObject() {
  QString tagName = _tagName->text();

  if (tagName != plugin_defaultTag && KstData::self()->dataTagNameNotUnique(tagName, true, this)) {
    _tagName->setFocus();
    return false;
  }
  KstCPluginPtr plugin;
  int pitem = _w->PluginCombo->currentItem();
  if (pitem >= 0 && _w->PluginCombo->count() > 0) {
    KstSharedPtr<Plugin> pPtr = PluginCollection::self()->plugin(_pluginList[pitem]);
    if (pPtr) {
      plugin = new KstCPlugin;
      plugin->writeLock();
      if (!saveInputs(plugin, pPtr)) {
        KMessageBox::sorry(this, i18n("One or more of the inputs was undefined."));
        plugin->unlock();
        plugin = 0L;
        return false;
      }

      plugin->setPlugin(pPtr);

      if (tagName == plugin_defaultTag) {
        tagName = KST::suggestPluginName(_pluginList[pitem]);
      }
      plugin->setTagName(tagName);
      if (!saveOutputs(plugin, pPtr)) {
        KMessageBox::sorry(this, i18n("One or more of the outputs was undefined."));
        plugin->unlock();
        plugin = 0L;
        return false;
      }
      plugin->unlock();
    }
  }

  if (!plugin || !plugin->isValid()) {
    KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
    return false;
  }

  plugin->setDirty();
  KST::dataObjectList.lock().writeLock();
  KST::dataObjectList.append(plugin.data());
  KST::dataObjectList.lock().unlock();
  plugin = 0L;
  emit modified();

  return true;
}
Пример #4
0
void KstViewFitsDialog::fillComboBox(const QString& str) {
  KstCPluginList::iterator it;
  KstCPluginList fits;
  QString fitName = str;
  bool changed = false;
  uint i;

  _comboBoxFits->clear();
  fits = kstObjectSubList<KstDataObject,KstCPlugin>(KST::dataObjectList);

  for (it = fits.begin(); it != fits.end(); ++it) {
    KstCPluginPtr fit;

    fit = *it;
    fit->readLock();

    if (fit->plugin()->data()._isFit) {
      _comboBoxFits->insertItem(0, fit->tagName());
      if (fitName == fit->tagName() || fitName.isEmpty()) {
        _comboBoxFits->setCurrentIndex(_comboBoxFits->count() - 1);
        if (fitName.isEmpty()) {
          fitName = fit->tagName();
        }
        changed = true;
        fitChanged(fitName);
      }
    }

    fit->unlock();
  }

  if (!changed) {
    fitChanged(_comboBoxFits->currentText());
  }
}
Пример #5
0
bool KstFilterDialogI::newObject() {
  QString tagName = _tagName->text();

  if (KstData::self()->dataTagNameNotUnique(tagName, true, this)) {
    _tagName->setFocus();
    return false;
  } else {
    int pitem = _w->PluginCombo->currentItem();

    if (pitem >= 0 && _w->PluginCombo->count() > 0) {
      KstSharedPtr<Plugin> pPtr = PluginCollection::self()->plugin(_pluginList[pitem]);

      if (pPtr) {
        KstCPluginPtr plugin = new KstCPlugin;
        plugin->writeLock();
        plugin->setDirty();
        if (saveInputs(plugin, pPtr)) {
          if (tagName == plugin_defaultTag) {
            tagName = KST::suggestPluginName(_pluginList[pitem], KstObjectTag::fromString(_yvector));
          }

          plugin->setTagName(KstObjectTag(tagName, KstObjectTag::globalTagContext)); // FIXME: tag context always global?

          plugin->setPlugin(pPtr);

          if (saveOutputs(plugin, pPtr)) {
            if (plugin->isValid()) {
              if (!createCurve(plugin)) {
                KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
              } else {
                KST::dataObjectList.lock().writeLock();
                KST::dataObjectList.append(plugin.data());
                KST::dataObjectList.lock().unlock();
              }
            } else {
              KMessageBox::sorry(this, i18n("There is an error in the plugin you entered."));
            }
          }
        }
        plugin->unlock();
      }
    }
    emit modified();
  }
  return true;
}
void KstPluginDialogI::fillVectorScalarCombos(KstSharedPtr<Plugin> plugin) {
    bool DPvalid = false;
    KstCPluginPtr pp = kst_cast<KstCPlugin>(_dp);

    if (pp) {
        pp->readLock();
        DPvalid = pp->isValid();
        pp->unlock();
    }

    if (plugin) {
        if (DPvalid) {
            pp->readLock();
        }
        // Update input vector and scalar combos
        const QValueList<Plugin::Data::IOValue>& itable = plugin->data()._inputs;
        for (QValueList<Plugin::Data::IOValue>::ConstIterator it = itable.begin();
                it != itable.end(); ++it) {
            if ((*it)._type == Plugin::Data::IOValue::TableType) { // vector
                QObject *field = _w->_pluginInputOutputFrame->child((*it)._name.latin1(), "VectorSelector");
                assert(field);
                if (field) {
                    VectorSelector *vs = static_cast<VectorSelector*>(field);
                    QString selectedVector = vs->selectedVector();
                    vs->update();
                    if (DPvalid) {
                        vs->setSelection(pp->inputVectors()[(*it)._name]->tag().displayString());
                    } else {
                        vs->setSelection(selectedVector);
                    }
                }
            } else if ((*it)._type == Plugin::Data::IOValue::StringType) {
                QObject *field = _w->_pluginInputOutputFrame->child((*it)._name.latin1(), "StringSelector");
                assert(field);
                if (field) {
                    StringSelector *ss = static_cast<StringSelector*>(field);
                    QString selectedString = ss->selectedString();
                    ss->update();
                    if (DPvalid) {
                        ss->setSelection(pp->inputStrings()[(*it)._name]->tag().displayString());
                    } else {
                        ss->setSelection(selectedString);
                    }
                }
            } else if ((*it)._type == Plugin::Data::IOValue::PidType) {
                // Nothing
            } else {
                QObject *field = _w->_pluginInputOutputFrame->child((*it)._name.latin1(), "ScalarSelector");
                assert(field);
                if (field) {
                    ScalarSelector *ss = static_cast<ScalarSelector*>(field);
                    QString selectedScalar = ss->selectedScalar();
                    ss->update();
                    if (DPvalid) {
                        ss->setSelection(pp->inputScalars()[(*it)._name]->tag().displayString());
                    } else {
                        ss->setSelection(selectedScalar);
                    }
                }
            }
        }

        // Update output vector and scalar lineedits
        if (DPvalid) {
            const QValueList<Plugin::Data::IOValue>& otable = plugin->data()._outputs;
            for (QValueList<Plugin::Data::IOValue>::ConstIterator it = otable.begin(); it != otable.end(); ++it) {
                QObject *field = _w->_pluginInputOutputFrame->child((*it)._name.latin1(), "QLineEdit");
                assert(field);
                if (field) {
                    QLineEdit *li = static_cast<QLineEdit*>(field);
                    QString ts;
                    if ((*it)._type == Plugin::Data::IOValue::TableType) { // vector
                        ts = pp->outputVectors()[(*it)._name]->tagName();
                    } else if ((*it)._type == Plugin::Data::IOValue::PidType) {
                    } else if ((*it)._type == Plugin::Data::IOValue::StringType) {
                        ts = pp->outputStrings()[(*it)._name]->tagName();
                    } else { // scalar
                        ts = pp->outputScalars()[(*it)._name]->tagName();
                    }
                    li->setText(ts);
                }
            }
            pp->unlock();
        }
    } else { // invalid plugin
        PluginCollection *pc = PluginCollection::self();
        QString cur = _pluginList[_w->PluginCombo->currentItem()];
        Plugin::Data pdata = pc->pluginList()[pc->pluginNameList()[cur]];
        for (QValueList<Plugin::Data::IOValue>::ConstIterator it = pdata._outputs.begin(); it != pdata._outputs.end(); ++it) {
            QObject *field = _w->_pluginInputOutputFrame->child((*it)._name.latin1(), "QLineEdit");
            if (field) {
                static_cast<QLineEdit*>(field)->setText(QString::null);
            }
        }
    }
}
Пример #7
0
void renderLabel(RenderContext& rc, Label::Chunk *fi) {
  // FIXME: RTL support
  int oldSize = rc.size;
  int oldY = rc.y;
  int oldX = rc.x;
  
  while (fi) {
    if (fi->vOffset != Label::Chunk::None) {
      if (fi->vOffset == Label::Chunk::Up) {
        rc.y -= int(0.4 * rc.fontHeight());
      } else { // Down
        rc.y += int(0.4 * rc.fontHeight());
      }
      if (rc.size > 5) {
        rc.size = (rc.size*2)/3;
      }
    }

    QFont f = rc.font();
    if (rc.fontSize() != rc.size) {
      f.setPointSize(rc.size);
    }

    f.setBold(fi->attributes.bold);
    f.setItalic(fi->attributes.italic);
    f.setUnderline(fi->attributes.underline);
    if (rc.p && fi->attributes.color.isValid()) {
      rc.p->setPen(fi->attributes.color);
    } else if (rc.p) {
      rc.p->setPen(rc.pen);
    }
    rc.setFont(f);

    if (fi->linebreak) {
      rc.x = oldX;
      rc.y += rc.fontAscent() + rc.fontDescent() + 1;
      fi = fi->next;
      continue;
    }

    if (!rc.substitute && (fi->scalar || fi->vector)) {
      QString txt = QString("[") + fi->text + "]";
      if (rc.p) {
        rc.p->drawText(rc.x, rc.y, txt);
      }
      rc.x += rc.fontWidth(txt);
    } else if (fi->scalar) { 
      // do scalar/string/fit substitution
      QString txt;
      if (!fi->text.isEmpty() && fi->text[0] == '=') {
        // Parse and evaluate as an equation
        bool ok = false;
        const QString s = fi->text.mid(1);
        const double eqResult(Equation::interpret(s.toLatin1(), &ok, s.length()));
        txt = QString::number(eqResult, 'g', rc.precision);
        if (rc._cache) {
          rc._cache->append(DataRef(DataRef::DRExpression, fi->text, QString::null, 0.0, QVariant(eqResult)));
        }
      } else {
        KST::scalarList.lock().readLock();
        KstScalarPtr scp = KST::scalarList.retrieveObject(KstObjectTag::fromString(fi->text));
        KST::scalarList.lock().unlock();
        if (scp) {
          scp->readLock();
          txt = QString::number(scp->value(), 'g', rc.precision);
          if (rc._cache) {
            rc._cache->append(DataRef(DataRef::DRScalar, fi->text, QString::null, 0.0, QVariant(scp->value())));
          }
          scp->unlock();
        } else {
          KST::stringList.lock().readLock();
          KstStringPtr stp = KST::stringList.retrieveObject(KstObjectTag::fromString(fi->text));
          KST::stringList.lock().unlock();
          if (stp) {
            stp->readLock();
            txt = stp->value();
            if (rc._cache) {
              rc._cache->append(DataRef(DataRef::DRString, fi->text, QString::null, 0.0, QVariant(stp->value())));
            }
            stp->unlock();
          } else {
            KST::dataObjectList.lock().readLock();
            KstDataObjectList::Iterator oi = KST::dataObjectList.findTag(fi->text);
            KST::dataObjectList.lock().unlock();
            if (oi != KST::dataObjectList.end()) {
              KstCPluginPtr fit = kst_cast<KstCPlugin>(*oi);
              if (fit) {
                fit->readLock();
                const QString txtAll = fit->label(rc.precision);
                fit->unlock();

                const Q3ValueList<QString> strList = txtAll.split('\n');
                Q3ValueListConstIterator<QString> last = --(strList.end());
                for (Q3ValueListConstIterator<QString> iter = strList.begin(); iter != strList.end(); ++iter) {
                  txt = (*iter);

                  if (iter != last) {
                    if (rc.p) {
                      rc.p->drawText(rc.x, rc.y, txt);
                    } else {
                      rc.ascent = qMax(rc.ascent, -rc.y + rc.fontAscent());
                      if (-rc.y - rc.fontDescent() < 0) {
                        rc.descent = qMax(rc.descent, rc.fontDescent() + rc.y);
                      }
                    }
                    rc.x   += rc.fontWidth(txt);
                    rc.xMax = qMax(rc.xMax, rc.x);

                    rc.x    = oldX;
                    rc.y   += rc.fontAscent() + rc.fontDescent() + 1;
                  }
                }
                if (rc._cache) {
                  rc._cache->append(DataRef(DataRef::DRFit, fi->text, txtAll, rc.precision, QVariant(0.0)));
                }
              }
            }
          }
        }
      }
      if (rc.p) {
        rc.p->drawText(rc.x, rc.y, txt);
      }
      rc.x += rc.fontWidth(txt);
    } else if (fi->vector) {
      QString txt;
      KST::vectorList.lock().readLock();
      KstVectorPtr vp = *KST::vectorList.findTag(fi->text);
      KST::vectorList.lock().unlock();
      if (vp) {
        if (!fi->expression.isEmpty()) {
          // Parse and evaluate as an equation
          bool ok = false;
          // FIXME: make more efficient: cache the parsed equation
          const double idx = Equation::interpret(fi->expression.toLatin1(), &ok, fi->expression.length());
          if (ok) {
            vp->readLock();
            const double vVal(vp->value()[int(idx)]);
            txt = QString::number(vVal, 'g', rc.precision);
            if (rc._cache) {
              rc._cache->append(DataRef(DataRef::DRVector, fi->text, fi->expression, idx, QVariant(vVal)));
            }
            vp->unlock();
          } else {
            txt = "NAN";
          }
        }
      }
      if (rc.p) {
        rc.p->drawText(rc.x, rc.y, txt);
      }
      rc.x += rc.fontWidth(txt);
    } else if (fi->tab) {
      const int tabWidth = rc.fontWidth("MMMMMMMM");
      const int toSkip = tabWidth - (rc.x - rc.xStart) % tabWidth;
      if (rc.p && fi->attributes.underline) {
        const int spaceWidth = rc.fontWidth(" ");
        const int spacesToSkip = tabWidth / spaceWidth + (tabWidth % spaceWidth > 0 ? 1 : 0);
        rc.p->drawText(rc.x, rc.y, QString().fill(' ', spacesToSkip));
      }
      rc.x += toSkip;
    } else {
      if (rc.p) {
#ifdef BENCHMARK
        QTime t;
        t.start();
#endif
        rc.p->drawText(rc.x, rc.y, fi->text);
#ifdef BENCHMARK
        qDebug() << "Renderer did draw, time: " << t.elapsed() << endl;
#endif
      }
      rc.x += rc.fontWidth(fi->text);
    }

    if (!rc.p) {
      // No need to compute ascent and descent when really painting
      rc.ascent = qMax(rc.ascent, -rc.y + rc.fontAscent());
      if (-rc.y - rc.fontDescent() < 0) {
        rc.descent = qMax(rc.descent, rc.fontDescent() + rc.y);
      }
    }

    int xNext = rc.x;
    if (fi->group) {
      renderLabel(rc, fi->group);
      xNext = rc.x;
    }

    if (fi->up) {
      int xPrev = rc.x;
      renderLabel(rc, fi->up);
      xNext = qMax(xNext, rc.x);
      rc.x = xPrev;
    }

    if (fi->down) {
      renderLabel(rc, fi->down);
      xNext = qMax(xNext, rc.x);
    }

    rc.x = xNext;
    rc.xMax = qMax(rc.xMax, rc.x);

    fi = fi->next;
  }

  rc.size = oldSize;
  rc.y = oldY;
}
void KstViewLabel::DataCache::update() {
  for (QValueVector<DataRef>::ConstIterator i = data.begin(); valid && i != data.end(); ++i) {
    switch ((*i).type) {
      case DataRef::DataRef::DRScalar:
        {
          KST::scalarList.lock().readLock();
          KstScalarPtr p = *KST::scalarList.findTag((*i).name);
          KST::scalarList.lock().unlock();
          if (p) {
            p->readLock();
            if (QVariant(p->value()) != (*i).value) {
              valid = false;
            }
            p->unlock();
          }
        }
        break;

      case DataRef::DRString:
        {
          KST::stringList.lock().readLock();
          KstStringPtr p = *KST::stringList.findTag((*i).name);
          KST::stringList.lock().unlock();
          if (p) {
            p->readLock();
            if (QVariant(p->value()) != (*i).value) {
              valid = false;
            }
            p->unlock();
          }
        }
        break;

      case DataRef::DRExpression:
        {
          bool ok = false;
          const double val = Equation::interpret((*i).name.latin1(), &ok, (*i).name.length());
          if (QVariant(val) != (*i).value) {
            valid = false;
          }
        }
        break;

      case DataRef::DRVector:
        {
          bool ok = false;
          const double idx = Equation::interpret((*i).index.latin1(), &ok, (*i).index.length());
          if (idx != (*i).indexValue) {
            valid = false;
            break;
          }
          KST::vectorList.lock().readLock();
          KstVectorPtr p = *KST::vectorList.findTag((*i).name);
          KST::vectorList.lock().unlock();
          if (p) {
            p->readLock();
            if (QVariant(p->value(int((*i).indexValue))) != (*i).value) {
              valid = false;
            }
            p->unlock();
          }
        }
        break;

      case DataRef::DataRef::DRFit:
        {
          KST::dataObjectList.lock().readLock();
          KstDataObjectList::Iterator oi = KST::dataObjectList.findTag((*i).name);
          KST::dataObjectList.lock().unlock();
          if (oi != KST::dataObjectList.end()) {
            KstCPluginPtr fit = kst_cast<KstCPlugin>(*oi);
            if (fit) {
              fit->readLock(); 
              if (fit->label((int)((*i).indexValue)) != (*i).index) {
                valid = false;
              }
              fit->unlock();
            }
          }
        }
        break;
    }
  }
}
Пример #9
0
void KstViewFitsDialog::fitChanged(const QString& strFit) {
  KstCPluginList fits;
  KstCPluginPtr plugin;
  double* params = 0L;
  double* covars = 0L;
  double chi2Nu = 0.0;
  int numParams = 0;
  int numCovars = 0;
  int i;

  fits = kstObjectSubList<KstDataObject,KstCPlugin>(KST::dataObjectList);
  plugin = *(fits.findTag(strFit));
  if (plugin) {
    KstScalarPtr scalarChi2Nu;
    KstVectorPtr vectorParam;
    plugin->readLock();

    const KstScalarMap& scalars = plugin->outputScalars();
    
    scalarChi2Nu = scalars["chi^2/nu"];
    if (scalarChi2Nu) {
      scalarChi2Nu->readLock();
      chi2Nu = scalarChi2Nu->value();
      scalarChi2Nu->unlock();
    }

    const KstVectorMap& vectors = plugin->outputVectors();
    vectorParam = vectors["Parameters"];

    if (vectorParam) {
      KstVectorPtr vectorCovar;

      vectorParam->readLock();
      vectorCovar = vectors["Covariance"];
      if (vectorCovar) {
        vectorCovar->readLock();
        numParams = vectorParam->length();
        numCovars = vectorCovar->length();

        if (numParams > 0 && numCovars > 0) {
          params = new double[numParams];
          covars = new double[numCovars];

          for (i = 0; i < numParams; i++) {
            params[i] = vectorParam->value(i);
          }

          for (i = 0; i < numCovars; i++) {
            covars[i] = vectorCovar->value(i);
          }
        }
        vectorCovar->unlock();
      }
      vectorParam->unlock();
    }
    plugin->unlock();
  }

  _tableFits->setParameters(params, numParams, covars, numCovars, chi2Nu);

  if (numParams > 0) {
    _tableFits->horizontalHeaderItem(0)->setText(QObject::tr("Value"));
    _tableFits->horizontalHeaderItem(1)->setText(QObject::tr("Covariance:"));

    _tableFits->verticalHeaderItem(numParams+0)->setText("---");
    _tableFits->verticalHeaderItem(numParams+1)->setText(QObject::tr("Chi^2/Nu"));

    if (plugin) {
      QExplicitlySharedDataPointer<Plugin> pluginBase;

      plugin->readLock();
      pluginBase = plugin->plugin();

      if (pluginBase) {
        textLabelFit->setText(pluginBase->data()._readableName);
        for (i = 0; i < numParams; i++) {
          QString parameterName = pluginBase->parameterName(i);
          _tableFits->horizontalHeaderItem(i+2)->setText(parameterName);
          _tableFits->verticalHeaderItem(i)->setText(parameterName);
        }
      }
      plugin->unlock();
    }
  }

  _tableFits->update();
}