<commit_before>/*-----------------------------------------------------------------------------
 This source file is part of Hopsan NG

 Copyright (c) 2011 
    <NAME>, <NAME>, <NAME>, <NAME>,
    <NAME>, <NAME>, <NAME>, <NAME>

 This file is provided "as is", with no guarantee or warranty for the
 functionality or reliability of the contents. All contents in this file is
 the original work of the copyright holders at the Division of Fluid and
 Mechatronic Systems (Flumes) at Linköping University. Modifying, using or
 redistributing any part of this file is prohibited without explicit
 permission from the copyright holders.
-----------------------------------------------------------------------------*/

//!
//! @file   loadFunctions.cpp
//! @author Flumes <<EMAIL>>
//! @date   2010-01-01
//!
//! @brief Contains functions used when models are loaded from hmf
//!
//$Id$

#include "loadFunctions.h"

#include "global.h"
#include "GUIObjects/GUIModelObject.h"
#include "GUIObjects/GUIContainerObject.h"
#include "GUIObjects/GUISystem.h"
#include "GUIObjects/GUIWidgets.h"
#include "GUIConnector.h"
#include "GUIPort.h"
#include "LibraryHandler.h"
#include "UndoStack.h"
#include "MessageHandler.h"
#include "Utilities/GUIUtilities.h"

#include <QMap>

//! @brief Loads a Connector from the supplied load data
//! @param[in] rDomElement The DOM element to load from
//! @param[in] pContainer The Container Object to load into
//! @param[in] undoSettings Whether or not to register undo for the operation
bool loadConnector(QDomElement &rDomElement, ContainerObject* pContainer, UndoStatusEnumT undoSettings)
{
    // -----First read from DOM element-----
    QString startComponentName, endComponentName, startPortName, endPortName;
    bool isDashed;
    QVector<QPointF> pointVector;
    QStringList geometryList;

    // Read core specific stuff
    startComponentName  = rDomElement.attribute(HMF_CONNECTORSTARTCOMPONENTTAG);
    startPortName       = rDomElement.attribute(HMF_CONNECTORSTARTPORTTAG);
    endComponentName    = rDomElement.attribute(HMF_CONNECTORENDCOMPONENTTAG);
    endPortName         = rDomElement.attribute(HMF_CONNECTORENDPORTTAG);
    //qDebug() << "loadConnector: " << startComponentName << " " << startPortName << " " << endComponentName << " " << endPortName;

    //! @todo To upconvert old models these are needed, may not be necessary later
    santizeName(startComponentName);
    santizeName(startPortName);
    santizeName(endComponentName);
    santizeName(endPortName);

    // Read gui specific stuff
    double x,y;
    QDomElement guiData         = rDomElement.firstChildElement(HMF_HOPSANGUITAG);
    QDomElement guiCoordinates  = guiData.firstChildElement(HMF_COORDINATES);
    QDomElement coordTag        = guiCoordinates.firstChildElement(HMF_COORDINATETAG);
    while (!coordTag.isNull())
    {
        parseCoordinateTag(coordTag, x, y);
        pointVector.push_back(QPointF(x,y));
        coordTag = coordTag.nextSiblingElement(HMF_COORDINATETAG);
    }
    QDomElement guiGeometries   = guiData.firstChildElement(HMF_GEOMETRIES);
    QDomElement geometryTag     = guiGeometries.firstChildElement(HMF_GEOMETRYTAG);
    while (!geometryTag.isNull())
    {
        geometryList.append(geometryTag.text());
        geometryTag = geometryTag.nextSiblingElement(HMF_GEOMETRYTAG);
    }
    QDomElement styleTag = guiData.firstChildElement(HMF_STYLETAG);
    if(!styleTag.isNull())
    {
        isDashed = (styleTag.text() == "dashed");
    }
    else
    {
        isDashed = false;
    }
    QDomElement colorTag = guiData.firstChildElement(HMF_COLORTAG);
    QColor color =  QColor();
    if(!colorTag.isNull())
    {
        QString rgb = colorTag.text();
        double r, g, b;
        parseRgbString(rgb, r, g, b);
        color.setRgb(r, g, b);
    }

    // -----Now establish the connection-----
    bool success=false;
    Port *startPort = pContainer->getModelObjectPort(startComponentName, startPortName);
    Port *endPort = pContainer->getModelObjectPort(endComponentName, endPortName);

    if (startPort && endPort)
    {
        Connector* pConn = pContainer->createConnector(startPort, endPort, NoUndo);
        if (pConn)
        {
            if(pointVector.isEmpty() && !pConn->isDangling() && !pConn->isBroken())   //Create a diagonal connector if no points were loaded from HMF
            {
                pointVector.push_back(pConn->getStartPort()->boundingRect().center());
                pointVector.push_back(pConn->getEndPort()->boundingRect().center());
                geometryList.clear();
                geometryList.append("diagonal");
            }
            pConn->setPointsAndGeometries(pointVector, geometryList);
            pConn->setDashed(isDashed);
            pConn->refreshConnectorAppearance();
            pConn->setColor(color);

            if(undoSettings == Undo)
            {
                pContainer->getUndoStackPtr()->registerAddedConnector(pConn);
            }
            if (pConn->isConnected())
            {
                success = true;
            }
        }
    }
    else
    {
        success = false;
    }

    gpMessageHandler->collectHopsanCoreMessages();;

    if (!success)
    {
        const QString str("Failed to load connector between: "+startComponentName+"->"+startPortName+" and "+endComponentName+"->"+endPortName+" in system: "+pContainer->getName());
        gpMessageHandler->addErrorMessage(str, "FailedLoadConnector");
    }

    return success;
}



