File: PointwiseAxisItem.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 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 (117 lines) | stat: -rw-r--r-- 3,305 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Axis/PointwiseAxisItem.cpp
//! @brief     Implements pointwise axis item.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Axis/PointwiseAxisItem.h"
#include "Base/Axis/Frame.h"
#include "Base/Axis/MakeScale.h"
#include "Base/Axis/Scale.h"
#include "Base/Const/Units.h"
#include "Base/Util/Assert.h"
#include "Device/Data/Datafield.h"
#include "Device/IO/ReadWriteINT.h"
#include "GUI/Model/Util/UtilXML.h"
#include <sstream>

namespace {
namespace Tag {

const QString BinaryData("BinaryData");
const QString BaseData("BaseData");

} // namespace Tag
} // namespace

PointwiseAxisItem::PointwiseAxisItem()
    : BasicAxisItem()
{
}

PointwiseAxisItem::~PointwiseAxisItem() = default;

void PointwiseAxisItem::setScale(const Scale& axis)
{
    m_scale = std::unique_ptr<Scale>(axis.clone());
}

const Scale* PointwiseAxisItem::scale() const
{
    return m_scale.get();
}

QByteArray PointwiseAxisItem::serializeBinaryData() const
{
    if (!m_scale)
        return {};

    Datafield df(std::vector<const Scale*>{m_scale->clone()});

    std::stringstream ss;
    Util::RW::writeBAInt(df, ss);
    return {ss.str().c_str(), static_cast<int>(ss.str().size())};
}

void PointwiseAxisItem::deserializeBinaryData(const QByteArray& data)
{
    if (data.isEmpty())
        return;

    std::istringstream str(data.toStdString());
    Datafield d = Util::RW::readBAInt(str);
    m_scale = std::unique_ptr<Scale>(d.axis(0).clone());
}

void PointwiseAxisItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeBaseElement<BasicAxisItem>(w, XML::Tag::BaseData, this);

    // axis binary data
    QByteArray a = serializeBinaryData();
    if (!a.isEmpty()) {
        w->writeStartElement(Tag::BinaryData);
        w->writeCharacters(a.toBase64());
        w->writeEndElement();
    }
}

void PointwiseAxisItem::readFrom(QXmlStreamReader* r)
{
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();
        if (tag == Tag::BaseData)
            XML::readBaseElement<BasicAxisItem>(r, tag, this);
        else if (tag == Tag::BinaryData) {
            QString valueAsBase64 = r->readElementText(QXmlStreamReader::SkipChildElements);
            const auto data = QByteArray::fromBase64(valueAsBase64.toLatin1());
            deserializeBinaryData(data);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}

void PointwiseAxisItem::updateAxIndicators(const Frame& frame)
{
    ASSERT(m_scale);
    ASSERT(m_scale->unit() != "bin");

    if (frame.axis(0).unit() == "rad") {
        setMin(Units::rad2deg(frame.axis(0).min()));
        setMax(Units::rad2deg(frame.axis(0).max()));
    } else {
        setMin(frame.axis(0).min());
        setMax(frame.axis(0).max());
    }
    resize(static_cast<int>(m_scale->size()));
}