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

  This source file is part of the tomviz project.

  Copyright Kitware, Inc.

  This source code is released under the New BSD License, (the "License").

  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.

******************************************************************************/
#include "AddExpressionReaction.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "EditOperatorDialog.h"
#include "OperatorPython.h"
#include "Utilities.h"

#include <pqCoreUtilities.h>

namespace tomviz {

AddExpressionReaction::AddExpressionReaction(QAction* parentObject)
  : Superclass(parentObject)
{
  connect(&ActiveObjects::instance(), SIGNAL(dataSourceChanged(DataSource*)),
          SLOT(updateEnableState()));
  updateEnableState();
}

void AddExpressionReaction::updateEnableState()
{
  parentAction()->setEnabled(
    ActiveObjects::instance().activeDataSource() != nullptr);
}

OperatorPython* AddExpressionReaction::addExpression(DataSource* source)
{
  source = source ? source : ActiveObjects::instance().activeDataSource();
  if (!source) {
    return nullptr;
  }

  QString script = getDefaultExpression(source);

  OperatorPython* opPython = new OperatorPython();
  opPython->setScript(script);
  opPython->setLabel("Transform Data");

  // Create a non-modal dialog, delete it once it has been closed.
  EditOperatorDialog* dialog = new EditOperatorDialog(
    opPython, source, true, pqCoreUtilities::mainWidget());
  dialog->setAttribute(Qt::WA_DeleteOnClose, true);
  dialog->show();
  connect(opPython, SIGNAL(destroyed()), dialog, SLOT(reject()));
  return nullptr;
}

QString AddExpressionReaction::getDefaultExpression(DataSource* source)
{
  QString actionString = parentAction()->text();
  if (actionString == "Custom ITK Transform") {
    return readInPythonScript("DefaultITKTransform");
  } else {
    // Build the default script for the python operator
    // This was done in the Dialog's UI file, but since it needs to change
    // based on the type of dataset, do it here
    return QString("def transform_scalars(dataset):\n"<commit_msg>Add warning comment not to change function name<commit_after>/******************************************************************************

  This source file is part of the tomviz project.

  Copyright Kitware, Inc.

  This source code is released under the New BSD License, (the "License").

  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.

******************************************************************************/
#include "AddExpressionReaction.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "EditOperatorDialog.h"
#include "OperatorPython.h"
#include "Utilities.h"

#include <pqCoreUtilities.h>

namespace tomviz {

AddExpressionReaction::AddExpressionReaction(QAction* parentObject)
  : Superclass(parentObject)
{
  connect(&ActiveObjects::instance(), SIGNAL(dataSourceChanged(DataSource*)),
          SLOT(updateEnableState()));
  updateEnableState();
}

void AddExpressionReaction::updateEnableState()
{
  parentAction()->setEnabled(
    ActiveObjects::instance().activeDataSource() != nullptr);
}

OperatorPython* AddExpressionReaction::addExpression(DataSource* source)
{
  source = source ? source : ActiveObjects::instance().activeDataSource();
  if (!source) {
    return nullptr;
  }

  QString script = getDefaultExpression(source);

  OperatorPython* opPython = new OperatorPython();
  opPython->setScript(script);
  opPython->setLabel("Transform Data");

  // Create a non-modal dialog, delete it once it has been closed.
  EditOperatorDialog* dialog = new EditOperatorDialog(
    opPython, source, true, pqCoreUtilities::mainWidget());
  dialog->setAttribute(Qt::WA_DeleteOnClose, true);
  dialog->show();
  connect(opPython, SIGNAL(destroyed()), dialog, SLOT(reject()));
  return nullptr;
}

QString AddExpressionReaction::getDefaultExpression(DataSource* source)
{
  QString actionString = parentAction()->text();
  if (actionString == "Custom ITK Transform") {
    return readInPythonScript("DefaultITKTransform");
  } else {
    // Build the default script for the python operator
    // This was done in the Dialog's UI file, but since it needs to change
    // based on the type of dataset, do it here
    return QString("# Transform entry point, do not change function name.\n"
                   "def transform_scalars(dataset):\n"
{'fraction_non_alphanumeric': 0.15955101222689919, 'fraction_numerical': 0.0, 'mean_word_length': 5.413881748071979, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5504138', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1252, 'n_words': 409}