File: data.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (253 lines) | stat: -rw-r--r-- 7,501 bytes parent folder | download | duplicates (5)
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
252
253
/***************************************************************************
  data.cpp
  -------------------
  Brewing data
  -------------------
  Copyright 2001-2008 David Johnson
  Please see the header file for copyright and license information.
 ***************************************************************************/

#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QMutex>
#include <QStringList>
#include <QTextStream>

#include "datareader.h"
#include "qbrew.h"
#include "recipe.h"
#include "resource.h"

#include "data.h"

using namespace Resource;

Data *Data::instance_ = 0;
static QMutex instancelock;

// Construction, Destruction /////////////////////////////////////////////////

// Private constructor
Data::Data()
    : defaultsize_(Volume(5.0, Volume::gallon)), defaultstyle_(),
      defaulthoptype_(Hop::PELLET_STRING), defaultgrainunit_(&Weight::pound),
      defaulthopunit_(&Weight::ounce),  defaultmiscunit_(&Quantity::generic),
      grainmap_(), hopmap_(), miscmap_(), stylemap_(), utable_(),
      steepyield_(0.5), efficiency_(0.75), tinseth_(true), morey_(true)
{ ; }

// Private destructor
Data::~Data() { ; }

// Return pointer to the data
Data *Data::instance()
{
    if (!instance_) {
        QMutexLocker lock(&instancelock);
        if (!instance_) instance_ = new Data();
    }
    return instance_;
}

// Initialize
void Data::initialize(const ConfigState &state)
{
    // set defaults
    if (state.calc.units == UNIT_METRIC) {
        setDefaultSize(Volume(state.recipe.batch, Volume::liter));
        setDefaultGrainUnit(Weight::kilogram);
        setDefaultHopUnit(Weight::gram);
        setDefaultTempUnit(Temperature::celsius);
    } else if (state.calc.units == UNIT_US) {
        setDefaultSize(Volume(state.recipe.batch, Volume::gallon));
        setDefaultGrainUnit(Weight::pound);
        setDefaultHopUnit(Weight::ounce);
        setDefaultTempUnit(Temperature::fahrenheit);
    }
    setDefaultStyle(state.recipe.style);
    setDefaultHopType(state.recipe.hoptype);
    setDefaultMiscUnit(Quantity::generic);
    setEfficiency(state.calc.efficiency);

    // load data file - look in standard locations
    // TODO: use QDesktopServices in next non-bugfix release (0.5.0)
    bool status = false;
    // try home directory first
    if (!status) status = loadData(QDIR_HOME + "/." + DATA_FILE, true);
    // then try system data directory
    if (!status) status = loadData(QBrew::instance()->dataBase() + DATA_FILE);

    if (!status) {
        qWarning() << "Warning: could not open data file";
        QMessageBox::warning(0, TITLE, QObject::tr("Could not open data file"));
    }
}

//////////////////////////////////////////////////////////////////////////////
// Data Access                                                              //
//////////////////////////////////////////////////////////////////////////////

void Data::setDefaultStyle(const QString &style)
{
    if (stylemap_.contains(style)) defaultstyle_ = stylemap_.value(style);
}

//////////////////////////////////////////////////////////////////////////////
// style()
// -------
// Return a style given its name

Style Data::style(const QString &name)
{
    if (stylemap_.contains(name)) return stylemap_.value(name);
    return Style();
}

//////////////////////////////////////////////////////////////////////////////
// grain()
// -------
// Return grain given its name

Grain Data::grain(const QString &name)
{
    Grain grain;
    if (grainmap_.contains(name)) grain = grainmap_.value(name);
    grain.setWeight(Weight(1.0, *defaultgrainunit_));
    return grain;
}

//////////////////////////////////////////////////////////////////////////////
// hop()
// -----
// Return hop given its name

Hop Data::hop(const QString &name)
{
    Hop hop;
    if (hopmap_.contains(name)) hop = hopmap_.value(name);
    hop.setWeight(Weight(1.0, *defaulthopunit_));
    hop.setType(defaulthoptype_);
    return hop;
}

//////////////////////////////////////////////////////////////////////////////
// misc()
// ------
// Return misc ingredient given its name

Misc Data::misc(const QString &name)
{
    Misc misc;
    if (miscmap_.contains(name)) misc = miscmap_.value(name);
    misc.setQuantity(Quantity(1.0, *defaultmiscunit_));
    return misc;
}

//////////////////////////////////////////////////////////////////////////////
// utilization
// -----------
// Look up the utilization for the given time

double Data::utilization(unsigned time)
{
    foreach(UEntry entry, utable_) {
        if (time >= entry.time) return (double(entry.utilization));
    }
    return 0.0;
}

//////////////////////////////////////////////////////////////////////////////
// addUEntry()
// -----------
// Add an entry to the utilization table [static]

void Data::addUEntry(const UEntry &entry)
{
    // keep the list sorted from highest time to lowest
    QList<UEntry>::Iterator it;
    if (utable_.isEmpty()) {
        utable_.append(entry);
    } else {
        for (it=utable_.begin(); it != utable_.end(); ++it) {
            if ((*it).time < entry.time) break;
        }
        utable_.insert(it, entry);
    }
}

//////////////////////////////////////////////////////////////////////////////
// Serialization                                                            //
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// loadData()
// ----------
// Load the data

bool Data::loadData(const QString &filename, bool quiet)
{
    // open file
    QFile datafile(filename);
    if (!datafile.open(QFile::ReadOnly | QFile::Text)) {
        // error opening file
        if (!quiet) {
            qWarning() << "Error: Cannot read" << filename;
            QMessageBox::warning(0, TITLE,
                                 QObject::tr("Cannot read file %1:\n%2")
                                 .arg(filename)
                                 .arg(datafile.errorString()));
        }
        datafile.close();
        return false;
    }

    QApplication::setOverrideCursor(Qt::WaitCursor);

    // parse file
    DataReader reader(&datafile);
    bool status = reader.readData(this);
    datafile.close();

    if (!status) {
        qWarning() << "Error: Problem reading file" << filename;
        qWarning() << reader.errorString();
        QMessageBox::warning(0, TITLE,
                             QObject::tr("Error reading file %1").arg(filename));
        QApplication::restoreOverrideCursor();
        return false;
    }

    QApplication::restoreOverrideCursor();
    return true;
}

//////////////////////////////////////////////////////////////////////////////
// saveData()
// ------------
// Save info to data file

bool Data::saveData(const QString &filename)
{
    // open file
    QFile datafile(filename);
    if (!datafile.open(QFile::WriteOnly | QFile::Text)) {
        // error opening file
        qWarning() << "Error: Cannot open file" << filename;
        QMessageBox::warning(0, TITLE,
                             QObject::tr("Cannot write file %1:\n%2")
                             .arg(filename)
                             .arg(datafile.errorString()));
        datafile.close();
        QApplication::restoreOverrideCursor();
        return false;
    }

    // write it out
    DataWriter writer(&datafile);
    writer.writeData(this);
    datafile.close();

    QApplication::restoreOverrideCursor();
    return true;
}