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 2001-2010 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 <vector>
00031 #include <algorithm>
00032 #include <utils/gui/globjects/GUIGlObject.h>
00033 #include <utils/gui/globjects/GUIGlObjectStorage.h>
00034 #include "GUISelectedStorage.h"
00035 #include "GUIDialog_GLChosenEditor.h"
00036 #include <utils/iodevices/OutputDevice.h>
00037 #include <utils/common/ToString.h>
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // member method definitions
00046 // ===========================================================================
00047 /* -------------------------------------------------------------------------
00048  * for GUISelectedStorage::SingleTypeSelections
00049  * ----------------------------------------------------------------------- */
00050 GUISelectedStorage::SingleTypeSelections::SingleTypeSelections() throw() {}
00051 
00052 
00053 GUISelectedStorage::SingleTypeSelections::~SingleTypeSelections() throw() {}
00054 
00055 
00056 bool
00057 GUISelectedStorage::SingleTypeSelections::isSelected(GLuint id) throw() {
00058     std::vector<GLuint>::iterator i = find(mySelected.begin(), mySelected.end(), id);
00059     return i!=mySelected.end();
00060 }
00061 
00062 
00063 void
00064 GUISelectedStorage::SingleTypeSelections::select(GLuint id) throw() {
00065     std::vector<GLuint>::iterator i = find(mySelected.begin(), mySelected.end(), id);
00066     if (i==mySelected.end()) {
00067         mySelected.push_back(id);
00068     }
00069 }
00070 
00071 
00072 void
00073 GUISelectedStorage::SingleTypeSelections::deselect(GLuint id) throw() {
00074     std::vector<GLuint>::iterator i = find(mySelected.begin(), mySelected.end(), id);
00075     if (i!=mySelected.end()) {
00076         mySelected.erase(i);
00077     }
00078 }
00079 
00080 
00081 void
00082 GUISelectedStorage::SingleTypeSelections::clear() throw() {
00083     mySelected.clear();
00084 }
00085 
00086 
00087 void
00088 GUISelectedStorage::SingleTypeSelections::load(const std::string &filename) throw(IOError) {
00089     std::ifstream strm(filename.c_str());
00090     while (strm.good()) {
00091         std::string name;
00092         strm >> name;
00093     }
00094 }
00095 
00096 
00097 void
00098 GUISelectedStorage::SingleTypeSelections::save(const std::string &filename) throw(IOError) {
00099     OutputDevice &dev = OutputDevice::getDevice(filename);
00100     for (std::vector<GLuint>::iterator i=mySelected.begin(); i!=mySelected.end(); ++i) {
00101         GUIGlObject *object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(*i);
00102         if (object!=0) {
00103             std::string name = object->getFullName();
00104             dev << name << "\n";
00105             GUIGlObjectStorage::gIDStorage.unblockObject(*i);
00106         }
00107     }
00108     dev.close();
00109 }
00110 
00111 
00112 const std::vector<GLuint> &
00113 GUISelectedStorage::SingleTypeSelections::getSelected() const throw() {
00114     return mySelected;
00115 }
00116 
00117 
00118 
00119 /* -------------------------------------------------------------------------
00120  * for GUISelectedStorage
00121  * ----------------------------------------------------------------------- */
00122 GUISelectedStorage::GUISelectedStorage() throw() {}
00123 
00124 
00125 GUISelectedStorage::~GUISelectedStorage() throw() {}
00126 
00127 
00128 bool
00129 GUISelectedStorage::isSelected(int type, GLuint id) throw(ProcessError) {
00130     if (type==-1) {
00131         GUIGlObject *object =
00132             GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00133         if (object!=0) {
00134             type = object->getType();
00135             GUIGlObjectStorage::gIDStorage.unblockObject(id);
00136         } else {
00137             throw ProcessError("Unkown object in GUISelectedStorage::isSelected (id=" + toString(id) + ").");
00138         }
00139     }
00140     switch (type) {
00141     case GLO_NETWORK:
00142         return false;
00143     case GLO_VEHICLE:
00144         return mySelectedVehicles.isSelected(id);
00145     case GLO_TLLOGIC:
00146         return mySelectedTLLogics.isSelected(id);
00147     case GLO_DETECTOR:
00148         return mySelectedDetectors.isSelected(id);
00149     case GLO_EMITTER:
00150         return mySelectedEmitters.isSelected(id);
00151     case GLO_LANE:
00152         return mySelectedLanes.isSelected(id);
00153     case GLO_EDGE:
00154         return mySelectedEdges.isSelected(id);
00155     case GLO_JUNCTION:
00156         return mySelectedJunctions.isSelected(id);
00157     case GLO_TRIGGER:
00158         return mySelectedTriggers.isSelected(id);
00159     case GLO_SHAPE:
00160         return mySelectedShapes.isSelected(id);
00161     case GLO_ADDITIONAL:
00162         return
00163             mySelectedTriggers.isSelected(id)
00164             | mySelectedEmitters.isSelected(id)
00165             | mySelectedDetectors.isSelected(id);
00166     default:
00167         throw ProcessError("Unkown object type in GUISelectedStorage::isSelected (type=" + toString(type) + ").");
00168     }
00169 }
00170 
00171 
00172 void
00173 GUISelectedStorage::select(int type, GLuint id, bool update) throw(ProcessError) {
00174     if (type==-1) {
00175         GUIGlObject *object =
00176             GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00177         if (object!=0) {
00178             type = object->getType();
00179             GUIGlObjectStorage::gIDStorage.unblockObject(id);
00180         } else {
00181             throw ProcessError("Unkown object in GUISelectedStorage::select (id=" + toString(id) + ").");
00182         }
00183     }
00184     switch (type) {
00185     case GLO_VEHICLE:
00186         mySelectedVehicles.select(id);
00187         break;
00188     case GLO_TLLOGIC:
00189         mySelectedTLLogics.select(id);
00190         break;
00191     case GLO_DETECTOR:
00192         mySelectedDetectors.select(id);
00193         break;
00194     case GLO_EMITTER:
00195         mySelectedEmitters.select(id);
00196         break;
00197     case GLO_LANE:
00198         mySelectedLanes.select(id);
00199         break;
00200     case GLO_EDGE:
00201         mySelectedEdges.select(id);
00202         break;
00203     case GLO_JUNCTION:
00204         mySelectedJunctions.select(id);
00205         break;
00206     case GLO_TRIGGER:
00207         mySelectedTriggers.select(id);
00208         break;
00209     case GLO_SHAPE:
00210         mySelectedShapes.select(id);
00211         break;
00212     default:
00213         throw ProcessError("Unkown object type in GUISelectedStorage::select (type=" + toString(type) + ").");
00214     }
00215     std::vector<GLuint>::iterator i = find(mySelected.begin(), mySelected.end(), id);
00216     if (i==mySelected.end()) {
00217         mySelected.push_back(id);
00218     }
00219     if (update&&my2Update!=0) {
00220         my2Update->rebuildList();
00221         my2Update->update();
00222     }
00223 }
00224 
00225 
00226 void
00227 GUISelectedStorage::deselect(int type, GLuint id) throw(ProcessError) {
00228     if (type==-1) {
00229         GUIGlObject *object =
00230             GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00231         if (object!=0) {
00232             type = object->getType();
00233             GUIGlObjectStorage::gIDStorage.unblockObject(id);
00234         } else {
00235             throw ProcessError("Unkown object in GUISelectedStorage::deselect (id=" + toString(id) + ").");
00236         }
00237     }
00238     switch (type) {
00239     case GLO_VEHICLE:
00240         mySelectedVehicles.deselect(id);
00241         break;
00242     case GLO_TLLOGIC:
00243         mySelectedTLLogics.deselect(id);
00244         break;
00245     case GLO_DETECTOR:
00246         mySelectedDetectors.deselect(id);
00247         break;
00248     case GLO_EMITTER:
00249         mySelectedEmitters.deselect(id);
00250         break;
00251     case GLO_LANE:
00252         mySelectedLanes.deselect(id);
00253         break;
00254     case GLO_EDGE:
00255         mySelectedEdges.deselect(id);
00256         break;
00257     case GLO_JUNCTION:
00258         mySelectedJunctions.deselect(id);
00259         break;
00260     case GLO_TRIGGER:
00261         mySelectedTriggers.deselect(id);
00262         break;
00263     case GLO_SHAPE:
00264         mySelectedShapes.deselect(id);
00265         break;
00266     default:
00267         throw ProcessError("Unkown object type in GUISelectedStorage::deselect (type=" + toString(type) + ").");
00268     }
00269     std::vector<GLuint>::iterator i = find(mySelected.begin(), mySelected.end(), id);
00270     if (i!=mySelected.end()) {
00271         mySelected.erase(i);
00272     }
00273     if (my2Update!=0) {
00274         my2Update->rebuildList();
00275         my2Update->update();
00276     }
00277 }
00278 
00279 
00280 void
00281 GUISelectedStorage::toggleSelection(GLuint id) throw(ProcessError) {
00282     GUIGlObject *o =
00283         GUIGlObjectStorage::gIDStorage.getObjectBlocking(id);
00284     if (o==0) {
00285         throw ProcessError("Unkown object in GUISelectedStorage::toggleSelection (id=" + toString(id) + ").");
00286     }
00287     bool selected = isSelected(-1, id);
00288     if (!selected) {
00289         select(o->getType(), id);
00290     } else {
00291         deselect(o->getType(), id);
00292     }
00293     GUIGlObjectStorage::gIDStorage.unblockObject(id);
00294 }
00295 
00296 
00297 const std::vector<GLuint> &
00298 GUISelectedStorage::getSelected() const throw() {
00299     return mySelected;
00300 }
00301 
00302 
00303 const std::vector<GLuint> &
00304 GUISelectedStorage::getSelected(GUIGlObjectType type) const throw(ProcessError) {
00305     switch (type) {
00306     case GLO_VEHICLE:
00307         return mySelectedVehicles.getSelected();
00308     case GLO_TLLOGIC:
00309         return mySelectedTLLogics.getSelected();
00310     case GLO_DETECTOR:
00311         return mySelectedDetectors.getSelected();
00312     case GLO_EMITTER:
00313         return mySelectedEmitters.getSelected();
00314     case GLO_LANE:
00315         return mySelectedLanes.getSelected();
00316     case GLO_EDGE:
00317         return mySelectedEdges.getSelected();
00318     case GLO_JUNCTION:
00319         return mySelectedJunctions.getSelected();
00320     case GLO_TRIGGER:
00321         return mySelectedTriggers.getSelected();
00322     case GLO_SHAPE:
00323         return mySelectedShapes.getSelected();
00324     }
00325     throw ProcessError("Unkown object type in GUISelectedStorage::getSelected (type=" + toString(type) + ").");
00326 }
00327 
00328 
00329 void
00330 GUISelectedStorage::clear() throw() {
00331     mySelectedVehicles.clear();
00332     mySelectedTLLogics.clear();
00333     mySelectedDetectors.clear();
00334     mySelectedEmitters.clear();
00335     mySelectedLanes.clear();
00336     mySelectedEdges.clear();
00337     mySelectedJunctions.clear();
00338     mySelectedTriggers.clear();
00339     mySelectedShapes.clear();
00340     mySelected.clear();
00341     if (my2Update!=0) {
00342         my2Update->rebuildList();
00343         my2Update->update();
00344     }
00345 }
00346 
00347 
00348 void
00349 GUISelectedStorage::load(int type, const std::string &filename) throw(IOError) {
00350     if (type!=-1) {
00351         switch (type) {
00352         case GLO_VEHICLE:
00353             mySelectedVehicles.load(filename);
00354             break;
00355         case GLO_TLLOGIC:
00356             mySelectedTLLogics.load(filename);
00357             break;
00358         case GLO_DETECTOR:
00359             mySelectedDetectors.load(filename);
00360             break;
00361         case GLO_EMITTER:
00362             mySelectedEmitters.load(filename);
00363             break;
00364         case GLO_LANE:
00365             mySelectedLanes.load(filename);
00366             break;
00367         case GLO_EDGE:
00368             mySelectedEdges.load(filename);
00369             break;
00370         case GLO_JUNCTION:
00371             mySelectedJunctions.load(filename);
00372             break;
00373         case GLO_TRIGGER:
00374             mySelectedTriggers.load(filename);
00375             break;
00376         case GLO_SHAPE:
00377             mySelectedShapes.load(filename);
00378             break;
00379         default:
00380             throw ProcessError("Unkown object type in GUISelectedStorage::load (type=" + toString(type) + ").");
00381         }
00382         return;
00383     }
00384 
00385 }
00386 
00387 
00388 void
00389 GUISelectedStorage::save(int type, const std::string &filename) throw(IOError) {
00390     if (type!=-1) {
00391         switch (type) {
00392         case GLO_VEHICLE:
00393             mySelectedVehicles.save(filename);
00394             break;
00395         case GLO_TLLOGIC:
00396             mySelectedTLLogics.save(filename);
00397             break;
00398         case GLO_DETECTOR:
00399             mySelectedDetectors.save(filename);
00400             break;
00401         case GLO_EMITTER:
00402             mySelectedEmitters.save(filename);
00403             break;
00404         case GLO_LANE:
00405             mySelectedLanes.save(filename);
00406             break;
00407         case GLO_EDGE:
00408             mySelectedEdges.save(filename);
00409             break;
00410         case GLO_JUNCTION:
00411             mySelectedJunctions.save(filename);
00412             break;
00413         case GLO_TRIGGER:
00414             mySelectedTriggers.save(filename);
00415             break;
00416         case GLO_SHAPE:
00417             mySelectedShapes.save(filename);
00418             break;
00419         default:
00420             throw ProcessError("Unkown object type in GUISelectedStorage::load (type=" + toString(type) + ").");
00421         }
00422         return;
00423     }
00424     // ok, save all
00425     OutputDevice &dev = OutputDevice::getDevice(filename);
00426     for (std::vector<GLuint>::iterator i=mySelected.begin(); i!=mySelected.end(); ++i) {
00427         GUIGlObject *object = GUIGlObjectStorage::gIDStorage.getObjectBlocking(*i);
00428         if (object!=0) {
00429             std::string name = object->getFullName();
00430             dev << name << '\n';
00431             GUIGlObjectStorage::gIDStorage.unblockObject(*i);
00432         }
00433     }
00434     dev.close();
00435 }
00436 
00437 
00438 void
00439 GUISelectedStorage::add2Update(GUIDialog_GLChosenEditor *ed) throw() {
00440     my2Update = ed;
00441 }
00442 
00443 
00444 void
00445 GUISelectedStorage::remove2Update(GUIDialog_GLChosenEditor *) throw() {
00446     my2Update = 0;
00447 }
00448 
00449 
00450 
00451 /****************************************************************************/
00452 

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6