| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* BEGIN software license | ||
| 2 | * | ||
| 3 | * MsXpertSuite - mass spectrometry software suite | ||
| 4 | * ----------------------------------------------- | ||
| 5 | * Copyright(C) 2009,...,2018 Filippo Rusconi | ||
| 6 | * | ||
| 7 | * http://www.msxpertsuite.org | ||
| 8 | * | ||
| 9 | * This file is part of the MsXpertSuite project. | ||
| 10 | * | ||
| 11 | * The MsXpertSuite project is the successor of the massXpert project. This | ||
| 12 | * project now includes various independent modules: | ||
| 13 | * | ||
| 14 | * - massXpert, model polymer chemistries and simulate mass spectrometric data; | ||
| 15 | * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner; | ||
| 16 | * | ||
| 17 | * This program is free software: you can redistribute it and/or modify | ||
| 18 | * it under the terms of the GNU General Public License as published by | ||
| 19 | * the Free Software Foundation, either version 3 of the License, or | ||
| 20 | * (at your option) any later version. | ||
| 21 | * | ||
| 22 | * This program is distributed in the hope that it will be useful, | ||
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | * GNU General Public License for more details. | ||
| 26 | * | ||
| 27 | * You should have received a copy of the GNU General Public License | ||
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 29 | * | ||
| 30 | * END software license | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | /////////////////////// Local includes | ||
| 35 | #include "Prop.hpp" | ||
| 36 | #include "ChemicalGroup.hpp" | ||
| 37 | #include "CrossLink.hpp" | ||
| 38 | |||
| 39 | |||
| 40 | namespace MsXpS | ||
| 41 | { | ||
| 42 | |||
| 43 | namespace libXpertMass | ||
| 44 | { | ||
| 45 | |||
| 46 | /*! | ||
| 47 | \class MsXpS::libXpertMass::Prop | ||
| 48 | \inmodule libXpertMass | ||
| 49 | \ingroup ThePropSystem | ||
| 50 | \inheaderfile Prop.hpp | ||
| 51 | |||
| 52 | \brief The Prop class is the abstract base class for a number of specialized | ||
| 53 | properties. | ||
| 54 | |||
| 55 | Properties are libmass' way of extending the capabilities of | ||
| 56 | objects. A property is merely an encapsulation of: | ||
| 57 | |||
| 58 | \list | ||
| 59 | \li a name(a QString); | ||
| 60 | |||
| 61 | \li a pointer to data specifically allocated for this property to | ||
| 62 | become the owner of these data (the member data pointer is a void *); | ||
| 63 | \endlist | ||
| 64 | |||
| 65 | In order to perform tasks in the derived classes using dynamic | ||
| 66 | binding, virtual functions are available to derived classes to | ||
| 67 | perform: | ||
| 68 | |||
| 69 | \list | ||
| 70 | \li XML string formatting (formatXmlElement()); | ||
| 71 | |||
| 72 | \li XML element rendering (renderXmlElement()); | ||
| 73 | |||
| 74 | \li data destruction (deleteData()). | ||
| 75 | \endlist | ||
| 76 | |||
| 77 | Derived classes should be named according the following scheme: | ||
| 78 | XxxYyyZzzzProp, like StringProp or MonomerProp or ModifProp. | ||
| 79 | |||
| 80 | \note When a derived class is created, it should register | ||
| 81 | itself to the system by registering its name in the | ||
| 82 | propAllocator(const QString &) function. This function will be able | ||
| 83 | to allocate a property using the proper constructor based on the | ||
| 84 | property name that is passed as argument. It returns a pointer to | ||
| 85 | the newly allocated instance. | ||
| 86 | |||
| 87 | The classes that benefit from this Property-based extension mechanism all | ||
| 88 | derive from \l PropListHolder. | ||
| 89 | |||
| 90 | \sa StringProp, PropListHolder | ||
| 91 | */ | ||
| 92 | |||
| 93 | /*! | ||
| 94 | \variable int MsXpS::libXpertMass::Prop::m_name | ||
| 95 | |||
| 96 | \brief The name of the property. Initialized to "NOT_SET". | ||
| 97 | */ | ||
| 98 | |||
| 99 | /*! | ||
| 100 | \variable int MsXpS::libXpertMass::Prop::mpa_data | ||
| 101 | |||
| 102 | \brief The allocated data belonging to this Prop instance. Initialized to | ||
| 103 | nullptr. | ||
| 104 | */ | ||
| 105 | |||
| 106 | |||
| 107 | /*! | ||
| 108 | \brief Constructs a Prop instance. | ||
| 109 | */ | ||
| 110 | ✗ | Prop::Prop() | |
| 111 | { | ||
| 112 | ✗ | } | |
| 113 | |||
| 114 | |||
| 115 | /*! | ||
| 116 | \brief Constructs a Prop instance with \a name. | ||
| 117 | */ | ||
| 118 | ✗ | Prop::Prop(const QString &name) : m_name(name) | |
| 119 | { | ||
| 120 | ✗ | } | |
| 121 | |||
| 122 | |||
| 123 | /*! | ||
| 124 | \brief Constructs a Prop instance as a copy of \a other. | ||
| 125 | |||
| 126 | \note The data are not duplicated. | ||
| 127 | */ | ||
| 128 | ✗ | Prop::Prop(const Prop &other) : m_name(other.m_name) | |
| 129 | { | ||
| 130 | // Now check how to duplicate the data. | ||
| 131 | |||
| 132 | ✗ | if(other.mpa_data == nullptr) | |
| 133 | ✗ | return; | |
| 134 | |||
| 135 | // We cannot duplicate the data because we do not know their type. | ||
| 136 | } | ||
| 137 | |||
| 138 | /*! | ||
| 139 | \brief Destructs this Prop instance. | ||
| 140 | */ | ||
| 141 | ✗ | Prop::~Prop() | |
| 142 | { | ||
| 143 | // Do nothing here. | ||
| 144 | ✗ | } | |
| 145 | |||
| 146 | /*! | ||
| 147 | \brief Sets the \a name. | ||
| 148 | */ | ||
| 149 | void | ||
| 150 | ✗ | Prop::setName(QString &name) | |
| 151 | { | ||
| 152 | ✗ | m_name = name; | |
| 153 | ✗ | } | |
| 154 | |||
| 155 | |||
| 156 | /*! | ||
| 157 | \brief Returns the name. | ||
| 158 | */ | ||
| 159 | const QString & | ||
| 160 | ✗ | Prop::name() | |
| 161 | { | ||
| 162 | ✗ | return m_name; | |
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | /*! | ||
| 167 | \brief Sets the \a data | ||
| 168 | */ | ||
| 169 | void | ||
| 170 | ✗ | Prop::setData(void *data) | |
| 171 | { | ||
| 172 | ✗ | if(mpa_data != 0) | |
| 173 | ✗ | deleteData(); | |
| 174 | |||
| 175 | ✗ | mpa_data = data; | |
| 176 | ✗ | } | |
| 177 | |||
| 178 | /*! | ||
| 179 | \brief Returns the data. | ||
| 180 | */ | ||
| 181 | void * | ||
| 182 | ✗ | Prop::data() const | |
| 183 | { | ||
| 184 | ✗ | return mpa_data; | |
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | void | ||
| 189 | ✗ | Prop::deleteData() | |
| 190 | { | ||
| 191 | // Do nothing here. | ||
| 192 | ✗ | } | |
| 193 | |||
| 194 | /*! | ||
| 195 | \brief Assigns \a other to this Prop instance. | ||
| 196 | */ | ||
| 197 | Prop & | ||
| 198 | ✗ | Prop::operator=(const Prop &other) | |
| 199 | { | ||
| 200 | ✗ | if(&other == this) | |
| 201 | ✗ | return *this; | |
| 202 | |||
| 203 | ✗ | m_name = other.m_name; | |
| 204 | |||
| 205 | // We cannot duplicate the data because we do not know their type. | ||
| 206 | |||
| 207 | ✗ | return *this; | |
| 208 | } | ||
| 209 | |||
| 210 | bool | ||
| 211 | ✗ | Prop::renderXmlElement([[maybe_unused]] const QDomElement &element, | |
| 212 | [[maybe_unused]] int version) | ||
| 213 | { | ||
| 214 | // Do nothing here. | ||
| 215 | ✗ | return false; | |
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | QString * | ||
| 220 | ✗ | Prop::formatXmlElement([[maybe_unused]] int offset, | |
| 221 | [[maybe_unused]] const QString &indent) | ||
| 222 | |||
| 223 | { | ||
| 224 | // Do nothing here. | ||
| 225 | ✗ | return nullptr; | |
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | //////////////////////// StringProp //////////////////////// | ||
| 230 | //////////////////////// StringProp //////////////////////// | ||
| 231 | |||
| 232 | /*! | ||
| 233 | \class MsXpS::libXpertMass::StringProp | ||
| 234 | \inmodule libXpertMass | ||
| 235 | \ingroup ThePropSystem | ||
| 236 | |||
| 237 | \brief The StringProp class is the specialized class for properties | ||
| 238 | that hold data in the form of string objects. | ||
| 239 | |||
| 240 | A StringProp property is a simple property in which the data is a pointer to an | ||
| 241 | allocated QString. | ||
| 242 | */ | ||
| 243 | |||
| 244 | /*! | ||
| 245 | \brief Constructs a StringProp instance. | ||
| 246 | |||
| 247 | \list | ||
| 248 | \li \a name: The name of the property. | ||
| 249 | \li \a data: The data of the property. | ||
| 250 | \endlist | ||
| 251 | |||
| 252 | The string passed as \a data is used to dynamically allocate a new string with | ||
| 253 | the same contents and then is set to the member mpa_data pointer. | ||
| 254 | */ | ||
| 255 | ✗ | StringProp::StringProp(const QString &name, const QString &data) | |
| 256 | { | ||
| 257 | ✗ | if(!name.isEmpty()) | |
| 258 | ✗ | m_name = name; | |
| 259 | else | ||
| 260 | ✗ | m_name = QString(); | |
| 261 | |||
| 262 | ✗ | if(!data.isEmpty()) | |
| 263 | ✗ | mpa_data = static_cast<void *>(new QString(data)); | |
| 264 | else | ||
| 265 | ✗ | mpa_data = nullptr; | |
| 266 | ✗ | } | |
| 267 | |||
| 268 | |||
| 269 | /*! | ||
| 270 | \brief Constructs a StringProp instance. | ||
| 271 | |||
| 272 | \list | ||
| 273 | \li \a name: The name of the property. | ||
| 274 | \li \a data: The data of the property. | ||
| 275 | \endlist | ||
| 276 | |||
| 277 | The string passed as \a data is used to dynamically allocate a new string with | ||
| 278 | the same contents and then is set to the member mpa_data pointer. | ||
| 279 | */ | ||
| 280 | ✗ | StringProp::StringProp(const QString &name, QString *data) | |
| 281 | { | ||
| 282 | ✗ | if(!name.isEmpty()) | |
| 283 | ✗ | m_name = name; | |
| 284 | else | ||
| 285 | ✗ | m_name = QString(); | |
| 286 | |||
| 287 | ✗ | if(data) | |
| 288 | ✗ | mpa_data = new QString(*data); | |
| 289 | else | ||
| 290 | ✗ | mpa_data = nullptr; | |
| 291 | ✗ | } | |
| 292 | |||
| 293 | /*! | ||
| 294 | \brief Constructs a StringProp instance as a copy of \a other. | ||
| 295 | */ | ||
| 296 | ✗ | StringProp::StringProp(const StringProp &other) : Prop(other) | |
| 297 | { | ||
| 298 | ✗ | if(mpa_data != nullptr) | |
| 299 | ✗ | deleteData(); | |
| 300 | |||
| 301 | ✗ | if(other.mpa_data != nullptr) | |
| 302 | { | ||
| 303 | ✗ | QString *text = static_cast<QString *>(other.mpa_data); | |
| 304 | ✗ | mpa_data = static_cast<void *>(new QString(*text)); | |
| 305 | } | ||
| 306 | else | ||
| 307 | ✗ | mpa_data = nullptr; | |
| 308 | ✗ | } | |
| 309 | |||
| 310 | /*! | ||
| 311 | \brief Destructs this StringProp instance. | ||
| 312 | |||
| 313 | Deletion of the data is delegated to \l deleteData(). | ||
| 314 | */ | ||
| 315 | ✗ | StringProp::~StringProp() | |
| 316 | { | ||
| 317 | ✗ | deleteData(); | |
| 318 | ✗ | } | |
| 319 | |||
| 320 | /*! | ||
| 321 | \brief Deletes the member data. | ||
| 322 | */ | ||
| 323 | void | ||
| 324 | ✗ | StringProp::deleteData() | |
| 325 | { | ||
| 326 | ✗ | if(mpa_data != nullptr && !static_cast<QString *>(mpa_data)->isNull()) | |
| 327 | { | ||
| 328 | ✗ | delete static_cast<QString *>(mpa_data); | |
| 329 | ✗ | mpa_data = nullptr; | |
| 330 | } | ||
| 331 | ✗ | } | |
| 332 | |||
| 333 | /*! | ||
| 334 | \brief Assigns \a other to this StringProp instance. | ||
| 335 | |||
| 336 | Returns a reference to this StringProp instance. | ||
| 337 | */ | ||
| 338 | StringProp & | ||
| 339 | ✗ | StringProp::operator=(const StringProp &other) | |
| 340 | { | ||
| 341 | ✗ | if(&other == this) | |
| 342 | ✗ | return *this; | |
| 343 | |||
| 344 | ✗ | Prop::operator=(other); | |
| 345 | |||
| 346 | ✗ | if(mpa_data != nullptr) | |
| 347 | ✗ | deleteData(); | |
| 348 | |||
| 349 | ✗ | if(other.mpa_data != nullptr) | |
| 350 | { | ||
| 351 | ✗ | QString *text = static_cast<QString *>(other.mpa_data); | |
| 352 | ✗ | mpa_data = static_cast<void *>(new QString(*text)); | |
| 353 | } | ||
| 354 | else | ||
| 355 | ✗ | mpa_data = nullptr; | |
| 356 | |||
| 357 | ✗ | return *this; | |
| 358 | } | ||
| 359 | |||
| 360 | /*! | ||
| 361 | \brief Duplicates this StringProp instance and returns a pointer to it. | ||
| 362 | */ | ||
| 363 | StringProp * | ||
| 364 | ✗ | StringProp::cloneOut() const | |
| 365 | { | ||
| 366 | ✗ | StringProp *new_p = new StringProp(*this); | |
| 367 | |||
| 368 | ✗ | return new_p; | |
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | /*! | ||
| 373 | \brief Parses the string-only property XML \a element using a \a{version}ed | ||
| 374 | function. | ||
| 375 | |||
| 376 | Parses the string-only property XML element passed as argument and for each | ||
| 377 | encountered data(name and data) will set the data to this string-only | ||
| 378 | property (this is called XML rendering). | ||
| 379 | |||
| 380 | Returns true if parsing was successful, false otherwise. | ||
| 381 | */ | ||
| 382 | bool | ||
| 383 | ✗ | StringProp::renderXmlElement(const QDomElement &element, | |
| 384 | [[maybe_unused]] int version) | ||
| 385 | { | ||
| 386 | ✗ | QDomElement child; | |
| 387 | |||
| 388 | /* This is what we expect. | ||
| 389 | * <prop> | ||
| 390 | * <name>MODIF</name> | ||
| 391 | * <data>acetylation</data> | ||
| 392 | * </prop> | ||
| 393 | */ | ||
| 394 | |||
| 395 | ✗ | if(element.tagName() != "prop") | |
| 396 | ✗ | return false; | |
| 397 | |||
| 398 | ✗ | child = element.firstChildElement("name"); | |
| 399 | |||
| 400 | ✗ | if(child.isNull()) | |
| 401 | ✗ | return false; | |
| 402 | |||
| 403 | ✗ | m_name = child.text(); | |
| 404 | |||
| 405 | // And now we have to manage the prop objects. | ||
| 406 | ✗ | child = child.nextSiblingElement(); | |
| 407 | |||
| 408 | ✗ | if(child.isNull()) | |
| 409 | ✗ | return false; | |
| 410 | |||
| 411 | ✗ | mpa_data = static_cast<void *>(new QString(child.text())); | |
| 412 | |||
| 413 | ✗ | return true; | |
| 414 | ✗ | } | |
| 415 | |||
| 416 | |||
| 417 | /*! | ||
| 418 | \brief Formats a string suitable to use as an XML element. | ||
| 419 | |||
| 420 | Formats a string suitable to be used as an XML element in a polymer sequence | ||
| 421 | file. Typical string-only property elements that might be generated in this | ||
| 422 | function look like this: | ||
| 423 | |||
| 424 | \code | ||
| 425 | <prop> | ||
| 426 | <name>MODIF</name> | ||
| 427 | <data>Phosphorylation</data> | ||
| 428 | </prop> | ||
| 429 | <prop> | ||
| 430 | <name>COMMENT</name> | ||
| 431 | <data>Phosphorylation is only partial</data> | ||
| 432 | </prop> | ||
| 433 | \endcode | ||
| 434 | |||
| 435 | \a offset times the \a indent string must be used as a lead in the | ||
| 436 | formatting of elements. | ||
| 437 | |||
| 438 | Returns a dynamically allocated string that needs to be freed after use. | ||
| 439 | */ | ||
| 440 | QString * | ||
| 441 | ✗ | StringProp::formatXmlElement(int offset, const QString &indent) | |
| 442 | { | ||
| 443 | int newOffset; | ||
| 444 | ✗ | int iter = 0; | |
| 445 | |||
| 446 | ✗ | QString lead(""); | |
| 447 | ✗ | QString *string = new QString(); | |
| 448 | |||
| 449 | // Prepare the lead. | ||
| 450 | ✗ | newOffset = offset; | |
| 451 | ✗ | while(iter < newOffset) | |
| 452 | { | ||
| 453 | ✗ | lead += indent; | |
| 454 | ✗ | ++iter; | |
| 455 | } | ||
| 456 | |||
| 457 | /* We are willing to create an <prop> node that should look like this: | ||
| 458 | * <prop> | ||
| 459 | * <name>MODIF</name> | ||
| 460 | * <data>Phosphorylation</data> | ||
| 461 | * </prop> | ||
| 462 | * <prop> | ||
| 463 | * <name>COMMENT</name> | ||
| 464 | * <data>Phosphorylation is only partial</data> | ||
| 465 | * </prop> | ||
| 466 | * | ||
| 467 | * As shown, all the member data of the prop object are simple | ||
| 468 | * strings. The name string is never dynamically allocated, while | ||
| 469 | * the data string is always dynamically allocated. | ||
| 470 | */ | ||
| 471 | |||
| 472 | ✗ | *string += QString("%1<prop>\n").arg(lead); | |
| 473 | |||
| 474 | // Prepare the lead. | ||
| 475 | ✗ | ++newOffset; | |
| 476 | ✗ | lead.clear(); | |
| 477 | ✗ | iter = 0; | |
| 478 | ✗ | while(iter < newOffset) | |
| 479 | { | ||
| 480 | ✗ | lead += indent; | |
| 481 | ✗ | ++iter; | |
| 482 | } | ||
| 483 | |||
| 484 | // Continue with indented elements. | ||
| 485 | |||
| 486 | ✗ | *string += QString("%1<name>%2</name>\n").arg(lead).arg(m_name); | |
| 487 | |||
| 488 | ✗ | *string += QString("%1<data>%2</data>\n") | |
| 489 | ✗ | .arg(lead) | |
| 490 | ✗ | .arg(*static_cast<QString *>(mpa_data)); | |
| 491 | |||
| 492 | // Prepare the lead for the closing element. | ||
| 493 | ✗ | --newOffset; | |
| 494 | ✗ | lead.clear(); | |
| 495 | ✗ | iter = 0; | |
| 496 | ✗ | while(iter < newOffset) | |
| 497 | { | ||
| 498 | ✗ | lead += indent; | |
| 499 | ✗ | ++iter; | |
| 500 | } | ||
| 501 | |||
| 502 | ✗ | *string += QString("%1</prop>\n").arg(lead); | |
| 503 | |||
| 504 | ✗ | return string; | |
| 505 | ✗ | } | |
| 506 | |||
| 507 | |||
| 508 | //////////////////////// IntProp //////////////////////// | ||
| 509 | //////////////////////// IntProp //////////////////////// | ||
| 510 | |||
| 511 | |||
| 512 | /*! | ||
| 513 | \class MsXpS::libXpertMass::IntProp | ||
| 514 | \inmodule libXpertMass | ||
| 515 | \ingroup ThePropSystem | ||
| 516 | |||
| 517 | \brief The IntProp class is the specialized class for properties | ||
| 518 | that hold data in the form of integer values. | ||
| 519 | |||
| 520 | A IntProp property is a simple property in which the data is a pointer to an | ||
| 521 | allocated integer. | ||
| 522 | */ | ||
| 523 | |||
| 524 | |||
| 525 | /*! | ||
| 526 | \brief Constructs a IntProp instance. | ||
| 527 | |||
| 528 | \list | ||
| 529 | \li \a name: The name of the property. | ||
| 530 | \li \a data: The data of the property. | ||
| 531 | \endlist | ||
| 532 | |||
| 533 | The integer passed as \a data is used to dynamically allocate a new integer with | ||
| 534 | the same contents and then is set to the member mpa_data pointer. | ||
| 535 | */ | ||
| 536 | ✗ | IntProp::IntProp(const QString &name, int data) : Prop(name) | |
| 537 | { | ||
| 538 | ✗ | mpa_data = static_cast<void *>(new int(data)); | |
| 539 | ✗ | } | |
| 540 | |||
| 541 | |||
| 542 | /*! | ||
| 543 | \brief Constructs a IntProp instance as a copy of \a other. | ||
| 544 | */ | ||
| 545 | ✗ | IntProp::IntProp(const IntProp &other) : Prop(other) | |
| 546 | { | ||
| 547 | ✗ | if(other.mpa_data != nullptr) | |
| 548 | { | ||
| 549 | ✗ | int *value = static_cast<int *>(other.mpa_data); | |
| 550 | ✗ | mpa_data = static_cast<void *>(new int(*value)); | |
| 551 | } | ||
| 552 | ✗ | } | |
| 553 | |||
| 554 | |||
| 555 | /*! | ||
| 556 | \brief Destructs this IntProp instance. | ||
| 557 | |||
| 558 | Deletion of the data is delegated to \l deleteData(). | ||
| 559 | */ | ||
| 560 | ✗ | IntProp::~IntProp() | |
| 561 | { | ||
| 562 | ✗ | deleteData(); | |
| 563 | ✗ | } | |
| 564 | |||
| 565 | /*! | ||
| 566 | \brief Deletes the member data. | ||
| 567 | */ | ||
| 568 | void | ||
| 569 | ✗ | IntProp::deleteData() | |
| 570 | { | ||
| 571 | ✗ | if(mpa_data != nullptr) | |
| 572 | { | ||
| 573 | ✗ | delete static_cast<int *>(mpa_data); | |
| 574 | ✗ | mpa_data = 0; | |
| 575 | } | ||
| 576 | ✗ | } | |
| 577 | |||
| 578 | /*! | ||
| 579 | \brief Assigns \a other to this IntProp instance. | ||
| 580 | |||
| 581 | Returns a reference to this IntProp instance. | ||
| 582 | */ | ||
| 583 | IntProp & | ||
| 584 | ✗ | IntProp::operator=(const IntProp &other) | |
| 585 | { | ||
| 586 | ✗ | if(&other == this) | |
| 587 | ✗ | return *this; | |
| 588 | |||
| 589 | ✗ | Prop::operator=(other); | |
| 590 | |||
| 591 | ✗ | if(mpa_data != nullptr) | |
| 592 | ✗ | deleteData(); | |
| 593 | |||
| 594 | ✗ | if(other.mpa_data != nullptr) | |
| 595 | { | ||
| 596 | ✗ | int *value = static_cast<int *>(other.mpa_data); | |
| 597 | ✗ | mpa_data = static_cast<void *>(new int(*value)); | |
| 598 | } | ||
| 599 | else | ||
| 600 | ✗ | mpa_data = nullptr; | |
| 601 | |||
| 602 | ✗ | return *this; | |
| 603 | } | ||
| 604 | |||
| 605 | |||
| 606 | /*! | ||
| 607 | \brief Duplicates this IntProp instance and returns a pointer to it. | ||
| 608 | */ | ||
| 609 | IntProp * | ||
| 610 | ✗ | IntProp::cloneOut() const | |
| 611 | { | ||
| 612 | ✗ | IntProp *new_p = new IntProp(*this); | |
| 613 | |||
| 614 | ✗ | return new_p; | |
| 615 | } | ||
| 616 | |||
| 617 | |||
| 618 | /*! | ||
| 619 | \brief Parses a integer property XML \a element using a \a{version}ed | ||
| 620 | function. | ||
| 621 | |||
| 622 | Parses the integer property XML element passed as argument and for each | ||
| 623 | encountered data (name and data) will set the data to this IntProp | ||
| 624 | instance (this is called XML rendering). | ||
| 625 | |||
| 626 | Returns true if parsing was successful, false otherwise.) | ||
| 627 | */ | ||
| 628 | bool | ||
| 629 | ✗ | IntProp::renderXmlElement(const QDomElement &element, | |
| 630 | [[maybe_unused]] int version) | ||
| 631 | { | ||
| 632 | ✗ | QDomElement child; | |
| 633 | |||
| 634 | /* This is what we expect. | ||
| 635 | * <prop> | ||
| 636 | <name>IONIZATION_LEVEL</name> | ||
| 637 | <data>5</data> | ||
| 638 | * </prop> | ||
| 639 | */ | ||
| 640 | |||
| 641 | ✗ | if(element.tagName() != "prop") | |
| 642 | ✗ | return false; | |
| 643 | |||
| 644 | ✗ | child = element.firstChildElement("name"); | |
| 645 | |||
| 646 | ✗ | if(child.isNull()) | |
| 647 | ✗ | return false; | |
| 648 | |||
| 649 | ✗ | m_name = child.text(); | |
| 650 | |||
| 651 | // And now we have to manage the prop objects. | ||
| 652 | ✗ | child = child.nextSiblingElement(); | |
| 653 | |||
| 654 | ✗ | if(child.isNull()) | |
| 655 | ✗ | return false; | |
| 656 | |||
| 657 | ✗ | mpa_data = static_cast<void *>(new int(child.text().toInt())); | |
| 658 | |||
| 659 | ✗ | return true; | |
| 660 | ✗ | } | |
| 661 | |||
| 662 | |||
| 663 | /*! | ||
| 664 | \brief Formats a string suitable to use as an XML element. | ||
| 665 | |||
| 666 | Formats a string suitable to be used as an XML element. Typical integer property | ||
| 667 | elements that might be generated in this function look like this: | ||
| 668 | |||
| 669 | \code | ||
| 670 | <prop> | ||
| 671 | <name>IONIZATION_LEVEL</name> | ||
| 672 | <data>5</data> | ||
| 673 | </prop> | ||
| 674 | \endcode | ||
| 675 | |||
| 676 | \a offset times the \a indent string must be used as a lead in the | ||
| 677 | formatting of elements. | ||
| 678 | |||
| 679 | Returns a dynamically allocated string that needs to be freed after use. | ||
| 680 | */ | ||
| 681 | QString * | ||
| 682 | ✗ | IntProp::formatXmlElement(int offset, const QString &indent) | |
| 683 | { | ||
| 684 | int newOffset; | ||
| 685 | ✗ | int iter = 0; | |
| 686 | |||
| 687 | ✗ | QString lead(""); | |
| 688 | ✗ | QString *string = new QString(); | |
| 689 | |||
| 690 | // Prepare the lead. | ||
| 691 | ✗ | newOffset = offset; | |
| 692 | ✗ | while(iter < newOffset) | |
| 693 | { | ||
| 694 | ✗ | lead += indent; | |
| 695 | ✗ | ++iter; | |
| 696 | } | ||
| 697 | |||
| 698 | /* We are willing to create an <prop> node that should look like this: | ||
| 699 | * | ||
| 700 | * <prop> | ||
| 701 | * <name>SEARCHED_MASS</name> | ||
| 702 | * <data>1000.234</data> | ||
| 703 | * </prop> | ||
| 704 | * | ||
| 705 | */ | ||
| 706 | |||
| 707 | ✗ | *string += QString("%1<prop>\n").arg(lead); | |
| 708 | |||
| 709 | // Prepare the lead. | ||
| 710 | ✗ | ++newOffset; | |
| 711 | ✗ | lead.clear(); | |
| 712 | ✗ | iter = 0; | |
| 713 | ✗ | while(iter < newOffset) | |
| 714 | { | ||
| 715 | ✗ | lead += indent; | |
| 716 | ✗ | ++iter; | |
| 717 | } | ||
| 718 | |||
| 719 | // Continue with indented elements. | ||
| 720 | |||
| 721 | ✗ | *string += QString("%1<name>%2</name>\n").arg(lead).arg(m_name); | |
| 722 | |||
| 723 | ✗ | QString value; | |
| 724 | ✗ | value = QString::number(*static_cast<int *>(mpa_data), 'g', 10); | |
| 725 | |||
| 726 | ✗ | *string += QString("%1<data>%2</data>\n").arg(lead).arg(value); | |
| 727 | |||
| 728 | // Prepare the lead for the closing element. | ||
| 729 | ✗ | --newOffset; | |
| 730 | ✗ | lead.clear(); | |
| 731 | ✗ | iter = 0; | |
| 732 | ✗ | while(iter < newOffset) | |
| 733 | { | ||
| 734 | ✗ | lead += indent; | |
| 735 | ✗ | ++iter; | |
| 736 | } | ||
| 737 | |||
| 738 | ✗ | *string += QString("%1</prop>\n").arg(lead); | |
| 739 | |||
| 740 | ✗ | return string; | |
| 741 | ✗ | } | |
| 742 | |||
| 743 | |||
| 744 | //////////////////////// DoubleProp //////////////////////// | ||
| 745 | //////////////////////// DoubleProp //////////////////////// | ||
| 746 | |||
| 747 | |||
| 748 | /*! | ||
| 749 | \class MsXpS::libXpertMass::DoubleProp | ||
| 750 | \inmodule libXpertMass | ||
| 751 | \ingroup ThePropSystem | ||
| 752 | |||
| 753 | \brief The DoubleProp class is the specialized class for properties | ||
| 754 | that hold data in the form of double values. | ||
| 755 | |||
| 756 | A DoubleProp property is a simple property in which the data is a pointer to an | ||
| 757 | allocated double. | ||
| 758 | */ | ||
| 759 | |||
| 760 | |||
| 761 | /*! | ||
| 762 | \brief Constructs a DoubleProp instance. | ||
| 763 | |||
| 764 | \list | ||
| 765 | \li \a name: The name of the property. | ||
| 766 | \li \a data: The data of the property. | ||
| 767 | \endlist | ||
| 768 | |||
| 769 | The integer passed as \a data is used to dynamically allocate a new double with | ||
| 770 | the same contents and then is set to the member mpa_data pointer. | ||
| 771 | */ | ||
| 772 | ✗ | DoubleProp::DoubleProp(const QString &name, double data) : Prop(name) | |
| 773 | { | ||
| 774 | ✗ | mpa_data = static_cast<void *>(new double(data)); | |
| 775 | ✗ | } | |
| 776 | |||
| 777 | |||
| 778 | /*! | ||
| 779 | \brief Constructs a DoubleProp instance as a copy of \a other. | ||
| 780 | */ | ||
| 781 | ✗ | DoubleProp::DoubleProp(const DoubleProp &other) : Prop(other) | |
| 782 | { | ||
| 783 | ✗ | if(other.mpa_data != nullptr) | |
| 784 | { | ||
| 785 | ✗ | double *value = static_cast<double *>(other.mpa_data); | |
| 786 | ✗ | mpa_data = static_cast<void *>(new double(*value)); | |
| 787 | } | ||
| 788 | ✗ | } | |
| 789 | |||
| 790 | |||
| 791 | /*! | ||
| 792 | \brief Destructs this DoubleProp instance. | ||
| 793 | |||
| 794 | Deletion of the data is delegated to \l deleteData(). | ||
| 795 | */ | ||
| 796 | ✗ | DoubleProp::~DoubleProp() | |
| 797 | { | ||
| 798 | ✗ | deleteData(); | |
| 799 | ✗ | } | |
| 800 | |||
| 801 | |||
| 802 | /*! | ||
| 803 | \brief Deletes the member data. | ||
| 804 | */ | ||
| 805 | void | ||
| 806 | ✗ | DoubleProp::deleteData() | |
| 807 | { | ||
| 808 | ✗ | if(mpa_data) | |
| 809 | { | ||
| 810 | ✗ | delete static_cast<double *>(mpa_data); | |
| 811 | ✗ | mpa_data = 0; | |
| 812 | } | ||
| 813 | ✗ | } | |
| 814 | |||
| 815 | |||
| 816 | /*! | ||
| 817 | \brief Assigns \a other to this DoubleProp instance. | ||
| 818 | |||
| 819 | Returns a reference to this DoubleProp instance. | ||
| 820 | */ | ||
| 821 | DoubleProp & | ||
| 822 | ✗ | DoubleProp::operator=(const DoubleProp &other) | |
| 823 | { | ||
| 824 | ✗ | if(&other == this) | |
| 825 | ✗ | return *this; | |
| 826 | |||
| 827 | ✗ | Prop::operator=(other); | |
| 828 | |||
| 829 | ✗ | if(mpa_data != nullptr) | |
| 830 | ✗ | deleteData(); | |
| 831 | |||
| 832 | ✗ | if(other.mpa_data != nullptr) | |
| 833 | { | ||
| 834 | ✗ | double *value = static_cast<double *>(other.mpa_data); | |
| 835 | ✗ | mpa_data = static_cast<void *>(new double(*value)); | |
| 836 | } | ||
| 837 | else | ||
| 838 | ✗ | mpa_data = nullptr; | |
| 839 | |||
| 840 | ✗ | return *this; | |
| 841 | } | ||
| 842 | |||
| 843 | |||
| 844 | /*! | ||
| 845 | \brief Duplicates this DoubleProp instance and returns a pointer to it. | ||
| 846 | */ | ||
| 847 | DoubleProp * | ||
| 848 | ✗ | DoubleProp::cloneOut() const | |
| 849 | { | ||
| 850 | ✗ | DoubleProp *new_p = new DoubleProp(*this); | |
| 851 | |||
| 852 | ✗ | return new_p; | |
| 853 | } | ||
| 854 | |||
| 855 | |||
| 856 | /*! | ||
| 857 | \brief Parses a double property XML \a element using a \a{version}ed | ||
| 858 | function. | ||
| 859 | |||
| 860 | Parses the double property XML element passed as argument and for each | ||
| 861 | encountered data (name and data) will set the data to this DoubleProp | ||
| 862 | instance (this is called XML rendering). | ||
| 863 | |||
| 864 | Returns true if parsing was successful, false otherwise.) | ||
| 865 | */ | ||
| 866 | bool | ||
| 867 | ✗ | DoubleProp::renderXmlElement(const QDomElement &element, | |
| 868 | [[maybe_unused]] int version) | ||
| 869 | { | ||
| 870 | ✗ | QDomElement child; | |
| 871 | |||
| 872 | /* This is what we expect. | ||
| 873 | * <prop> | ||
| 874 | * <name>SEARCHED_MASS</name> | ||
| 875 | * <data>1000.234</data> | ||
| 876 | * </prop> | ||
| 877 | */ | ||
| 878 | |||
| 879 | ✗ | if(element.tagName() != "prop") | |
| 880 | ✗ | return false; | |
| 881 | |||
| 882 | ✗ | child = element.firstChildElement("name"); | |
| 883 | |||
| 884 | ✗ | if(child.isNull()) | |
| 885 | ✗ | return false; | |
| 886 | |||
| 887 | ✗ | m_name = child.text(); | |
| 888 | |||
| 889 | // And now we have to manage the prop objects. | ||
| 890 | ✗ | child = child.nextSiblingElement(); | |
| 891 | |||
| 892 | ✗ | if(child.isNull()) | |
| 893 | ✗ | return false; | |
| 894 | |||
| 895 | ✗ | mpa_data = static_cast<void *>(new double(child.text().toDouble())); | |
| 896 | |||
| 897 | ✗ | return true; | |
| 898 | ✗ | } | |
| 899 | |||
| 900 | |||
| 901 | /*! | ||
| 902 | \brief Formats a string suitable to use as an XML element. | ||
| 903 | |||
| 904 | Formats a string suitable to be used as an XML element. Typical double property | ||
| 905 | elements that might be generated in this function look like this: | ||
| 906 | |||
| 907 | \code | ||
| 908 | <prop> | ||
| 909 | <name>SEARCHED_MASS</name> | ||
| 910 | <data>1000.234</data> | ||
| 911 | </prop> | ||
| 912 | \endcode | ||
| 913 | |||
| 914 | \a offset times the \a indent string must be used as a lead in the | ||
| 915 | formatting of elements. | ||
| 916 | |||
| 917 | Returns a dynamically allocated string that needs to be freed after use. | ||
| 918 | */ | ||
| 919 | QString * | ||
| 920 | ✗ | DoubleProp::formatXmlElement(int offset, const QString &indent) | |
| 921 | { | ||
| 922 | int newOffset; | ||
| 923 | ✗ | int iter = 0; | |
| 924 | |||
| 925 | ✗ | QString lead(""); | |
| 926 | ✗ | QString *string = new QString(); | |
| 927 | |||
| 928 | // Prepare the lead. | ||
| 929 | ✗ | newOffset = offset; | |
| 930 | ✗ | while(iter < newOffset) | |
| 931 | { | ||
| 932 | ✗ | lead += indent; | |
| 933 | ✗ | ++iter; | |
| 934 | } | ||
| 935 | |||
| 936 | /* We are willing to create an <prop> node that should look like this: | ||
| 937 | * | ||
| 938 | * <prop> | ||
| 939 | * <name>SEARCHED_MASS</name> | ||
| 940 | * <data>1000.234</data> | ||
| 941 | * </prop> | ||
| 942 | * | ||
| 943 | */ | ||
| 944 | |||
| 945 | ✗ | *string += QString("%1<prop>\n").arg(lead); | |
| 946 | |||
| 947 | // Prepare the lead. | ||
| 948 | ✗ | ++newOffset; | |
| 949 | ✗ | lead.clear(); | |
| 950 | ✗ | iter = 0; | |
| 951 | ✗ | while(iter < newOffset) | |
| 952 | { | ||
| 953 | ✗ | lead += indent; | |
| 954 | ✗ | ++iter; | |
| 955 | } | ||
| 956 | |||
| 957 | // Continue with indented elements. | ||
| 958 | |||
| 959 | ✗ | *string += QString("%1<name>%2</name>\n").arg(lead).arg(m_name); | |
| 960 | |||
| 961 | ✗ | QString value; | |
| 962 | ✗ | value = QString::number(*static_cast<double *>(mpa_data), 'g', 10); | |
| 963 | |||
| 964 | ✗ | *string += QString("%1<data>%2</data>\n").arg(lead).arg(value); | |
| 965 | |||
| 966 | // Prepare the lead for the closing element. | ||
| 967 | ✗ | --newOffset; | |
| 968 | ✗ | lead.clear(); | |
| 969 | ✗ | iter = 0; | |
| 970 | ✗ | while(iter < newOffset) | |
| 971 | { | ||
| 972 | ✗ | lead += indent; | |
| 973 | ✗ | ++iter; | |
| 974 | } | ||
| 975 | |||
| 976 | ✗ | *string += QString("%1</prop>\n").arg(lead); | |
| 977 | |||
| 978 | ✗ | return string; | |
| 979 | ✗ | } | |
| 980 | |||
| 981 | |||
| 982 | /////////////////// NoDeletePointerProp /////////////////// | ||
| 983 | /////////////////// NoDeletePointerProp /////////////////// | ||
| 984 | |||
| 985 | |||
| 986 | /*! | ||
| 987 | \class MsXpS::libXpertMass::NoDeletePointerProp | ||
| 988 | \inmodule libXpertMass | ||
| 989 | \ingroup ThePropSystem | ||
| 990 | |||
| 991 | \brief The NoDeletePointerProp class provides a pointer property. | ||
| 992 | |||
| 993 | A NoDeletePointerProp property is a simple property in which the data is a | ||
| 994 | pointer to an allocated instance, but which may never be destroyed by the | ||
| 995 | property itself. This property is regarded as a simple "message-containing | ||
| 996 | property". The message is nothing but the name of the property. | ||
| 997 | */ | ||
| 998 | |||
| 999 | |||
| 1000 | /*! | ||
| 1001 | \brief Constructs a NoDeletePointerProp instance. | ||
| 1002 | |||
| 1003 | \list | ||
| 1004 | \li \a name: The name of the property. | ||
| 1005 | \li \a no_delete_data_p: The data of the property. | ||
| 1006 | \endlist | ||
| 1007 | |||
| 1008 | The member data pointer (\l mpa_data) is assigned \a no_delete_data_p and is | ||
| 1009 | not going to be deleted upon destruction of this NoDeletePointerProp instance. | ||
| 1010 | */ | ||
| 1011 | ✗ | NoDeletePointerProp::NoDeletePointerProp(const QString &name, | |
| 1012 | ✗ | void *no_delete_data_p) | |
| 1013 | ✗ | : Prop(name) | |
| 1014 | { | ||
| 1015 | ✗ | mpa_data = no_delete_data_p; | |
| 1016 | ✗ | } | |
| 1017 | |||
| 1018 | |||
| 1019 | /*! | ||
| 1020 | \brief Constructs a NoDeletePointerProp instance as a copy of \a other. | ||
| 1021 | */ | ||
| 1022 | ✗ | NoDeletePointerProp::NoDeletePointerProp(const NoDeletePointerProp &other) | |
| 1023 | ✗ | : Prop(other) | |
| 1024 | { | ||
| 1025 | ✗ | mpa_data = static_cast<void *>(other.mpa_data); | |
| 1026 | ✗ | } | |
| 1027 | |||
| 1028 | |||
| 1029 | /*! | ||
| 1030 | \brief Destructs this NoDeletePointerProp instance. | ||
| 1031 | |||
| 1032 | Deletion of the data is delegated to \l deleteData(), that won't delete the | ||
| 1033 | data. | ||
| 1034 | */ | ||
| 1035 | ✗ | NoDeletePointerProp::~NoDeletePointerProp() | |
| 1036 | { | ||
| 1037 | ✗ | deleteData(); | |
| 1038 | ✗ | } | |
| 1039 | |||
| 1040 | |||
| 1041 | /*! | ||
| 1042 | \brief Does not delete the member data. | ||
| 1043 | */ | ||
| 1044 | void | ||
| 1045 | ✗ | NoDeletePointerProp::deleteData() | |
| 1046 | { | ||
| 1047 | // We do not do anything here. | ||
| 1048 | ✗ | } | |
| 1049 | |||
| 1050 | |||
| 1051 | /*! | ||
| 1052 | \brief Assigns \a other to this NoDeletePointerProp instance. | ||
| 1053 | |||
| 1054 | Returns a reference to this NoDeletePointerProp instance. | ||
| 1055 | */ | ||
| 1056 | NoDeletePointerProp & | ||
| 1057 | ✗ | NoDeletePointerProp::operator=(const NoDeletePointerProp &other) | |
| 1058 | { | ||
| 1059 | ✗ | if(&other == this) | |
| 1060 | ✗ | return *this; | |
| 1061 | |||
| 1062 | ✗ | Prop::operator=(other); | |
| 1063 | |||
| 1064 | ✗ | mpa_data = static_cast<void *>(other.mpa_data); | |
| 1065 | |||
| 1066 | ✗ | return *this; | |
| 1067 | } | ||
| 1068 | |||
| 1069 | |||
| 1070 | /*! | ||
| 1071 | \brief Duplicates this NoDeletePointerProp instance and returns a pointer to it. | ||
| 1072 | */ | ||
| 1073 | NoDeletePointerProp * | ||
| 1074 | ✗ | NoDeletePointerProp::cloneOut() const | |
| 1075 | { | ||
| 1076 | ✗ | NoDeletePointerProp *new_p = new NoDeletePointerProp(*this); | |
| 1077 | |||
| 1078 | ✗ | return new_p; | |
| 1079 | } | ||
| 1080 | |||
| 1081 | /*! | ||
| 1082 | \brief This function is a no-op. | ||
| 1083 | |||
| 1084 | The \a element and the \a version parameters are not used. The | ||
| 1085 | NoDeletePointerProp class is not used to store or read data to or from files. | ||
| 1086 | |||
| 1087 | Returns false if the \a element tag name is not "prop", true otherwise. | ||
| 1088 | */ | ||
| 1089 | bool | ||
| 1090 | ✗ | NoDeletePointerProp::renderXmlElement(const QDomElement &element, | |
| 1091 | [[maybe_unused]] int version) | ||
| 1092 | { | ||
| 1093 | ✗ | if(element.tagName() != "prop") | |
| 1094 | ✗ | return false; | |
| 1095 | |||
| 1096 | ✗ | return true; | |
| 1097 | } | ||
| 1098 | |||
| 1099 | /*! | ||
| 1100 | \brief This function is a no-op. | ||
| 1101 | |||
| 1102 | The \a offset and the \a indent parameters are not used. The | ||
| 1103 | NoDeletePointerProp class is not used to store or read data to or from files. | ||
| 1104 | */ | ||
| 1105 | QString * | ||
| 1106 | ✗ | NoDeletePointerProp::formatXmlElement(int offset, const QString &indent) | |
| 1107 | { | ||
| 1108 | QString *string = | ||
| 1109 | ✗ | new QString(QObject::tr("%1-This function does not return anything " | |
| 1110 | "interesting-%2") | ||
| 1111 | ✗ | .arg(offset) | |
| 1112 | ✗ | .arg(indent)); | |
| 1113 | |||
| 1114 | ✗ | return string; | |
| 1115 | } | ||
| 1116 | |||
| 1117 | |||
| 1118 | |||
| 1119 | } // namespace libXpertMass | ||
| 1120 | |||
| 1121 | } // namespace MsXpS | ||
| 1122 |