SUMO - Simulation of Urban MObility
GUIDialog_Breakpoints.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Editor for simulation breakpoints
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 <string>
00034 #include <vector>
00035 #include <iostream>
00036 #include <fstream>
00037 #include <set>
00038 #include <gui/GUIApplicationWindow.h>
00039 #include <utils/gui/windows/GUIAppEnum.h>
00040 #include <gui/GUIGlobals.h>
00041 #include <utils/gui/globjects/GUIGlObject.h>
00042 #include <utils/foxtools/MFXUtils.h>
00043 #include <utils/common/ToString.h>
00044 #include <utils/common/TplConvert.h>
00045 #include "GUIDialog_Breakpoints.h"
00046 #include <utils/gui/windows/GUISUMOAbstractView.h>
00047 #include <utils/foxtools/MFXAddEditTypedTable.h>
00048 #include <utils/common/FileHelpers.h>
00049 #include <utils/common/MsgHandler.h>
00050 #include <utils/options/OptionsCont.h>
00051 #include <utils/gui/div/GUIIOGlobals.h>
00052 #include <utils/gui/images/GUIIconSubSys.h>
00053 #include <utils/iodevices/OutputDevice.h>
00054 
00055 #ifdef CHECK_MEMORY_LEAKS
00056 #include <foreign/nvwa/debug_new.h>
00057 #endif // CHECK_MEMORY_LEAKS
00058 
00059 
00060 // ===========================================================================
00061 // definitions
00062 // ===========================================================================
00063 #define INVALID_VALUE -1
00064 #define INVALID_VALUE_STR "-1"
00065 
00066 
00067 // ===========================================================================
00068 // FOX callback mapping
00069 // ===========================================================================
00070 FXDEFMAP(GUIDialog_Breakpoints) GUIDialog_BreakpointsMap[] = {
00071     FXMAPFUNC(SEL_COMMAND,  MID_CHOOSEN_LOAD,                   GUIDialog_Breakpoints::onCmdLoad),
00072     FXMAPFUNC(SEL_COMMAND,  MID_CHOOSEN_SAVE,                   GUIDialog_Breakpoints::onCmdSave),
00073     FXMAPFUNC(SEL_COMMAND,  MID_CHOOSEN_CLEAR,                  GUIDialog_Breakpoints::onCmdClear),
00074     FXMAPFUNC(SEL_COMMAND,  MID_CANCEL,                         GUIDialog_Breakpoints::onCmdClose),
00075     FXMAPFUNC(SEL_CHANGED,  MFXAddEditTypedTable::ID_TEXT_CHANGED,  GUIDialog_Breakpoints::onCmdEditTable),
00076 };
00077 
00078 
00079 FXIMPLEMENT(GUIDialog_Breakpoints, FXMainWindow, GUIDialog_BreakpointsMap, ARRAYNUMBER(GUIDialog_BreakpointsMap))
00080 
00081 
00082 // ===========================================================================
00083 // method definitions
00084 // ===========================================================================
00085 GUIDialog_Breakpoints::GUIDialog_Breakpoints(GUIMainWindow* parent)
00086     : FXMainWindow(parent->getApp(), "Breakpoints Editor", NULL, NULL, DECOR_ALL, 20, 20, 300, 300),
00087       myParent(parent) {
00088     FXHorizontalFrame* hbox = new FXHorizontalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y, 0, 0, 0, 0, 0, 0, 0, 0);
00089 
00090     // build the table
00091     myTable = new MFXAddEditTypedTable(hbox, this, MID_TABLE, LAYOUT_FILL_X | LAYOUT_FILL_Y);
00092     myTable->setVisibleRows(20);
00093     myTable->setVisibleColumns(1);
00094     myTable->setTableSize(20, 1);
00095     myTable->setBackColor(FXRGB(255, 255, 255));
00096     myTable->setCellType(0, CT_REAL);
00097     SUMOTime begin = string2time(OptionsCont::getOptions().getString("begin"));
00098     SUMOTime end = string2time(OptionsCont::getOptions().getString("end"));
00099     if (end < 0) {
00100         end = SUMOTime_MAX;
00101     }
00102     myTable->setNumberCellParams(0, begin / 1000, end / 1000, 1, 10, 100, "%.2f");
00103     myTable->getRowHeader()->setWidth(0);
00104     rebuildList();
00105     // build the layout
00106     FXVerticalFrame* layout = new FXVerticalFrame(hbox, LAYOUT_TOP, 0, 0, 0, 0, 4, 4, 4, 4);
00107     // "Load"
00108     new FXButton(layout, "Load\t\t", 0, this, MID_CHOOSEN_LOAD, ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED, 0, 0, 0, 0, 4, 4, 3, 3);
00109     // "Save"
00110     new FXButton(layout, "Save\t\t", 0, this, MID_CHOOSEN_SAVE, ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED, 0, 0, 0, 0, 4, 4, 3, 3);
00111     new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
00112     // "Clear List"
00113     new FXButton(layout, "Clear\t\t", 0, this, MID_CHOOSEN_CLEAR, ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED, 0, 0, 0, 0, 4, 4, 3, 3);
00114     new FXHorizontalSeparator(layout, SEPARATOR_GROOVE | LAYOUT_FILL_X);
00115     // "Close"
00116     new FXButton(layout, "Close\t\t", 0, this, MID_CANCEL, ICON_BEFORE_TEXT | LAYOUT_FILL_X | FRAME_THICK | FRAME_RAISED, 0, 0, 0, 0, 4, 4, 3, 3);
00117     //
00118     setIcon(GUIIconSubSys::getIcon(ICON_APP_BREAKPOINTS));
00119     myParent->addChild(this);
00120 }
00121 
00122 
00123 GUIDialog_Breakpoints::~GUIDialog_Breakpoints() {
00124     myParent->removeChild(this);
00125 }
00126 
00127 
00128 void
00129 GUIDialog_Breakpoints::rebuildList() {
00130     myTable->clearItems();
00131     sort(GUIGlobals::gBreakpoints.begin(), GUIGlobals::gBreakpoints.end());
00132     // set table attributes
00133     myTable->setTableSize((FXint) GUIGlobals::gBreakpoints.size() + 1, 1);
00134     myTable->setColumnText(0, "Time");
00135     FXHeader* header = myTable->getColumnHeader();
00136     header->setHeight(getApp()->getNormalFont()->getFontHeight() + getApp()->getNormalFont()->getFontAscent());
00137     int k;
00138     for (k = 0; k < 1; k++) {
00139         header->setItemJustify(k, JUSTIFY_CENTER_X);
00140     }
00141     // insert into table
00142     FXint row = 0;
00143     std::vector<int>::iterator j;
00144     for (j = GUIGlobals::gBreakpoints.begin(); j != GUIGlobals::gBreakpoints.end(); ++j) {
00145         myTable->setItemText(row, 0, time2string(*j).c_str());
00146         row++;
00147     }
00148     // insert dummy last field
00149     for (k = 0; k < 1; k++) {
00150         myTable->setItemText(row, k, " ");
00151     }
00152 }
00153 
00154 
00155 long
00156 GUIDialog_Breakpoints::onCmdLoad(FXObject*, FXSelector, void*) {
00157     FXFileDialog opendialog(this, "Save Breakpoints");
00158     opendialog.setIcon(GUIIconSubSys::getIcon(ICON_EMPTY));
00159     opendialog.setSelectMode(SELECTFILE_ANY);
00160     opendialog.setPatternList("*.txt");
00161     if (gCurrentFolder.length() != 0) {
00162         opendialog.setDirectory(gCurrentFolder);
00163     }
00164     if (opendialog.execute()) {
00165         gCurrentFolder = opendialog.getDirectory();
00166         std::string file = opendialog.getFilename().text();
00167         std::ifstream strm(file.c_str());
00168         while (strm.good()) {
00169             std::string val;
00170             strm >> val;
00171             try {
00172                 SUMOTime value = string2time(val);
00173                 GUIGlobals::gBreakpoints.push_back(value);
00174             } catch (NumberFormatException&) {
00175                 WRITE_ERROR(" A breakpoint-value must be an int, is:" + val);
00176             } catch (EmptyData&) {}
00177         }
00178         rebuildList();
00179     }
00180     return 1;
00181 }
00182 
00183 
00184 long
00185 GUIDialog_Breakpoints::onCmdSave(FXObject*, FXSelector, void*) {
00186     FXString file = MFXUtils::getFilename2Write(this, "Save Breakpoints", ".txt", GUIIconSubSys::getIcon(ICON_EMPTY), gCurrentFolder);
00187     if (file == "") {
00188         return 1;
00189     }
00190     std::string content = encode2TXT();
00191     try {
00192         OutputDevice& dev = OutputDevice::getDevice(file.text());
00193         dev << content;
00194         dev.close();
00195     } catch (IOError& e) {
00196         FXMessageBox::error(this, MBOX_OK, "Storing failed!", "%s", e.what());
00197     }
00198     return 1;
00199 }
00200 
00201 
00202 std::string
00203 GUIDialog_Breakpoints::encode2TXT() {
00204     std::ostringstream strm;
00205     std::sort(GUIGlobals::gBreakpoints.begin(), GUIGlobals::gBreakpoints.end());
00206     for (std::vector<int>::iterator j = GUIGlobals::gBreakpoints.begin(); j != GUIGlobals::gBreakpoints.end(); ++j) {
00207         if ((*j) != INVALID_VALUE) {
00208             strm << time2string(*j) << std::endl;
00209         }
00210     }
00211     return strm.str();
00212 }
00213 
00214 
00215 long
00216 GUIDialog_Breakpoints::onCmdClear(FXObject*, FXSelector, void*) {
00217     GUIGlobals::gBreakpoints.clear();
00218     rebuildList();
00219     return 1;
00220 }
00221 
00222 
00223 
00224 long
00225 GUIDialog_Breakpoints::onCmdClose(FXObject*, FXSelector, void*) {
00226     close(true);
00227     return 1;
00228 }
00229 
00230 
00231 long
00232 GUIDialog_Breakpoints::onCmdEditTable(FXObject*, FXSelector, void* data) {
00233     MFXEditedTableItem* i = (MFXEditedTableItem*) data;
00234     std::string value = i->item->getText().text();
00235     // check whether the inserted value is empty
00236     if (value.find_first_not_of(" ") == std::string::npos) {
00237         // replace by invalid if so
00238         value = INVALID_VALUE_STR;
00239     }
00240     int row = i->row;
00241     if (row == (int) GUIGlobals::gBreakpoints.size()) {
00242         GUIGlobals::gBreakpoints.push_back(INVALID_VALUE);
00243     }
00244 
00245     switch (i->col) {
00246         case 0:
00247             try {
00248                 GUIGlobals::gBreakpoints[row] = string2time(value);
00249             } catch (NumberFormatException&) {
00250                 std::string msg = "The value must be an int, is:" + value;
00251                 FXMessageBox::error(this, MBOX_OK, "Number format error", "%s", msg.c_str());
00252             }
00253             break;
00254         default:
00255             break;
00256     }
00257     if (!i->updateOnly) {
00258         rebuildList();
00259     }
00260     return 1;
00261 }
00262 
00263 
00264 /****************************************************************************/
00265 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines