Path: blob/main/src/utils/gui/div/GUIDialog_GLChosenEditor.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 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 GUIDialog_GLChosenEditor.cpp14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Thu, 11.03.200418///19// Editor for the list of chosen objects20/****************************************************************************/21#include <config.h>2223#include <string>24#include <vector>25#include <iostream>26#include <fstream>27#include <fxkeys.h>28#include <utils/common/MsgHandler.h>29#include <utils/gui/windows/GUIAppEnum.h>30#include <utils/gui/globjects/GUIGlObject.h>31#include <utils/foxtools/MFXUtils.h>32#include "GUIDialog_GLChosenEditor.h"33#include <utils/gui/div/GUIGlobalSelection.h>34#include <utils/gui/globjects/GUIGlObjectStorage.h>35#include <utils/gui/div/GUIIOGlobals.h>36#include <utils/gui/div/GUIDesigns.h>37#include <utils/gui/windows/GUIMainWindow.h>38#include <utils/gui/images/GUIIconSubSys.h>394041// ===========================================================================42// FOX callback mapping43// ===========================================================================44FXDEFMAP(GUIDialog_GLChosenEditor) GUIDialog_GLChosenEditorMap[] = {45FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_LOAD, GUIDialog_GLChosenEditor::onCmdLoad),46FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_SAVE, GUIDialog_GLChosenEditor::onCmdSave),47FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_DESELECT, GUIDialog_GLChosenEditor::onCmdDeselect),48FXMAPFUNC(SEL_COMMAND, MID_CHOOSEN_CLEAR, GUIDialog_GLChosenEditor::onCmdClear),49FXMAPFUNC(SEL_COMMAND, MID_CANCEL, GUIDialog_GLChosenEditor::onCmdClose),50FXMAPFUNC(SEL_KEYPRESS, 0, GUIDialog_GLChosenEditor::onKeyPress),51};5253FXIMPLEMENT(GUIDialog_GLChosenEditor, FXMainWindow, GUIDialog_GLChosenEditorMap, ARRAYNUMBER(GUIDialog_GLChosenEditorMap))545556// ===========================================================================57// method definitions58// ===========================================================================59#ifdef _MSC_VER60#pragma warning(push)61#pragma warning(disable: 4355) // mask warning about "this" in initializers62#endif63GUIDialog_GLChosenEditor::GUIDialog_GLChosenEditor(GUIMainWindow* parent, GUISelectedStorage* str) :64FXMainWindow(parent->getApp(), "List of Selected Items", GUIIconSubSys::getIcon(GUIIcon::APP_SELECTOR), nullptr, GUIDesignChooserDialog),65GUIPersistentWindowPos(this, "DIALOG_EDIT_SELECTED", true, 20, 40, 300, 350),66myParent(parent), myStorage(str) {67myStorage->add2Update(this);68FXHorizontalFrame* hbox = new FXHorizontalFrame(this, GUIDesignAuxiliarFrame);69// create layout left70FXVerticalFrame* layoutLeft = new FXVerticalFrame(hbox, GUIDesignChooserLayoutLeft);71// create frame for list72FXVerticalFrame* layoutList = new FXVerticalFrame(layoutLeft, GUIDesignChooserLayoutList);73// build the list and rebuild it74myList = new FXList(layoutList, this, MID_CHOOSER_LIST, GUIDesignChooserListMultiple);75rebuildList();76// build the layout77FXVerticalFrame* layout = new FXVerticalFrame(hbox, GUIDesignChooserLayoutRight);78// "Load"79GUIDesigns::buildFXButton(layout, TL("&Load selection"), "", "", GUIIconSubSys::getIcon(GUIIcon::OPEN), this, MID_CHOOSEN_LOAD, GUIDesignChooserButtons);80// "Save"81GUIDesigns::buildFXButton(layout, TL("&Save selection"), "", "", GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_CHOOSEN_SAVE, GUIDesignChooserButtons);82// extra separator83new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);84// "Deselect Chosen"85GUIDesigns::buildFXButton(layout, TL("&Deselect chosen"), "", "", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_DESELECT, GUIDesignChooserButtons);86// "Clear List"87GUIDesigns::buildFXButton(layout, TL("&Clear selection"), "", "", GUIIconSubSys::getIcon(GUIIcon::FLAG), this, MID_CHOOSEN_CLEAR, GUIDesignChooserButtons);88// extra separator89new FXHorizontalSeparator(layout, GUIDesignHorizontalSeparator);90// "Close"91GUIDesigns::buildFXButton(layout, TL("Cl&ose"), "", "", GUIIconSubSys::getIcon(GUIIcon::NO), this, MID_CANCEL, GUIDesignChooserButtons);92myParent->addChild(this);93loadWindowPos();94}95#ifdef _MSC_VER96#pragma warning(pop)97#endif9899100GUIDialog_GLChosenEditor::~GUIDialog_GLChosenEditor() {101myStorage->remove2Update();102myParent->removeChild(this);103}104105106void107GUIDialog_GLChosenEditor::rebuildList() {108myList->clearItems();109const auto& chosen = gSelected.getSelected();110for (auto i : chosen) {111GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(i);112if (object != nullptr) {113std::string name = object->getFullName();114FXListItem* item = myList->getItem(myList->appendItem(name.c_str()));115item->setData(object);116GUIGlObjectStorage::gIDStorage.unblockObject(i);117}118}119}120121122void123GUIDialog_GLChosenEditor::selectionUpdated() {124rebuildList();125FXMainWindow::update();126}127128129long130GUIDialog_GLChosenEditor::onCmdLoad(FXObject*, FXSelector, void*) {131// get the new file name132FXFileDialog opendialog(this, TL("Open List of Selected Items"));133opendialog.setIcon(GUIIconSubSys::getIcon(GUIIcon::OPEN));134opendialog.setSelectMode(SELECTFILE_EXISTING);135opendialog.setPatternList(SUMOXMLDefinitions::TXTFileExtensions.getMultilineString().c_str());136if (gCurrentFolder.length() != 0) {137opendialog.setDirectory(gCurrentFolder);138}139if (opendialog.execute()) {140gCurrentFolder = opendialog.getDirectory();141std::string file = opendialog.getFilename().text();142std::string msg = gSelected.load(file);143if (msg != "") {144FXMessageBox::error(this, MBOX_OK, TL("Errors while loading Selection"), "%s", msg.c_str());145}146rebuildList();147myParent->updateChildren();148}149return 1;150}151152153long154GUIDialog_GLChosenEditor::onCmdSave(FXObject*, FXSelector, void*) {155FXString file = MFXUtils::getFilename2Write(this, TL("Save List of selected Items"),156SUMOXMLDefinitions::TXTFileExtensions.getMultilineString().c_str(),157GUIIconSubSys::getIcon(GUIIcon::SAVE), gCurrentFolder);158if (file == "") {159return 1;160}161try {162gSelected.save(file.text());163} catch (IOError& e) {164FXMessageBox::error(this, MBOX_OK, TL("Storing failed!"), "%s", e.what());165}166return 1;167}168169170long171GUIDialog_GLChosenEditor::onCmdDeselect(FXObject*, FXSelector, void*) {172FXint no = myList->getNumItems();173FXint i;174std::vector<GUIGlID> selected;175for (i = 0; i < no; ++i) {176if (myList->getItem(i)->isSelected()) {177selected.push_back(static_cast<GUIGlObject*>(myList->getItem(i)->getData())->getGlID());178}179}180// remove items from list181for (i = 0; i < (FXint) selected.size(); ++i) {182gSelected.deselect(selected[i]);183}184// rebuild list185rebuildList();186myParent->updateChildren();187return 1;188}189190191long192GUIDialog_GLChosenEditor::onKeyPress(FXObject* o, FXSelector sel, void* ptr) {193const FXEvent* e = (FXEvent*) ptr;194if(e->code==KEY_Escape){195onCmdClose(nullptr, 0, nullptr);196return 1;197}198return FXMainWindow::onKeyPress(o, sel, ptr);199}200201202long203GUIDialog_GLChosenEditor::onCmdClear(FXObject*, FXSelector, void*) {204myList->clearItems();205gSelected.clear();206myParent->updateChildren();207return 1;208}209210211long212GUIDialog_GLChosenEditor::onCmdClose(FXObject*, FXSelector, void*) {213close(true);214return 1;215}216217218/****************************************************************************/219220221