FileDialog.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/utils/FileDialog.cpp
22 
23  \brief An instance of QFileDialog that includes file extention if necessary.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../core/translator/Translator.h"
29 #include "../../../dataaccess/datasource/DataSource.h"
30 #include "../../../dataaccess/datasource/DataSourceCapabilities.h"
31 #include "../../../dataaccess/datasource/DataSourceFactory.h"
32 #include "../Utils.h"
33 #include "FileDialog.h"
34 
35 // STL
36 #include <map>
37 #include <memory>
38 #include <string>
39 
40 // Qt
41 #include <QString>
42 
43 // BOOST
44 #include <boost/filesystem.hpp>
45 #include <boost/regex.hpp>
46 
47 te::qt::widgets::FileDialog::FileDialog(QWidget *parent, FileType type, std::string fileInfo):
48  m_parent(parent),
49  m_type(type),
50  m_fileInfo(fileInfo)
51 {
52 }
53 
55 {
56  QString extensions = "";
57  std::map<QString, QString> mappedExtensions;
58 
59  if(m_type == RASTER)
60  extensions = GetDiskRasterFileSelFilter(true);
61  else if(m_type == VECTOR)
62  extensions = GetDiskVectorFileSelFilter();
63 
64  mappedExtensions = getMappedExtensions(extensions);
65 
66  QString filteredExtention;
67 
68  QFileDialog fileDialog(m_parent);
69 
70  QString filePath = fileDialog.getSaveFileName(m_parent,
71  QString(TE_TR("Save as...")),
73  extensions,
74  &filteredExtention,
75  QFileDialog::DontConfirmOverwrite);
76 
77  if (filePath.isEmpty())
78  throw te::common::Exception(TE_TR("File path is empty"));
79 
80  // Verifies if filename has special characters.
81  boost::filesystem::path path(filePath.toUtf8().data());
82  std::string filename = path.stem().string();
83  QString fileExtension = path.extension().string().c_str();
84 
85  if(!path.extension().empty())
86  if(mappedExtensions[filteredExtention].compare(fileExtension.toLower().toUtf8().data()))
87  throw te::common::Exception(TE_TR("The file extension is invalid."));
88 
89  if (hasInvalidCharacters(filename))
90  throw te::common::Exception(TE_TR("File name has special characters."));
91 
92  // Append sufix in file path.
93  QFileInfo fileInfo( filePath );
94  if(fileInfo.suffix().isEmpty())
95  filePath.append(mappedExtensions[filteredExtention]);
96 
97  // Set file information in class members.
98  setPath(filePath.toUtf8().data());
99  setFileName(filePath.toUtf8().data());
100  setExtension(filePath.toUtf8().data());
101 }
102 
104 {
105  return m_path;
106 }
107 
108 void te::qt::widgets::FileDialog::setPath(std::string filePath)
109 {
110  m_path = filePath;
111 }
112 
114 {
115  return m_fileName;
116 }
117 
119 {
120  boost::filesystem::path path(filePath);
121  m_fileName = path.leaf().string();
122 }
123 
125 {
126  return m_extension;
127 }
128 
130 {
131  boost::filesystem::path path(filePath);
132  m_extension = path.extension().string();
133 }
134 
135 std::map<QString, QString> te::qt::widgets::FileDialog::getMappedExtensions(QString extensions)
136 {
137  std::map<QString, QString> mappedExtensions;
138 
139  std::vector< std::string > tokens;
140  te::common::Tokenize( extensions.toStdString(), tokens, ";;" );
141  for(auto extension : tokens)
142  {
143  boost::regex expression("[.]+[a-z0-9]+");
144  boost::smatch extMatched;
145  if(boost::regex_search(extension, extMatched, expression))
146  {
147  mappedExtensions[QString::fromStdString(extension)] = QString::fromStdString(extMatched[0]);
148  }
149  }
150 
151  return mappedExtensions;
152 }
153 
155 {
156  boost::regex expression{"\\W+"};
157 
158  if (boost::regex_search(filename, expression))
159  return true;
160 
161  return false;
162 }
163 
TEQTWIDGETSEXPORT QString GetDiskRasterFileSelFilter(const bool creationSupport)
Returns a disk raster file selection filter base on current supported formats.
An instance of QFileDialog that includes file extention if necessary.
std::string getExtension()
This method will return the file extension.
Definition: FileDialog.cpp:124
std::string m_extension
The extension of the file.
Definition: FileDialog.h:134
std::map< QString, QString > getMappedExtensions(QString extensions)
Genarate an extensions map with the extensions returned by driver.
Definition: FileDialog.cpp:135
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void setPath(std::string filePath)
Sets the file path.
Definition: FileDialog.cpp:108
bool hasInvalidCharacters(std::string filename)
Verify if the file name has special characters.
Definition: FileDialog.cpp:154
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:221
std::string m_path
The path of the file.
Definition: FileDialog.h:132
void exec()
This method will open the dialog of file selection and populate the class members with the chosen fil...
Definition: FileDialog.cpp:54
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::string getPath()
This method will return the chosen path.
Definition: FileDialog.cpp:103
TEQTWIDGETSEXPORT QString GetDiskVectorFileSelFilter()
Returns a disk vector file selection filter base on current supported formats.
void setExtension(std::string filePath)
Sets the file extension.
Definition: FileDialog.cpp:129
std::string m_fileName
The name of the file.
Definition: FileDialog.h:133
QWidget * m_parent
The QWidget parent argumnt.
Definition: FileDialog.h:129
FileType m_type
The type of file.
Definition: FileDialog.h:130
std::string m_fileInfo
The file path info.
Definition: FileDialog.h:131
FileDialog(QWidget *parent, FileType type, std::string pathInfo="")
Constructor.
Definition: FileDialog.cpp:47
TEQTWIDGETSEXPORT QString GetFilePathFromSettings(const QString &typeFile)
Returns the value of the last saved file path for the typeFile required.
std::string getFileName()
This method will return the file name.
Definition: FileDialog.cpp:113
void setFileName(std::string filePath)
Sets the file name.
Definition: FileDialog.cpp:118