//! @brief xml version
//! @todo Make undo settings work or remove it
//! @todo Make loadParameterValue and loadSystemParameter same function
void loadParameterValue(QDomElement &rDomElement, ModelObject* pObject, UndoStatusEnumT /*undoSettings*/)
{
    QString parameterName;
    QString parameterValue;
    QString parameterType;

    parameterName = rDomElement.attribute(HMF_NAMETAG);<commit_msg>Added error message when loading connector fails due to no points and no end port.<commit_after>/*-----------------------------------------------------------------------------
 This source file is part of Hopsan NG

 Copyright (c) 2011 
    <NAME>, <NAME>, <NAME>, <NAME>,
    <NAME>, <NAME>, <NAME>, <NAME>

 This file is provided "as is", with no guarantee or warranty for the
 functionality or reliability of the contents. All contents in this file is
 the original work of the copyright holders at the Division of Fluid and
 Mechatronic Systems (Flumes) at Linköping University. Modifying, using or
 redistributing any part of this file is prohibited without explicit
 permission from the copyright holders.
-----------------------------------------------------------------------------*/

//!
//! @file   loadFunctions.cpp
//! @author Flumes <<EMAIL>>
//! @date   2010-01-01
//!
//! @brief Contains functions used when models are loaded from hmf
//!
//$Id$

#include "loadFunctions.h"

#include "global.h"
#include "GUIObjects/GUIModelObject.h"
#include "GUIObjects/GUIContainerObject.h"
#include "GUIObjects/GUISystem.h"
#include "GUIObjects/GUIWidgets.h"
#include "GUIConnector.h"
#include "GUIPort.h"
#include "LibraryHandler.h"
#include "UndoStack.h"
#include "MessageHandler.h"
#include "Utilities/GUIUtilities.h"

#include <QMap>

//! @brief Loads a Connector from the supplied load data
//! @param[in] rDomElement The DOM element to load from
//! @param[in] pContainer The Container Object to load into
//! @param[in] undoSettings Whether or not to register undo for the operation
bool loadConnector(QDomElement &rDomElement, ContainerObject* pContainer, UndoStatusEnumT undoSettings)
{
    // -----First read from DOM element-----
    QString startComponentName, endComponentName, startPortName, endPortName;
    bool isDashed;
    QVector<QPointF> pointVector;
    QStringList geometryList;

    // Read core specific stuff
    startComponentName  = rDomElement.attribute(HMF_CONNECTORSTARTCOMPONENTTAG);
    startPortName       = rDomElement.attribute(HMF_CONNECTORSTARTPORTTAG);
    endComponentName    = rDomElement.attribute(HMF_CONNECTORENDCOMPONENTTAG);
    endPortName         = rDomElement.attribute(HMF_CONNECTORENDPORTTAG);
    //qDebug() << "loadConnector: " << startComponentName << " " << startPortName << " " << endComponentName << " " << endPortName;

    //! @todo To upconvert old models these are needed, may not be necessary later
    santizeName(startComponentName);
    santizeName(startPortName);
    santizeName(endComponentName);
    santizeName(endPortName);

    // Read gui specific stuff
    double x,y;
    QDomElement guiData         = rDomElement.firstChildElement(HMF_HOPSANGUITAG);
    QDomElement guiCoordinates  = guiData.firstChildElement(HMF_COORDINATES);
    QDomElement coordTag        = guiCoordinates.firstChildElement(HMF_COORDINATETAG);
    while (!coordTag.isNull())
    {
        parseCoordinateTag(coordTag, x, y);
        pointVector.push_back(QPointF(x,y));
        coordTag = coordTag.nextSiblingElement(HMF_COORDINATETAG);
    }
    QDomElement guiGeometries   = guiData.firstChildElement(HMF_GEOMETRIES);
    QDomElement geometryTag     = guiGeometries.firstChildElement(HMF_GEOMETRYTAG);
    while (!geometryTag.isNull())
    {
        geometryList.append(geometryTag.text());
        geometryTag = geometryTag.nextSiblingElement(HMF_GEOMETRYTAG);
    }
    QDomElement styleTag = guiData.firstChildElement(HMF_STYLETAG);
    if(!styleTag.isNull())
    {
        isDashed = (styleTag.text() == "dashed");
    }
    else
    {
        isDashed = false;
    }
    QDomElement colorTag = guiData.firstChildElement(HMF_COLORTAG);
    QColor color =  QColor();
    if(!colorTag.isNull())
    {
        QString rgb = colorTag.text();
        double r, g, b;
        parseRgbString(rgb, r, g, b);
        color.setRgb(r, g, b);
    }

    // -----Now establish the connection-----
    bool success=false;
    Port *startPort = pContainer->getModelObjectPort(startComponentName, startPortName);
    Port *endPort = pContainer->getModelObjectPort(endComponentName, endPortName);

    if (startPort && endPort)
    {
        Connector* pConn = pContainer->createConnector(startPort, endPort, NoUndo);
        if (pConn)
        {
            if(pointVector.isEmpty() && !pConn->isDangling() && !pConn->isBroken())   //Create a diagonal connector if no points were loaded from HMF
            {
                pointVector.push_back(pConn->getStartPort()->boundingRect().center());
                pointVector.push_back(pConn->getEndPort()->boundingRect().center());
                geometryList.clear();
                geometryList.append("diagonal");
            }
            pConn->setPointsAndGeometries(pointVector, geometryList);
            pConn->setDashed(isDashed);
            pConn->refreshConnectorAppearance();
            pConn->setColor(color);

            if(undoSettings == Undo)
            {
                pContainer->getUndoStackPtr()->registerAddedConnector(pConn);
            }
            if (pConn->isConnected())
            {
                success = true;
            }
        }
        else
        {
            success = false;
        }
    }
    else
    {
        success = false;
    }

    gpMessageHandler->collectHopsanCoreMessages();;

    if (!success)
    {
        const QString str("Failed to load connector between: "+startComponentName+"->"+startPortName+" and "+endComponentName+"->"+endPortName+" in system: "+pContainer->getName());
        gpMessageHandler->addErrorMessage(str, "FailedLoadConnector");
    }

    return success;
}



//! @brief xml version
//! @todo Make undo settings work or remove it
//! @todo Make loadParameterValue and loadSystemParameter same function
void loadParameterValue(QDomElement &rDomElement, ModelObject* pObject, UndoStatusEnumT /*undoSettings*/)
{
    QString parameterName;
    QString parameterValue;
    QString parameterType;

    parameterName = rDomElement.attribute(HMF_NAMETAG);
{'fraction_non_alphanumeric': 0.1329479768786127, 'fraction_numerical': 0.0020105554159336515, 'mean_word_length': 2.9013071895424836, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2198718', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3411, 'n_words': 806}