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