<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   ModelHandler.cpp
//! @author <NAME> <<EMAIL>>
//! @date   2013-05-16
//!
//! @brief Contains a model handler object
//!
//$Id$

#include <QFileDialog>
#include <QMessageBox>


//Hopsan includes
#include "common.h"
#include "global.h"
#include "Configuration.h"
#include "DesktopHandler.h"
#include "GraphicsView.h"
#include "GUIObjects/GUISystem.h"
#include "MessageHandler.h"
#include "LogDataHandler.h"
#include "MainWindow.h"
#include "ModelHandler.h"
#include "SimulationThreadHandler.h"
#include "version_gui.h"
#include "Widgets/DebuggerWidget.h"
#include "MessageHandler.h"
#include "Widgets/LibraryWidget.h"
#include "Widgets/ModelWidget.h"
#include "Widgets/ProjectTabWidget.h"

ModelHandler::ModelHandler(QObject *parent)
    : QObject(parent)
{
    mpDebugger = 0;

    mNumberOfUntitledModels=0;

    mpSimulationThreadHandler = new SimulationThreadHandler(); //!< @todo is this ever deleted

    mCurrentIdx = -1;

    connect(this, SIGNAL(checkMessages()),      gpMessageHandler,    SLOT(collectHopsanCoreMessages()), Qt::UniqueConnection);
}

void ModelHandler::addModelWidget(ModelWidget *pModelWidget, const QString &name, bool detatched)
{
    pModelWidget->setParent(gpCentralTabWidget);    //! @todo Should probably use ModelHandler as parent

    connect(gpMainWindow->mpDebug2Action, SIGNAL(triggered()), pModelWidget, SLOT(generateModelicaCode()));

    // If the Modelwidget should not be hidden then add it as a tab and switch to that tab
    if(!detatched)
    {
        mModelPtrs.append(pModelWidget);
        mCurrentIdx = mModelPtrs.size()-1;
        gpCentralTabWidget->setCurrentIndex(gpCentralTabWidget->addTab(pModelWidget, name));
        emit newModelWidgetAdded();
    }
}


//! @brief Adds a ModelWidget object (a new tab) to itself.
//! @see closeModel(int index)
ModelWidget *ModelHandler::addNewModel(QString modelName, bool hidden)
{
    modelName.append(QString::number(mNumberOfUntitledModels));

    ModelWidget *pNewModelWidget = new ModelWidget(this,gpCentralTabWidget);    //! @todo Should probably use ModelHandler as parent
    pNewModelWidget->getTopLevelSystemContainer()->setName(modelName);

    connect(pNewModelWidget->getTopLevelSystemContainer()->getLogDataHandler(), SIGNAL(dataAddedFromModel(bool)), gpMainWindow->mpShowLossesAction, SLOT(setEnabled(bool)));

    addModelWidget(pNewModelWidget, modelName, hidden);

    pNewModelWidget->setSaved(true);
    mNumberOfUntitledModels += 1;

    return pNewModelWidget;
}


void ModelHandler::setCurrentModel(int idx)
{
    if ( (idx>=0) && (idx<mModelPtrs.size()) )
    {
        mCurrentIdx = idx;
        // Also switch tab if it is visible among the tabs
        if(gpCentralTabWidget->indexOf(mModelPtrs[idx]) != -1)
            gpCentralTabWidget->setCurrentWidget(mModelPtrs[idx]);
    }
}


void ModelHandler::setCurrentModel(ModelWidget *pWidget)
{
    setCurrentModel(mModelPtrs.indexOf(pWidget));
}

