<commit_before>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/UI/CopasiFileDialog.cpp,v $
//   $Revision: 1.14 $
//   $Name:  $
//   $Author: pwilly $
//   $Date: 2008/07/25 05:53:24 $
// End CVS Header

// Copyright (C) 2008 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

// Copyright (C) 2001 - 2007 by <NAME>, Virginia Tech Intellectual
// Properties, Inc. and EML Research, gGmbH.
// All rights reserved.

#include <qapplication.h>
#include <qlayout.h>
#include <qtoolbutton.h>
#include <qfileinfo.h>

#include "copasi.h"

#include "CopasiFileDialog.h"
#include "qtUtilities.h"
#include "CQMessageBox.h"

#include "commandline/COptions.h"
#include "utilities/CDirEntry.h"

QDir CopasiFileDialog::mLastDir; // = QDir::current();

CopasiFileDialog::CopasiFileDialog(QWidget * parent ,
                                   const char * name ,
                                   bool modal)
    : QFileDialog(parent , name , modal)
{
  setDir(mLastDir);

  mpGrp = new CQFileDialogBtnGrp(this);
  addLeftWidget(mpGrp);

  connect(mpGrp->mpBtnExamples, SIGNAL(pressed()),
          this, SLOT(slotExampleDir()));
  connect(mpGrp->mpBtnHome, SIGNAL(pressed()),
          this, SLOT(slotHomeDir()));
}

CopasiFileDialog::~CopasiFileDialog()
{
  const QDir * pTmp = dir();
  mLastDir = * pTmp;
  delete pTmp;
}

QString CopasiFileDialog::getOpenFileName(const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)

{
  if (!startWith.isNull()) this->setSelection(startWith);
  if (!filter.isNull()) this->setFilters(filter);
  if (!caption.isNull()) this->setCaption(caption);
  if (selectedFilter) this->setSelectedFilter(selectedFilter);

  this->setMode(QFileDialog::ExistingFile);

  QString newFile = "";
  if (this->exec() == QDialog::Accepted)
    {
      newFile = this->selectedFile();
      return newFile;
    }
  return NULL;
}

QString CopasiFileDialog::getSaveFileName(const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  if (!startWith.isNull()) this->setSelection(startWith);
  if (!filter.isNull()) this->setFilters(filter);
  if (!caption.isNull()) this->setCaption(caption);
  if (selectedFilter) this->setSelectedFilter(selectedFilter);

  this->setMode(QFileDialog::AnyFile);

  QString newFile = "";
  if (this->exec() == QDialog::Accepted)
    {
      newFile = this->selectedFile();
      return newFile;
    }
  return NULL;
}

void CopasiFileDialog::slotExampleDir()
{
  std::string ExampleDir;
  COptions::getValue("ExampleDir", ExampleDir);

  if (CDirEntry::isDir(ExampleDir))
    {
      setDir(FROM_UTF8(ExampleDir));
    }
  else
    {
      CQMessageBox::information(this, "Directory Not Found", FROM_UTF8(ExampleDir),
                                QMessageBox::Ok, 0);
      mpGrp->mpBtnExamples->setDown(false);
    }
}

void CopasiFileDialog::slotHomeDir()
{
  std::string homeDir;
  COptions::getValue("Home", homeDir);

  if (CDirEntry::isDir(homeDir))
    {
      setDir(FROM_UTF8(homeDir));
    }
  else
    {
      CQMessageBox::information(this, "Directory Not Found", FROM_UTF8(homeDir),
                                QMessageBox::Ok, 0);
      mpGrp->mpBtnHome->setDown(false);
    }
}

QString CopasiFileDialog::getOpenFileName(QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getOpenFileName(startWith, filter, caption, selectedFilter);
  delete pDialog;

  return File;
}

QString CopasiFileDialog::getSaveFileName(QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getSaveFileName(startWith, filter, caption, selectedFilter);
  if (!File) return "";

  // check the extension and replace the uncorrect extension with the correct one according to the filter
  QString Filter;

  if (pDialog->selectedFilter().contains("png"))
    Filter = "png";
  else if (pDialog->selectedFilter().contains("svg"))
    Filter = "svg";
  else if (pDialog->selectedFilter().contains("tex"))
    Filter = "tex";
  else if (pDialog->selectedFilter().contains("mml"))
    Filter = "mml";
  else if (pDialog->selectedFilter().contains("xml"))
    Filter = "xml";

  if (!File.endsWith("." + Filter))
    {
      if (File.contains("."))
        {
          int pos = File.find(".");
          File.truncate(pos);
        }

      File += "." + Filter;
    }

  delete pDialog;

  return File;
}

QString CopasiFileDialog::getSaveFileNameAndFilter(QString & newFilter,
    QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getSaveFileName(startWith, filter, caption, selectedFilter);
  if (!File) return "";

  newFilter = pDialog->selectedFilter();

  // check the extension and replace the uncorrect extension with the correct one according to the filter
  QString Filter;

  if (newFilter.contains("png"))
    Filter = "png";
  else if (newFilter.contains("svg"))
    Filter = "svg";
  else if (newFilter.contains("tex"))
    Filter = "tex";
  else if (newFilter.contains("mml"))
    Filter = "mml";
  else if (newFilter.contains("xml"))
    Filter = "xml";

  if (!File.endsWith("." + Filter))
    {
      if (File.contains("."))
        {
          int pos = File.find(".");
          File.truncate(pos);
        }

      File += "." + Filter;
    }

  delete pDialog;

  return File;
}
<commit_msg>The filter is only applied when requested. Enforcing the filter breaks allowing the user to specify a COPASI file without an extesnion by ending the file name with a "."<commit_after>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/UI/CopasiFileDialog.cpp,v $
//   $Revision: 1.15 $
//   $Name:  $
//   $Author: shoops $
//   $Date: 2008/08/04 18:48:37 $
// End CVS Header

