File: MMLComponent.cpp

package info (click to toggle)
camitk 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 343,588 kB
  • sloc: cpp: 78,476; xml: 1,210; sh: 723; ansic: 142; makefile: 101; perl: 84; sed: 20
file content (206 lines) | stat: -rw-r--r-- 7,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2016 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/

//-- MML Component
#include "MMLComponent.h"
#include "MMLDisplay.h"

//-- PML Component
#include <PMLComponent.h>

//-- MML includes
#include <MonitoringManager.h>
#include <MonitoringDialog.h>
#include <MonitoringGuiManager.h>

//-- CamiTK
#include <Application.h>
#include <MainWindow.h>
#include <Action.h>
#include <Log.h>

using namespace camitk;

//-- Qt
#include <QDockWidget>
#include <QMessageBox>

// -------------------- default constructor --------------------
MMLComponent::MMLComponent(const QString & fileName) throw(AbortException) : Component(fileName, QFileInfo(fileName).baseName()) {
    QString file = fileName;
    QFileInfo fi = QFileInfo(fileName);

    try {
        // initialize the MML export file name
        exportedMml = "";

        // create a simple MML from a Sofa scn file
        if (fi.completeSuffix() == "scn") {
            // create mml file in temp directory
            file = QDir::tempPath() + "/" + QFileInfo(fi).baseName() + ".mml";

            // check if the mml already exist
            if (QFileInfo(file).exists()) {
                CAMITK_WARNING("MMLComponent", "constructor", "Creating MMLComponent from " + fileName.toStdString() + ": mml file already exists in " + file.toStdString() + " (overwriting it)");
            }

            // create an empty mmlIn file with .scn (in the same directory as the .scn)
            mml::TimeParameter dt = mml::TimeParameter(0.1, "s");
            mml::TimeParameter refresh = mml::TimeParameter(1, "s");
            mml::MonitoringIn mmlIn(dt, refresh, "sofa");
            mmlIn.simulatorFile(fileName.toStdString().c_str());

            xml_schema::namespace_infomap map;
            map[""].name = "";
            map[""].schema = "";

            exportedMml = file;

            ofstream ofs(exportedMml.toStdString().c_str());
            mml::monitoringIn(ofs, mmlIn, map);
            // to make sure it will be saved again
            setModified();
        }

        mmlGUI = new MonitoringGuiManager;
        mmlGUI->getDialog()->hide();
        display = new MMLDisplay(this);

        // never selected yet
        neverSelected = true;

        if (mmlGUI->loadMmlInFile(file)) {
            // in order to give the ownership of the pml pointer to the PMLComponent, we need to use takePml() not getPml()
            pmlComponent = new PMLComponent( mmlGUI->getMonitoringManager()->takePml(),  mmlGUI->getMonitoringManager()->getPmlFileName().c_str());
            // in order to give the ownership of the lml pointer to the LoadsManager of the PMManagerDC, we need to use takeLml() not getLml()
            //TODO FIXME : display the LML, one way or another
            //pmlComponent->getLoadsManager()->setLoads(mmlGUI->getMonitoringManager()->takeLml());
            addChild(pmlComponent);
        } else {
            if (display) {
                delete display;
                display = NULL;
            }

            if (mmlGUI) {
                delete mmlGUI;
                mmlGUI = NULL;
            }
            throw AbortException("Cannot open "+ myFileName.toStdString() + "\nThe given simulator is probably not supported");
        }
    } catch (xml_schema::exception & e) {
        CAMITK_ERROR("MMLComponent", "constructor", "xml_schema exception, attempt to read from " + myFileName.toStdString() << endl << e);
        throw AbortException("Cannot create MMLComponent from " + myFileName.toStdString() + ": xml_schema exception.\nReason: " + e.what());
    }
}

// ---------------------- destructor ----------------------------
MMLComponent::~MMLComponent() {
    if (pmlComponent) {
        delete pmlComponent;
        pmlComponent = NULL;
    }

    if (display) {
        delete display;
        display = NULL;
    }

    if (mmlGUI) {
        delete mmlGUI;
        mmlGUI = NULL;
    }

}

//-------------------- setSelected -------------------
void MMLComponent::setSelected(const bool selected, const bool recursive) {
    camitk::Component::setSelected(selected, false);
    // set the default action to be the PML explorer
    if (neverSelected) {
        Action *defaultAction = Application::getAction("Simulation Dialog");
        if (defaultAction)
            defaultAction->getQAction()->trigger();
        neverSelected = false;
    }
}

// -------------------- getMonitoringGuiManager --------------------
MonitoringGuiManager* MMLComponent::getMonitoringGuiManager() {
    return mmlGUI;
}

// -------------------- getPMLComponent --------------------
PMLComponent* MMLComponent::getPMLComponent() {
    return pmlComponent;
}

// -------------------- getDisplay --------------------
MMLDisplay* MMLComponent::getDisplay() {
    return display;
}


// -------------------- connectPml --------------------
void MMLComponent::connectPml() {
    removeChild(pmlComponent);
    delete pmlComponent;
    pmlComponent = NULL;
    deleteChildren();
    refreshInterfaceNode();
    // this gives the ownership of pml pointer to the PML Component, use takePml() not getPml()
    pmlComponent = new PMLComponent( mmlGUI->getMonitoringManager()->takePml(),  mmlGUI->getMonitoringManager()->getPmlFileName().c_str());
    addChild(pmlComponent);
    refreshInterfaceNode();
    pmlComponent->refresh();
    display->updateDisplay();
}

// -------------------- getModified --------------------
bool MMLComponent::getModified() const {
    return (camitk::Component::getModified()
            || pmlComponent->getModified());
}

// -------------------- saveMML --------------------
void MMLComponent::saveMML() {
    // save mml
    getMonitoringGuiManager()->saveMmlInFile(getFileName());
    // TODO
    // The next blocked is commented because it is too dangerous!
    // use case: the user changed a property in the phyisical model
    // and ran a simulation to check it was correct
    // => the physical model is modified (property AND position !)
    // => it is therefore saved... property AND position (deformed!)
    // The physical model properties should be changed directly in the PMManagerDC property tab
    /*
    // save pml if needed
    if (getMonitoringGuiManager()->getMonitoringManager()->getPml()->isModified()) {
        std::ofstream outputFile(getMonitoringGuiManager()->getMonitoringManager()->getPmlFileName().c_str());
        getMonitoringGuiManager()->getMonitoringManager()->getPml()->xmlPrint(outputFile);
        outputFile.close();
    }
    */
}