Path: blob/main/src/netedit/dialogs/file/GNEFileDialog.cpp
193674 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GNEFileDialog.cpp14/// @author Pablo Alvarez Lopez15/// @date Aug 202516///17// Dialog used for opening/saving files18/****************************************************************************/1920#include <utils/foxtools/MFXTextFieldIcon.h>21#include <utils/gui/div/GUIIOGlobals.h>22#include <netedit/GNEApplicationWindow.h>2324#include "GNEFileDialog.h"25#include "GNEFileSelector.h"2627// ===========================================================================28// member method definitions29// ===========================================================================3031GNEFileDialog::GNEFileDialog(GNEApplicationWindow* applicationWindow, const std::string elementFile,32const std::vector<std::string>& extensions, GNEFileDialog::OpenMode openMode,33GNEFileDialog::ConfigType configType, const std::string initialFolder) :34GNEDialog(applicationWindow, TLF("Save % as", elementFile), GUIIcon::SAVE,35DialogType::FILE, GNEDialog::Buttons::ACCEPT_CANCEL, GNEDialog::OpenType::MODAL,36GNEDialog::ResizeMode::RESIZABLE, 500, 300) {37buildFileDialog(elementFile, extensions, openMode, configType, initialFolder);38}394041GNEFileDialog::GNEFileDialog(GNEApplicationWindow* applicationWindow, GNEDialog* parentDialog,42const std::string elementFile, const std::vector<std::string>& extensions,43GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType,44const std::string initialFolder) :45GNEDialog(applicationWindow, parentDialog, TLF("Save % as", elementFile), GUIIcon::SAVE,46DialogType::FILE, GNEDialog::Buttons::ACCEPT_CANCEL, GNEDialog::OpenType::MODAL,47GNEDialog::ResizeMode::RESIZABLE, 500, 300) {48buildFileDialog(elementFile, extensions, openMode, configType, initialFolder);49}505152GNEFileDialog::~GNEFileDialog() {53getApp()->reg().writeIntEntry("GNEFileDialog", "width", getWidth());54getApp()->reg().writeIntEntry("GNEFileDialog", "height", getHeight());55getApp()->reg().writeUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle());56getApp()->reg().writeUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles());57}585960void61GNEFileDialog::runInternalTest(const InternalTestStep::DialogArgument* dialogArgument) {62if (dialogArgument->getCustomAction().size() > 0) {63myFileSelector->setFilter(dialogArgument->getIndex());64myFileSelector->setPath(dialogArgument->getCustomAction());65}66}676869std::string70GNEFileDialog::getFilename() const {71return assureExtension(myFileSelector->getFilename());72}737475std::vector<std::string>76GNEFileDialog::getFilenames() const {77std::vector<std::string> filenames;78// assure extension for each file79for (auto& filename : myFileSelector->getFilenames()) {80filenames.push_back(assureExtension(filename));81}82return filenames;83}848586std::string87GNEFileDialog::getDirectory() const {88return myFileSelector->getDirectory();89}909192std::string93GNEFileDialog::assureExtension(const std::string& filename) const {94// get group of extensions selected in comboBox95const auto& extensions = myFileSelector->getFileExtension();96// iterate all groups of extensions97for (const auto& extension : extensions) {98// iterate over all extension to check if is the same extension99if (StringUtils::endsWith(filename, extension)) {100return filename;101}102}103// in this point, we have to give an extension (if exist)104if (extensions.size() > 0) {105return (filename + extensions.front());106} else {107return filename;108}109}110111112long113GNEFileDialog::onCmdAccept(FXObject*, FXSelector, void*) {114const FXString directory = myFileSelector->getDirectory().c_str();115const FXString filename = myFileSelector->getFilename().c_str();116// update current folder117if (directory.length() > 0) {118gCurrentFolder = directory;119} else if (filename.length() > 0) {120gCurrentFolder = FXPath::directory(filename);121}122// close dialog accepting changes123return closeDialogAccepting();124}125126127void128GNEFileDialog::buildFileDialog(const std::string elementFile, const std::vector<std::string>& extensions,129GNEFileDialog::OpenMode openMode, GNEFileDialog::ConfigType configType,130const std::string initialFolder) {131// update title and icon if we are opening132if (openMode != GNEFileDialog::OpenMode::SAVE) {133updateIcon(GUIIcon::OPEN);134updateTitle(TLF("Open %", elementFile));135}136// create file selector137myFileSelector = new GNEFileSelector(this, extensions, openMode, configType);138// retarget accept button to file selector139myAcceptButton->setTarget(myFileSelector);140myAcceptButton->setSelector(FXFileSelector::ID_ACCEPT);141// check if we have saved settings in registry142setWidth(getApp()->reg().readIntEntry("GNEFileDialog", "width", getWidth()));143setHeight(getApp()->reg().readIntEntry("GNEFileDialog", "height", getHeight()));144myFileSelector->setFileBoxStyle(getApp()->reg().readUnsignedEntry("GNEFileDialog", "style", myFileSelector->getFileBoxStyle()));145myFileSelector->showHiddenFiles((getApp()->reg().readUnsignedEntry("GNEFileDialog", "showhidden", myFileSelector->showHiddenFiles()) == 1) ? TRUE : FALSE);146// set initial directory147if (initialFolder.size() > 0) {148myFileSelector->setDirectory(initialFolder.c_str());149} else if (gCurrentFolder.length() > 0) {150myFileSelector->setDirectory(gCurrentFolder);151}152// open dialog without focusing the button153openDialog(myFileSelector->getFilenameTextField());154}155156/****************************************************************************/157158159