<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   ComponentPropertiesDialog.cpp
//! @author <NAME> <<EMAIL>>
//! @date   2010-03-01
//!
//! @brief Contains a dialog class for changing component properties
//!
//$Id$

#include <QtGui>
#include <cassert>
#include <iostream>

#include "ComponentPropertiesDialog.h"
#include "../MainWindow.h"
#include "../GUIPort.h"
#include "../Widgets/MessageWidget.h"
#include "../GUIObjects/GUIComponent.h"
#include "../GUIObjects/GUIContainerObject.h"
#include "../GUIObjects/GUISystem.h"
#include "../UndoStack.h"
#include "../Widgets/ProjectTabWidget.h"
#include "../Widgets/SystemParametersWidget.h"
#include "../Widgets/LibraryWidget.h"
#include "../Configuration.h"
#include "../Utilities/GUIUtilities.h"


//! @class ComponentPropertiesDialog
//! @brief The ComponentPropertiesDialog class is a Widget used to interact with component parameters.
//!
//! It reads and writes parameters to the core components.
//!


//! @brief Constructor for the parameter dialog for components
//! @param pGUIComponent Pointer to the component
//! @param parent Pointer to the parent widget
ComponentPropertiesDialog::ComponentPropertiesDialog(GUIComponent *pGUIComponent, MainWindow *parent)
    : QDialog(parent)
{
    mpGUIComponent = pGUIComponent;
    this->setPalette(gConfig.getPalette());
    createEditStuff();
}


//! @brief Check if the parameter is a start value
//! @param [in,out] parameterDescription The description of the parameter/startvalue
//! @returns true if it is a startvalue, otherwise false
//!
//! This method is used to determine whether or not a parameter should be interpretted
//! as a start value by the GUI. In HOPSANcore there is no difference between parameters
//! and start values. The start values are registred and stored in the same container.
//! But, a start value is taged by "startvalue:" in the description.
bool ComponentPropertiesDialog::interpretedAsStartValue(QString &parameterDescription)
{
    QString startValueString = "startvalue:";
    bool res=false;
    if(parameterDescription.contains(startValueString, Qt::CaseInsensitive))
    {
        parameterDescription.remove(startValueString, Qt::CaseInsensitive);
        res = true;
    }
    return res;
}


