File: CppHotPlugAction.cpp

package info (click to toggle)
camitk 6.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,508 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (207 lines) | stat: -rw-r--r-- 7,419 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
207
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
 *
 * 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$
 ****************************************************************************/

#include "CppHotPlugAction.h"
#include "CppHotPlugActionExtension.h"

#include "Core.h"
#include "ActionWidget.h"
#include "Application.h"
#include "Log.h"

#include <TransformEngine.h>
#include <QFileInfo>
#include <QDir>

namespace camitk {

// ------------------- Constructor -------------------
CppHotPlugAction::CppHotPlugAction(HotPlugActionExtension* extension, const VariantDataModel& data) : HotPlugAction(extension, data) {
    TransformEngine transformEngine;
    QJsonObject dataObject = data.getValue().toJsonObject();
    actionLibName = transformEngine.transformToString("$joinKebabCase(name)$", dataObject);

    // setup UserActionLib functors
    updateSucceeded = false;
    CppHotPlugAction::update();
}

// ------------------- init -------------------
bool CppHotPlugAction::init() {
    // call user-defined init()
    if (initFunctor != nullptr) {
        initFunctor(this);
    }
    return true;
}

// ------------------- needsUpdate -------------------
bool CppHotPlugAction::needsUpdate() {
    if (!updateSucceeded) {
        return true;
    }

    QDateTime lastModified = QFileInfo(actionLib.fileName()).lastModified();
    return (actionLib.fileName().isEmpty() || (lastModified > lastLoaded));
}

// ------------------- update -------------------
bool CppHotPlugAction::update() {
    // Do nothing if the library was not changed since the last update or
    // if it being rebuilt
    if (!CppHotPlugAction::needsUpdate()) {
        updateSucceeded = true;
        return updateSucceeded;
    }

    // force reload
    if (actionLib.isLoaded()) {
        actionLib.unload();
    }

    // reset functors
    processFunctor = nullptr;
    initFunctor = nullptr;
    getUIFunctor = nullptr;
    targetDefinedFunctor = nullptr;
    parameterChangedFunctor = nullptr;

    // location of the UserActionLib is relative to the CamiTK extension file
    QString sourcePath = QFileInfo(getExtension()->getLocation()).absolutePath();
    QDir buildExtensionDir;
    buildExtensionDir.setPath(sourcePath + "/build/lib/" + QString(Core::shortVersion()) + "/actions");

    // There are no prefix for libraries on Windows
    QString libPrefix = "";
    // There might be a debug postfix on Windows
    QString debugPostfix = "";
#if defined(_WIN32) || defined(_WIN64)
    if (Core::isDebugBuild()) {
        debugPostfix = QString(Core::debugPostfix());
    }
#else
    // but on Unix and Apple, all libraries are prefixed with "lib"
    libPrefix = "lib";
#endif
    QString libFilePath = buildExtensionDir.filePath(libPrefix + actionLibName + debugPostfix);

    // QLibrary will look for { "*.so", "*.dll", "*.dylib"} depending on the OS
    actionLib.setFileName(libFilePath);

    // load and update functors
    if (actionLib.load()) {
        // resolve symbols
        processFunctor = (ProcessFunction) actionLib.resolve("process");

        // check
        if (processFunctor == nullptr) {
            CAMITK_WARNING(QString(R"(Error loading %1: %2\nCannot find mandatory \"process(..)\" method for action \"%3\".
            This action will therefore not be able to do anything. Please verify the CamiTKActionImplementation section and check that the method process(..) is defined with the proper signature: \"Action::ApplyStatus process(Action* self)\")").arg(actionLib.fileName()).arg(actionLib.errorString()).arg(getName()));
            updateSucceeded = false;
        }
        else {
            initFunctor = (VoidFunction) actionLib.resolve("init");
            getUIFunctor = (GetUIFunction) actionLib.resolve("getUI");
            targetDefinedFunctor = (VoidFunction) actionLib.resolve("targetDefined");
            parameterChangedFunctor = (ParameterChangedFunction) actionLib.resolve("parameterChanged");
            dynamic_cast<CppHotPlugActionExtension*>(hotPlugExtension)->watchActionLibrary(actionLib.fileName(), this);
            updateSucceeded = true;
            lastLoaded = QDateTime::currentDateTime();
        }
    }
    else {
        CAMITK_INFO(QString("%1 (%2) cannot be loaded:\n%3\nTry to rebuild the extension.").arg(libFilePath).arg(actionLib.fileName()).arg(actionLib.errorString()));
        updateSucceeded = false;
    }

    return updateSucceeded;
}

// ------------------- getWidget -------------------
QWidget* CppHotPlugAction::getWidget() {
    // Get UI, then call targetDefined
    if (actionWidget == nullptr) {
        // first call: check the user defined widget
        if (getUIFunctor != nullptr) {
            actionWidget = getUIFunctor(this);
        }
        if (actionWidget != nullptr) {
            // call UserAction method
            if (targetDefinedFunctor  != nullptr) {
                targetDefinedFunctor(this);
            }
            return actionWidget;
        }
        else {
            // no user defined UI, use default
            QWidget* defaultWidget = Action::getWidget();
            // call UserAction method
            if (targetDefinedFunctor  != nullptr) {
                targetDefinedFunctor(this);
            }
            return defaultWidget;
        }
    }
    else {
        // call UserAction method
        if (targetDefinedFunctor  != nullptr) {
            targetDefinedFunctor(this);
        }
        ActionWidget* defaultActionWidget = dynamic_cast<ActionWidget*>(actionWidget);
        if (defaultActionWidget != nullptr) {
            // this is a default action widget, make sure the widget has updated targets
            defaultActionWidget->update();
        }
        return actionWidget;
    }
}

// ------------------- apply -------------------
Action::ApplyStatus CppHotPlugAction::apply() {
    // set waiting cursor
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

    // call user-defined process() if defined
    Action::ApplyStatus applyStatus = Action::ERROR;
    if (processFunctor != nullptr) {
        applyStatus = processFunctor(this);
    }

    // restore normal cursor
    QApplication::restoreOverrideCursor();

    return applyStatus;
}


// ---------------------- parameterChangedEvent ----------------------------
void CppHotPlugAction::parameterChangedEvent(QString parameterName) {
    // call user-defined parameterChanged()
    if (parameterChangedFunctor != nullptr) {
        parameterChangedFunctor(this, parameterName);
    }
}

} // namespace camitk