<commit_before>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Single cell view information graphs widget
//==============================================================================

#include "cellmlfileruntime.h"
#include "filemanager.h"
#include "singlecellviewgraphpanelwidget.h"
#include "singlecellviewinformationgraphswidget.h"
#include "singlecellviewplugin.h"
#include "singlecellviewsimulation.h"
#include "singlecellviewsimulationwidget.h"
#include "singlecellviewwidget.h"

//==============================================================================

#include <QFileInfo>
#include <QMenu>
#include <QScrollBar>
#include <QSettings>

//==============================================================================

#include "ui_singlecellviewinformationgraphswidget.h"

//==============================================================================

namespace OpenCOR {
namespace SingleCellView {

//==============================================================================

SingleCellViewInformationGraphsWidget::SingleCellViewInformationGraphsWidget(SingleCellViewPlugin *pPlugin,
                                                                             SingleCellViewSimulationWidget *pSimulationWidget,
                                                                             QWidget *pParent) :
    QStackedWidget(pParent),
    Core::CommonWidget(pParent),
    mGui(new Ui::SingleCellViewInformationGraphsWidget),
    mPlugin(pPlugin),
    mSimulationWidget(pSimulationWidget),
    mGraphPanels(QMap<Core::PropertyEditorWidget *, SingleCellViewGraphPanelWidget *>()),
    mPropertyEditors(QMap<SingleCellViewGraphPanelWidget *, Core::PropertyEditorWidget *>()),
    mPropertyEditor(0),
    mGraphs(QMap<Core::Property *, SingleCellViewGraphPanelPlotGraph *>()),
    mGraphProperties(QMap<SingleCellViewGraphPanelPlotGraph *, Core::Property *>()),
    mParameterActions(QMap<QAction *, CellMLSupport::CellmlFileRuntimeParameter *>()),
    mHorizontalScrollBarValue(0)
{
    // Set up the GUI

    mGui->setupUi(this);

    // Create our context menus and populate our main context menu

    mContextMenu = new QMenu(this);
    mParametersContextMenu = new QMenu(this);

    mContextMenu->addAction(mGui->actionAddGraph);
    mContextMenu->addSeparator();
    mContextMenu->addAction(mGui->actionRemoveCurrentGraph);
    mContextMenu->addAction(mGui->actionRemoveAllGraphs);
    mContextMenu->addSeparator();
    mContextMenu->addAction(mGui->actionSelectAllGraphs);
    mContextMenu->addAction(mGui->actionUnselectAllGraphs);
}

//==============================================================================

SingleCellViewInformationGraphsWidget::~SingleCellViewInformationGraphsWidget()
{
    // Delete the GUI

    delete mGui;
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::retranslateUi()
{
    // Retranslate our GUI

    mGui->retranslateUi(this);

    // Retranslate all our property editors

    foreach (Core::PropertyEditorWidget *propertyEditor, mPropertyEditors)
        propertyEditor->retranslateUi();

    // Retranslate the information about our graphs properties
    // Note: no need to do this for all our property editors (i.e. call
    //       updateAllGraphsInfo()) since this will automatically be done when
    //       selecting another graph panel. So, instead, we just do this for our
    //       current graph panel...

    updateGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::initialize(SingleCellViewSimulation *pSimulation)
{
    // Populate our parameters context menu

    populateParametersContextMenu(pSimulation->runtime());

    // Update our graphs information

    updateAllGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::finalize()
{
    // Clear our parameters context menu

    mParametersContextMenu->clear();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::initialize(SingleCellViewGraphPanelWidget *pGraphPanel)
{
    // Retrieve the property editor for the given file name or create one, if
    // none exists

    Core::PropertyEditorWidget *oldPropertyEditor = mPropertyEditor;

    mPropertyEditor = mPropertyEditors.value(pGraphPanel);

    if (!mPropertyEditor) {
        // No property editor exists for the given graph panel, so create one

        mPropertyEditor = new Core::PropertyEditorWidget(false, false, this);

        // We want our own context menu for our property editor

        mPropertyEditor->setContextMenuPolicy(Qt::CustomContextMenu);

        connect(mPropertyEditor, SIGNAL(customContextMenuRequested(const QPoint &)),
                this, SLOT(propertyEditorContextMenu(const QPoint &)));

        // Keep track of changes to the horizontal bar's value

        connect(mPropertyEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)),
                this, SLOT(propertyEditorHorizontalScrollBarValueChanged(const int &)));

        // Keep track of changes to columns' width

        connect(mPropertyEditor->header(), SIGNAL(sectionResized(int, int, int)),
                this, SLOT(propertyEditorSectionResized(const int &, const int &, const int &)));

        // Keep track of when the user changes a property value

        connect(mPropertyEditor, SIGNAL(propertyChanged(Core::Property *)),
                this, SLOT(graphChanged(Core::Property *)));

        // Add our new property editor to ourselves

        addWidget(mPropertyEditor);

        // Keep track of the link between our existing graph panel and our new
        // property editor

        mGraphPanels.insert(mPropertyEditor, pGraphPanel);
        mPropertyEditors.insert(pGraphPanel, mPropertyEditor);
    }

    // Set the value of the property editor's horizontal scroll bar

    mPropertyEditor->horizontalScrollBar()->setValue(mHorizontalScrollBarValue);

    // Set our property editor's columns' width

    if (oldPropertyEditor) {
        for (int i = 0, iMax = oldPropertyEditor->header()->count(); i < iMax; ++i)
            mPropertyEditor->setColumnWidth(i, oldPropertyEditor->columnWidth(i));
    }

    // Set our retrieved property editor as our current widget

    setCurrentWidget(mPropertyEditor);

    // Update our graphs information
    // Note: this is in case the user changed the locale and then switched to a
    //       different graph panel...

    updateAllGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::finalize(SingleCellViewGraphPanelWidget *pGraphPanel)
{
    // Remove track of the link betwen our graph panel and our property editor<commit_msg>Some minor cleaning up [ci skip].<commit_after>/*******************************************************************************

Licensed to the OpenCOR team under one or more contributor license agreements.
See the NOTICE.txt file distributed with this work for additional information
regarding copyright ownership. The OpenCOR team licenses this file to you under
the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

*******************************************************************************/

//==============================================================================
// Single cell view information graphs widget
//==============================================================================

#include "cellmlfileruntime.h"
#include "filemanager.h"
#include "singlecellviewgraphpanelwidget.h"
#include "singlecellviewinformationgraphswidget.h"
#include "singlecellviewplugin.h"
#include "singlecellviewsimulation.h"
#include "singlecellviewsimulationwidget.h"
#include "singlecellviewwidget.h"

//==============================================================================

#include <QFileInfo>
#include <QMenu>
#include <QScrollBar>
#include <QSettings>

//==============================================================================

#include "ui_singlecellviewinformationgraphswidget.h"

//==============================================================================

namespace OpenCOR {
namespace SingleCellView {

//==============================================================================

SingleCellViewInformationGraphsWidget::SingleCellViewInformationGraphsWidget(SingleCellViewPlugin *pPlugin,
                                                                             SingleCellViewSimulationWidget *pSimulationWidget,
                                                                             QWidget *pParent) :
    QStackedWidget(pParent),
    Core::CommonWidget(pParent),
    mGui(new Ui::SingleCellViewInformationGraphsWidget),
    mPlugin(pPlugin),
    mSimulationWidget(pSimulationWidget),
    mGraphPanels(QMap<Core::PropertyEditorWidget *, SingleCellViewGraphPanelWidget *>()),
    mPropertyEditors(QMap<SingleCellViewGraphPanelWidget *, Core::PropertyEditorWidget *>()),
    mPropertyEditor(0),
    mGraphs(QMap<Core::Property *, SingleCellViewGraphPanelPlotGraph *>()),
    mGraphProperties(QMap<SingleCellViewGraphPanelPlotGraph *, Core::Property *>()),
    mParameterActions(QMap<QAction *, CellMLSupport::CellmlFileRuntimeParameter *>()),
    mHorizontalScrollBarValue(0)
{
    // Set up the GUI

    mGui->setupUi(this);

    // Create our context menus and populate our main context menu

    mContextMenu = new QMenu(this);
    mParametersContextMenu = new QMenu(this);

    mContextMenu->addAction(mGui->actionAddGraph);
    mContextMenu->addSeparator();
    mContextMenu->addAction(mGui->actionRemoveCurrentGraph);
    mContextMenu->addAction(mGui->actionRemoveAllGraphs);
    mContextMenu->addSeparator();
    mContextMenu->addAction(mGui->actionSelectAllGraphs);
    mContextMenu->addAction(mGui->actionUnselectAllGraphs);
}

//==============================================================================

SingleCellViewInformationGraphsWidget::~SingleCellViewInformationGraphsWidget()
{
    // Delete the GUI

    delete mGui;
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::retranslateUi()
{
    // Retranslate our GUI

    mGui->retranslateUi(this);

    // Retranslate all our property editors

    foreach (Core::PropertyEditorWidget *propertyEditor, mPropertyEditors)
        propertyEditor->retranslateUi();

    // Retranslate the information about our graphs properties
    // Note: no need to do this for all our property editors (i.e. call
    //       updateAllGraphsInfo()) since this will automatically be done when
    //       selecting another graph panel. So, instead, we just do this for our
    //       current graph panel...

    updateGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::initialize(SingleCellViewSimulation *pSimulation)
{
    // Populate our parameters context menu

    populateParametersContextMenu(pSimulation->runtime());

    // Update our graphs information

    updateAllGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::finalize()
{
    // Clear our parameters context menu

    mParametersContextMenu->clear();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::initialize(SingleCellViewGraphPanelWidget *pGraphPanel)
{
    // Retrieve the property editor for the given file name or create one, if
    // none exists

    Core::PropertyEditorWidget *oldPropertyEditor = mPropertyEditor;

    mPropertyEditor = mPropertyEditors.value(pGraphPanel);

    if (!mPropertyEditor) {
        // No property editor exists for the given graph panel, so create one

        mPropertyEditor = new Core::PropertyEditorWidget(false, false, this);

        // We want our own context menu for our property editor

        mPropertyEditor->setContextMenuPolicy(Qt::CustomContextMenu);

        connect(mPropertyEditor, SIGNAL(customContextMenuRequested(const QPoint &)),
                this, SLOT(propertyEditorContextMenu(const QPoint &)));

        // Keep track of changes to the horizontal bar's value

        connect(mPropertyEditor->horizontalScrollBar(), SIGNAL(valueChanged(int)),
                this, SLOT(propertyEditorHorizontalScrollBarValueChanged(const int &)));

        // Keep track of changes to columns' width

        connect(mPropertyEditor->header(), SIGNAL(sectionResized(int, int, int)),
                this, SLOT(propertyEditorSectionResized(const int &, const int &, const int &)));

        // Keep track of when the user changes a property value

        connect(mPropertyEditor, SIGNAL(propertyChanged(Core::Property *)),
                this, SLOT(graphChanged(Core::Property *)));

        // Add our new property editor to ourselves

        addWidget(mPropertyEditor);

        // Keep track of the link between our existing graph panel and our new
        // property editor

        mGraphPanels.insert(mPropertyEditor, pGraphPanel);
        mPropertyEditors.insert(pGraphPanel, mPropertyEditor);
    }

    // Set the value of the property editor's horizontal scroll bar

    mPropertyEditor->horizontalScrollBar()->setValue(mHorizontalScrollBarValue);

    // Make sure that our 'new' property editor's columns' width is the same as
    // that of our 'old' property editor

    if (oldPropertyEditor) {
        for (int i = 0, iMax = oldPropertyEditor->header()->count(); i < iMax; ++i)
            mPropertyEditor->setColumnWidth(i, oldPropertyEditor->columnWidth(i));
    }

    // Set our retrieved property editor as our current widget

    setCurrentWidget(mPropertyEditor);

    // Update our graphs information
    // Note: this is in case the user changed the locale and then switched to a
    //       different graph panel...

    updateAllGraphsInfo();
}

//==============================================================================

void SingleCellViewInformationGraphsWidget::finalize(SingleCellViewGraphPanelWidget *pGraphPanel)
{
    // Remove track of the link betwen our graph panel and our property editor
{'fraction_non_alphanumeric': 0.21312716945408647, 'fraction_numerical': 0.0008835594824865888, 'mean_word_length': 4.402659393112854, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '537594', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3525, 'n_words': 1015}