Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEFileDialog.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Dialog used for opening/saving files
19/****************************************************************************/
20
24
25#include "GNEFileDialog.h"
26#include "GNEFileSelector.h"
27
28// ===========================================================================
29// member method definitions
30// ===========================================================================
31
32GNEFileDialog::GNEFileDialog(GNEApplicationWindow* applicationWindow, const std::string elementFile,
33 const std::vector<std::string>& extensions, GNEFileDialog::OpenMode openMode,
34 GNEFileDialog::ConfigType configType, const std::string initialFolder) :
35 GNEDialog(applicationWindow, TLF("Save % as", elementFile), GUIIcon::SAVE,
36 DialogType::FILE, GNEDialog::Buttons::ACCEPT_CANCEL, GNEDialog::OpenType::MODAL,
37 GNEDialog::ResizeMode::RESIZABLE, 500, 300) {
38 buildFileDialog(elementFile, extensions, openMode, configType, initialFolder);
39}
40
41
43 const std::string elementFile, const std::vector<std::string>& extensions,
45 const std::string initialFolder) :
46 GNEDialog(applicationWindow, parentDialog, TLF("Save % as", elementFile), GUIIcon::SAVE,
47 DialogType::FILE, GNEDialog::Buttons::ACCEPT_CANCEL, GNEDialog::OpenType::MODAL,
48 GNEDialog::ResizeMode::RESIZABLE, 500, 300) {
49 buildFileDialog(elementFile, extensions, openMode, configType, initialFolder);
50}
51
52
54 getApp()->reg().writeIntEntry("GNEFileDialog", "width", getWidth());
55 getApp()->reg().writeIntEntry("GNEFileDialog", "height", getHeight());
56 getApp()->reg().writeUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle());
57 getApp()->reg().writeUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles());
58}
59
60
61void
63 if (dialogArgument->getCustomAction().size() > 0) {
64 myFileSelector->setFilter(dialogArgument->getIndex());
65 myFileSelector->setPath(dialogArgument->getCustomAction());
66 }
67}
68
69
70std::string
72 return assureExtension(myFileSelector->getFilename());
73}
74
75
76std::vector<std::string>
78 std::vector<std::string> filenames;
79 // assure extension for each file
80 for (auto& filename : myFileSelector->getFilenames()) {
81 filenames.push_back(assureExtension(filename));
82 }
83 return filenames;
84}
85
86
87std::string
89 return myFileSelector->getDirectory();
90}
91
92
93std::string
94GNEFileDialog::assureExtension(const std::string& filename) const {
95 // get group of extensions selected in comboBox
96 const auto& extensions = myFileSelector->getFileExtension();
97 // iterate all groups of extensions
98 for (const auto& extension : extensions) {
99 // iterate over all extension to check if is the same extension
100 if (StringUtils::endsWith(filename, extension)) {
101 return filename;
102 }
103 }
104 // in this point, we have to give an extension (if exist)
105 if (extensions.size() > 0) {
106 return (filename + extensions.front());
107 } else {
108 return filename;
109 }
110}
111
112
113long
114GNEFileDialog::onCmdAccept(FXObject*, FXSelector, void*) {
115 const FXString directory = myFileSelector->getDirectory().c_str();
116 const FXString filename = myFileSelector->getFilename().c_str();
117 // update current folder
118 if (directory.length() > 0) {
119 gCurrentFolder = directory;
120 } else if (filename.length() > 0) {
121 gCurrentFolder = FXPath::directory(filename);
122 }
123 // close dialog accepting changes
124 return closeDialogAccepting();
125}
126
127
128void
129GNEFileDialog::buildFileDialog(const std::string elementFile, const std::vector<std::string>& extensions,
131 const std::string initialFolder) {
132 // update title and icon if we are opening
133 if (openMode != GNEFileDialog::OpenMode::SAVE) {
135 updateTitle(TLF("Open %", elementFile));
136 }
137 // create file selector
138 myFileSelector = new GNEFileSelector(this, extensions, openMode, configType);
139 // retarget accept button to file selector
140 myAcceptButton->setTarget(myFileSelector);
141 myAcceptButton->setSelector(FXFileSelector::ID_ACCEPT);
142 // check if we have saved settings in registry
143 setWidth(getApp()->reg().readIntEntry("GNEFileDialog", "width", getWidth()));
144 setHeight(getApp()->reg().readIntEntry("GNEFileDialog", "height", getHeight()));
145 myFileSelector->setFileBoxStyle(getApp()->reg().readUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle()));
146 myFileSelector->showHiddenFiles((getApp()->reg().readUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles()) == 1) ? TRUE : FALSE);
147 // set initial directory
148 if (initialFolder.size() > 0) {
149 myFileSelector->setDirectory(initialFolder.c_str());
150 } else if (gCurrentFolder.length() > 0) {
151 myFileSelector->setDirectory(gCurrentFolder);
152 }
153 // open dialog without focusing the button
154 openDialog(myFileSelector->getFilenameTextField());
155}
156
157/****************************************************************************/
DialogType
Definition DialogType.h:30
FXString gCurrentFolder
The folder used as last.
GUIIcon
An enumeration of icons used by the gui applications.
Definition GUIIcons.h:33
@ OPEN
open icons
Definition GUIIcons.h:79
@ SAVE
save icons
Definition GUIIcons.h:92
#define TLF(string,...)
Definition MsgHandler.h:306
void updateIcon(GUIIcon newIcon)
update icon
void openDialog(FXWindow *focusableElement=nullptr)
open dialog
FXButton * myAcceptButton
accept button
Definition GNEDialog.h:162
long closeDialogAccepting()
close dialog accepting the changes
OpenType
Open dialog type.
Definition GNEDialog.h:58
GNEDialog(GNEApplicationWindow *applicationWindow, const std::string &name, GUIIcon titleIcon, DialogType type, Buttons buttons, OpenType openType, ResizeMode resizeMode)
basic constructor
Definition GNEDialog.cpp:60
void updateTitle(const std::string &newTitle)
update title
void runInternalTest(const InternalTestStep::DialogArgument *dialogArgument)
run internal test
std::vector< std::string > getFilenames() const
Return empty-string terminated list of selected file names, or NULL if none selected.
std::string getDirectory() const
Return directory.
void buildFileDialog(const std::string elementFile, const std::vector< std::string > &extensions, GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType, const std::string initialFolder)
builder
OpenMode
file open mode
std::string assureExtension(const std::string &filename) const
check extensions
GNEFileSelector * myFileSelector
the file selector widget
long onCmdAccept(FXObject *, FXSelector, void *)
called when accept or yes button is pressed (can be reimplemented in children)
~GNEFileDialog()
destructor
ConfigType
config type
std::string getFilename() const
Return file name, if any.
GNEFileDialog(GNEApplicationWindow *applicationWindow, const std::string elementFile, const std::vector< std::string > &extensions, GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType, const std::string initialFolder="")
constructor
dialog arguments, used for certain modal dialogs that can not be edited using tab
const std::string & getCustomAction() const
get custom action
static bool endsWith(const std::string &str, const std::string suffix)
Checks whether a given string ends with the suffix.