7 #include "fwGuiQt/dialog/LocationDialog.hpp" 9 #include <fwData/location/Folder.hpp> 10 #include <fwData/location/MultiFiles.hpp> 11 #include <fwData/location/SingleFile.hpp> 13 #include <fwGui/dialog/ILocationDialog.hpp> 14 #include <fwGui/registry/macros.hpp> 16 #include <boost/filesystem/path.hpp> 17 #include <boost/tokenizer.hpp> 19 #include <QApplication> 21 #include <QFileDialog> 36 m_style(::
fwGui::dialog::ILocationDialog::NONE),
37 m_type(::
fwGui::dialog::ILocationDialog::SINGLE_FILE)
45 QWidget *parent = qApp->activeWindow();
46 QString caption = QString::fromStdString(this->
getTitle());
48 QString path = QString::fromStdString(defaultPath.string());
49 QString filter = this->fileFilters();
50 ::fwData::location::ILocation::sptr location;
53 dialog.setDirectory(path);
54 dialog.setNameFilter(filter);
55 dialog.setWindowTitle(caption);
57 if (m_style & ::fwGui::dialog::ILocationDialog::READ)
59 dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
63 dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptSave);
67 if (m_type == ::fwGui::dialog::ILocationDialog::MULTI_FILES)
69 SLM_ASSERT(
"MULTI_FILES type must have a READ style", m_style & ::fwGui::dialog::ILocationDialog::READ);
71 dialog.setFilter(QDir::Filter::Files);
72 dialog.setFileMode(QFileDialog::FileMode::ExistingFiles);
76 files = dialog.selectedFiles();
77 m_wildcard = dialog.selectedNameFilter().toStdString();
82 ::fwData::location::MultiFiles::sptr multifiles = ::fwData::location::MultiFiles::New();
83 std::vector< ::boost::filesystem::path > paths;
84 for (QString filename : files)
86 ::boost::filesystem::path bpath( filename.toStdString() );
87 paths.push_back(bpath);
89 multifiles->setPaths(paths);
90 location = multifiles;
93 else if (m_type == ::fwGui::dialog::ILocationDialog::SINGLE_FILE)
96 if ( (m_style & ::fwGui::dialog::ILocationDialog::READ) ||
97 (m_style & ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST) )
99 dialog.setFileMode(QFileDialog::FileMode::ExistingFile);
100 if (dialog.exec() && !dialog.selectedFiles().empty())
102 fileName = dialog.selectedFiles()[0];
103 m_wildcard = dialog.selectedNameFilter().toStdString();
106 else if ( m_style & ::fwGui::dialog::ILocationDialog::WRITE )
108 if (dialog.exec() && !dialog.selectedFiles().empty())
110 fileName = dialog.selectedFiles()[0];
111 m_wildcard = dialog.selectedNameFilter().toStdString();
115 if(!fileName.isNull())
117 ::boost::filesystem::path bpath( fileName.toStdString());
118 location = ::fwData::location::SingleFile::New(bpath);
121 else if (m_type == ::fwGui::dialog::ILocationDialog::FOLDER)
123 dialog.setFilter(QDir::Filter::Dirs);
124 dialog.setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
125 dialog.setFileMode(QFileDialog::FileMode::Directory);
128 if (dialog.exec() && !dialog.selectedFiles().empty())
130 dir = dialog.selectedFiles()[0];
135 ::boost::filesystem::path bpath( dir.toStdString() );
136 location = ::fwData::location::Folder::New(bpath);
153 if ( option == ::fwGui::dialog::ILocationDialog::WRITE )
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);
158 else if ( option == ::fwGui::dialog::ILocationDialog::READ )
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);
163 else if ( option == ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST )
166 (::fwGui::dialog::ILocationDialog::Options) (m_style |
167 ::fwGui::dialog::ILocationDialog::FILE_MUST_EXIST);
178 m_filters.push_back( std::make_pair( filterName, wildcardList ));
184 QString LocationDialog::fileFilters()
187 std::vector< std::pair < std::string, std::string > >::const_iterator iter;
188 for ( iter = m_filters.begin(); iter!= m_filters.end(); ++iter)
190 std::string filterName = iter->first;
191 std::string rawWildcards = iter->second;
193 if (iter!=m_filters.begin() )
197 result += filterName +
" (" + rawWildcards +
")";
199 return QString::fromStdString(result);
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)
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())
215 extension = &rawWildcards[1];
The namespace fwGui contains the base interface for IHM services.
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...
FWGUI_API const std::string & getTitle() override
Returns the title for the dialog.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
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)