fw4spl
t/src/fwGuiQt/dialog/LocationDialog.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwGuiQt/dialog/LocationDialog.hpp"
8 
9 #include <fwData/location/Folder.hpp>
10 #include <fwData/location/MultiFiles.hpp>
11 #include <fwData/location/SingleFile.hpp>
12 
13 #include <fwGui/dialog/ILocationDialog.hpp>
14 #include <fwGui/registry/macros.hpp>
15 
16 #include <boost/filesystem/path.hpp>
17 #include <boost/tokenizer.hpp>
18 #include <functional>
19 #include <QApplication>
20 
21 #include <QFileDialog>
22 #include <QString>
23 
24 
26 
27 
28 namespace fwGuiQt
29 {
30 namespace dialog
31 {
32 
33 //------------------------------------------------------------------------------
34 
35 LocationDialog::LocationDialog(::fwGui::GuiBaseObject::Key key) :
36  m_style(::fwGui::dialog::ILocationDialog::NONE),
37  m_type(::fwGui::dialog::ILocationDialog::SINGLE_FILE)
38 {
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 ::fwData::location::ILocation::sptr LocationDialog::show()
44 {
45  QWidget *parent = qApp->activeWindow();
46  QString caption = QString::fromStdString(this->getTitle());
47  const ::boost::filesystem::path defaultPath = this->getDefaultLocation();
48  QString path = QString::fromStdString(defaultPath.string());
49  QString filter = this->fileFilters();
50  ::fwData::location::ILocation::sptr location;
51 
52  QFileDialog dialog;
53  dialog.setDirectory(path);
54  dialog.setNameFilter(filter);
55  dialog.setWindowTitle(caption);
56 
57  if (m_style & ::fwGui::dialog::ILocationDialog::READ)
58  {
59  dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
60  }
61  else
62  {
63  dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptSave);
64  }
65 
66 
67  if (m_type == ::fwGui::dialog::ILocationDialog::MULTI_FILES)
68  {
69  SLM_ASSERT("MULTI_FILES type must have a READ style", m_style & ::fwGui::dialog::ILocationDialog::READ);
70 
71  dialog.setFilter(QDir::Filter::Files);
72  dialog.setFileMode(QFileDialog::FileMode::ExistingFiles);
73  QStringList files;
74  if (dialog.exec())
75  {
76  files = dialog.selectedFiles();
77  m_wildcard = dialog.selectedNameFilter().toStdString();
78 
79  }
80  if(!files.isEmpty())
81  {
82  ::fwData::location::MultiFiles::sptr multifiles = ::fwData::location::MultiFiles::New();
83  std::vector< ::boost::filesystem::path > paths;
84  for (QString filename : files)
85  {
86  ::boost::filesystem::path bpath( filename.toStdString() );
87  paths.push_back(bpath);
88  }
89  multifiles->setPaths(paths);
90  location = multifiles;
91  }
92  }
93  else if (m_type == ::fwGui::dialog::ILocationDialog::SINGLE_FILE)
94  {
95  QString fileName;
96  if ( (m_style & ::fwGui::dialog::ILocationDialog::READ) ||
97  (m_style & ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST) )
98  {
99  dialog.setFileMode(QFileDialog::FileMode::ExistingFile);
100  if (dialog.exec() && !dialog.selectedFiles().empty())
101  {
102  fileName = dialog.selectedFiles()[0];
103  m_wildcard = dialog.selectedNameFilter().toStdString();
104  }
105  }
106  else if ( m_style & ::fwGui::dialog::ILocationDialog::WRITE )
107  {
108  if (dialog.exec() && !dialog.selectedFiles().empty())
109  {
110  fileName = dialog.selectedFiles()[0];
111  m_wildcard = dialog.selectedNameFilter().toStdString();
112  }
113 
114  }
115  if(!fileName.isNull())
116  {
117  ::boost::filesystem::path bpath( fileName.toStdString());
118  location = ::fwData::location::SingleFile::New(bpath);
119  }
120  }
121  else if (m_type == ::fwGui::dialog::ILocationDialog::FOLDER)
122  {
123  dialog.setFilter(QDir::Filter::Dirs);
124  dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
125  dialog.setFileMode(QFileDialog::FileMode::Directory);
126 
127  QString dir;
128  if (dialog.exec() && !dialog.selectedFiles().empty())
129  {
130  dir = dialog.selectedFiles()[0];
131  }
132 
133  if(!dir.isNull())
134  {
135  ::boost::filesystem::path bpath( dir.toStdString() );
136  location = ::fwData::location::Folder::New(bpath);
137  }
138  }
139  return location;
140 }
141 
142 //------------------------------------------------------------------------------
143 
144 void LocationDialog::setType( ::fwGui::dialog::ILocationDialog::Types type )
145 {
146  m_type = type;
147 }
148 
149 //------------------------------------------------------------------------------
150 
151 ::fwGui::dialog::ILocationDialog& LocationDialog::setOption( ::fwGui::dialog::ILocationDialog::Options option)
152 {
153  if ( option == ::fwGui::dialog::ILocationDialog::WRITE )
154  {
155  m_style = (::fwGui::dialog::ILocationDialog::Options) (m_style & ~::fwGui::dialog::ILocationDialog::READ);
156  m_style = (::fwGui::dialog::ILocationDialog::Options) (m_style | ::fwGui::dialog::ILocationDialog::WRITE);
157  }
158  else if ( option == ::fwGui::dialog::ILocationDialog::READ )
159  {
160  m_style = (::fwGui::dialog::ILocationDialog::Options) (m_style & ~::fwGui::dialog::ILocationDialog::WRITE);
161  m_style = (::fwGui::dialog::ILocationDialog::Options) (m_style | ::fwGui::dialog::ILocationDialog::READ);
162  }
163  else if ( option == ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST )
164  {
165  m_style =
166  (::fwGui::dialog::ILocationDialog::Options) (m_style |
167  ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
168  }
169 
170  return *this;
171 }
172 
173 //------------------------------------------------------------------------------
174 
175 // exemple ( addFilter("images","*.png *.jpg");
176 void LocationDialog::addFilter(const std::string &filterName, const std::string &wildcardList )
177 {
178  m_filters.push_back( std::make_pair( filterName, wildcardList ));
179 }
180 
181 //------------------------------------------------------------------------------
182 
183 // "BMP and GIF files (*.bmp *.gif)|*.bmp *.gif|PNG files (*.png)|*.png"
184 QString LocationDialog::fileFilters()
185 {
186  std::string result;
187  std::vector< std::pair < std::string, std::string > >::const_iterator iter;
188  for ( iter = m_filters.begin(); iter!= m_filters.end(); ++iter)
189  {
190  std::string filterName = iter->first;
191  std::string rawWildcards = iter->second;
192 
193  if (iter!=m_filters.begin() )
194  {
195  result += ";;";
196  }
197  result += filterName +" (" + rawWildcards +")";
198  }
199  return QString::fromStdString(result);
200 }
201 
202 //------------------------------------------------------------------------------
203 
205 {
206  std::string extension;
207  std::vector< std::pair < std::string, std::string > >::const_iterator iter;
208  for ( iter = m_filters.begin(); iter != m_filters.end(); ++iter)
209  {
210  const std::string& filterName = iter->first;
211  const std::string& rawWildcards = iter->second;
212  const std::string& availableFilters = filterName + " (" + rawWildcards + ")";
213  if (!m_wildcard.compare(availableFilters) && iter != m_filters.begin())
214  {
215  extension = &rawWildcards[1];
216  break;
217  }
218  }
219 
220  return extension;
221 }
222 
223 //------------------------------------------------------------------------------
224 } // namespace dialog
225 } //namespace fwGuiQt
The namespace fwGui contains the base interface for IHM services.
Definition: SJobBar.hpp:23
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
Defines the generic file/folder dialog for IHM.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
The namespace fwGuiQt contains classes which provide the implementation of the Gui using Qt library...
Definition: WindowLevel.hpp:32
FWGUI_API const std::string & getTitle() override
Returns the title for the dialog.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static FWGUI_API const FactoryRegistryKeyType REGISTRY_KEY
this unique key should be used for all factory for specific LocationDialog(qt,wx,...)
FWGUI_APIconst::boost::filesystem::path getDefaultLocation() override
Gets the default location for the dialog (from preferences or specified by user)
FWGUI_API std::string getCurrentSelection() const override
Gets the current extension file selection.
FWGUI_API void setType(::fwGui::dialog::ILocationDialog::Types type) override
Set the type of location for the dialog (SINGLE_FILE, FORLDER, MULTI_FILES)