1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    GNEDemandElementDialog.cpp
11 /// @author  Pablo Alvarez Lopez
12 /// @date    Jan 2018
13 /// @version $Id$
14 ///
15 // A abstract class for editing additional elements
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <iostream>
24 #include <utils/gui/windows/GUIAppEnum.h>
25 #include <utils/gui/div/GUIDesigns.h>
26 #include <utils/gui/images/GUIIconSubSys.h>
27 #include <netedit/demandelements/GNEDemandElement.h>
28 #include <netedit/GNEViewNet.h>
29 #include <netedit/GNEUndoList.h>
30 
31 #include "GNEDemandElementDialog.h"
32 
33 // ===========================================================================
34 // FOX callback mapping
35 // ===========================================================================
36 
37 FXDEFMAP(GNEDemandElementDialog) GNEDemandElementDialogMap[] = {
38     FXMAPFUNC(SEL_KEYPRESS,     0,                                      GNEDemandElementDialog::onKeyPress),
39     FXMAPFUNC(SEL_KEYRELEASE,   0,                                      GNEDemandElementDialog::onKeyRelease),
40     FXMAPFUNC(SEL_CLOSE,        0,                                      GNEDemandElementDialog::onCmdCancel),
41     FXMAPFUNC(SEL_COMMAND,      MID_GNE_ADDITIONALDIALOG_BUTTONACCEPT,  GNEDemandElementDialog::onCmdAccept),
42     FXMAPFUNC(SEL_COMMAND,      MID_GNE_ADDITIONALDIALOG_BUTTONCANCEL,  GNEDemandElementDialog::onCmdCancel),
43     FXMAPFUNC(SEL_COMMAND,      MID_GNE_ADDITIONALDIALOG_BUTTONRESET,   GNEDemandElementDialog::onCmdReset),
44 };
45 
46 // Object abstract implementation
FXIMPLEMENT_ABSTRACT(GNEDemandElementDialog,FXTopWindow,GNEDemandElementDialogMap,ARRAYNUMBER (GNEDemandElementDialogMap))47 FXIMPLEMENT_ABSTRACT(GNEDemandElementDialog, FXTopWindow, GNEDemandElementDialogMap, ARRAYNUMBER(GNEDemandElementDialogMap))
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
52 
53 GNEDemandElementDialog::GNEDemandElementDialog(GNEDemandElement* editedDemandElement, bool updatingElement, int width, int height) :
54     FXTopWindow(editedDemandElement->getViewNet(), ("Edit '" + editedDemandElement->getID() + "' data").c_str(), editedDemandElement->getIcon(), editedDemandElement->getIcon(), GUIDesignDialogBoxExplicit(width, height)),
55     myEditedDemandElement(editedDemandElement),
56     myUpdatingElement(updatingElement),
57     myChangesDescription("change " + editedDemandElement->getTagStr() + " values"),
58     myNumberOfChanges(0) {
59     // create main frame
60     FXVerticalFrame* mainFrame = new FXVerticalFrame(this, GUIDesignAuxiliarFrame);
61     // Create frame for contents
62     myContentFrame = new FXVerticalFrame(mainFrame, GUIDesignContentsFrame);
63     // create buttons centered
64     FXHorizontalFrame* buttonsFrame = new FXHorizontalFrame(mainFrame, GUIDesignHorizontalFrame);
65     new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
66     myAcceptButton = new FXButton(buttonsFrame, "accept\t\tclose accepting changes",  GUIIconSubSys::getIcon(ICON_ACCEPT), this, MID_GNE_ADDITIONALDIALOG_BUTTONACCEPT, GUIDesignButtonAccept);
67     myCancelButton = new FXButton(buttonsFrame, "cancel\t\tclose discarding changes", GUIIconSubSys::getIcon(ICON_CANCEL), this, MID_GNE_ADDITIONALDIALOG_BUTTONCANCEL, GUIDesignButtonCancel);
68     myResetButton = new FXButton(buttonsFrame,  "reset\t\treset to previous values",  GUIIconSubSys::getIcon(ICON_RESET),  this, MID_GNE_ADDITIONALDIALOG_BUTTONRESET,  GUIDesignButtonReset);
69     new FXHorizontalFrame(buttonsFrame, GUIDesignAuxiliarHorizontalFrame);
70 }
71 
72 
~GNEDemandElementDialog()73 GNEDemandElementDialog::~GNEDemandElementDialog() {
74     // return focus to GNEViewNet to avoid minimization
75     getParent()->setFocus();
76 }
77 
78 
79 FXint
openAsModalDialog(FXuint placement)80 GNEDemandElementDialog::openAsModalDialog(FXuint placement) {
81     // create Dialog
82     create();
83     // show in the given position
84     show(placement);
85     // refresh APP
86     getApp()->refresh();
87     // open as modal dialog (will block all windows until stop() or stopModal() is called)
88     return getApp()->runModalFor(this);
89 }
90 
91 
92 GNEDemandElement*
getEditedDemandElement() const93 GNEDemandElementDialog::getEditedDemandElement() const {
94     return myEditedDemandElement;
95 }
96 
97 
98 long
onKeyPress(FXObject * sender,FXSelector sel,void * ptr)99 GNEDemandElementDialog::onKeyPress(FXObject* sender, FXSelector sel, void* ptr) {
100     return FXTopWindow::onKeyPress(sender, sel, ptr);
101 }
102 
103 
104 long
onKeyRelease(FXObject * sender,FXSelector sel,void * ptr)105 GNEDemandElementDialog::onKeyRelease(FXObject* sender, FXSelector sel, void* ptr) {
106     return FXTopWindow::onKeyRelease(sender, sel, ptr);
107 }
108 
109 
110 void
changeDemandElementDialogHeader(const std::string & newHeader)111 GNEDemandElementDialog::changeDemandElementDialogHeader(const std::string& newHeader) {
112     // change FXDialogBox title
113     setTitle(newHeader.c_str());
114 }
115 
116 
117 void
initChanges()118 GNEDemandElementDialog::initChanges() {
119     // init commandGroup
120     myEditedDemandElement->getViewNet()->getUndoList()->p_begin(myChangesDescription);
121     // save number of command group changes
122     myNumberOfChanges = myEditedDemandElement->getViewNet()->getUndoList()->currentCommandGroupSize();
123 }
124 
125 
126 void
acceptChanges()127 GNEDemandElementDialog::acceptChanges() {
128     // commit changes or abort last command group depending of number of changes did
129     if (myNumberOfChanges < myEditedDemandElement->getViewNet()->getUndoList()->currentCommandGroupSize()) {
130         myEditedDemandElement->getViewNet()->getUndoList()->p_end();
131     } else {
132         myEditedDemandElement->getViewNet()->getUndoList()->p_abortLastCommandGroup();
133     }
134 }
135 
136 
137 void
cancelChanges()138 GNEDemandElementDialog::cancelChanges() {
139     myEditedDemandElement->getViewNet()->getUndoList()->p_abortLastCommandGroup();
140 }
141 
142 
143 void
resetChanges()144 GNEDemandElementDialog::resetChanges() {
145     // abort last command group an start editing again
146     myEditedDemandElement->getViewNet()->getUndoList()->p_abortLastCommandGroup();
147     myEditedDemandElement->getViewNet()->getUndoList()->p_begin(myChangesDescription);
148 }
149 
150 /****************************************************************************/
151