Esempio n. 1
1
void OutputCalibrationPage::setupVehicleItems()
{
    m_vehicleItems.clear();
    m_vehicleBoundsItem = new QGraphicsSvgItem();
    m_vehicleBoundsItem->setSharedRenderer(m_vehicleRenderer);
    m_vehicleBoundsItem->setElementId(m_vehicleElementIds[0]);
    m_vehicleBoundsItem->setZValue(-1);
    m_vehicleBoundsItem->setOpacity(0);
    m_vehicleScene->addItem(m_vehicleBoundsItem);

    QRectF parentBounds = m_vehicleRenderer->boundsOnElement(m_vehicleElementIds[0]);

    for (int i = 1; i < m_vehicleElementIds.size(); i++) {
        QGraphicsSvgItem *item = new QGraphicsSvgItem();
        item->setSharedRenderer(m_vehicleRenderer);
        item->setElementId(m_vehicleElementIds[i]);
        item->setZValue(i);
        item->setOpacity(1.0);

        QRectF itemBounds = m_vehicleRenderer->boundsOnElement(m_vehicleElementIds[i]);
        item->setPos(itemBounds.x() - parentBounds.x(), itemBounds.y() - parentBounds.y());

        m_vehicleScene->addItem(item);
        m_vehicleItems << item;
    }
}
void ConnectionDiagram::setupGraphicsSceneItems(QList<QString> elementsToShow)
{
    qreal z = 0;
    QRectF backgBounds = m_renderer->boundsOnElement("background");

    foreach(QString elementId, elementsToShow) {
        if (m_renderer->elementExists(elementId)) {
            QGraphicsSvgItem *element = new QGraphicsSvgItem();
            element->setSharedRenderer(m_renderer);
            element->setElementId(elementId);
            element->setZValue(z++);
            element->setOpacity(1.0);

            QMatrix matrix = m_renderer->matrixForElement(elementId);
            QRectF orig    = matrix.mapRect(m_renderer->boundsOnElement(elementId));
            element->setPos(orig.x(), orig.y());

            // QRectF orig = m_renderer->boundsOnElement(elementId);
            // element->setPos(orig.x() - backgBounds.x(), orig.y() - backgBounds.y());

            m_scene->addItem(element);
            qDebug() << "Adding " << elementId << " to scene at " << element->pos();
        } else {
            qDebug() << "Element with id: " << elementId << " not found.";
        }
    }
}
Esempio n. 3
0
MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    // create a scene with items,
    // use the view to show the scene
    QGraphicsScene *scene = new QGraphicsScene();
    QGraphicsView *gv = new QGraphicsView(this);
    gv->setScene(scene);
    if (gv->scene())
    {
        printf("renc: scene is not null.\n");

        // these two lines are at scene origin pos.
        scene->addLine(0, 0, 100, 0, QPen(Qt::red));
        scene->addLine(0, 0, 0, 100, QPen(Qt::green));

        /*QGraphicsRectItem *rect = */
        scene->addRect(QRectF(0,0,50,50), QPen(Qt::red), QBrush(Qt::green));

        // svg image
        QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(svgFile2);
        scene->addItem((svgItem));
        svgItem->setPos(400, 0);
        bool rValid1 = svgItem->renderer()->isValid();
        printf("--%d--\n", rValid1);

        // QWidget to scene
        QPushButton *btn = new QPushButton("button 1");// at (0,0) by default.
        QGraphicsProxyWidget *btnWidget = scene->addWidget(btn);
        btnWidget->setPos(0, 400);

        //
        {
            SvgPushButton *btn2 = new SvgPushButton(svgFile1);
            QGraphicsProxyWidget *btnWidget2 = scene->addWidget(btn2);
            btnWidget2->setPos(400, 400);
        }
    }
    else
        printf("renc: scene is null.\n");

    //setCentralWidget(gv); setWindowTitle("Demo: graphics view");//QMainWindow
    setStyleSheet("background-color: rgb(100,120,50);");
}
Esempio n. 4
0
void SymbolDataEditor::load(const QString & fileName)
{
    loadSettings();

    doc = QDomDocument("SVG");
    QFile file(fileName);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    if (!doc.setContent(&file))
    {
        file.close();
        return;
    }

    file.close();

    //scale viewbox because it is necessary in SvgView::insertSymbol()
    QDomElement svgElement = doc.elementsByTagName("svg").item(0).toElement();
    SvgView::scaleViewBox(svgElement);
    QByteArray scaledFile = doc.toString(0).replace(">\n<tspan", "><tspan").toUtf8();

    QGraphicsSvgItem *symbolItem = new QGraphicsSvgItem();
    symbolItem->setSharedRenderer(new QSvgRenderer(scaledFile));

    QSizeF itemSize = symbolItem->renderer()->defaultSize();
    clear();

    //add changed item to the scene
    scene->setSceneRect(0, 0, itemSize.width() * sceneScale, itemSize.height() * sceneScale);
    symbolItem->setPos(scene->width() / 2 - itemSize.width() / 2.0,
                       scene->height() / 2 - itemSize.height() / 2.0);
    scene->addItem(symbolItem);

    //autoscale graphicsview depending on items and views size
    limitScale(qMax(qreal(width()) / itemSize.width(),
                    qreal(height()) / itemSize.height()) / currentScaleFactor);
    centerOn(symbolItem);
    setDragMode(QGraphicsView::ScrollHandDrag);
}
Esempio n. 5
0
QGraphicsSvgItem* GraphicsWorkflow::addAllSvgItems( const QString& name,  const QMap<int,QString>& cmds) {


     QGraphicsSvgItem *myitem = NULL;

     if ( myitem == NULL  ) {

         QByteArray myxml = SafetYAWL::readStringFromFile(name);
          QMap<QString,QPair<QPointF,QString> > ids = getXmlIds(myxml);

          QString curtitle = SafetYAWL::getConf()["GeneralOptions/currentflowtitle"];
         // qDebug(".......curtitle: |%s|", qPrintable(curtitle));
          QGraphicsTextItem* titleitem = NULL;
          QString includetitle = SafetYAWL::getConf()["GeneralOptions/currentflowtitle.include"];
          qDebug("includetitle: %s", qPrintable(includetitle));

          if ( includetitle.compare("on") == 0 ) {
            titleitem = NULL;
            addInfoGraphText(ids,curtitle,titleitem);
            if ( titleitem ) {
                _items.append(titleitem);
            }

          }
          QString includedate = SafetYAWL::getConf()["Graphs/infotext.include"];

          if ( includedate.compare("on") == 0 ) {
              SYD << tr("QGraphicsTextItem* myitem = addInfoGraphDateText(ids);");
              QGraphicsTextItem* myitem = addInfoGraphDateText(ids);
              if ( myitem) {
                  _items.append(myitem);
              }

          }


          QMap<QString,QString>  svgs = extractSvgElements(myxml);
            for(int i =0; i < svgs.keys().count(); i++) {
                QString key = svgs.keys().at(i);
                QString svg = svgs[ key];
                GraphicsSvgItemLive localitem(svg);
                myitem = localitem.loadSvgItem();
                if ( MainWindow::configurator->getWorkflow() != NULL ) {
                    QString info = MainWindow::configurator->getWorkflow()->infoModeMap()[ids[key].second];
                    if (key.startsWith("node")) {
                        myitem->setToolTip(tr("%1").arg(info));
                    }
                }
                myitem->setFlags(QGraphicsItem::ItemClipsToShape| QGraphicsItem::ItemIsMovable);
                myitem->setCacheMode(QGraphicsItem::NoCache);
                myitem->setZValue(0);                

                scene()->addItem(myitem);

                myitem->setPos(ids[key].first);
                _items.append(myitem);
            }

      }
     else {
          //QSvgRenderer *renderer = new QSvgRenderer(name);
     }
     qDebug("...addAllSvgItems..........(6)...");
     SafetYAWL::streamlog << SafetLog::Action
             << tr("Gráfico de \"Flujo de Trabajo\" generado correctamente...ok!");
     doGroup();
     SafetYAWL::streamlog << SafetLog::Action
             << tr("Agrupando Nodos...ok!");

     return myitem;
}
Esempio n. 6
0
int SvgView::renderText(const QStringRef &text)
{
    scene->clear();

    QRectF currentMarginsRect;

    if (changeMargins)
        currentMarginsRect = QRectF(QPointF(sheetRect.topRight().x() - marginsRect.topRight().x(),
                                            marginsRect.topLeft().y()),
                                    QPointF(sheetRect.bottomRight().x() - marginsRect.bottomLeft().x(),
                                            marginsRect.bottomRight().y()));
    else
        currentMarginsRect = marginsRect;

    scene->addRect(sheetRect);
    scene->addRect(currentMarginsRect, QPen(Qt::darkGray));

    QPointF cursor(currentMarginsRect.x(), currentMarginsRect.y());
    int endOfSheet = 0;

    for (QChar symbol : text)
    {
        qreal letterWidth = fontSize * dpmm / 4, letterHeight = fontSize * dpmm;

        //don't try to go beyond the right margin
        if (cursor.x() > (currentMarginsRect.x() + currentMarginsRect.width() - letterWidth))
        {
            if (symbol.isSpace())   //ignore whitespace-symbols at the end of the current line
                continue;
            cursor += QPointF(currentMarginsRect.x() - cursor.x(), letterHeight + lineSpacing * dpmm);
        }

        //stop rendering when you reach the end of sheet
        if (cursor.y() > currentMarginsRect.bottomRight().y() - letterHeight)
            return endOfSheet;

        if (symbol.isSpace())
        {
            switch (symbol.toLatin1())
            {
            case '\t':
                cursor += QPointF(letterWidth * spacesInTab, 0.0);
                endOfSheet++;
                continue;

            case '\n':
                cursor += QPointF(currentMarginsRect.x() - cursor.x(), letterHeight + lineSpacing * dpmm);
                endOfSheet++;
                continue;

            default:
                cursor += QPointF(letterWidth, 0.0);
                endOfSheet++;
                continue;
            }
        }

        if (!font.contains(symbol))
        {
            cursor += QPointF(letterWidth, 0.0);
            endOfSheet++;
            continue;
        }


        QGraphicsSvgItem *letter = new QGraphicsSvgItem(font.values(symbol).at(qrand() % font.values(symbol).size()));
        if (useCustomFontColor)
        {
            QGraphicsColorizeEffect *colorEffect = new QGraphicsColorizeEffect();
            colorEffect->setColor(fontColor);
            letter->setGraphicsEffect(colorEffect);
        }

        letter->setScale(letterHeight / letter->boundingRect().height());
        letterWidth = letter->boundingRect().width() * letter->scale() + letterSpacing * dpmm;
        letter->setPos(cursor);
        cursor += QPointF(letterWidth, 0.0);

        scene->addItem(letter);
        endOfSheet++;
    }

    return endOfSheet;
}
void OutputCalibrationPage::setupVehicleItems()
{
    m_vehicleItems.clear();
    m_arrowsItems.clear();
    m_vehicleBoundsItem = new QGraphicsSvgItem();
    m_vehicleBoundsItem->setSharedRenderer(m_vehicleRenderer);
    m_vehicleBoundsItem->setElementId(m_vehicleElementIds[0]);
    m_vehicleBoundsItem->setZValue(-1);
    m_vehicleBoundsItem->setOpacity(0);
    m_vehicleScene->addItem(m_vehicleBoundsItem);

    QRectF parentBounds = m_vehicleRenderer->boundsOnElement(m_vehicleElementIds[0]);

    for (int i = 1; i < m_vehicleElementIds.size(); i++) {
        QGraphicsSvgItem *item = new QGraphicsSvgItem();
        item->setSharedRenderer(m_vehicleRenderer);
        item->setElementId(m_vehicleElementIds[i]);
        item->setZValue(i);
        item->setOpacity(1.0);

        QRectF itemBounds = m_vehicleRenderer->boundsOnElement(m_vehicleElementIds[i]);
        item->setPos(itemBounds.x() - parentBounds.x(), itemBounds.y() - parentBounds.y());

        m_vehicleScene->addItem(item);
        m_vehicleItems << item;

        bool addArrows = false;

        if ((m_vehicleElementIds[i].contains("left")) || (m_vehicleElementIds[i].contains("right"))
            || (m_vehicleElementIds[i].contains("elevator")) || (m_vehicleElementIds[i].contains("rudder"))
            || (m_vehicleElementIds[i].contains("steering")) || (m_vehicleElementIds[i] == "singleaileron-aileron")) {
            addArrows = true;
        }

        if (addArrows) {
            QString arrowUp   = "-up"; // right if rudder / steering
            QString arrowDown = "-down"; // left

            QGraphicsSvgItem *itemUp = new QGraphicsSvgItem();

            itemUp->setSharedRenderer(m_vehicleRenderer);
            QString elementUp = m_vehicleElementIds[i] + arrowUp;
            itemUp->setElementId(elementUp);
            itemUp->setZValue(i + 10);
            itemUp->setOpacity(0);

            QRectF itemBounds = m_vehicleRenderer->boundsOnElement(elementUp);
            itemUp->setPos(itemBounds.x() - parentBounds.x(), itemBounds.y() - parentBounds.y());
            m_vehicleScene->addItem(itemUp);

            m_arrowsItems << itemUp;

            QGraphicsSvgItem *itemDown = new QGraphicsSvgItem();
            itemDown->setSharedRenderer(m_vehicleRenderer);
            QString elementDown = m_vehicleElementIds[i] + arrowDown;
            itemDown->setElementId(elementDown);
            itemDown->setZValue(i + 10);
            itemDown->setOpacity(0);

            itemBounds = m_vehicleRenderer->boundsOnElement(elementDown);
            itemDown->setPos(itemBounds.x() - parentBounds.x(), itemBounds.y() - parentBounds.y());
            m_vehicleScene->addItem(itemDown);

            m_arrowsItems << itemDown;
        }
    }
}
QGraphicsSvgItem *
dmz::QtPluginCanvasObjectBasic::_create_svg_item (
      ObjectStruct &os,
      QGraphicsItem *parent,
      const Config &Data,
      HashTableStringTemplate<String> &table) {

   QGraphicsSvgItem *item (new QGraphicsSvgItem (parent));

   Boolean center (True);

   if (_file_request (item, Data, table)) {

      ConfigIterator it;
      Config cd;

      while (Data.get_next_config (it, cd)) {

         const String DataName (cd.get_name ().to_lower ());

         if (DataName == "translate") {

            Vector vec (config_to_vector (cd));
            String itemName = config_to_string ("name", cd);

            if (itemName) {

               QGraphicsItem *img = os.itemTable.lookup (itemName);
               if (img) {

                  QRectF rect = img->boundingRect ();
                  Vector rectVec;
                  if (vec.get_x () == 0) { rectVec.set_x (0); }
                  else if (vec.get_x() > 0) { rectVec.set_x (rect.center ().x ()); }
                  else { rectVec.set_x (-rect.center ().x ()); }

                  if (vec.get_y () == 0) { rectVec.set_y (0); }
                  else if (vec.get_y () > 0) { rectVec.set_y (rect.center ().y ()); }
                  else { rectVec.set_y (-rect.center ().y ()); }

                  QPointF center = item->boundingRect ().center ();
                  rectVec.set_x (rectVec.get_x () - center.x ());
                  rectVec.set_y (rectVec.get_y () - center.y ());
                  vec += rectVec;
                  item->setPos (vec.get_x (), vec.get_y ());
               }
            }
            else { item->translate (vec.get_x (), vec.get_y ()); }

            center = False;
         }
         else if (DataName == "scale") {

            Vector vec (config_to_vector (cd));

            if (vec.get_x () && vec.get_y ()) {

               item->scale (vec.get_x (), vec.get_y ());
            }
         }
      }
   }
   else { delete item; item = 0; }

   if (item && center) {

      QRectF bound = item->boundingRect ();

      item->translate (bound.width () * -0.5f, bound.height () * -0.5f);
   }

   return item;
}