| 1 | /*************************************************************************** |
| 2 | data.cpp |
| 3 | ------------------- |
| 4 | Brewing data |
| 5 | ------------------- |
| 6 | Copyright 2001-2008 David Johnson |
| 7 | Please see the header file for copyright and license information. |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QDir> |
| 11 | #include <QFile> |
| 12 | #include <QMessageBox> |
| 13 | #include <QMutex> |
| 14 | #include <QStringList> |
| 15 | #include <QTextStream> |
| 16 | |
| 17 | #include "datareader.h" |
| 18 | #include "qbrew.h" |
| 19 | #include "recipe.h" |
| 20 | #include "resource.h" |
| 21 | |
| 22 | #include "data.h" |
| 23 | |
| 24 | using namespace Resource; |
| 25 | |
| 26 | Data *Data::instance_ = 0; |
| 27 | static QMutex instancelock; |
| 28 | |
| 29 | // Construction, Destruction ///////////////////////////////////////////////// |
| 30 | |
| 31 | // Private constructor |
| 32 | Data::Data() |
| 33 | : defaultsize_(Volume(5.0, Volume::gallon)), defaultstyle_(), |
| 34 | defaulthoptype_(Hop::PELLET_STRING), defaultgrainunit_(&Weight::pound), |
| 35 | defaulthopunit_(&Weight::ounce), defaultmiscunit_(&Quantity::generic), |
| 36 | grainmap_(), hopmap_(), miscmap_(), stylemap_(), utable_(), |
| 37 | steepyield_(0.5), efficiency_(0.75), tinseth_(true), morey_(true) |
| 38 | { ; } |
| 39 | |
| 40 | // Private destructor |
| 41 | Data::~Data() { ; } |
| 42 | |
| 43 | // Return pointer to the data |
| 44 | Data *Data::instance() |
| 45 | { |
| 46 | if (!instance_) { |
| 47 | QMutexLocker lock(&instancelock); |
| 48 | if (!instance_) instance_ = new Data(); |
| 49 | } |
| 50 | return instance_; |
| 51 | } |
| 52 | |
| 53 | // Initialize |
| 54 | void Data::initialize(const ConfigState &state) |
| 55 | { |
| 56 | // set defaults |
| 57 | if (state.calc.units == UNIT_METRIC) { |
| 58 | setDefaultSize(Volume(state.recipe.batch, Volume::liter)); |
| 59 | setDefaultGrainUnit(Weight::kilogram); |
| 60 | setDefaultHopUnit(Weight::gram); |
| 61 | setDefaultTempUnit(Temperature::celsius); |
| 62 | } else if (state.calc.units == UNIT_US) { |
| 63 | setDefaultSize(Volume(state.recipe.batch, Volume::gallon)); |
| 64 | setDefaultGrainUnit(Weight::pound); |
| 65 | setDefaultHopUnit(Weight::ounce); |
| 66 | setDefaultTempUnit(Temperature::fahrenheit); |
| 67 | } |
| 68 | setDefaultStyle(state.recipe.style); |
| 69 | setDefaultHopType(state.recipe.hoptype); |
| 70 | setDefaultMiscUnit(Quantity::generic); |
| 71 | setEfficiency(state.calc.efficiency); |
| 72 | |
| 73 | // load data file - look in standard locations |
| 74 | // TODO: use QDesktopServices in next non-bugfix release (0.5.0) |
| 75 | bool status = false; |
| 76 | // try home directory first |
| 77 | if (!status) status = loadData(QDIR_HOME + "/." + DATA_FILE, true); |
| 78 | // then try system data directory |
| 79 | if (!status) status = loadData(QBrew::instance()->dataBase() + DATA_FILE); |
| 80 | |
| 81 | if (!status) { |
| 82 | qWarning() << "Warning: could not open data file"; |
| 83 | QMessageBox::warning(0, TITLE, QObject::tr("Could not open data file")); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ////////////////////////////////////////////////////////////////////////////// |
| 88 | // Data Access // |
| 89 | ////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | void Data::setDefaultStyle(const QString &style) |
| 92 | { |
| 93 | if (stylemap_.contains(style)) defaultstyle_ = stylemap_.value(style); |
| 94 | } |
| 95 | |
| 96 | ////////////////////////////////////////////////////////////////////////////// |
| 97 | // style() |
| 98 | // ------- |
| 99 | // Return a style given its name |
| 100 | |
| 101 | Style Data::style(const QString &name) |
| 102 | { |
| 103 | if (stylemap_.contains(name)) return stylemap_.value(name); |
| 104 | return Style(); |
| 105 | } |
| 106 | |
| 107 | ////////////////////////////////////////////////////////////////////////////// |
| 108 | // grain() |
| 109 | // ------- |
| 110 | // Return grain given its name |
| 111 | |
| 112 | Grain Data::grain(const QString &name) |
| 113 | { |
| 114 | Grain grain; |
| 115 | if (grainmap_.contains(name)) grain = grainmap_.value(name); |
| 116 | grain.setWeight(Weight(1.0, *defaultgrainunit_)); |
| 117 | return grain; |
| 118 | } |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | // hop() |
| 122 | // ----- |
| 123 | // Return hop given its name |
| 124 | |
| 125 | Hop Data::hop(const QString &name) |
| 126 | { |
| 127 | Hop hop; |
| 128 | if (hopmap_.contains(name)) hop = hopmap_.value(name); |
| 129 | hop.setWeight(Weight(1.0, *defaulthopunit_)); |
| 130 | hop.setType(defaulthoptype_); |
| 131 | return hop; |
| 132 | } |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | // misc() |
| 136 | // ------ |
| 137 | // Return misc ingredient given its name |
| 138 | |
| 139 | Misc Data::misc(const QString &name) |
| 140 | { |
| 141 | Misc misc; |
| 142 | if (miscmap_.contains(name)) misc = miscmap_.value(name); |
| 143 | misc.setQuantity(Quantity(1.0, *defaultmiscunit_)); |
| 144 | return misc; |
| 145 | } |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | // utilization |
| 149 | // ----------- |
| 150 | // Look up the utilization for the given time |
| 151 | |
| 152 | double Data::utilization(unsigned time) |
| 153 | { |
| 154 | foreach(UEntry entry, utable_) { |
| 155 | if (time >= entry.time) return (double(entry.utilization)); |
| 156 | } |
| 157 | return 0.0; |
| 158 | } |
| 159 | |
| 160 | ////////////////////////////////////////////////////////////////////////////// |
| 161 | // addUEntry() |
| 162 | // ----------- |
| 163 | // Add an entry to the utilization table [static] |
| 164 | |
| 165 | void Data::addUEntry(const UEntry &entry) |
| 166 | { |
| 167 | // keep the list sorted from highest time to lowest |
| 168 | QList<UEntry>::Iterator it; |
| 169 | if (utable_.isEmpty()) { |
| 170 | utable_.append(entry); |
| 171 | } else { |
| 172 | for (it=utable_.begin(); it != utable_.end(); ++it) { |
| 173 | if ((*it).time < entry.time) break; |
| 174 | } |
| 175 | utable_.insert(it, entry); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ////////////////////////////////////////////////////////////////////////////// |
| 180 | // Serialization // |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
| 183 | ////////////////////////////////////////////////////////////////////////////// |
| 184 | // loadData() |
| 185 | // ---------- |
| 186 | // Load the data |
| 187 | |
| 188 | bool Data::loadData(const QString &filename, bool quiet) |
| 189 | { |
| 190 | // open file |
| 191 | QFile datafile(filename); |
| 192 | if (!datafile.open(QFile::ReadOnly | QFile::Text)) { |
| 193 | // error opening file |
| 194 | if (!quiet) { |
| 195 | qWarning() << "Error: Cannot read" << filename; |
| 196 | QMessageBox::warning(0, TITLE, |
| 197 | QObject::tr("Cannot read file %1:\n%2") |
| 198 | .arg(filename) |
| 199 | .arg(datafile.errorString())); |
| 200 | } |
| 201 | datafile.close(); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | QApplication::setOverrideCursor(Qt::WaitCursor); |
| 206 | |
| 207 | // parse file |
| 208 | DataReader reader(&datafile); |
| 209 | bool status = reader.readData(this); |
| 210 | datafile.close(); |
| 211 | |
| 212 | if (!status) { |
| 213 | qWarning() << "Error: Problem reading file" << filename; |
| 214 | qWarning() << reader.errorString(); |
| 215 | QMessageBox::warning(0, TITLE, |
| 216 | QObject::tr("Error reading file %1").arg(filename)); |
| 217 | QApplication::restoreOverrideCursor(); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | QApplication::restoreOverrideCursor(); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | ////////////////////////////////////////////////////////////////////////////// |
| 226 | // saveData() |
| 227 | // ------------ |
| 228 | // Save info to data file |
| 229 | |
| 230 | bool Data::saveData(const QString &filename) |
| 231 | { |
| 232 | // open file |
| 233 | QFile datafile(filename); |
| 234 | if (!datafile.open(QFile::WriteOnly | QFile::Text)) { |
| 235 | // error opening file |
| 236 | qWarning() << "Error: Cannot open file" << filename; |
| 237 | QMessageBox::warning(0, TITLE, |
| 238 | QObject::tr("Cannot write file %1:\n%2") |
| 239 | .arg(filename) |
| 240 | .arg(datafile.errorString())); |
| 241 | datafile.close(); |
| 242 | QApplication::restoreOverrideCursor(); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | // write it out |
| 247 | DataWriter writer(&datafile); |
| 248 | writer.writeData(this); |
| 249 | datafile.close(); |
| 250 | |
| 251 | QApplication::restoreOverrideCursor(); |
| 252 | return true; |
| 253 | } |