ModelWidget *ModelHandler::getModel(const QString &rModelFilePath)
{
    for (int i=0; i<mModelPtrs.size(); ++i)
    {
        if (mModelPtrs[i]->getTopLevelSystemContainer()->getModelFileInfo().filePath() == rModelFilePath)
        {
            return mModelPtrs[i];
        }
    }
    return 0;<commit_msg>Made sure main window connections are refreshed when explitly setting current model in model handler.<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   ModelHandler.cpp
//! @author <NAME> <<EMAIL>>
//! @date   2013-05-16
//!
//! @brief Contains a model handler object
//!
//$Id$

#include <QFileDialog>
#include <QMessageBox>


//Hopsan includes
#include "common.h"
#include "global.h"
#include "Configuration.h"
#include "DesktopHandler.h"
#include "GraphicsView.h"
#include "GUIObjects/GUISystem.h"
#include "MessageHandler.h"
#include "LogDataHandler.h"
#include "MainWindow.h"
#include "ModelHandler.h"
#include "SimulationThreadHandler.h"
#include "version_gui.h"
#include "Widgets/DebuggerWidget.h"
#include "MessageHandler.h"
#include "Widgets/LibraryWidget.h"
#include "Widgets/ModelWidget.h"
#include "Widgets/ProjectTabWidget.h"

ModelHandler::ModelHandler(QObject *parent)
    : QObject(parent)
{
    mpDebugger = 0;

    mNumberOfUntitledModels=0;

    mpSimulationThreadHandler = new SimulationThreadHandler(); //!< @todo is this ever deleted

    mCurrentIdx = -1;

    connect(this, SIGNAL(checkMessages()),      gpMessageHandler,    SLOT(collectHopsanCoreMessages()), Qt::UniqueConnection);
}

void ModelHandler::addModelWidget(ModelWidget *pModelWidget, const QString &name, bool detatched)
{
    pModelWidget->setParent(gpCentralTabWidget);    //! @todo Should probably use ModelHandler as parent

    connect(gpMainWindow->mpDebug2Action, SIGNAL(triggered()), pModelWidget, SLOT(generateModelicaCode()));

    // If the Modelwidget should not be hidden then add it as a tab and switch to that tab
    if(!detatched)
    {
        mModelPtrs.append(pModelWidget);
        mCurrentIdx = mModelPtrs.size()-1;
        gpCentralTabWidget->setCurrentIndex(gpCentralTabWidget->addTab(pModelWidget, name));
        emit newModelWidgetAdded();
    }
}


//! @brief Adds a ModelWidget object (a new tab) to itself.
//! @see closeModel(int index)
ModelWidget *ModelHandler::addNewModel(QString modelName, bool hidden)
{
    modelName.append(QString::number(mNumberOfUntitledModels));

    ModelWidget *pNewModelWidget = new ModelWidget(this,gpCentralTabWidget);    //! @todo Should probably use ModelHandler as parent
    pNewModelWidget->getTopLevelSystemContainer()->setName(modelName);

    connect(pNewModelWidget->getTopLevelSystemContainer()->getLogDataHandler(), SIGNAL(dataAddedFromModel(bool)), gpMainWindow->mpShowLossesAction, SLOT(setEnabled(bool)));

    addModelWidget(pNewModelWidget, modelName, hidden);

    pNewModelWidget->setSaved(true);
    mNumberOfUntitledModels += 1;

    return pNewModelWidget;
}


void ModelHandler::setCurrentModel(int idx)
{
    if ( (idx>=0) && (idx<mModelPtrs.size()) )
    {
        mCurrentIdx = idx;
        // Also switch tab if it is visible among the tabs
        if(gpCentralTabWidget->indexOf(mModelPtrs[idx]) != -1)
            gpCentralTabWidget->setCurrentWidget(mModelPtrs[idx]);
        refreshMainWindowConnections();
    }
}


void ModelHandler::setCurrentModel(ModelWidget *pWidget)
{
    setCurrentModel(mModelPtrs.indexOf(pWidget));
}

ModelWidget *ModelHandler::getModel(const QString &rModelFilePath)
{
    for (int i=0; i<mModelPtrs.size(); ++i)
    {
        if (mModelPtrs[i]->getTopLevelSystemContainer()->getModelFileInfo().filePath() == rModelFilePath)
        {
            return mModelPtrs[i];
        }
    }
    return 0;
{'fraction_non_alphanumeric': 0.15177904951480467, 'fraction_numerical': 0.005473998507091316, 'mean_word_length': 3.6602898550724636, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6057813', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2314, 'n_words': 588}