Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netedit/dialogs/tools/GNERunPythonToolDialog.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file GNERunPythonToolDialog.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Mar 2023
17
///
18
// Dialog for running tools
19
/****************************************************************************/
20
21
#include <netedit/GNEApplicationWindow.h>
22
#include <netedit/tools/GNEPythonTool.h>
23
#include <netedit/tools/GNERunPythonTool.h>
24
#include <utils/gui/div/GUIDesigns.h>
25
#include <utils/gui/events/GUIEvent_Message.h>
26
27
#include "GNERunPythonToolDialog.h"
28
29
// ===========================================================================
30
// Defines
31
// ===========================================================================
32
33
#define MARGIN 4
34
35
// ===========================================================================
36
// FOX callback mapping
37
// ===========================================================================
38
39
FXDEFMAP(GNERunPythonToolDialog) GNERunPythonToolDialogMap[] = {
40
FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_SAVE, GNERunPythonToolDialog::onCmdSaveLog),
41
FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_ABORT, GNERunPythonToolDialog::onCmdAbort),
42
FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_RERUN, GNERunPythonToolDialog::onCmdRerun),
43
FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_BACK, GNERunPythonToolDialog::onCmdBack),
44
// threads events
45
FXMAPFUNC(FXEX::SEL_THREAD_EVENT, ID_LOADTHREAD_EVENT, GNERunPythonToolDialog::onThreadEvent),
46
FXMAPFUNC(FXEX::SEL_THREAD, ID_LOADTHREAD_EVENT, GNERunPythonToolDialog::onThreadEvent)
47
};
48
49
// Object implementation
50
FXIMPLEMENT(GNERunPythonToolDialog, GNEDialog, GNERunPythonToolDialogMap, ARRAYNUMBER(GNERunPythonToolDialogMap))
51
52
// ============================================-===============================
53
// member method definitions
54
// ===========================================================================
55
56
GNERunPythonToolDialog::GNERunPythonToolDialog(GNEApplicationWindow* applicationWindow, GNEPythonTool* tool) :
57
GNEDialog(applicationWindow, TL("Python Tool"), GUIIcon::TOOL_PYTHON,
58
GNEDialog::Buttons::ABORT_RERUN_BACK_CLOSE,
59
OpenType::MODAL, ResizeMode::RESIZABLE, 640, 480) {
60
// build the thread - io
61
myThreadEvent.setTarget(this);
62
myThreadEvent.setSelector(ID_LOADTHREAD_EVENT);
63
// create run tool
64
myRunTool = new GNERunPythonTool(this, myEvents, myThreadEvent);
65
// create header frame
66
auto headerFrame = new FXHorizontalFrame(myContentFrame, GUIDesignHorizontalFrame);
67
// adjust padding
68
headerFrame->setPadLeft(0);
69
headerFrame->setPadRight(0);
70
GUIDesigns::buildFXButton(headerFrame, "", "", + TL("Save output"),
71
GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_GNE_BUTTON_SAVE, GUIDesignButtonIcon);
72
new FXLabel(headerFrame, TL("Console output"), nullptr, GUIDesignLabelThick(JUSTIFY_LEFT));
73
// create text
74
auto textFrame = new FXVerticalFrame(myContentFrame, GUIDesignFrameThick);
75
myText = new FXText(textFrame, 0, 0, (TEXT_READONLY | LAYOUT_FILL_X | LAYOUT_FILL_Y));
76
// set styled
77
myText->setHiliteStyles(GUIMessageWindow::getStyles());
78
myText->setStyled(true);
79
// set title
80
setTitle((tool->getToolName() + " output").c_str());
81
// refresh APP
82
getApp()->refresh();
83
// clear text
84
myText->setText("");
85
// show dialog
86
GNEDialog::show(PLACEMENT_SCREEN);
87
// set tool
88
myPythonTool = tool;
89
// open modal dialog
90
openDialog();
91
// run tool
92
myRunTool->runTool(tool);
93
}
94
95
96
GNERunPythonToolDialog::~GNERunPythonToolDialog() {}
97
98
99
void
100
GNERunPythonToolDialog::runInternalTest(const InternalTestStep::DialogArgument* /*dialogTest*/) {
101
// finish
102
}
103
104
105
void
106
GNERunPythonToolDialog::updateDialog() {
107
// update buttons
108
/*
109
if (myRunTool->isRunning()) {
110
myAbortButton->enable();
111
myRerunButton->disable();
112
myBackButton->disable();
113
myCloseButton->disable();
114
} else {
115
myAbortButton->disable();
116
myRerunButton->enable();
117
myBackButton->enable();
118
myCloseButton->enable();
119
}
120
*/
121
// update dialog
122
GNEDialog::update();
123
}
124
125
126
long
127
GNERunPythonToolDialog::onCmdSaveLog(FXObject*, FXSelector, void*) {
128
// get log file
129
const auto logFile = GNEApplicationWindowHelper::saveToolLog(this);
130
// check that file is valid
131
if (logFile.size() > 0) {
132
OutputDevice& dev = OutputDevice::getDevice(logFile);
133
dev << myText->getText().text();
134
dev.close();
135
}
136
return 1;
137
}
138
139
140
long
141
GNERunPythonToolDialog::onCmdAbort(FXObject*, FXSelector, void*) {
142
// abort tool
143
myRunTool->abortTool();
144
return 1;
145
}
146
147
148
long
149
GNERunPythonToolDialog::onCmdRerun(FXObject*, FXSelector, void*) {
150
// add line and info
151
std::string line("-------------------------------------------\n");
152
myText->appendStyledText(line.c_str(), (int)line.length(), 4, TRUE);
153
myText->appendStyledText("rerun tool\n", 1, TRUE);
154
myText->layout();
155
myText->update();
156
// run tool
157
myRunTool->runTool(myPythonTool);
158
return 1;
159
}
160
161
162
long
163
GNERunPythonToolDialog::onCmdBack(FXObject*, FXSelector, void*) {
164
// close runTool dialog and open tool dialog
165
onCmdClose(nullptr, 0, nullptr);
166
return myApplicationWindow->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_OPENPYTHONTOOLDIALOG), nullptr);
167
}
168
169
170
long
171
GNERunPythonToolDialog::onCmdCancel(FXObject* obj, FXSelector, void*) {
172
// abort tool
173
myRunTool->abortTool();
174
// hide dialog
175
hide();
176
return 1;
177
}
178
179
180
long
181
GNERunPythonToolDialog::onCmdAccept(FXObject* obj, FXSelector, void*) {
182
// abort tool
183
myRunTool->abortTool();
184
// execute post processing
185
myApplicationWindow->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_POSTPROCESSINGPYTHONTOOL), nullptr);
186
// hide dialog
187
hide();
188
return 1;
189
}
190
191
192
long
193
GNERunPythonToolDialog::onThreadEvent(FXObject*, FXSelector, void*) {
194
while (!myEvents.empty()) {
195
// get the next event
196
GUIEvent* e = myEvents.top();
197
myEvents.pop();
198
// process
199
FXint style = -1;
200
switch (e->getOwnType()) {
201
case GUIEventType::TOOL_ENDED:
202
break;
203
case GUIEventType::MESSAGE_OCCURRED:
204
style = 1;
205
break;
206
case GUIEventType::OUTPUT_OCCURRED:
207
style = 2;
208
break;
209
case GUIEventType::ERROR_OCCURRED:
210
style = 3;
211
break;
212
default:
213
break;
214
}
215
if (style >= 0) {
216
GUIEvent_Message* ec = static_cast<GUIEvent_Message*>(e);
217
myText->appendStyledText(ec->getMsg().c_str(), (int)ec->getMsg().length(), style, TRUE);
218
myText->layout();
219
myText->update();
220
}
221
delete e;
222
updateDialog();
223
}
224
return 1;
225
}
226
227
/****************************************************************************/
228
229