Path: blob/main/src/netedit/dialogs/tools/GNERunPythonToolDialog.cpp
169684 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GNERunPythonToolDialog.cpp14/// @author Pablo Alvarez Lopez15/// @date Mar 202316///17// Dialog for running tools18/****************************************************************************/1920#include <netedit/GNEApplicationWindow.h>21#include <netedit/tools/GNEPythonTool.h>22#include <netedit/tools/GNERunPythonTool.h>23#include <utils/gui/div/GUIDesigns.h>24#include <utils/gui/events/GUIEvent_Message.h>2526#include "GNERunPythonToolDialog.h"2728// ===========================================================================29// Defines30// ===========================================================================3132#define MARGIN 43334// ===========================================================================35// FOX callback mapping36// ===========================================================================3738FXDEFMAP(GNERunPythonToolDialog) GNERunPythonToolDialogMap[] = {39FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_SAVE, GNERunPythonToolDialog::onCmdSaveLog),40FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_ABORT, GNERunPythonToolDialog::onCmdAbort),41FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_RERUN, GNERunPythonToolDialog::onCmdRerun),42FXMAPFUNC(SEL_COMMAND, MID_GNE_BUTTON_BACK, GNERunPythonToolDialog::onCmdBack),43// threads events44FXMAPFUNC(FXEX::SEL_THREAD_EVENT, ID_LOADTHREAD_EVENT, GNERunPythonToolDialog::onThreadEvent),45FXMAPFUNC(FXEX::SEL_THREAD, ID_LOADTHREAD_EVENT, GNERunPythonToolDialog::onThreadEvent)46};4748// Object implementation49FXIMPLEMENT(GNERunPythonToolDialog, GNEDialog, GNERunPythonToolDialogMap, ARRAYNUMBER(GNERunPythonToolDialogMap))5051// ============================================-===============================52// member method definitions53// ===========================================================================5455GNERunPythonToolDialog::GNERunPythonToolDialog(GNEApplicationWindow* applicationWindow, GNEPythonTool* tool) :56GNEDialog(applicationWindow, TL("Python Tool"), GUIIcon::TOOL_PYTHON,57GNEDialog::Buttons::ABORT_RERUN_BACK_CLOSE,58OpenType::MODAL, ResizeMode::RESIZABLE, 640, 480) {59// build the thread - io60myThreadEvent.setTarget(this);61myThreadEvent.setSelector(ID_LOADTHREAD_EVENT);62// create run tool63myRunTool = new GNERunPythonTool(this, myEvents, myThreadEvent);64// create header frame65auto headerFrame = new FXHorizontalFrame(myContentFrame, GUIDesignHorizontalFrame);66// adjust padding67headerFrame->setPadLeft(0);68headerFrame->setPadRight(0);69GUIDesigns::buildFXButton(headerFrame, "", "", + TL("Save output"),70GUIIconSubSys::getIcon(GUIIcon::SAVE), this, MID_GNE_BUTTON_SAVE, GUIDesignButtonIcon);71new FXLabel(headerFrame, TL("Console output"), nullptr, GUIDesignLabelThick(JUSTIFY_LEFT));72// create text73auto textFrame = new FXVerticalFrame(myContentFrame, GUIDesignFrameThick);74myText = new FXText(textFrame, 0, 0, (TEXT_READONLY | LAYOUT_FILL_X | LAYOUT_FILL_Y));75// set styled76myText->setHiliteStyles(GUIMessageWindow::getStyles());77myText->setStyled(true);78// set title79setTitle((tool->getToolName() + " output").c_str());80// refresh APP81getApp()->refresh();82// clear text83myText->setText("");84// show dialog85GNEDialog::show(PLACEMENT_SCREEN);86// set tool87myPythonTool = tool;88// open modal dialog89openDialog();90// run tool91myRunTool->runTool(tool);92}939495GNERunPythonToolDialog::~GNERunPythonToolDialog() {}969798void99GNERunPythonToolDialog::runInternalTest(const InternalTestStep::DialogArgument* /*dialogTest*/) {100// finish101}102103104void105GNERunPythonToolDialog::updateDialog() {106// update buttons107/*108if (myRunTool->isRunning()) {109myAbortButton->enable();110myRerunButton->disable();111myBackButton->disable();112myCloseButton->disable();113} else {114myAbortButton->disable();115myRerunButton->enable();116myBackButton->enable();117myCloseButton->enable();118}119*/120// update dialog121GNEDialog::update();122}123124125long126GNERunPythonToolDialog::onCmdSaveLog(FXObject*, FXSelector, void*) {127// get log file128const auto logFile = GNEApplicationWindowHelper::saveToolLog(this);129// check that file is valid130if (logFile.size() > 0) {131OutputDevice& dev = OutputDevice::getDevice(logFile);132dev << myText->getText().text();133dev.close();134}135return 1;136}137138139long140GNERunPythonToolDialog::onCmdAbort(FXObject*, FXSelector, void*) {141// abort tool142myRunTool->abortTool();143return 1;144}145146147long148GNERunPythonToolDialog::onCmdRerun(FXObject*, FXSelector, void*) {149// add line and info150std::string line("-------------------------------------------\n");151myText->appendStyledText(line.c_str(), (int)line.length(), 4, TRUE);152myText->appendStyledText("rerun tool\n", 1, TRUE);153myText->layout();154myText->update();155// run tool156myRunTool->runTool(myPythonTool);157return 1;158}159160161long162GNERunPythonToolDialog::onCmdBack(FXObject*, FXSelector, void*) {163// close runTool dialog and open tool dialog164onCmdClose(nullptr, 0, nullptr);165return myApplicationWindow->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_OPENPYTHONTOOLDIALOG), nullptr);166}167168169long170GNERunPythonToolDialog::onCmdCancel(FXObject* obj, FXSelector, void*) {171// abort tool172myRunTool->abortTool();173// hide dialog174hide();175return 1;176}177178179long180GNERunPythonToolDialog::onCmdAccept(FXObject* obj, FXSelector, void*) {181// abort tool182myRunTool->abortTool();183// execute post processing184myApplicationWindow->handle(myPythonTool->getMenuCommand(), FXSEL(SEL_COMMAND, MID_GNE_POSTPROCESSINGPYTHONTOOL), nullptr);185// hide dialog186hide();187return 1;188}189190191long192GNERunPythonToolDialog::onThreadEvent(FXObject*, FXSelector, void*) {193while (!myEvents.empty()) {194// get the next event195GUIEvent* e = myEvents.top();196myEvents.pop();197// process198FXint style = -1;199switch (e->getOwnType()) {200case GUIEventType::TOOL_ENDED:201break;202case GUIEventType::MESSAGE_OCCURRED:203style = 1;204break;205case GUIEventType::OUTPUT_OCCURRED:206style = 2;207break;208case GUIEventType::ERROR_OCCURRED:209style = 3;210break;211default:212break;213}214if (style >= 0) {215GUIEvent_Message* ec = static_cast<GUIEvent_Message*>(e);216myText->appendStyledText(ec->getMsg().c_str(), (int)ec->getMsg().length(), style, TRUE);217myText->layout();218myText->update();219}220delete e;221updateDialog();222}223return 1;224}225226/****************************************************************************/227228229