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