File: VectorProperty.cpp

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (94 lines) | stat: -rw-r--r-- 2,921 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Descriptor/VectorProperty.cpp
//! @brief     Implements class VectorProperty.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Descriptor/VectorProperty.h"
#include "GUI/Model/Util/UtilXML.h"
#include <QUuid>

namespace {
namespace Tag {

const QString X("X");
const QString Y("Y");
const QString Z("Z");

} // namespace Tag
} // namespace

void VectorProperty::init(const QString& label, const QString& units, const QString& tooltip,
                          const QString& uidPrefix)
{
    RealLimits limits = RealLimits::limitless();
    init(label, units, tooltip, R3(), 3, limits.isLimited(), 0.01, limits, uidPrefix);
}

void VectorProperty::init(const QString& label, const QString& units, const QString& tooltip,
                          const R3& value, uint decimals, bool useFixedStep, double step,
                          const RealLimits& limits, const QString& uidPrefix)
{
    m_label = label;
    m_units = units;

    m_x.init("x", "", tooltip, value.x(), decimals, useFixedStep, step, limits, uidPrefix);
    m_y.init("y", "", tooltip, value.y(), decimals, useFixedStep, step, limits, uidPrefix);
    m_z.init("z", "", tooltip, value.z(), decimals, useFixedStep, step, limits, uidPrefix);

    QString uid;
    if (uidPrefix.size() > 0)
        uid = uidPrefix + "/" + QUuid::createUuid().toString();
    else
        uid = QUuid::createUuid().toString();

    m_x.setUid(uid + "/x");
    m_y.setUid(uid + "/y");
    m_z.setUid(uid + "/z");
}

bool VectorProperty::operator==(const VectorProperty& other) const
{
    return (m_label == other.m_label) && (m_units == other.m_units)
           && (m_x.dVal() == other.m_x.dVal()) && (m_y.dVal() == other.m_y.dVal())
           && (m_z.dVal() == other.m_z.dVal());
}

QString VectorProperty::label() const
{
    if (m_units == "")
        return m_label;

    return m_label + " [" + m_units + "]";
}

void VectorProperty::writeTo(QXmlStreamWriter* w) const
{
    m_x.writeTo2(w, Tag::X);
    m_y.writeTo2(w, Tag::Y);
    m_z.writeTo2(w, Tag::Z);
}

void VectorProperty::readFrom(QXmlStreamReader* r)
{
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();

        if (tag == Tag::X)
            m_x.readFrom2(r, tag);
        else if (tag == Tag::Y)
            m_y.readFrom2(r, tag);
        else if (tag == Tag::Z)
            m_z.readFrom2(r, tag);
        else
            r->skipCurrentElement();
    }
}