void KstPluginDialogI::generateEntries(bool input, int& cnt, QWidget *parent, QGridLayout *grid, const QValueList<Plugin::Data::IOValue>& table) {
    QString scalarLabelTemplate, vectorLabelTemplate, stringLabelTemplate;

    if (input) {
        stringLabelTemplate = i18n("Input string - %1:");
        scalarLabelTemplate = i18n("Input scalar - %1:");
        vectorLabelTemplate = i18n("Input vector - %1:");
    } else {
        stringLabelTemplate = i18n("Output string - %1:");
        scalarLabelTemplate = i18n("Output scalar - %1:");
        vectorLabelTemplate = i18n("Output vector - %1:");
    }

    for (QValueList<Plugin::Data::IOValue>::ConstIterator it = table.begin(); it != table.end(); ++it) {
        QString labellabel;
        bool scalar = false;
        bool string = false;
        switch ((*it)._type) {
        case Plugin::Data::IOValue::PidType:
            continue;
        case Plugin::Data::IOValue::StringType:
            labellabel = stringLabelTemplate.arg((*it)._name);
            string = true;
            break;
        case Plugin::Data::IOValue::FloatType:
            labellabel = scalarLabelTemplate.arg((*it)._name);
            scalar = true;
            break;
        case Plugin::Data::IOValue::TableType:
            if ((*it)._subType == Plugin::Data::IOValue::FloatSubType ||
                    (*it)._subType == Plugin::Data::IOValue::FloatNonVectorSubType) {
                labellabel = vectorLabelTemplate.arg((*it)._name);
            } else {
                // unsupported
                continue;
            }
            break;
        default:
            // unsupported
            continue;
        }

        QLabel *label = new QLabel(labellabel, parent, input ? "Input label" : "Output label");

        QWidget *widget = 0L;

        if (input) {
            if (scalar) {
                ScalarSelector *w = new ScalarSelector(parent, (*it)._name.latin1());
                widget = w;
                connect(w->_scalar, SIGNAL(activated(const QString&)), this, SLOT(updateScalarTooltip(const QString&)));
                connect(widget, SIGNAL(newScalarCreated()), this, SIGNAL(modified()));
                if (!(*it)._default.isEmpty()) {
                    w->_scalar->insertItem((*it)._default);
                    w->_scalar->setCurrentText((*it)._default);
                }
                KstScalarPtr p = *KST::scalarList.findTag(w->_scalar->currentText());
                w->allowDirectEntry(true);
                if (p) {
                    p->readLock();
                    QToolTip::remove(w->_scalar);
                    QToolTip::add(w->_scalar, QString::number(p->value()));
                    p->unlock();
                }
            } else if (string) {
                StringSelector *w = new StringSelector(parent, (*it)._name.latin1());
                widget = w;
                connect(w->_string, SIGNAL(activated(const QString&)), this, SLOT(updateStringTooltip(const QString&)));
                connect(widget, SIGNAL(newStringCreated()), this, SIGNAL(modified()));
                if (!(*it)._default.isEmpty()) {
                    w->_string->insertItem((*it)._default);
                    w->_string->setCurrentText((*it)._default);
                }
                KstStringPtr p = *KST::stringList.findTag(w->_string->currentText());
                w->allowDirectEntry(true);
                if (p) {
                    p->readLock();
                    QToolTip::remove(w->_string);
                    QToolTip::add(w->_string, p->value());
                    p->unlock();
                }
            } else {
                widget = new VectorSelector(parent, (*it)._name.latin1());
                connect(widget, SIGNAL(newVectorCreated(const QString&)), this, SIGNAL(modified()));
            }
        } else {
Example #2
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 double eqResult(Equation::interpret(fi->text.mid(1).latin1(), &ok));
        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.findTag(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.findTag(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()) {
              KstPluginPtr fit = kst_cast<KstPlugin>(*oi);
              if (fit) {
                fit->readLock();
                const QString txtAll = fit->label(rc.precision);
                fit->unlock();
                
                const QValueList<QString> strList = QStringList::split('\n', txtAll);
                QValueListConstIterator<QString> last = --(strList.end());
                for (QValueListConstIterator<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 = kMax(rc.ascent, -rc.y + rc.fontAscent());
                      if (-rc.y - rc.fontDescent() < 0) {
                        rc.descent = kMax(rc.descent, rc.fontDescent() + rc.y);
                      }
                    }
                    rc.x   += rc.fontWidth(txt);
                    rc.xMax = kMax(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.latin1(), &ok);
          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
        kstdDebug() << "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 = kMax(rc.ascent, -rc.y + rc.fontAscent());
      if (-rc.y - rc.fontDescent() < 0) {
        rc.descent = kMax(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 = kMax(xNext, rc.x);
      rc.x = xPrev;
    }

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

    rc.x = xNext;
    rc.xMax = kMax(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;
    }
  }
}