LCOV - code coverage report
Current view: top level - src/utils/gui/div - GUISelectedStorage.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 31.6 % 136 43
Test Date: 2025-11-13 15:38:19 Functions: 50.0 % 28 14

            Line data    Source code
       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    GUISelectedStorage.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @date    Jun 2004
      19              : ///
      20              : // Storage for "selected" objects
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <algorithm>
      25              : #include <utils/gui/globjects/GUIGlObject.h>
      26              : #include <utils/gui/globjects/GUIGlObjectStorage.h>
      27              : #include <utils/iodevices/OutputDevice.h>
      28              : #include <utils/common/ToString.h>
      29              : 
      30              : #include "GUISelectedStorage.h"
      31              : #include "GUIDialog_GLChosenEditor.h"
      32              : 
      33              : 
      34              : // ===========================================================================
      35              : // member method definitions
      36              : // ===========================================================================
      37              : 
      38              : /* -------------------------------------------------------------------------
      39              :  * for GUISelectedStorage::SingleTypeSelections
      40              :  * ----------------------------------------------------------------------- */
      41              : 
      42        23661 : GUISelectedStorage::SingleTypeSelections::SingleTypeSelections() {}
      43              : 
      44              : 
      45        23659 : GUISelectedStorage::SingleTypeSelections::~SingleTypeSelections() {}
      46              : 
      47              : 
      48              : bool
      49    103317827 : GUISelectedStorage::SingleTypeSelections::isSelected(GUIGlID id) {
      50    103317827 :     return mySelected.count(id) > 0;
      51              : }
      52              : 
      53              : 
      54              : void
      55            2 : GUISelectedStorage::SingleTypeSelections::select(GUIGlID id) {
      56              :     mySelected.insert(id);
      57            2 : }
      58              : 
      59              : 
      60              : void
      61       526369 : GUISelectedStorage::SingleTypeSelections::deselect(GUIGlID id) {
      62              :     mySelected.erase(id);
      63       526369 : }
      64              : 
      65              : 
      66              : void
      67        45466 : GUISelectedStorage::SingleTypeSelections::clear() {
      68              :     mySelected.clear();
      69        45466 : }
      70              : 
      71              : 
      72              : void
      73            0 : GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
      74            0 :     GUISelectedStorage::save(filename, mySelected);
      75            0 : }
      76              : 
      77              : 
      78              : const std::unordered_set<GUIGlID>&
      79            0 : GUISelectedStorage::SingleTypeSelections::getSelected() const {
      80            0 :     return mySelected;
      81              : }
      82              : 
      83              : /* -------------------------------------------------------------------------
      84              :  * for GUISelectedStorage
      85              :  * ----------------------------------------------------------------------- */
      86              : 
      87         8578 : GUISelectedStorage::GUISelectedStorage() {}
      88              : 
      89              : 
      90         8578 : GUISelectedStorage::~GUISelectedStorage() {}
      91              : 
      92              : 
      93              : bool
      94    103317827 : GUISelectedStorage::isSelected(GUIGlObjectType type, GUIGlID id) {
      95    103317827 :     switch (type) {
      96              :         case GLO_NETWORK:
      97              :             return false;
      98    103317827 :         default:
      99    103317827 :             return mySelections[type].isSelected(id);
     100              :     }
     101              : }
     102              : 
     103              : bool
     104     50095081 : GUISelectedStorage::isSelected(const GUIGlObject* o) {
     105     50095081 :     if (o == nullptr) {
     106              :         return false;
     107              :     } else {
     108     50095081 :         return isSelected(o->getType(), o->getGlID());
     109              :     }
     110              : }
     111              : 
     112              : void
     113            2 : GUISelectedStorage::select(GUIGlID id, bool update) {
     114            2 :     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
     115            2 :     if (!object) {
     116            0 :         throw ProcessError("Unknown object in GUISelectedStorage::select (id=" + toString(id) + ").");
     117              :     }
     118            2 :     GUIGlObjectType type = object->getType();
     119            2 :     GUIGlObjectStorage::gIDStorage.unblockObject(id);
     120              : 
     121            2 :     mySelections[type].select(id);
     122              :     myAllSelected.insert(id);
     123            2 :     if (update && myUpdateTarget) {
     124            0 :         myUpdateTarget->selectionUpdated();
     125              :     }
     126            2 : }
     127              : 
     128              : 
     129              : void
     130            0 : GUISelectedStorage::deselect(GUIGlID id) {
     131            0 :     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
     132            0 :     if (!object) {
     133            0 :         throw ProcessError("Unknown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
     134              :     }
     135            0 :     GUIGlObjectType type = object->getType();
     136            0 :     GUIGlObjectStorage::gIDStorage.unblockObject(id);
     137              : 
     138            0 :     mySelections[type].deselect(id);
     139              :     myAllSelected.erase(id);
     140            0 :     if (myUpdateTarget) {
     141            0 :         myUpdateTarget->selectionUpdated();
     142              :     }
     143            0 : }
     144              : 
     145              : 
     146              : void
     147       526369 : GUISelectedStorage::deselect(GUIGlObjectType type, GUIGlID id) {
     148       526369 :     mySelections[type].deselect(id);
     149              :     myAllSelected.erase(id);
     150       526369 : }
     151              : 
     152              : 
     153              : void
     154            2 : GUISelectedStorage::toggleSelection(GUIGlID id) {
     155            2 :     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
     156            2 :     if (!object) {
     157            0 :         throw ProcessError("Unknown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
     158              :     }
     159              : 
     160            2 :     bool selected = isSelected(object->getType(), id);
     161            2 :     if (!selected) {
     162            2 :         select(id);
     163              :     } else {
     164            0 :         deselect(id);
     165              :     }
     166            2 :     GUIGlObjectStorage::gIDStorage.unblockObject(id);
     167            2 : }
     168              : 
     169              : 
     170              : const std::unordered_set<GUIGlID>&
     171            0 : GUISelectedStorage::getSelected() const {
     172            0 :     return myAllSelected;
     173              : }
     174              : 
     175              : 
     176              : const std::unordered_set<GUIGlID>&
     177            0 : GUISelectedStorage::getSelected(GUIGlObjectType type) {
     178            0 :     return mySelections[type].getSelected();
     179              : }
     180              : 
     181              : 
     182              : void
     183        23460 : GUISelectedStorage::clear() {
     184        68926 :     for (auto& selection : mySelections) {
     185        45466 :         selection.second.clear();
     186              :     }
     187              :     myAllSelected.clear();
     188        23460 :     if (myUpdateTarget) {
     189            0 :         myUpdateTarget->selectionUpdated();
     190              :     }
     191        23460 : }
     192              : 
     193              : 
     194              : void
     195            0 : GUISelectedStorage::notifyChanged() {
     196            0 :     if (myUpdateTarget) {
     197            0 :         myUpdateTarget->selectionUpdated();
     198              :     }
     199            0 : }
     200              : 
     201              : 
     202              : std::set<GUIGlID>
     203            0 : GUISelectedStorage::loadIDs(std::istream& strm, std::string& msgOut, GUIGlObjectType type, std::ostream* dynamicNotFound, int maxErrors) {
     204              :     std::set<GUIGlID> result;
     205            0 :     std::ostringstream msg;
     206              :     int numIgnored = 0;
     207              :     int numMissing = 0;
     208            0 :     while (strm.good()) {
     209              :         std::string line;
     210            0 :         strm >> line;
     211            0 :         if (line.length() == 0) {
     212            0 :             continue;
     213              :         }
     214            0 :         if (StringUtils::startsWith(line, "node:")) {
     215            0 :             line = StringUtils::replace(line, "node:", "junction:");
     216              :         }
     217              : 
     218            0 :         GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(line);
     219            0 :         if (object) {
     220            0 :             if (type != GLO_MAX && (object->getType() != type)) {
     221            0 :                 numIgnored++;
     222            0 :                 if (numIgnored + numMissing <= maxErrors) {
     223            0 :                     msg << TLF("Ignoring item '%' because of invalid type %\n", line, toString(object->getType()));
     224              :                 }
     225              :             } else {
     226            0 :                 result.insert(object->getGlID());
     227              :             }
     228              :         } else {
     229            0 :             numMissing++;
     230            0 :             if (dynamicNotFound != nullptr && (
     231            0 :                         StringUtils::startsWith(line, "vehicle:") ||
     232            0 :                         StringUtils::startsWith(line, "person:") ||
     233            0 :                         StringUtils::startsWith(line, "container:"))) {
     234            0 :                 (*dynamicNotFound) << line << "\n";
     235              :             } else {
     236            0 :                 if (numIgnored + numMissing <= maxErrors) {
     237            0 :                     msg << TLF("Item '%' not found\n", line);
     238              :                 }
     239              :             }
     240            0 :             continue;
     241              :         }
     242              :     }
     243            0 :     if (numIgnored + numMissing > maxErrors) {
     244            0 :         msg << "...\n" << TLF("% objects ignored, % objects not found\n", numIgnored, numMissing);
     245              :     }
     246            0 :     msgOut = msg.str();
     247            0 :     return result;
     248            0 : }
     249              : 
     250              : 
     251              : std::string
     252            0 : GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type, std::ostream* dynamicNotFound) {
     253            0 :     std::ifstream strm(filename.c_str());
     254            0 :     if (!strm.good()) {
     255            0 :         return TLF("Could not open '%'.\n", filename);
     256              :     }
     257            0 :     std::string errors = load(strm, type, dynamicNotFound);
     258            0 :     strm.close();
     259            0 :     return errors;
     260            0 : }
     261              : 
     262              : 
     263              : std::string
     264            0 : GUISelectedStorage::load(std::istream& strm, GUIGlObjectType type, std::ostream* dynamicNotFound) {
     265              :     std::string errors;
     266            0 :     const std::set<GUIGlID> ids = loadIDs(strm, errors, type, dynamicNotFound);
     267            0 :     for (const auto glID : ids) {
     268            0 :         select(glID, false);
     269              :     }
     270            0 :     if (myUpdateTarget) {
     271            0 :         myUpdateTarget->selectionUpdated();
     272              :     }
     273            0 :     return errors;
     274              : }
     275              : 
     276              : 
     277              : void
     278            0 : GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
     279            0 :     mySelections[type].save(filename);
     280            0 : }
     281              : 
     282              : 
     283              : void
     284            0 : GUISelectedStorage::save(const std::string& filename) const {
     285            0 :     save(filename, myAllSelected);
     286            0 : }
     287              : 
     288              : 
     289              : void
     290            0 : GUISelectedStorage::add2Update(UpdateTarget* updateTarget) {
     291            0 :     myUpdateTarget = updateTarget;
     292            0 : }
     293              : 
     294              : 
     295              : void
     296            0 : GUISelectedStorage::remove2Update() {
     297            0 :     myUpdateTarget = nullptr;
     298            0 : }
     299              : 
     300              : 
     301              : void
     302            0 : GUISelectedStorage::save(const std::string& filename, const std::unordered_set<GUIGlID>& ids) {
     303            0 :     OutputDevice& dev = OutputDevice::getDevice(filename);
     304            0 :     for (const auto glID : ids) {
     305            0 :         GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(glID);
     306            0 :         if (object != nullptr) {
     307              :             std::string name = object->getFullName();
     308            0 :             dev << name << "\n";
     309            0 :             GUIGlObjectStorage::gIDStorage.unblockObject(glID);
     310              :         }
     311              :     }
     312            0 :     dev.close();
     313            0 : }
     314              : 
     315              : 
     316              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1