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-2025 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):
35 GNEFileDialog(applicationWindow, applicationWindow, elementFile, extensions, openMode, configType) {
36}
37
38
39GNEFileDialog::GNEFileDialog(FXWindow* restoringWindow, GNEApplicationWindow* applicationWindow,
40 const std::string elementFile, const std::vector<std::string>& extensions,
42 GNEDialog(applicationWindow, TLF("Save % as", elementFile), GUIIcon::SAVE,
43 DialogType::FILE, GNEDialog::Buttons::ACCEPT_CANCEL, GNEDialog::OpenType::MODAL,
44 GNEDialog::ResizeMode::RESIZABLE, 500, 300) {
45 // update title and icon if we are opening
46 if (openMode != GNEFileDialog::OpenMode::SAVE) {
48 updateTitle(TLF("Open %", elementFile));
49 }
50 // create file selector
51 myFileSelector = new GNEFileSelector(this, extensions, openMode, configType);
52 // retarget accept button to file selector
54 myAcceptButton->setSelector(FXFileSelector::ID_ACCEPT);
55 // check if we have saved settings in registry
56 setWidth(getApp()->reg().readIntEntry("GNEFileDialog", "width", getWidth()));
57 setHeight(getApp()->reg().readIntEntry("GNEFileDialog", "height", getHeight()));
58 myFileSelector->setFileBoxStyle(getApp()->reg().readUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle()));
59 myFileSelector->showHiddenFiles((getApp()->reg().readUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles()) == 1) ? TRUE : FALSE);
60 // set initial directory
61 if (gCurrentFolder.length() > 0) {
62 myFileSelector->setDirectory(gCurrentFolder);
63 }
64 // set restoring window
65 setRestoringFocusWindow(restoringWindow);
66 // open dialog without focusing the button
67 openDialog(myFileSelector->getFilenameTextField());
68}
69
70
72 getApp()->reg().writeIntEntry("GNEFileDialog", "width", getWidth());
73 getApp()->reg().writeIntEntry("GNEFileDialog", "height", getHeight());
74 getApp()->reg().writeUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle());
75 getApp()->reg().writeUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles());
76}
77
78
79void
81 if (dialogArgument->getCustomAction().size() > 0) {
82 myFileSelector->setFilter(dialogArgument->getIndex());
83 myFileSelector->setPath(dialogArgument->getCustomAction());
84 }
85}
86
87
88std::string
90 return assureExtension(myFileSelector->getFilename());
91}
92
93
94std::vector<std::string>
96 std::vector<std::string> filenames;
97 // assure extension for each file
98 for (auto& filename : myFileSelector->getFilenames()) {
99 filenames.push_back(assureExtension(filename));
100 }
101 return filenames;
102}
103
104
105std::string
107 return myFileSelector->getDirectory();
108}
109
110
111std::string
112GNEFileDialog::assureExtension(const std::string& filename) const {
113 // get group of extensions selected in comboBox
114 const auto& extensions = myFileSelector->getFileExtension();
115 // iterate all groups of extensions
116 for (const auto& extension : extensions) {
117 // iterate over all extension to check if is the same extension
118 if (StringUtils::endsWith(filename, extension)) {
119 return filename;
120 }
121 }
122 // in this point, we have to give an extension (if exist)
123 if (extensions.size() > 0) {
124 return (filename + extensions.front());
125 } else {
126 return filename;
127 }
128}
129
130
131long
132GNEFileDialog::onCmdAccept(FXObject*, FXSelector, void*) {
133 const FXString directory = myFileSelector->getDirectory().c_str();
134 const FXString filename = myFileSelector->getFilename().c_str();
135 // update current folder
136 if (directory.length() > 0) {
137 gCurrentFolder = directory;
138 } else if (filename.length() > 0) {
139 gCurrentFolder = FXPath::directory(filename);
140 }
141 // close dialog accepting changes
142 return closeDialogAccepting();
143}
144
145/****************************************************************************/
DialogType
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:77
#define TLF(string,...)
Definition MsgHandler.h:306
The main window of Netedit.
void setRestoringFocusWindow(FXWindow *window)
wet FXWindows uses for restoring focus
void updateIcon(GUIIcon newIcon)
update icon
void openDialog(FXWindow *focusableElement=nullptr)
open dialog
FXButton * myAcceptButton
accept button
Definition GNEDialog.h:155
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.
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)
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.