Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/dialogs/file/GNEFileDialog.cpp
169684 views
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
/****************************************************************************/
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):
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
// update title and icon if we are opening
39
if (openMode != GNEFileDialog::OpenMode::SAVE) {
40
updateIcon(GUIIcon::OPEN);
41
updateTitle(TLF("Open %", elementFile));
42
}
43
// create file selector
44
myFileSelector = new GNEFileSelector(this, extensions, openMode, configType);
45
// retarget accept button to file selector
46
myAcceptButton->setTarget(myFileSelector);
47
myAcceptButton->setSelector(FXFileSelector::ID_ACCEPT);
48
// check if we have saved settings in registry
49
setWidth(getApp()->reg().readIntEntry("GNEFileDialog", "width", getWidth()));
50
setHeight(getApp()->reg().readIntEntry("GNEFileDialog", "height", getHeight()));
51
myFileSelector->setFileBoxStyle(getApp()->reg().readUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle()));
52
myFileSelector->showHiddenFiles((getApp()->reg().readUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles()) == 1) ? TRUE : FALSE);
53
// set initial directory
54
if (gCurrentFolder.length() > 0) {
55
myFileSelector->setDirectory(gCurrentFolder);
56
}
57
// open dialog without focusing the button
58
openDialog(myFileSelector->getFilenameTextField());
59
}
60
61
62
GNEFileDialog::~GNEFileDialog() {
63
getApp()->reg().writeIntEntry("GNEFileDialog", "width", getWidth());
64
getApp()->reg().writeIntEntry("GNEFileDialog", "height", getHeight());
65
getApp()->reg().writeUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle());
66
getApp()->reg().writeUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles());
67
}
68
69
70
void
71
GNEFileDialog::runInternalTest(const InternalTestStep::DialogArgument* dialogArgument) {
72
if (dialogArgument->getCustomAction().size() > 0) {
73
myFileSelector->setFilter(dialogArgument->getIndex());
74
myFileSelector->setPath(dialogArgument->getCustomAction());
75
}
76
}
77
78
79
std::string
80
GNEFileDialog::getFilename() const {
81
return assureExtension(myFileSelector->getFilename());
82
}
83
84
85
std::vector<std::string>
86
GNEFileDialog::getFilenames() const {
87
std::vector<std::string> filenames;
88
// assure extension for each file
89
for (auto& filename : myFileSelector->getFilenames()) {
90
filenames.push_back(assureExtension(filename));
91
}
92
return filenames;
93
}
94
95
96
std::string
97
GNEFileDialog::getDirectory() const {
98
return myFileSelector->getDirectory();
99
}
100
101
102
long
103
GNEFileDialog::onCmdAccept(FXObject*, FXSelector, void*) {
104
// update current folder
105
gCurrentFolder = myFileSelector->getDirectory().c_str();
106
// close dialog accepting changes
107
return closeDialogAccepting();
108
}
109
110
111
std::string
112
GNEFileDialog::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
/****************************************************************************/
131
132