Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/dialogs/file/GNEFileDialog.cpp
193674 views
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
/****************************************************************************/
14
/// @file GNEFileDialog.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Aug 2025
17
///
18
// Dialog used for opening/saving files
19
/****************************************************************************/
20
21
#include <utils/foxtools/MFXTextFieldIcon.h>
22
#include <utils/gui/div/GUIIOGlobals.h>
23
#include <netedit/GNEApplicationWindow.h>
24
25
#include "GNEFileDialog.h"
26
#include "GNEFileSelector.h"
27
28
// ===========================================================================
29
// member method definitions
30
// ===========================================================================
31
32
GNEFileDialog::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
42
GNEFileDialog::GNEFileDialog(GNEApplicationWindow* applicationWindow, GNEDialog* parentDialog,
43
const std::string elementFile, const std::vector<std::string>& extensions,
44
GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType,
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
53
GNEFileDialog::~GNEFileDialog() {
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
61
void
62
GNEFileDialog::runInternalTest(const InternalTestStep::DialogArgument* dialogArgument) {
63
if (dialogArgument->getCustomAction().size() > 0) {
64
myFileSelector->setFilter(dialogArgument->getIndex());
65
myFileSelector->setPath(dialogArgument->getCustomAction());
66
}
67
}
68
69
70
std::string
71
GNEFileDialog::getFilename() const {
72
return assureExtension(myFileSelector->getFilename());
73
}
74
75
76
std::vector<std::string>
77
GNEFileDialog::getFilenames() const {
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
87
std::string
88
GNEFileDialog::getDirectory() const {
89
return myFileSelector->getDirectory();
90
}
91
92
93
std::string
94
GNEFileDialog::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
113
long
114
GNEFileDialog::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
128
void
129
GNEFileDialog::buildFileDialog(const std::string elementFile, const std::vector<std::string>& extensions,
130
GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType,
131
const std::string initialFolder) {
132
// update title and icon if we are opening
133
if (openMode != GNEFileDialog::OpenMode::SAVE) {
134
updateIcon(GUIIcon::OPEN);
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
/****************************************************************************/
158
159