// Copyright (C) 2008 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

// Copyright (C) 2001 - 2007 by <NAME>, Virginia Tech Intellectual
// Properties, Inc. and EML Research, gGmbH.
// All rights reserved.

#include <qapplication.h>
#include <qlayout.h>
#include <qtoolbutton.h>
#include <qfileinfo.h>

#include "copasi.h"

#include "CopasiFileDialog.h"
#include "qtUtilities.h"
#include "CQMessageBox.h"

#include "commandline/COptions.h"
#include "utilities/CDirEntry.h"

QDir CopasiFileDialog::mLastDir; // = QDir::current();

CopasiFileDialog::CopasiFileDialog(QWidget * parent ,
                                   const char * name ,
                                   bool modal)
    : QFileDialog(parent , name , modal)
{
  setDir(mLastDir);

  mpGrp = new CQFileDialogBtnGrp(this);
  addLeftWidget(mpGrp);

  connect(mpGrp->mpBtnExamples, SIGNAL(pressed()),
          this, SLOT(slotExampleDir()));
  connect(mpGrp->mpBtnHome, SIGNAL(pressed()),
          this, SLOT(slotHomeDir()));
}

CopasiFileDialog::~CopasiFileDialog()
{
  const QDir * pTmp = dir();
  mLastDir = * pTmp;
  delete pTmp;
}

QString CopasiFileDialog::getOpenFileName(const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)

{
  if (!startWith.isNull()) this->setSelection(startWith);
  if (!filter.isNull()) this->setFilters(filter);
  if (!caption.isNull()) this->setCaption(caption);
  if (selectedFilter) this->setSelectedFilter(selectedFilter);

  this->setMode(QFileDialog::ExistingFile);

  QString newFile = "";
  if (this->exec() == QDialog::Accepted)
    {
      newFile = this->selectedFile();
      return newFile;
    }
  return NULL;
}

QString CopasiFileDialog::getSaveFileName(const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  if (!startWith.isNull()) this->setSelection(startWith);
  if (!filter.isNull()) this->setFilters(filter);
  if (!caption.isNull()) this->setCaption(caption);
  if (selectedFilter) this->setSelectedFilter(selectedFilter);

  this->setMode(QFileDialog::AnyFile);

  QString newFile = "";
  if (this->exec() == QDialog::Accepted)
    {
      newFile = this->selectedFile();
      return newFile;
    }
  return NULL;
}

void CopasiFileDialog::slotExampleDir()
{
  std::string ExampleDir;
  COptions::getValue("ExampleDir", ExampleDir);

  if (CDirEntry::isDir(ExampleDir))
    {
      setDir(FROM_UTF8(ExampleDir));
    }
  else
    {
      CQMessageBox::information(this, "Directory Not Found", FROM_UTF8(ExampleDir),
                                QMessageBox::Ok, 0);
      mpGrp->mpBtnExamples->setDown(false);
    }
}

void CopasiFileDialog::slotHomeDir()
{
  std::string homeDir;
  COptions::getValue("Home", homeDir);

  if (CDirEntry::isDir(homeDir))
    {
      setDir(FROM_UTF8(homeDir));
    }
  else
    {
      CQMessageBox::information(this, "Directory Not Found", FROM_UTF8(homeDir),
                                QMessageBox::Ok, 0);
      mpGrp->mpBtnHome->setDown(false);
    }
}

QString CopasiFileDialog::getOpenFileName(QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getOpenFileName(startWith, filter, caption, selectedFilter);
  delete pDialog;

  return File;
}

QString CopasiFileDialog::getSaveFileName(QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getSaveFileName(startWith, filter, caption, selectedFilter);
  delete pDialog;

  return File;
}

QString CopasiFileDialog::getSaveFileNameAndFilter(QString & newFilter,
    QWidget * parent,
    const char * name,
    const QString & startWith,
    const QString & filter,
    const QString & caption,
    QString selectedFilter)
{
  CopasiFileDialog * pDialog = new CopasiFileDialog(parent, name, true);

  QString File = pDialog->getSaveFileName(startWith, filter, caption, selectedFilter);
  if (!File) return "";

  newFilter = pDialog->selectedFilter();

  // check the extension and replace the uncorrect extension with the correct one according to the filter
  QString Filter;

  if (newFilter.contains("png"))
    Filter = "png";
  else if (newFilter.contains("svg"))
    Filter = "svg";
  else if (newFilter.contains("tex"))
    Filter = "tex";
  else if (newFilter.contains("mml"))
    Filter = "mml";
  else if (newFilter.contains("xml"))
    Filter = "xml";

  if (!File.endsWith("." + Filter))
    {
      if (File.contains("."))
        {
          int pos = File.find(".");
          File.truncate(pos);
        }

      File += "." + Filter;
    }

  delete pDialog;

  return File;
}

{'fraction_non_alphanumeric': 0.1331767571329158, 'fraction_numerical': 0.006089074460681976, 'mean_word_length': 3.406669221924109, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7295170', 'n_tokens_mistral': 3710, 'n_tokens_neox': 3594, 'n_words': 847}