diff --git a/src/unit.cpp b/src/unit.cpp index f829e74..23a64a0 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -1,218 +1,218 @@ /* * Copyright (C) 2007-2009 Petri Damstén * Copyright (C) 2014 John Layt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unit.h" #include "unit_p.h" #include "unitcategory.h" #include namespace KUnitConversion { UnitPrivate::UnitPrivate() : m_categoryId(InvalidCategory), m_id(InvalidUnit), m_multiplier(1.0) { } UnitPrivate::UnitPrivate(CategoryId categoryId, UnitId id, qreal multiplier, const QString &symbol, const QString &description, const QString &matchString, const KLocalizedString &symbolString, const KLocalizedString &realString, const KLocalizedString &integerString) : m_categoryId(categoryId), m_id(id), m_multiplier(multiplier), m_symbol(symbol), m_description(description), m_matchString(matchString), m_symbolString(symbolString), m_realString(realString), m_integerString(integerString) { } UnitPrivate::~UnitPrivate() { } UnitPrivate *UnitPrivate::clone() { return new UnitPrivate(*this); } bool UnitPrivate::operator==(const UnitPrivate &other) const { return (m_id == other.m_id && m_symbol == other.m_symbol); } bool UnitPrivate::operator!=(const UnitPrivate &other) const { return !(*this == other); } void UnitPrivate::setUnitMultiplier(qreal multiplier) { m_multiplier = multiplier; } qreal UnitPrivate::unitMultiplier() const { return m_multiplier; } qreal UnitPrivate::toDefault(qreal value) const { return value * m_multiplier; } qreal UnitPrivate::fromDefault(qreal value) const { return value / m_multiplier; } Unit::Unit() - : d(0) + : d(nullptr) { } Unit::Unit(UnitPrivate *dd) : d(dd) { } Unit::Unit(const Unit &other) : d(other.d) { } Unit::~Unit() { } Unit &Unit::operator=(const Unit &other) { d = other.d; return *this; } bool Unit::operator==(const Unit &other) const { if (d && other.d) return (*d == *other.d); else return (d == other.d); } bool Unit::operator!=(const Unit &other) const { if (d && other.d) return (*d != *other.d); else return (d != other.d); } bool Unit::isNull() const { return !d; } bool Unit::isValid() const { return (d && !d->m_symbol.isEmpty()); } UnitId Unit::id() const { if (d) return d->m_id; return InvalidUnit; } CategoryId Unit::categoryId() const { if (d) return d->m_categoryId; return InvalidCategory; } UnitCategory Unit::category() const { if (d) return d->m_category; return UnitCategory(); } QString Unit::description() const { if (d) return d->m_description; return QString(); } QString Unit::symbol() const { if (d) return d->m_symbol; return QString(); } void Unit::setUnitMultiplier(qreal multiplier) { if (d) d->setUnitMultiplier(multiplier); } qreal Unit::toDefault(qreal value) const { if (d) return d->toDefault(value); return 0; } qreal Unit::fromDefault(qreal value) const { if (d) return d->fromDefault(value); return 0; } QString Unit::toString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const { if (isNull()) return QString(); if ((int)value == value && precision < 1) { return d->m_integerString.subs((int)value).toString(); } return d->m_realString.subs(value, fieldWidth, format, precision, fillChar).toString(); } QString Unit::toSymbolString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const { if (d) return d->m_symbolString.subs(value, fieldWidth, format, precision, fillChar).subs(d->m_symbol).toString(); return QString(); } } diff --git a/src/unitcategory.cpp b/src/unitcategory.cpp index febb001..d16ade3 100644 --- a/src/unitcategory.cpp +++ b/src/unitcategory.cpp @@ -1,239 +1,239 @@ /* * Copyright (C) 2007-2009 Petri Damstén * Copyright (C) 2014 John Layt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unitcategory.h" #include "unitcategory_p.h" #include "unit_p.h" #include #include namespace KUnitConversion { UnitCategoryPrivate::UnitCategoryPrivate() : m_id(InvalidCategory) { } UnitCategoryPrivate::UnitCategoryPrivate(CategoryId id, const QString &name, const QString &description) : m_id(id), m_name(name), m_description(description) { } UnitCategoryPrivate::~UnitCategoryPrivate() { } UnitCategoryPrivate *UnitCategoryPrivate::clone() { return new UnitCategoryPrivate(*this); } bool UnitCategoryPrivate::operator==(const UnitCategoryPrivate &other) const { return (m_id == other.m_id); } bool UnitCategoryPrivate::operator!=(const UnitCategoryPrivate &other) const { return !(*this == other); } Value UnitCategoryPrivate::convert(const Value &value, const Unit &toUnit) { qreal v = value.unit().toDefault(value.number()); return Value(toUnit.fromDefault(v), toUnit); } UnitCategory::UnitCategory() - : d(0) + : d(nullptr) { } UnitCategory::UnitCategory(UnitCategoryPrivate *dd) : d(dd) { } UnitCategory::UnitCategory(const UnitCategory &other) : d(other.d) { } UnitCategory::~UnitCategory() { } UnitCategory &UnitCategory::operator=(const UnitCategory &other) { d = other.d; return *this; } bool UnitCategory::operator==(const UnitCategory &other) const { if (d && other.d) return (*d == *other.d); else return (d == other.d); } bool UnitCategory::operator!=(const UnitCategory &other) const { if (d && other.d) return (*d != *other.d); else return (d != other.d); } bool UnitCategory::isNull() const { return !d; } CategoryId UnitCategory::id() const { if (d) return d->m_id; return InvalidCategory; } QList UnitCategory::units() const { if (d) return d->m_units; return QList(); } QList UnitCategory::mostCommonUnits() const { if (d) return d->m_mostCommonUnits; return QList(); } QStringList UnitCategory::allUnits() const { if (d) return d->m_unitMap.keys(); return QStringList(); } bool UnitCategory::hasUnit(const QString &unit) const { if (d) return d->m_unitMap.contains(unit); return false; } Value UnitCategory::convert(const Value &value, const QString &toUnit) { if (d && (toUnit.isEmpty() || d->m_unitMap.contains(toUnit)) && value.unit().isValid()) { Unit to = toUnit.isEmpty() ? defaultUnit() : d->m_unitMap[toUnit]; return convert(value, to); } return Value(); } Value UnitCategory::convert(const Value &value, UnitId toUnit) { if (d && d->m_idMap.contains(toUnit) && value.unit().isValid()) { return convert(value, d->m_idMap[toUnit]); } return Value(); } Value UnitCategory::convert(const Value &value, const Unit &toUnit) { if (d && !toUnit.isNull()) { return d->convert(value, toUnit); } return Value(); } Unit UnitCategory::unit(const QString &s) const { if (d) return d->m_unitMap.value(s); return Unit(); } Unit UnitCategory::unit(UnitId unitId) const { if (d && d->m_idMap.contains(unitId)) { return d->m_idMap[unitId]; } return Unit(); } QString UnitCategory::name() const { if (d) return d->m_name; return QString(); } Unit UnitCategory::defaultUnit() const { if (d) return d->m_defaultUnit; return Unit(); } QString UnitCategory::description() const { if (d) return d->m_description; return QString(); } void UnitCategory::addDefaultUnit(const Unit &unit) { if (d) { addCommonUnit(unit); d->m_defaultUnit = unit; } } void UnitCategory::addCommonUnit(const Unit &unit) { if (d) { addUnit(unit); d->m_mostCommonUnits.append(unit); } } void UnitCategory::addUnit(const Unit &unit) { if (d) { unit.d->m_category = *this; const QStringList list = unit.d->m_matchString.split(';'); foreach (const QString &name, list) d->m_unitMap[name] = unit; d->m_idMap[unit.id()] = unit; d->m_units.append(unit); } } } diff --git a/src/value.cpp b/src/value.cpp index a283be7..c2436b7 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1,221 +1,221 @@ /* * Copyright (C) 2007-2009 Petri Damstén * Copyright (C) 2014 John Layt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "value.h" #include "converter.h" #include namespace KUnitConversion { class ValuePrivate : public QSharedData { public: ValuePrivate() : m_number(0) { } ValuePrivate(qreal number, UnitId unitId = InvalidUnit) : m_number(number) { m_unit = m_converter.unit(unitId); } ValuePrivate(qreal number, const Unit &unit) : m_number(number), m_unit(unit) { } ValuePrivate(qreal number, const QString &unitString) : m_number(number) { m_unit = m_converter.unit(unitString); } ValuePrivate(const ValuePrivate &other) : QSharedData(other), m_number(other.m_number), m_unit(other.m_unit) { } virtual ~ValuePrivate() { } ValuePrivate &operator=(const ValuePrivate &other) { m_number = other.m_number; m_unit = other.m_unit; return *this; } ValuePrivate *clone() { return new ValuePrivate(*this); } bool operator==(const ValuePrivate &other) const { return (m_number == other.m_number && m_unit == other.m_unit); } bool operator!=(const ValuePrivate &other) const { return !(*this == other); } qreal m_number; Unit m_unit; Converter m_converter; }; Value::Value() - : d(0) + : d(nullptr) { } Value::Value(const Value &other) : d(other.d) { } Value::Value(qreal number, const Unit &unit) : d(new ValuePrivate(number, unit)) { } Value::Value(qreal number, const QString &unitString) : d(new ValuePrivate(number, unitString)) { } Value::Value(qreal number, UnitId unitId) : d(new ValuePrivate(number, unitId)) { } Value::Value(const QVariant &number, const QString &unitString) : d(new ValuePrivate(number.toReal(), unitString)) { } Value::~Value() { } Value &Value::operator=(const Value &other) { d = other.d; return *this; } bool Value::operator==(const Value &other) const { if (d && other.d) return (*d == *other.d); else return (d == other.d); } bool Value::operator!=(const Value &other) const { if (d && other.d) return (*d != *other.d); else return (d != other.d); } bool Value::isNull() const { return !d; } bool Value::isValid() const { return (d && d->m_unit.isValid() && !qIsNaN(d->m_number)); } qreal Value::number() const { if (d) return d->m_number; return 0; } Unit Value::unit() const { if (d) return d->m_unit; return Unit(); } QString Value::toString(int fieldWidth, char format, int precision, const QChar &fillChar) const { if (isValid()) return d->m_unit.toString(d->m_number, fieldWidth, format, precision, fillChar); return QString(); } QString Value::toSymbolString(int fieldWidth, char format, int precision, const QChar &fillChar) const { if (isValid()) return d->m_unit.toSymbolString(d->m_number, fieldWidth, format, precision, fillChar); return QString(); } Value &Value::round(uint decimals) { if (!isValid()) return *this; uint div = qPow(10, decimals); qreal add = 0.5 / (qreal)div; d->m_number = (int)((d->m_number + add) * div) / (qreal)div; return *this; } Value Value::convertTo(const Unit &unit) const { if (d) return d->m_converter.convert(*this, unit); return Value(); } Value Value::convertTo(UnitId unitId) const { if (d) return d->m_converter.convert(*this, unitId); return Value(); } Value Value::convertTo(const QString &unitString) const { if (d) return d->m_converter.convert(*this, unitString); return Value(); } }