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