SUMO - Simulation of Urban MObility
GUICompleteSchemeStorage.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00010 // Storage for available visualization settings
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include "GUICompleteSchemeStorage.h"
00035 #include <utils/common/ToString.h>
00036 #include <utils/common/StringUtils.h>
00037 #include <utils/common/RGBColor.h>
00038 #include <utils/foxtools/MFXUtils.h>
00039 #include <utils/gui/settings/GUISettingsHandler.h>
00040 #include <utils/iodevices/OutputDevice_String.h>
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // static variable definitions
00049 // ===========================================================================
00050 GUICompleteSchemeStorage gSchemeStorage;
00051 
00052 
00053 // ===========================================================================
00054 // method definitions
00055 // ===========================================================================
00056 GUICompleteSchemeStorage::GUICompleteSchemeStorage() { }
00057 
00058 
00059 GUICompleteSchemeStorage::~GUICompleteSchemeStorage() { }
00060 
00061 
00062 
00063 void
00064 GUICompleteSchemeStorage::add(const GUIVisualizationSettings& scheme) {
00065     std::string name = scheme.name;
00066     if (std::find(mySortedSchemeNames.begin(), mySortedSchemeNames.end(), name) == mySortedSchemeNames.end()) {
00067         mySortedSchemeNames.push_back(name);
00068     }
00069     mySettings[name] = scheme;
00070 }
00071 
00072 
00073 GUIVisualizationSettings&
00074 GUICompleteSchemeStorage::get(const std::string& name) {
00075     return mySettings.find(name)->second;
00076 }
00077 
00078 
00079 GUIVisualizationSettings&
00080 GUICompleteSchemeStorage::getDefault() {
00081     return mySettings.find(myDefaultSettingName)->second;
00082 }
00083 
00084 
00085 bool
00086 GUICompleteSchemeStorage::contains(const std::string& name) const {
00087     return mySettings.find(name) != mySettings.end();
00088 }
00089 
00090 
00091 void
00092 GUICompleteSchemeStorage::remove(const std::string& name) {
00093     if (!contains(name)) {
00094         return;
00095     }
00096     mySortedSchemeNames.erase(find(mySortedSchemeNames.begin(), mySortedSchemeNames.end(), name));
00097     mySettings.erase(mySettings.find(name));
00098 }
00099 
00100 
00101 void
00102 GUICompleteSchemeStorage::setDefault(const std::string& name) {
00103     if (!contains(name)) {
00104         return;
00105     }
00106     myDefaultSettingName = name;
00107 }
00108 
00109 
00110 const std::vector<std::string> &
00111 GUICompleteSchemeStorage::getNames() const {
00112     return mySortedSchemeNames;
00113 }
00114 
00115 
00116 unsigned int
00117 GUICompleteSchemeStorage::getNumInitialSettings() const {
00118     return myNumInitialSettings;
00119 }
00120 
00121 
00122 RGBColor
00123 convert(const FXColor c) {
00124     return RGBColor(
00125                (SUMOReal) FXREDVAL(c) / (SUMOReal) 255.,
00126                (SUMOReal) FXGREENVAL(c) / (SUMOReal) 255.,
00127                (SUMOReal) FXBLUEVAL(c) / (SUMOReal) 255.);
00128 }
00129 
00130 
00131 void
00132 GUICompleteSchemeStorage::init(FXApp* app) {
00133     {
00134         GUIVisualizationSettings vs;
00135         vs.name = "standard";
00136         gSchemeStorage.add(vs);
00137     }
00138     {
00139         GUIVisualizationSettings vs;
00140         vs.name = "faster standard";
00141         vs.showLinkDecals = false;
00142         vs.showRails = false;
00143         gSchemeStorage.add(vs);
00144     }
00145     {
00146         GUIVisualizationSettings vs;
00147         vs.name = "real world";
00148         vs.vehicleQuality = 2;
00149         vs.backgroundColor = RGBColor((SUMOReal) .2, (SUMOReal) .5, (SUMOReal) .2);
00150         vs.laneShowBorders = true;
00151         vs.hideConnectors = true;
00152         vs.minVehicleSize = 0;
00153         gSchemeStorage.add(vs);
00154     }
00155     myNumInitialSettings = (unsigned int) mySortedSchemeNames.size();
00156     // add saved settings
00157     int noSaved = app->reg().readIntEntry("VisualizationSettings", "settingNo", 0);
00158     for (int i = 0; i < noSaved; ++i) {
00159         std::string name = "visset#" + toString(i);
00160         std::string setting = app->reg().readStringEntry("VisualizationSettings", name.c_str(), "");
00161         if (setting != "") {
00162             GUIVisualizationSettings vs;
00163 
00164             vs.name = setting;
00165             app->reg().readStringEntry("VisualizationSettings", name.c_str(), "");
00166 
00167             // add saved xml setting
00168             int xmlSize = app->reg().readIntEntry(name.c_str(), "xmlSize", 0);
00169             std::string content = "";
00170             int index = 0;
00171             while (xmlSize > 0) {
00172                 std::string part = app->reg().readStringEntry(name.c_str(), ("xml" + toString(index)).c_str(), "");
00173                 if (part == "") {
00174                     break;
00175                 }
00176                 content += part;
00177                 xmlSize -= (int) part.size();
00178                 index++;
00179             }
00180             if (content != "" && xmlSize == 0) {
00181                 try {
00182                     GUISettingsHandler handler(content, false);
00183                     handler.addSettings();
00184                 } catch (ProcessError) { }
00185             }
00186         }
00187     }
00188     myDefaultSettingName = mySortedSchemeNames[0];
00189     myX = myY = myZoom = 0;
00190 }
00191 
00192 
00193 void
00194 GUICompleteSchemeStorage::writeSettings(FXApp* app) {
00195     const std::vector<std::string> &names = getNames();
00196     app->reg().writeIntEntry("VisualizationSettings", "settingNo", (FXint) names.size() - myNumInitialSettings);
00197     size_t gidx = 0;
00198     for (std::vector<std::string>::const_iterator i = names.begin() + myNumInitialSettings; i != names.end(); ++i, ++gidx) {
00199         const GUIVisualizationSettings& item = mySettings.find(*i)->second;
00200         std::string sname = "visset#" + toString(gidx);
00201 
00202         app->reg().writeStringEntry("VisualizationSettings", sname.c_str(), item.name.c_str());
00203         OutputDevice_String dev;
00204         item.save(dev);
00205         std::string content = dev.getString();
00206         app->reg().writeIntEntry(sname.c_str(), "xmlSize", (FXint)(content.size()));
00207         const unsigned maxSize = 1500; // this is a fox limitation for registry entries
00208         for (unsigned int i = 0; i < content.size(); i += maxSize) {
00209             const std::string b = content.substr(i, maxSize);
00210             app->reg().writeStringEntry(sname.c_str(), ("xml" + toString(i / maxSize)).c_str(), b.c_str());
00211         }
00212     }
00213 }
00214 
00215 
00216 void
00217 GUICompleteSchemeStorage::saveViewport(const SUMOReal x, const SUMOReal y, const SUMOReal zoom) {
00218     myX = x;
00219     myY = y;
00220     myZoom = zoom;
00221 }
00222 
00223 
00224 void
00225 GUICompleteSchemeStorage::setViewport(GUISUMOAbstractView* view) {
00226     if (myZoom > 0) {
00227         view->setViewport(myZoom, myX, myY);
00228     } else {
00229         view->recenterView();
00230     }
00231 }
00232 
00233 
00234 /****************************************************************************/
00235 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines