Skip to content

Commit

Permalink
keep track of proto parents
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolSpy3 committed Jul 6, 2024
1 parent 24bcb24 commit c455435
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions scripts/packaging/generate_proto_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, path, name):
self.path = path.replace('\\', '/') # use cross-platform forward slashes
self.proto_type = None # direct node type, ex: for RoadSegment is Road
self.base_type = None # lowest node type, ex: for RoadSegment is Solid
self.parents = [] # all proto types this type extends, ex: for Road Segment is [Road]
self.license = None
self.license_url = None
self.description = ''
Expand Down Expand Up @@ -154,6 +155,7 @@ def generate_proto_list(current_tag=None, silent=False):
if info.base_type not in protos: # the current proto depends on a sub-proto: iterate until a base node is reached
raise RuntimeError(f'Error: "{info.base_type}" proto node does not exist. Either it was skipped or the regex '
'that retrieves the proto_type is incorrect.')
info.parents.append(info.base_type)
sub_proto = protos[info.base_type]
info.base_type = sub_proto.proto_type

Expand Down Expand Up @@ -201,6 +203,7 @@ def generate_proto_list(current_tag=None, silent=False):
proto_element = ET.SubElement(root, 'proto')
ET.SubElement(proto_element, 'name').text = info.name
ET.SubElement(proto_element, 'base-type').text = info.base_type
ET.SubElement(proto_element, 'parents').text = ','.join(info.parents)
ET.SubElement(proto_element, 'url').text = info.path.replace(WEBOTS_HOME + '/', prefix)

if info.license is not None:
Expand Down
13 changes: 10 additions & 3 deletions src/webots/vrml/WbProtoManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ void WbProtoManager::loadWebotsProtoMap() {
if (reader.name().toString() == "proto") {
bool needsRobotAncestor = false;
QString name, url, baseType, license, licenseUrl, documentationUrl, description, slotType;
QStringList tags, parameters;
QStringList tags, parameters, parents;
while (reader.readNextStartElement()) {
if (reader.name().toString() == "name") {
name = reader.readElementText();
Expand Down Expand Up @@ -478,14 +478,18 @@ void WbProtoManager::loadWebotsProtoMap() {
parameters = reader.readElementText().split("\\n", Qt::SkipEmptyParts);
reader.readNext();
}
if (reader.name().toString() == "parents") {
parents = reader.readElementText().split(",", Qt::SkipEmptyParts);
reader.readNext();
}
if (reader.name().toString() == "needs-robot-ancestor") {
needsRobotAncestor = reader.readElementText() == "true";
reader.readNext();
}
}
description = description.replace("\\n", "\n");
WbProtoInfo *const info = new WbProtoInfo(url, baseType, license, licenseUrl, documentationUrl, description, slotType,
tags, parameters, needsRobotAncestor);
tags, parameters, parents, needsRobotAncestor);
mWebotsProtoList.insert(name, info);
} else
reader.raiseError(tr("Expected 'proto' element."));
Expand Down Expand Up @@ -756,9 +760,12 @@ WbProtoInfo *WbProtoManager::generateInfoFromProtoFile(const QString &protoFileN
parameters << field;
}

// generate parents string (needed by PROTO wizard)
QStringList parents = protoModel->parentList();

WbProtoInfo *info = new WbProtoInfo(url, protoModel->baseType(), protoModel->license(), protoModel->licenseUrl(),
protoModel->documentationUrl(), protoModel->info(), protoModel->slotType(),
protoModel->tags(), parameters, needsRobotAncestor);
protoModel->tags(), parameters, parents, needsRobotAncestor);

protoModel->destroy();
return info;
Expand Down
7 changes: 5 additions & 2 deletions src/webots/vrml/WbProtoManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WbProtoInfo {
public:
WbProtoInfo(const QString &url, const QString &baseType, const QString &license, const QString &licenseUrl,
const QString &documentationUrl, const QString &description, const QString &slotType, const QStringList &tags,
const QStringList &parameters, bool needsRobotAncestor) :
const QStringList &parameters, const QStringList &parents, bool needsRobotAncestor) :
mUrl(url),
mBaseType(baseType),
mLicense(license),
Expand All @@ -66,6 +66,7 @@ class WbProtoInfo {
mSlotType(slotType),
mTags(tags),
mParameters(parameters),
mParents(parents),
mNeedsRobotAncestor(needsRobotAncestor),
mIsDirty(false) {
// extract parameter names
Expand All @@ -81,7 +82,7 @@ class WbProtoInfo {
// copy constructor
WbProtoInfo(const WbProtoInfo &other) :
WbProtoInfo(other.mUrl, other.mBaseType, other.mLicense, other.mLicenseUrl, other.mDocumentationUrl, other.mDescription,
other.mSlotType, other.mTags, other.mParameters, other.mNeedsRobotAncestor) {}
other.mSlotType, other.mTags, other.mParameters, other.mParents, other.mNeedsRobotAncestor) {}

const QString &url() const { return mUrl; }
const QString &baseType() const { return mBaseType; }
Expand All @@ -93,6 +94,7 @@ class WbProtoInfo {
const QStringList &tags() const { return mTags; }
const QStringList &parameters() const { return mParameters; }
const QStringList &parameterNames() const { return mParameterNames; }
const QStringList &parents() const { return mParents; }
const bool needsRobotAncestor() const { return mNeedsRobotAncestor; }
void setDirty(bool value) { mIsDirty = value; }
bool isDirty() const { return mIsDirty; }
Expand All @@ -107,6 +109,7 @@ class WbProtoInfo {
QString mSlotType;
QStringList mTags;
QStringList mParameters;
QStringList mParents;
bool mNeedsRobotAncestor;
bool mIsDirty;

Expand Down
9 changes: 9 additions & 0 deletions src/webots/vrml/WbProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,15 @@ WbNode *WbProtoModel::generateRoot(const QVector<WbField *> &parameters, const Q
return root;
}

QStringList WbProtoModel::parentList() const {
QStringList parents;
const WbProtoModel *parentProtoModel = this;
while ((parentProtoModel = parentProtoModel->ancestorProtoModel())) {
parents << parentProtoModel->name();
}
return parents;
}

void WbProtoModel::ref(bool isFromProtoInstanceCreation) {
mRefCount++;
if (isFromProtoInstanceCreation)
Expand Down
2 changes: 2 additions & 0 deletions src/webots/vrml/WbProtoModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class WbProtoModel : public QObject {

const QString &slotType() const { return mSlotType; }

QStringList parentList() const;

QStringList parameterNames() const;

void setIsTemplate(bool value);
Expand Down

0 comments on commit c455435

Please sign in to comment.