28 #include <QXmlStreamWriter>
35 throw Error(QString(
"Warning: ") + message);
43 if (
const auto* groupItem =
dynamic_cast<const GroupItem*
>(parent))
44 return groupItem->getItemOfType(modelType);
51 }
catch (
const std::exception&) {
62 ASSERT(modelRootItem);
67 writer->writeEndElement();
74 writer->writeStartElement(
ItemTag);
81 if (role == Qt::DisplayRole || role == Qt::EditRole)
94 writer->writeCharacters(a.toBase64());
95 writer->writeEndElement();
98 QString nonSessionItemContent;
99 QXmlStreamWriter nonSessionItemStream(&nonSessionItemContent);
101 const auto sizeBefore = nonSessionItemContent.size();
103 if (sizeBefore != nonSessionItemContent.size()) {
106 writer->writeEndElement();
110 writer->writeEndElement();
116 bool legacyDistributionFound =
false;
120 while (!reader->atEnd()) {
122 if (reader->isStartElement()) {
124 const QString model_type =
127 if (model_type ==
"ParticleDistribution") {
128 if (!legacyDistributionFound) {
130 "This file contains particle distributions in the sample. Elements of "
131 "this type are not supported any more. Reading the file will be "
132 "continued as much as possible, but you should review the sample.");
134 report_error(messageService, parent->
model(), message);
136 legacyDistributionFound =
true;
137 reader->skipCurrentElement();
142 QString displayName =
145 if (!topTag.isEmpty()) {
150 auto* newItem = createItem(parent, model_type, tag);
152 QString message = QString(
"Error while parsing XML. Can't create item of "
153 "modelType '%1' for tag '%2', parent '%3'")
154 .arg(model_type, tag, parent->
itemName());
155 report_error(messageService, parent->
model(), message);
157 reader->skipCurrentElement();
162 ASSERT(reader->name() ==
ItemTag);
167 if (reader->attributes().value(
XML::Version).toInt() == 1) {
168 QString valueAsBase64 =
169 reader->readElementText(QXmlStreamReader::SkipChildElements);
170 const auto data = QByteArray::fromBase64(
171 valueAsBase64.toLatin1());
180 }
else if (reader->isEndElement()) {
183 if (reader->name() == start_type)
192 const QString parameter_name =
194 const QString parameter_type =
200 QString(
"Attempt to set property '%1' for non existing item").arg(parameter_name));
203 if (parameter_type ==
"double") {
204 double parameter_value =
206 variant = parameter_value;
207 }
else if (parameter_type ==
"int") {
209 variant = parameter_value;
210 }
else if (parameter_type ==
"uint") {
211 unsigned parameter_value =
213 variant = parameter_value;
214 }
else if (parameter_type ==
"bool") {
216 variant = parameter_value;
217 }
else if (parameter_type ==
"QString") {
218 QString parameter_value =
220 variant = parameter_value;
221 }
else if (parameter_type ==
"QColor") {
222 QString parameter_value =
224 variant = QColor(parameter_value);
225 }
else if (parameter_type ==
"ExternalProperty") {
226 const bool isMaterialItemColor =
229 if (isMaterialItemColor) {
234 const QString colorName =
236 variant = QColor(colorName);
244 const QString identifier =
246 variant = identifier;
248 }
else if (parameter_type ==
"ComboProperty") {
256 variant = combo_property.
variant();
258 QString message = QString(
"SessionModel::readProperty: parameter type not supported '"
259 + parameter_type +
"'");
260 throw Error(message);
263 if (variant.isValid())
266 return parameter_name;
Defines class ComboProperty.
Defines class DeserializationException.
Defines class MaterialItem.
Defines MessageService class.
Defines class SessionFlags.
Defines class SessionModel.
Defines reader and writer classes for SessionModel.
Custom property to define list of string values with multiple selections. Intended for QVariant.
void setStringOfValues(const QString &values)
Sets values from the string containing delimeter ';'.
void setStringOfSelections(const QString &values)
Sets selected indices from string.
QVariant variant() const
Constructs variant enclosing given ComboProperty.
static DeserializationException tooNew()
The service to collect messages from different senders.
void addWarning(QObject *sender, const QString &description)
Base class for a GUI data item.
QString itemName() const
Get item name, return display name if no name is set.
QVariant value() const
Get value.
QVector< int > getRoles() const
Returns vector of all present roles.
bool hasModelType() const
const SessionItemTags * sessionItemTags() const
void setDisplayName(const QString &display_name)
Set display name.
virtual void deserializeBinaryData(const QByteArray &data)
SessionModel * model() const
Returns model of this item.
virtual QByteArray serializeBinaryData() const
SessionItem * parentItem() const
Returns parent of this item.
virtual bool allowWritingChildToXml(SessionItem *child) const
QVariant roleProperty(int role) const
Returns corresponding variant under given role, invalid variant when role is not present.
virtual void writeNonSessionItems(QXmlStreamWriter *writer) const
QVector< SessionItem * > children() const
Returns vector of all children.
QString modelType() const
Get model type.
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.
bool setRoleProperty(int role, const QVariant &value)
Set the contained role property to the given value. See also setTranslatorForRolePropertySetter.
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
virtual void readNonSessionItems(QXmlStreamReader *reader)
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
QString getModelTag() const
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
void writeItemAndChildItems(QXmlStreamWriter *writer, const SessionItem *item)
void writeVariant(QXmlStreamWriter *writer, QVariant variant, int role)
Write the variant as a complete tag, including the given role.
void readItems(QXmlStreamReader *reader, SessionItem *parent, QString topTag="", MessageService *messageService=nullptr)
constexpr auto ParameterTag("Parameter")
void writeModel(QXmlStreamWriter *writer, SessionItem *modelRootItem)
constexpr auto ModelTypeAttribute("ModelType")
QString readProperty(QXmlStreamReader *reader, SessionItem *item)
constexpr auto ItemTag("Item")
void gotoEndElementOfTag(QXmlStreamReader *reader, const QString &tag)