File: DataItem.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 (251 lines) | stat: -rw-r--r-- 6,459 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Data/DataItem.cpp
//! @brief     Implements class Data2DItem.
//!
//! @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/Data/DataItem.h"
#include "Base/Axis/Scale.h"
#include "Base/Util/Assert.h"
#include "Device/Data/Datafield.h"
#include "Device/IO/IOFactory.h"
#include "GUI/Model/Axis/AmplitudeAxisItem.h"
#include "GUI/Model/Util/UtilXML.h"
#include <QFile>
#include <utility>

namespace {
namespace Tag {

const QString FileName("FileName");
const QString LockArgRange("LockArgRange");
const QString XAxis("XAxis");
const QString YAxis("YAxis");

} // namespace Tag

const QString undefined_fname = "_undefined_";

} // namespace


DataItem::DataItem(const QString& modelType)
    : TYPE(modelType)
    , m_fname(undefined_fname)
    , m_lock_arg_range(false)
    , m_x_axis(std::make_unique<BasicAxisItem>())
    , m_y_axis(std::make_unique<AmplitudeAxisItem>())
    , m_last_modified(QDateTime::currentDateTime())
    , m_last_saved(m_last_modified.addSecs(-2023))
{
}

DataItem::~DataItem() = default;

void DataItem::setTheDatafield(const Datafield& data)
{
    std::unique_lock<std::mutex> lock(m_update_data_mutex);
    m_datafield = std::make_unique<Datafield>(data.plottableField());
    setLastModified(QDateTime::currentDateTime());
    emit datafieldChanged();
}

void DataItem::setRawDataVector(const std::vector<double>& data)
{
    ASSERT(m_datafield->size() == data.size());
    std::unique_lock<std::mutex> lock(m_update_data_mutex);
    m_datafield->setVector(data);

    setLastModified(QDateTime::currentDateTime());
}

QString DataItem::dataFullPath(const QString& projectDir) const
{
    return projectDir + "/" + m_fname;
}

void DataItem::setFileName(const QString& filename)
{
    m_fname = filename;
    setLastModified(QDateTime::currentDateTime());
}

void DataItem::loadDatafield(const QString& projectDir, int rank)
{
    if (!QFile::exists(projectDir))
        throw std::runtime_error("Cannot load datafield: project directory "
                                 + projectDir.toStdString() + " does not exist");
    if (m_fname == undefined_fname)
        return;

    const auto file = dataFullPath(projectDir);

    std::unique_ptr<Datafield> data;
    if (rank == 1)
        data = std::make_unique<Datafield>(
            IO::readData1D(file.toStdString(), IO::Filetype1D::bornagain1D));
    else if (rank == 2)
        data = std::make_unique<Datafield>(
            IO::readData2D(file.toStdString(), IO::Filetype2D::bornagain2D));
    else
        ASSERT_NEVER;

    ASSERT(data);
    setDatafield(*data);
    m_last_saved = m_last_modified;
}

void DataItem::saveDatafield(const QString& projectDir) const
{
    if (!m_datafield || !QFile::exists(projectDir))
        return;

    const auto path = dataFullPath(projectDir);

    if (QFile::exists(path) && !wasModifiedSinceLastSave())
        return;

    IO::writeDatafield(*c_field(), path.toStdString());

    m_last_saved = QDateTime::currentDateTime();
}

void DataItem::setLastModified(const QDateTime& dtime)
{
    m_last_modified = dtime;
}

int DataItem::xSize() const
{
    return axItemX()->size();
}

int DataItem::ySize() const
{
    return axItemY()->size();
}

double DataItem::lowerX() const
{
    return axItemX()->min().dVal();
}

double DataItem::upperX() const
{
    return axItemX()->max().dVal();
}

void DataItem::setXrange(double lower, double upper)
{
    axItemX()->setMin(lower);
    axItemX()->setMax(upper);
}

double DataItem::lowerY() const
{
    return axItemY()->min().dVal();
}

double DataItem::upperY() const
{
    return axItemY()->max().dVal();
}

void DataItem::setYrange(double lower, double upper)
{
    axItemY()->setMin(lower);
    axItemY()->setMax(upper);
}

void DataItem::alignXranges(DataItem* sourceItem)
{
    if (xSize() != sourceItem->xSize())
        throw std::runtime_error("Data and simulation have different number of x bins");

    if (sourceItem != this)
        setXrange(sourceItem->lowerX(), sourceItem->upperX());
}

void DataItem::alignYranges(DataItem* sourceItem)
{
    if (ySize() != sourceItem->ySize())
        throw std::runtime_error("Data and simulation have different number of y bins");

    if (sourceItem != this)
        setYrange(sourceItem->lowerY(), sourceItem->upperY());
}

void DataItem::alignXYranges(DataItem* sourceItem)
{
    alignXranges(sourceItem);
    alignYranges(sourceItem);
}

const BasicAxisItem* DataItem::axItemX() const
{
    return m_x_axis.get();
}

BasicAxisItem* DataItem::axItemX()
{
    return m_x_axis.get();
}

const AmplitudeAxisItem* DataItem::axItemY() const
{
    return m_y_axis.get();
}

AmplitudeAxisItem* DataItem::axItemY()
{
    return m_y_axis.get();
}

QString DataItem::xAxisLabel() const
{
    return m_datafield ? QString::fromStdString(m_datafield->xAxis().axisLabel()) : "";
}

QString DataItem::yAxisLabel() const
{
    return m_datafield ? QString::fromStdString(m_datafield->yAxis().axisLabel()) : "";
}

void DataItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeTaggedValue(w, Tag::FileName, m_fname);
    XML::writeTaggedValue(w, Tag::LockArgRange, m_lock_arg_range);
    XML::writeTaggedElement(w, Tag::XAxis, *m_x_axis);
    XML::writeTaggedElement(w, Tag::YAxis, *m_y_axis);
}

void DataItem::readFrom(QXmlStreamReader* r)
{
    while (r->readNextStartElement()) {
        QString tag = r->name().toString();
        if (tag == Tag::FileName)
            m_fname = XML::readTaggedString(r, tag);
        else if (tag == Tag::LockArgRange)
            m_lock_arg_range = XML::readTaggedBool(r, tag);
        else if (tag == Tag::XAxis)
            XML::readTaggedElement(r, tag, *m_x_axis);
        else if (tag == Tag::YAxis)
            XML::readTaggedElement(r, tag, *m_y_axis);
        else
            r->skipCurrentElement();
    }
}

bool DataItem::wasModifiedSinceLastSave() const
{
    // positive number means that m_last_saved is older than m_last_modified
    return m_last_saved.msecsTo(m_last_modified) > 0;
}