SUMO - Simulation of Urban MObility

GUISelectedStorage.cpp

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