SUMO - Simulation of Urban MObility
GUISelectedStorage.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Storage for "selected" objects
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <algorithm>
00034 #include <utils/gui/globjects/GUIGlObject.h>
00035 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00036 #include "GUISelectedStorage.h"
00037 #include "GUIDialog_GLChosenEditor.h"
00038 #include <utils/iodevices/OutputDevice.h>
00039 #include <utils/common/ToString.h>
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // member method definitions
00048 // ===========================================================================
00049 /* -------------------------------------------------------------------------
00050  * for GUISelectedStorage::SingleTypeSelections
00051  * ----------------------------------------------------------------------- */
00052 GUISelectedStorage::SingleTypeSelections::SingleTypeSelections() {}
00053 
00054 
00055 GUISelectedStorage::SingleTypeSelections::~SingleTypeSelections() {}
00056 
00057 
00058 bool
00059 GUISelectedStorage::SingleTypeSelections::isSelected(GUIGlID id) {
00060     return mySelected.count(id) > 0;
00061 }
00062 
00063 
00064 void
00065 GUISelectedStorage::SingleTypeSelections::select(GUIGlID id) {
00066     mySelected.insert(id);
00067 }
00068 
00069 
00070 void
00071 GUISelectedStorage::SingleTypeSelections::deselect(GUIGlID id) {
00072     mySelected.erase(id);
00073 }
00074 
00075 
00076 void
00077 GUISelectedStorage::SingleTypeSelections::clear() {
00078     mySelected.clear();
00079 }
00080 
00081 
00082 void
00083 GUISelectedStorage::SingleTypeSelections::save(const std::string& filename) {
00084     GUISelectedStorage::save(filename, mySelected);
00085 }
00086 
00087 
00088 const std::set<GUIGlID> &
00089 GUISelectedStorage::SingleTypeSelections::getSelected() const {
00090     return mySelected;
00091 }
00092 
00093 
00094 
00095 /* -------------------------------------------------------------------------
00096  * for GUISelectedStorage
00097  * ----------------------------------------------------------------------- */
00098 GUISelectedStorage::GUISelectedStorage() {}
00099 
00100 
00101 GUISelectedStorage::~GUISelectedStorage() {}
00102 
00103 
00104 bool
00105 GUISelectedStorage::isSelected(GUIGlObjectType type, GUIGlID id) {
00106     switch (type) {
00107         case GLO_NETWORK:
00108             return false;
00109         case GLO_ADDITIONAL:
00110             return isSelected(GLO_TRIGGER, id) || isSelected(GLO_DETECTOR, id);
00111         default:
00112             return mySelections[type].isSelected(id);
00113     }
00114 }
00115 
00116 
00117 void
00118 GUISelectedStorage::select(GUIGlID id, bool update) {
00119     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00120     if (!object) {
00121         throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
00122     }
00123     GUIGlObjectType type = object->getType();
00124     GUIGlObjectStorage::gIDStorage.unblockObject(id);
00125 
00126     mySelections[type].select(id);
00127     myAllSelected.insert(id);
00128     if (update && myUpdateTarget) {
00129         myUpdateTarget->selectionUpdated();
00130     }
00131 }
00132 
00133 
00134 void
00135 GUISelectedStorage::deselect(GUIGlID id) {
00136     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00137     if (!object) {
00138         throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
00139     }
00140     GUIGlObjectType type = object->getType();
00141     GUIGlObjectStorage::gIDStorage.unblockObject(id);
00142 
00143     mySelections[type].deselect(id);
00144     myAllSelected.erase(id);
00145     if (myUpdateTarget) {
00146         myUpdateTarget->selectionUpdated();
00147     }
00148 }
00149 
00150 
00151 void
00152 GUISelectedStorage::toggleSelection(GUIGlID id) {
00153     GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00154     if (!object) {
00155         throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
00156     }
00157 
00158     bool selected = isSelected(object->getType(), id);
00159     if (!selected) {
00160         select(id);
00161     } else {
00162         deselect(id);
00163     }
00164     GUIGlObjectStorage::gIDStorage.unblockObject(id);
00165 }
00166 
00167 
00168 const std::set<GUIGlID> &
00169 GUISelectedStorage::getSelected() const {
00170     return myAllSelected;
00171 }
00172 
00173 
00174 const std::set<GUIGlID> &
00175 GUISelectedStorage::getSelected(GUIGlObjectType type) {
00176     return mySelections[type].getSelected();
00177 }
00178 
00179 
00180 void
00181 GUISelectedStorage::clear() {
00182     for (std::map<GUIGlObjectType, SingleTypeSelections>::iterator it = mySelections.begin(); it != mySelections.end(); it++) {
00183         it->second.clear();
00184     }
00185     myAllSelected.clear();
00186     if (myUpdateTarget) {
00187         myUpdateTarget->selectionUpdated();
00188     }
00189 }
00190 
00191 
00192 std::set<GUIGlID>
00193 GUISelectedStorage::loadIDs(const std::string& filename, std::string& msgOut, GUIGlObjectType type) {
00194     std::set<GUIGlID> result;
00195     std::ostringstream msg;
00196     std::ifstream strm(filename.c_str());
00197     if (!strm.good()) {
00198         msgOut = "Could not open '" + filename + "'.\n";
00199         return result;
00200     }
00201     while (strm.good()) {
00202         std::string line;
00203         strm >> line;
00204         if (line.length() == 0) {
00205             continue;
00206         }
00207 
00208         GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(line);
00209         if (object) {
00210             if (type != GLO_MAX && (object->getType() != type)) {
00211                 msg << "Ignoring item '" << line << "' because of invalid type " << toString(object->getType()) << "\n";
00212             } else {
00213                 result.insert(object->getGlID());
00214             }
00215         } else {
00216             msg << "Item '" + line + "' not found\n";
00217             continue;
00218         }
00219     }
00220     strm.close();
00221     msgOut = msg.str();
00222     return result;
00223 }
00224 
00225 
00226 std::string
00227 GUISelectedStorage::load(const std::string& filename, GUIGlObjectType type) {
00228     std::string errors;
00229     const std::set<GUIGlID> ids = loadIDs(filename, errors, type);
00230     for (std::set<GUIGlID>::const_iterator it = ids.begin(); it != ids.end(); it++) {
00231         select(*it, false);
00232     }
00233     if (myUpdateTarget) {
00234         myUpdateTarget->selectionUpdated();
00235     }
00236     return errors;
00237 }
00238 
00239 
00240 void
00241 GUISelectedStorage::save(GUIGlObjectType type, const std::string& filename) {
00242     mySelections[type].save(filename);
00243 }
00244 
00245 
00246 void
00247 GUISelectedStorage::save(const std::string& filename) const {
00248     save(filename, myAllSelected);
00249 }
00250 
00251 
00252 void
00253 GUISelectedStorage::add2Update(UpdateTarget* updateTarget) {
00254     myUpdateTarget = updateTarget;
00255 }
00256 
00257 
00258 void
00259 GUISelectedStorage::remove2Update() {
00260     myUpdateTarget = 0;
00261 }
00262 
00263 
00264 void
00265 GUISelectedStorage::save(const std::string& filename, const std::set<GUIGlID>& ids) {
00266     OutputDevice& dev = OutputDevice::getDevice(filename);
00267     for (std::set<GUIGlID>::const_iterator i = ids.begin(); i != ids.end(); ++i) {
00268         GUIGlObject* object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(*i);
00269         if (object != 0) {
00270             std::string name = object->getFullName();
00271             dev << name << "\n";
00272             GUIGlObjectStorage::gIDStorage.unblockObject(*i);
00273         }
00274     }
00275     dev.close();
00276 }
00277 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines