<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 "SaveDataReaction.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "ModuleManager.h"
#include "pqActiveObjects.h"
#include "pqCoreUtilities.h"
#include "pqSaveDataReaction.h"
#include "pqPipelineSource.h"
#include "pqProxyWidgetDialog.h"
#include "pqFileDialog.h"
#include "vtkDataObject.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkSMCoreUtilities.h"
#include "vtkSMParaViewPipelineController.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMWriterFactory.h"
#include "vtkTrivialProducer.h"

#include <cassert>

#include <QDebug>
#include <QMessageBox>

namespace tomviz
{

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

SaveDataReaction::~SaveDataReaction()
{
}

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

void SaveDataReaction::onTriggered()
{
  pqServer* server = pqActiveObjects::instance().activeServer();
  DataSource *source = ActiveObjects::instance().activeDataSource();
  assert(source);
  vtkSMWriterFactory* writerFactory =
      vtkSMProxyManager::GetProxyManager()->GetWriterFactory();
  QString filters = writerFactory->GetSupportedFileTypes(source->producer());
  if (filters.isEmpty())
  {
    qCritical("Cannot determine writer to use.");
    return;
  }

  pqFileDialog fileDialog(server,
                          pqCoreUtilities::mainWidget(),
                          tr("Save File:"), QString(), filters);
  fileDialog.setObjectName("FileSaveDialog");
  fileDialog.setFileMode(pqFileDialog::AnyFile);
  // Default to saving tiff files
  fileDialog.setRecentlyUsedExtension(".tiff");
  if (fileDialog.exec() == QDialog::Accepted)
  {
    this->saveData(fileDialog.getSelectedFiles()[0]);
  }
}

bool SaveDataReaction::saveData(const QString &filename)
{
  pqServer* server = pqActiveObjects::instance().activeServer();
  DataSource *source = ActiveObjects::instance().activeDataSource();
  if (!server || !source)
  {<commit_msg>Disable saving data as PNG or JPEG<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 "SaveDataReaction.h"

#include "ActiveObjects.h"
#include "DataSource.h"
#include "ModuleManager.h"
#include "pqActiveObjects.h"
#include "pqCoreUtilities.h"
#include "pqSaveDataReaction.h"
#include "pqPipelineSource.h"
#include "pqProxyWidgetDialog.h"
#include "pqFileDialog.h"
#include "vtkDataObject.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkSMCoreUtilities.h"
#include "vtkSMParaViewPipelineController.h"
#include "vtkSMPropertyHelper.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMWriterFactory.h"
#include "vtkTrivialProducer.h"

#include <cassert>

#include <QDebug>
#include <QMessageBox>
#include <QRegularExpression>

namespace tomviz
{

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

SaveDataReaction::~SaveDataReaction()
{
}

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

void SaveDataReaction::onTriggered()
{
  pqServer* server = pqActiveObjects::instance().activeServer();
  DataSource *source = ActiveObjects::instance().activeDataSource();
  assert(source);
  vtkSMWriterFactory* writerFactory =
      vtkSMProxyManager::GetProxyManager()->GetWriterFactory();
  QString filters = writerFactory->GetSupportedFileTypes(source->producer());
  if (filters.isEmpty())
  {
    qCritical("Cannot determine writer to use.");
    return;
  }
  // Remove options to output in JPEG or PNG format since they don't support volumes
  QRegularExpression re(";;(JPEG|PNG)[^;]*;;");
  QString filteredFilters = filters.replace(re, ";;");

  pqFileDialog fileDialog(server,
                          pqCoreUtilities::mainWidget(),
                          tr("Save File:"), QString(), filteredFilters);
  fileDialog.setObjectName("FileSaveDialog");
  fileDialog.setFileMode(pqFileDialog::AnyFile);
  // Default to saving tiff files
  fileDialog.setRecentlyUsedExtension(".tiff");
  if (fileDialog.exec() == QDialog::Accepted)
  {
    this->saveData(fileDialog.getSelectedFiles()[0]);
  }
}

bool SaveDataReaction::saveData(const QString &filename)
{
  pqServer* server = pqActiveObjects::instance().activeServer();
  DataSource *source = ActiveObjects::instance().activeDataSource();
  if (!server || !source)
  {
{'fraction_non_alphanumeric': 0.16528791565287915, 'fraction_numerical': 0.00032441200324412003, 'mean_word_length': 5.790748898678414, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 20, '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': '6246203', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1741, 'n_words': 411}