Adding Custom Attributes for Custom View in uidesc

Hi all,

I may be being a doughnut, but I can’t find any references for how to add custom view attributes for a custom view into the uidesc file. The examples I can find online are XML, and I’m having trouble trying to mimic that with the new json ish format.

I would like the attributes there as I am mainly using the wysiwyg editor. Many thanks in advance

See here for how to do this.

1 Like

Thanks Arne,

I will share my naive attempt in case there are others like me not so sharp with fleshing it out.

In your view factory, override ViewCreatorAdapter getAttributeNames, add your attributes to the string list, and call the base class to sort out the default ones

bool getAttributeNames(StringList& attributeNames) const override {
	attributeNames.emplace_back("trunk-shape");
	attributeNames.emplace_back("slider-bar-style");
	attributeNames.emplace_back("slider-handle-style");

	return ViewCreatorAdapter::getAttributeNames(attributeNames);
}

Then override setAttributeType, again call the base class method after to sort out the others:

AttrType getAttributeType(const string& attributeName) const override {
	if (strcmp(attributeName.data(), "trunk-shape") == 0) {
		return AttrType::kListType;
	}

	return ViewCreatorAdapter::getAttributeType(attributeName);
}

Then override the getPossibleListValues so the editor knows what to show, again call base class method:

bool getPossibleListValues(const string& attributeName,
	ConstStringPtrList& values) const override {

	if (strcmp(attributeName.data(), "trunk-shape") == 0) {
		static const string op1 = "trunk-fat";
		static const string op2 = "trunk-thin";
		static const string op3 = "trunk-none";
		values.emplace_back(&op1);
		values.emplace_back(&op2);
		values.emplace_back(&op3);
		return true;
	}
return ViewCreatorAdapter::getPossibleListValues(attributeName,values); 
}

Finally override getAttributeValue then call the base method:

bool getAttributeValue(CView* view, const string& attributeName, string& stringValue,
	const IUIDescription* desc) const override
{
	if (attributeName == "trunk-shape")
	{
		// Cast the generic view to slider type
		ModernSlider* mySlider = dynamic_cast<ModernSlider*>(view);
		if (mySlider)
		{
			// Get the current shape from the control
			ModernSlider::SliderTrunkType shape = mySlider->getShape();

			switch (shape)
			{
			case ModernSlider::SliderTrunkType::kFat:
				stringValue = "trunk-fat";
				break;
			case ModernSlider::SliderTrunkType::kThin:
				stringValue = "shape-thin";
				break;
			case ModernSlider::SliderTrunkType::kNone:
				stringValue = "trunk-none";
				break;
			}
			return true;
		}
	}
return ViewCreatorAdapter::getAttributeValue(view, attributeName,stringValue, desc);
}

Now get the custom attribute value in your apply() method and set it in your view class

			const std::string* sliderShapeName = attributes.getAttributeValue("trunk-shape");
			if (sliderShapeName) {
				if (*sliderShapeName == "trunk-fat") {
					mySlider->setShape(ModernSlider::SliderTrunkType::kFat);
				}
				if (*sliderShapeName == "trunk-thin") {
					mySlider->setShape(ModernSlider::SliderTrunkType::kThin);
				}
				if (*sliderShapeName == "trunk-none") {
					mySlider->setShape(ModernSlider::SliderTrunkType::kNone);
				}
			}