Eclipse SUMO - Simulation of Urban MObility
GNERunPythonToolDialog.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Dialog for running tools
19 /****************************************************************************/
20 
26 
27 #include "GNERunPythonToolDialog.h"
28 
29 #define MARGING 4
30 
31 // ===========================================================================
32 // FOX callback mapping
33 // ===========================================================================
34 
35 FXDEFMAP(GNERunPythonToolDialog) GNERunPythonToolDialogMap[] = {
36  FXMAPFUNC(SEL_CLOSE, 0, GNERunPythonToolDialog::onCmdClose),
42  // threads events
45 };
46 
47 // Object implementation
48 FXIMPLEMENT(GNERunPythonToolDialog, FXDialogBox, GNERunPythonToolDialogMap, ARRAYNUMBER(GNERunPythonToolDialogMap))
49 
50 // ============================================-===============================
51 // member method definitions
52 // ===========================================================================
53 
55  FXDialogBox(GNEApp->getApp(), "Tool", GUIDesignAuxiliarDialogBoxResizable),
56  myGNEApp(GNEApp) {
57  // build the thread - io
58  myThreadEvent.setTarget(this);
59  myThreadEvent.setSelector(ID_LOADTHREAD_EVENT);
60  // create run tool
61  myRunTool = new GNERunPythonTool(this, myEvents, myThreadEvent);
62  // set icon
64  // create content frame
65  auto contentFrame = new FXVerticalFrame(this, GUIDesignAuxiliarFrame);
66  // create header frame
67  auto headerFrame = new FXHorizontalFrame(contentFrame, GUIDesignHorizontalFrame);
68  // adjust padding
69  headerFrame->setPadLeft(0);
70  headerFrame->setPadRight(0);
71  GUIDesigns::buildFXButton(headerFrame, "", "", + TL("Save output"),
73  new FXLabel(headerFrame, TL("Console output"), nullptr, GUIDesignLabelThick(JUSTIFY_LEFT));
74  // create text
75  auto textFrame = new FXVerticalFrame(contentFrame, GUIDesignFrameThick);
76  myText = new FXText(textFrame, 0, 0, (TEXT_READONLY | LAYOUT_FILL_X | LAYOUT_FILL_Y));
77  // set styled
78  myText->setHiliteStyles(GUIMessageWindow::getStyles());
79  myText->setStyled(true);
80  // create buttons Abort, rerun and back
81  auto buttonsFrame = new FXHorizontalFrame(contentFrame, GUIDesignHorizontalFrame);
82  new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
83  myAbortButton = GUIDesigns::buildFXButton(buttonsFrame, TL("Abort"), "", TL("abort running"),
85  myRerunButton = GUIDesigns::buildFXButton(buttonsFrame, TL("Rerun"), "", TL("rerun tool"),
87  myBackButton = GUIDesigns::buildFXButton(buttonsFrame, TL("Back"), "", TL("back to tool dialog"),
89  new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
90  // add separator
91  new FXSeparator(contentFrame);
92  // create button ok
93  buttonsFrame = new FXHorizontalFrame(contentFrame, GUIDesignHorizontalFrame);
94  new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
95  myCloseButton = GUIDesigns::buildFXButton(buttonsFrame, TL("Close"), "", TL("close dialog"),
97  new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
98  // resize
99  resize(640, 480);
100 }
101 
102 
104 
105 
108  return myGNEApp;
109 }
110 
111 
112 void
114  // set title
115  setTitle((tool->getToolName() + " output").c_str());
116  // refresh APP
117  getApp()->refresh();
118  // clear text
119  myText->setText("");
120  // show dialog
121  FXDialogBox::show(PLACEMENT_SCREEN);
122  // set tool
123  myPythonTool = tool;
124  // run tool
125  myRunTool->runTool(tool);
126 }
127 
128 
129 void
131  // update buttons
132  if (myRunTool->isRunning()) {
133  myAbortButton->enable();
134  myRerunButton->disable();
135  myBackButton->disable();
136  myCloseButton->disable();
137  } else {
138  myAbortButton->disable();
139  myRerunButton->enable();
140  myBackButton->enable();
141  myCloseButton->enable();
142  }
143  // update dialog
144  FXDialogBox::update();
145 }
146 
147 
148 long
149 GNERunPythonToolDialog::onCmdSaveLog(FXObject*, FXSelector, void*) {
150  // get log file
151  const auto logFile = GNEApplicationWindowHelper::saveToolLog(this);
152  // check that file is valid
153  if (logFile.size() > 0) {
154  OutputDevice& dev = OutputDevice::getDevice(logFile);
155  dev << myText->getText().text();
156  dev.close();
157  }
158  return 1;
159 }
160 
161 
162 long
163 GNERunPythonToolDialog::onCmdAbort(FXObject*, FXSelector, void*) {
164  // abort tool
165  myRunTool->abortTool();
166  return 1;
167 }
168 
169 
170 long
171 GNERunPythonToolDialog::onCmdRerun(FXObject*, FXSelector, void*) {
172  // add line and info
173  std::string line("-------------------------------------------\n");
174  myText->appendStyledText(line.c_str(), (int)line.length(), 4, TRUE);
175  myText->appendStyledText("rerun tool\n", 1, TRUE);
176  myText->layout();
177  myText->update();
178  // run tool
180  return 1;
181 }
182 
183 
184 long
185 GNERunPythonToolDialog::onCmdBack(FXObject*, FXSelector, void*) {
186  // close runTool dialog and open tool dialog
187  onCmdClose(nullptr, 0, nullptr);
188  return myGNEApp->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_OPENPYTHONTOOLDIALOG), nullptr);
189 }
190 
191 
192 long
193 GNERunPythonToolDialog::onCmdClose(FXObject* obj, FXSelector, void*) {
194  // abort tool
195  myRunTool->abortTool();
196  // check if we're closing using "close" button
197  if (obj == myCloseButton) {
198  // execute post processing
199  myGNEApp->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_POSTPROCESSINGPYTHONTOOL), nullptr);
200  }
201  // hide dialog
202  hide();
203  return 1;
204 }
205 
206 long
207 GNERunPythonToolDialog::onThreadEvent(FXObject*, FXSelector, void*) {
208  while (!myEvents.empty()) {
209  // get the next event
210  GUIEvent* e = myEvents.top();
211  myEvents.pop();
212  // process
213  FXint style = -1;
214  switch (e->getOwnType()) {
216  break;
218  style = 1;
219  break;
221  style = 2;
222  break;
224  style = 3;
225  break;
226  default:
227  break;
228  }
229  if (style >= 0) {
230  GUIEvent_Message* ec = static_cast<GUIEvent_Message*>(e);
231  myText->appendStyledText(ec->getMsg().c_str(), (int)ec->getMsg().length(), style, TRUE);
232  myText->layout();
233  myText->update();
234  }
235  delete e;
236  updateDialog();
237  }
238  return 1;
239 }
240 
241 
243  myGNEApp(nullptr) {
244 }
245 
246 /****************************************************************************/
FXDEFMAP(GNERunPythonToolDialog) GNERunPythonToolDialogMap[]
@ MID_GNE_OPENPYTHONTOOLDIALOG
call tool
Definition: GUIAppEnum.h:750
@ MID_GNE_POSTPROCESSINGPYTHONTOOL
call tool for post processing
Definition: GUIAppEnum.h:754
@ MID_GNE_BUTTON_RERUN
rerun button
Definition: GUIAppEnum.h:1412
@ MID_GNE_BUTTON_ABORT
abort button
Definition: GUIAppEnum.h:1414
@ MID_GNE_BUTTON_SAVE
save button
Definition: GUIAppEnum.h:1400
@ ID_LOADTHREAD_EVENT
The loading thread.
Definition: GUIAppEnum.h:344
@ MID_GNE_BUTTON_BACK
back button
Definition: GUIAppEnum.h:1416
@ MID_GNE_BUTTON_ACCEPT
accept button
Definition: GUIAppEnum.h:1392
#define GUIDesignFrameThick
Thick frame extended over frame parent.
Definition: GUIDesigns.h:331
#define GUIDesignButtonIcon
button only with icon
Definition: GUIDesigns.h:97
#define GUIDesignButtonAccept
Accept Button.
Definition: GUIDesigns.h:162
#define GUIDesignAuxiliarHorizontalFrame
design for auxiliar (Without borders) horizontal frame used to pack another frames
Definition: GUIDesigns.h:405
#define GUIDesignLabelThick(justify)
label extended over frame with thick and with text justify to left
Definition: GUIDesigns.h:255
#define GUIDesignAuxiliarDialogBoxResizable
design for standard dialog box (for example, about dialog)
Definition: GUIDesigns.h:614
#define GUIDesignButtonReset
Reset Button.
Definition: GUIDesigns.h:171
#define GUIDesignAuxiliarFrame
design for auxiliar (Without borders) frame extended in all directions
Definition: GUIDesigns.h:396
#define GUIDesignHorizontalFrame
Horizontal frame extended over frame parent with padding and spacing.
Definition: GUIDesigns.h:334
@ MESSAGE_OCCURRED
send when a message occured
@ ERROR_OCCURRED
send when a error occured
@ OUTPUT_OCCURRED
send when a tool produces output
@ TOOL_ENDED
send when a tool finishes
@ SAVE
save icons
#define TL(string)
Definition: MsgHandler.h:315
The main window of Netedit.
const std::string & getToolName() const
get tool name
FXMenuCommand * getMenuCommand() const
get menu command
Abstract dialog for tools.
GNERunPythonToolDialog()
FOX needs this.
GNEPythonTool * myPythonTool
tool
FXButton * myRerunButton
rerun button
void updateDialog()
update toolDialog
GNEApplicationWindow * myGNEApp
pointer to GNEApplicationWindow
long onCmdRerun(FXObject *, FXSelector, void *)
event after press rerun button
long onCmdBack(FXObject *, FXSelector, void *)
event after press back button
GNEApplicationWindow * getGNEApp() const
get to GNEApplicationWindow
FXButton * myCloseButton
close button
long onThreadEvent(FXObject *, FXSelector, void *)
called when the thread signals an event
GNERunPythonTool * myRunTool
thread for running tool
long onCmdSaveLog(FXObject *, FXSelector, void *)
event after press save button
FXButton * myBackButton
back button
long onCmdAbort(FXObject *, FXSelector, void *)
event after press abort button
void runTool(GNEPythonTool *tool)
run tool (this open windows)
MFXSynchQue< GUIEvent * > myEvents
List of received events.
long onCmdClose(FXObject *, FXSelector, void *)
event after press close button
FXButton * myAbortButton
abort button
Abstract dialog for tools.
bool isRunning() const
check if tool is running
void abortTool()
abort tool running
void runTool(const GNEPythonTool *tool)
run tool
static FXButton * buildFXButton(FXComposite *p, const std::string &text, const std::string &tip, const std::string &help, FXIcon *ic, FXObject *tgt, FXSelector sel, FXuint opts=BUTTON_NORMAL, FXint x=0, FXint y=0, FXint w=0, FXint h=0, FXint pl=DEFAULT_PAD, FXint pr=DEFAULT_PAD, FXint pt=DEFAULT_PAD, FXint pb=DEFAULT_PAD)
build button
Definition: GUIDesigns.cpp:128
const std::string & getMsg() const
Returns the message.
GUIEventType getOwnType() const
returns the event type
Definition: GUIEvent.h:89
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
static FXHiliteStyle * getStyles()
The text colors used.
void pop()
Definition: MFXSynchQue.h:64
bool empty()
Definition: MFXSynchQue.h:127
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
void close()
Closes the device and removes it from the dictionary.
static OutputDevice & getDevice(const std::string &name, bool usePrefix=true)
Returns the described OutputDevice.
@ SEL_THREAD_EVENT
Definition: fxexdefs.h:173
@ SEL_THREAD
Definition: fxexdefs.h:155
static std::string saveToolLog(FXWindow *window)
open tool file dialog