//! @brief Creates the contents in the parameter dialog
void ComponentPropertiesDialog::createEditStuff()
{
    mpNameEdit = new QLineEdit(mpGUIComponent->getName(), this);

    QFont fontH1;
    fontH1.setBold(true);

//    QFont fontH2;
//    fontH2.setBold(true);
//    fontH2.setItalic(true);

    QLabel *pHelpPicture = new QLabel();
    QPixmap helpPixMap;
    helpPixMap.load(mpGUIComponent->getAppearanceData()->getBasePath() + mpGUIComponent->getHelpPicture());
    pHelpPicture->setPixmap(helpPixMap);

    QLabel *pHelpHeading = new QLabel(gpMainWindow->mpLibrary->getAppearanceData(mpGUIComponent->getTypeName())->getName(), this);
    pHelpHeading->setAlignment(Qt::AlignCenter);
    QFont tempFont = pHelpHeading->font();
    tempFont.setPixelSize(16);
    tempFont.setBold(true);
    pHelpHeading->setFont(tempFont);
    QLabel *pHelpText = new QLabel(mpGUIComponent->getHelpText(), this);
    pHelpText->setWordWrap(true);
    QLabel *pParameterLabel = new QLabel("Parameters", this);
    pParameterLabel->setFont(fontH1);
    QLabel *pStartValueLabel = new QLabel("Start Values", this);
    pStartValueLabel->setFont(fontH1);

    QGridLayout *parameterLayout = new QGridLayout();
    QGridLayout *startValueLayout = new QGridLayout();

    QVector<QString> qParameterNames, qParameterValues, qDescriptions, qUnits, qTypes;
    mpGUIComponent->getParameters(qParameterNames, qParameterValues, qDescriptions, qUnits, qTypes);
    size_t nParam=0;
    size_t nStV=0;
    for(int i=0; i<qParameterNames.size(); ++i)
    {
        if(interpretedAsStartValue(qDescriptions[i]))
        {
            QString unit = gConfig.getDefaultUnit(qParameterNames[i].section("::", 1, 1));
            unit.prepend("[");
            unit.append("]");
            mvStartValueLayout.push_back(new ParameterLayout(qParameterNames[i], qDescriptions[i],
                                                             qParameterValues[i],
                                                             unit,
                                                             qTypes[i],
                                                             mpGUIComponent));
            startValueLayout->addLayout(mvStartValueLayout.back(), nParam, 0);
            ++nParam;
        }
        else
        {
            mvParameterLayout.push_back(new ParameterLayout(qParameterNames[i], qDescriptions[i],
                                                            qParameterValues[i],
                                                            qUnits[i],
                                                            qTypes[i],
                                                            mpGUIComponent));
            parameterLayout->addLayout(mvParameterLayout.back(), nStV, 0);
            ++nStV;
        }
    }

    //Adjust sizes of labels, to make sure that all text is visible and that the spacing is not too big between them
    int descriptionSize=30;
    int nameSize = 10;
    //Paramters
    for(int i=0; i<mvParameterLayout.size(); ++i)
    {
        descriptionSize = std::max(descriptionSize, mvParameterLayout.at(i)->mDescriptionNameLabel.width());
        nameSize = std::max(nameSize, mvParameterLayout.at(i)->mDataNameLabel.width());
    }
    //Start values
    for(int i=0; i<mvStartValueLayout.size(); ++i)<commit_msg>Made start values for multi ports hidden in GUI<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   ComponentPropertiesDialog.cpp
//! @author <NAME> <<EMAIL>>
//! @date   2010-03-01
//!
//! @brief Contains a dialog class for changing component properties
//!
//$Id$

#include <QtGui>
#include <cassert>
#include <iostream>

#include "ComponentPropertiesDialog.h"
#include "../MainWindow.h"
#include "../GUIPort.h"
#include "../Widgets/MessageWidget.h"
#include "../GUIObjects/GUIComponent.h"
#include "../GUIObjects/GUIContainerObject.h"
#include "../GUIObjects/GUISystem.h"
#include "../UndoStack.h"
#include "../Widgets/ProjectTabWidget.h"
#include "../Widgets/SystemParametersWidget.h"
#include "../Widgets/LibraryWidget.h"
#include "../Configuration.h"
#include "../Utilities/GUIUtilities.h"


//! @class ComponentPropertiesDialog
//! @brief The ComponentPropertiesDialog class is a Widget used to interact with component parameters.
//!
//! It reads and writes parameters to the core components.
//!


//! @brief Constructor for the parameter dialog for components
//! @param pGUIComponent Pointer to the component
//! @param parent Pointer to the parent widget
ComponentPropertiesDialog::ComponentPropertiesDialog(GUIComponent *pGUIComponent, MainWindow *parent)
    : QDialog(parent)
{
    mpGUIComponent = pGUIComponent;
    this->setPalette(gConfig.getPalette());
    createEditStuff();
}


//! @brief Check if the parameter is a start value
//! @param [in,out] parameterDescription The description of the parameter/startvalue
//! @returns true if it is a startvalue, otherwise false
//!
//! This method is used to determine whether or not a parameter should be interpretted
//! as a start value by the GUI. In HOPSANcore there is no difference between parameters
//! and start values. The start values are registred and stored in the same container.
//! But, a start value is taged by "startvalue:" in the description.
bool ComponentPropertiesDialog::interpretedAsStartValue(QString &parameterDescription)
{    
    QString startValueString = "startvalue:";
    bool res=false;
    if(parameterDescription.contains(startValueString, Qt::CaseInsensitive))
    {
        parameterDescription.remove(startValueString, Qt::CaseInsensitive);
        res = true;
    }
    return res;
}


//! @brief Creates the contents in the parameter dialog
void ComponentPropertiesDialog::createEditStuff()
{
    mpNameEdit = new QLineEdit(mpGUIComponent->getName(), this);

    QFont fontH1;
    fontH1.setBold(true);

//    QFont fontH2;
//    fontH2.setBold(true);
//    fontH2.setItalic(true);

    QLabel *pHelpPicture = new QLabel();
    QPixmap helpPixMap;
    helpPixMap.load(mpGUIComponent->getAppearanceData()->getBasePath() + mpGUIComponent->getHelpPicture());
    pHelpPicture->setPixmap(helpPixMap);

    QLabel *pHelpHeading = new QLabel(gpMainWindow->mpLibrary->getAppearanceData(mpGUIComponent->getTypeName())->getName(), this);
    pHelpHeading->setAlignment(Qt::AlignCenter);
    QFont tempFont = pHelpHeading->font();
    tempFont.setPixelSize(16);
    tempFont.setBold(true);
    pHelpHeading->setFont(tempFont);
    QLabel *pHelpText = new QLabel(mpGUIComponent->getHelpText(), this);
    pHelpText->setWordWrap(true);
    QLabel *pParameterLabel = new QLabel("Parameters", this);
    pParameterLabel->setFont(fontH1);
    QLabel *pStartValueLabel = new QLabel("Start Values", this);
    pStartValueLabel->setFont(fontH1);

    QGridLayout *parameterLayout = new QGridLayout();
    QGridLayout *startValueLayout = new QGridLayout();

    QVector<QString> qParameterNames, qParameterValues, qDescriptions, qUnits, qTypes;
    mpGUIComponent->getParameters(qParameterNames, qParameterValues, qDescriptions, qUnits, qTypes);
    size_t nParam=0;
    size_t nStV=0;
    for(int i=0; i<qParameterNames.size(); ++i)
    {
        if(interpretedAsStartValue(qDescriptions[i]))
        {
            QString portName = qDescriptions[i];
            portName.remove("Port ");
            QString portType = mpGUIComponent->getPort(portName)->getPortType();
            qDebug() << "The port type is " << portType;
            if((portType != "MULTIPORT") && (portType != "POWERMULTIPORT") && (portType != "READMULTIPORT"))
            {
                qDebug() << "Doing it!";
                QString unit = gConfig.getDefaultUnit(qParameterNames[i].section("::", 1, 1));
                unit.prepend("[");
                unit.append("]");
                mvStartValueLayout.push_back(new ParameterLayout(qParameterNames[i], qDescriptions[i],
                                                                 qParameterValues[i],
                                                                 unit,
                                                                 qTypes[i],
                                                                 mpGUIComponent));
                startValueLayout->addLayout(mvStartValueLayout.back(), nParam, 0);
                ++nParam;
            }
        }
        else
        {
            mvParameterLayout.push_back(new ParameterLayout(qParameterNames[i], qDescriptions[i],
                                                            qParameterValues[i],
                                                            qUnits[i],
                                                            qTypes[i],
                                                            mpGUIComponent));
            parameterLayout->addLayout(mvParameterLayout.back(), nStV, 0);
            ++nStV;
        }
    }

    //Adjust sizes of labels, to make sure that all text is visible and that the spacing is not too big between them
    int descriptionSize=30;
    int nameSize = 10;
    //Paramters
    for(int i=0; i<mvParameterLayout.size(); ++i)
    {
        descriptionSize = std::max(descriptionSize, mvParameterLayout.at(i)->mDescriptionNameLabel.width());
        nameSize = std::max(nameSize, mvParameterLayout.at(i)->mDataNameLabel.width());
    }
    //Start values
    for(int i=0; i<mvStartValueLayout.size(); ++i)
{'fraction_non_alphanumeric': 0.13377074488614435, 'fraction_numerical': 0.00524893863373215, 'mean_word_length': 2.513015184381779, 'pattern_counts': {'":': 2, '<': 45, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '527702', 'n_tokens_mistral': 3485, 'n_tokens_neox': 3410, 'n_words': 917}