File size: 5,931 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2025-10-28

  (C) Copyright 2025-2026 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include <QLabel>
#include <QVBoxLayout>
#include <QJsonObject>
#include <cisstMultiTask/mtsComponentModelQtNodes.h>
#include <iostream>

mtsComponentModelQtNodes::mtsComponentModelQtNodes(const std::string &processName,
                                                   const std::string &componentName,
                                                   const std::string &className,
                                                   const QColor &color)
    : m_processName(processName)
    , m_componentName(componentName)
    , m_className(className)
    , m_color(color) {}

QString mtsComponentModelQtNodes::caption(void) const {
    return QString("%1 : %2")
        .arg(QString::fromStdString(m_className),
             QString::fromStdString(m_componentName));
}

bool mtsComponentModelQtNodes::captionVisible(void) const {
    return false;
}

QString mtsComponentModelQtNodes::name(void) const {
    return QString("%1:%2")
        .arg(QString::fromStdString(m_processName),
             QString::fromStdString(m_componentName));
}

QJsonObject mtsComponentModelQtNodes::save() const {
    return NodeDelegateModel::save();
}

unsigned int
mtsComponentModelQtNodes::nPorts(QtNodes::PortType portType) const {
    switch (portType) {
    case QtNodes::PortType::In:
        return m_interfaces_required.size();
    case QtNodes::PortType::Out:
        return m_interfaces_provided.size();
    default:
        return 0;
    }
}

QtNodes::NodeDataType
mtsComponentModelQtNodes::dataType(QtNodes::PortType portType,
                                   QtNodes::PortIndex portIndex) const {
    QtNodes::NodeDataType type;
    type.id = "cisst";
    switch (portType) {
    case QtNodes::PortType::In: {
        if (portIndex < m_interfaces_required.size()) {
            type.name = QString("[R] %1").arg(
                                              QString::fromStdString(m_interfaces_required[portIndex]));
        }
        break;
    }
    case QtNodes::PortType::Out: {
        if (portIndex < m_interfaces_provided.size()) {
            type.name = QString("[P] %1").arg(
                                              QString::fromStdString(m_interfaces_provided[portIndex]));
        }
        break;
    }
    default:
        break;
    }
    return type;
}

QString
mtsComponentModelQtNodes::portCaption(QtNodes::PortType portType,
                                      QtNodes::PortIndex portIndex) const {
    switch (portType) {
    case QtNodes::PortType::In:
        if (portIndex < m_interfaces_required.size()) {
            return QString::fromStdString(m_interfaces_required[portIndex]);
        }
        break;
    case QtNodes::PortType::Out:
        if (portIndex < m_interfaces_provided.size()) {
            return QString::fromStdString(m_interfaces_provided[portIndex]);
        }
        break;
    default:
        break;
    }
    return QString();
}

bool mtsComponentModelQtNodes::portCaptionVisible(QtNodes::PortType,
                                                  QtNodes::PortIndex) const {
    return true;
}

void mtsComponentModelQtNodes::setInData(std::shared_ptr<QtNodes::NodeData>,
                                         QtNodes::PortIndex) {
    // Connections are managed by cisst, we don't need to do anything here
}

std::shared_ptr<QtNodes::NodeData>
mtsComponentModelQtNodes::outData(QtNodes::PortIndex) {
    // cisst manages the data flow, we don't need to do anything here
    return nullptr;
}

QWidget *mtsComponentModelQtNodes::embeddedWidget(void) {
    if (!m_widget) {
        m_widget = new QWidget();
        auto *layout = new QVBoxLayout(m_widget);
        layout->setContentsMargins(0, 0, 0, 0);
        layout->setSpacing(2);
        
        auto *titleLabel = new QLabel();
        titleLabel->setObjectName("titleLabel");
        titleLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        layout->addWidget(titleLabel);
        
        auto *stateLabel = new QLabel();
        stateLabel->setObjectName("stateLabel");
        stateLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
        layout->addWidget(stateLabel);

        m_widget->setAttribute(Qt::WA_StyleSheet);
        UpdateWidget();
    }
    return m_widget;
}

bool mtsComponentModelQtNodes::AddInterfaceProvided(const std::string &name) {
    m_interfaces_provided.push_back(name);
    return true;
}

bool mtsComponentModelQtNodes::AddInterfaceRequired(const std::string &name) {
    m_interfaces_required.push_back(name);
    return true;
}

void mtsComponentModelQtNodes::SetState(const std::string &state) {
    m_state = state;
    UpdateWidget();
}

void mtsComponentModelQtNodes::UpdateWidget(void) {
    if (m_widget) {
        auto *titleLabel = m_widget->findChild<QLabel *>("titleLabel");
        auto *stateLabel = m_widget->findChild<QLabel *>("stateLabel");
        
        if (titleLabel) {
            titleLabel->setText(caption());
            if (m_color.isValid()) {
                titleLabel->setStyleSheet(QString("background-color: %1; color: black; padding: 2px;").arg(m_color.name()));
            } else {
                titleLabel->setStyleSheet("padding: 2px;");
            }
        }
        if (stateLabel) {
            stateLabel->setText(QString::fromStdString(m_state));
            stateLabel->setStyleSheet(""); // Clear any previous background color
        }
    }
}