COPASI API  4.16.103
CopasiFileDialog.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <QtGui/QFileDialog>
16 
17 #include "copasi.h"
18 
19 #include "CopasiFileDialog.h"
20 #include "qtUtilities.h"
21 #include "CQMessageBox.h"
22 
23 #include "commandline/COptions.h"
24 #include "utilities/CDirEntry.h"
27 
28 #ifdef DEBUG_UI
29 #include <QtCore/QtDebug>
30 #endif
31 
32 // static
33 QString CopasiFileDialog::StartWith(const QString & startWith)
34 {
35  QString WorkingDirectory = FROM_UTF8(CCopasiRootContainer::getConfiguration()->getWorkingDirectory());
36 
37  if (startWith.isNull())
38  {
39  return WorkingDirectory;
40  }
41  else if (CDirEntry::dirName(TO_UTF8(startWith)) == "")
42  {
43  return WorkingDirectory + "/" + startWith;
44  }
45 
46  return startWith;
47 }
48 
49 // static
51 {
52  std::string ExampleDir;
53  COptions::getValue("ExampleDir", ExampleDir);
54 
55  if (CDirEntry::isDir(ExampleDir))
57  else
58  CQMessageBox::information(NULL, "Directory Not Found", FROM_UTF8(ExampleDir),
59  QMessageBox::Ok, QMessageBox::Ok);
60 }
61 
62 // static
63 QString CopasiFileDialog::getOpenFileName(QWidget * parent,
64  const char * /* name */,
65  const QString & startWith,
66  const QString & filter,
67  const QString & caption,
68  QString * pSelectedFilter,
69  QFileDialog::Options options)
70 {
71  QString newFilter = filter;
72 
73  if (filter.indexOf("(*)") < 0)
74  newFilter += ";;Any File (*)";
75 
76  QString newFile = QFileDialog::getOpenFileName(parent,
77  caption,
78  StartWith(startWith),
79  newFilter,
80  pSelectedFilter,
81  options);
82 
83  if (newFile != "")
85 
86  return newFile;
87 }
88 
89 // static
90 QString CopasiFileDialog::getSaveFileName(QWidget * parent,
91  const char * /* name */,
92  const QString & startWith,
93  const QString & filter,
94  const QString & caption,
95  QString * pSelectedFilter,
96  QFileDialog::Options options)
97 {
98  QString newFile, newFilter = filter;
99  bool fileValid;
100 
101  if (!pSelectedFilter)
102  pSelectedFilter = new QString("");
103 
104  if (filter.indexOf("(*)") < 0)
105  newFilter += ";;Any File (*)";
106 
107  do
108  {
109  fileValid = true;
110  newFile = QFileDialog::getSaveFileName(parent,
111  caption,
112  StartWith(startWith),
113  newFilter, pSelectedFilter,
114  QFileDialog::DontConfirmOverwrite | options);
115 
116  if (newFile != "")
118  else
119  return QString::null;
120 
121  if (pSelectedFilter->indexOf("(*)") >= 0)
122  {
123  QString fileName = FROM_UTF8(CDirEntry::fileName(TO_UTF8(newFile)));
124  QString suffix = FROM_UTF8(CDirEntry::suffix(TO_UTF8(newFile)));
125 
126  if (!suffix.length())
127  {
128  fileValid = true;
129  }
130  else
131  {
132  QRegExp rx("\\.\\S{1,4}$");
133 
134  if (rx.indexIn(fileName) < 0)
135  {
136  CQMessageBox::information(NULL, "Save File Error",
137  "Filename can have an extension 1 to 4 characters long.",
138  QMessageBox::Ok, QMessageBox::Ok);
139  fileValid = false;
140  continue;
141  }
142 
143  QRegExp rxDigits("\\.\\d{1,4}$");
144 
145  if (rxDigits.indexIn(fileName) > 0)
146  {
147  CQMessageBox::information(NULL, "Save File Error",
148  "All characters in the file extension cannot be digits.",
149  QMessageBox::Ok, QMessageBox::Ok);
150  fileValid = false;
151  continue;
152  }
153  }
154  }
155  }
156  while (fileValid == false);
157 
158 #ifdef DEBUG_UI
159 // qDebug() << "user selected Filter = " << usersFilter;
160  qDebug() << "user's Filter = " << *pSelectedFilter;
161 // qDebug() << "correct extension = " << correctExt;
162 // qDebug() << "filename = " << newFile;
163 #endif
164 
165  return newFile;
166 }
static bool isDir(const std::string &path)
Definition: CDirEntry.cpp:65
#define FROM_UTF8(__x)
Definition: qtUtilities.h:73
static std::string fileName(const std::string &path)
Definition: CDirEntry.cpp:119
static StandardButton information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
void setWorkingDirectory(const std::string &workingDirectory)
static std::string dirName(const std::string &path)
Definition: CDirEntry.cpp:135
static void openExampleDir()
static CConfigurationFile * getConfiguration()
static QString StartWith(const QString &startWith)
static QString getOpenFileName(QWidget *parent=0, const char *name=0, const QString &startWith=QString::null, const QString &filter=QString::null, const QString &caption=QString::null, QString *pSelectedFilter=NULL, QFileDialog::Options options=0)
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
#define TO_UTF8(__x)
Definition: qtUtilities.h:74
static std::string suffix(const std::string &path)
Definition: CDirEntry.cpp:159
static QString getSaveFileName(QWidget *parent=0, const char *name=0, const QString &startWith=QString::null, const QString &filter=QString::null, const QString &caption=QString::null, QString *pSelectedFilter=NULL, QFileDialog::Options options=0)