diff --git a/src/common/KReportItemBase.h b/src/common/KReportItemBase.h --- a/src/common/KReportItemBase.h +++ b/src/common/KReportItemBase.h @@ -148,6 +148,7 @@ Q_SLOT virtual void propertyChanged(KPropertySet &s, KProperty &p); private: + Q_DISABLE_COPY(KReportItemBase) class Private; Private * const d; }; diff --git a/src/common/KReportUnit.h b/src/common/KReportUnit.h --- a/src/common/KReportUnit.h +++ b/src/common/KReportUnit.h @@ -89,98 +89,101 @@ }; Q_DECLARE_FLAGS(ListOptions, ListOption) + /** Construction requires initialization. The factor is for variable factor units like pixel */ + explicit KReportUnit(Type type = Point, qreal factor = 1.0); + + KReportUnit(const KReportUnit &other); + + ~KReportUnit(); + + /// Assigns specified type and factor 1.0 to the object + /// @param unit Type of unit + KReportUnit& operator=(Type type); + + KReportUnit& operator=(const KReportUnit& other); + + bool operator!=(const KReportUnit &other) const; + + bool operator==(const KReportUnit &other) const; + + KReportUnit::Type type() const; + + void setFactor(qreal factor); + + qreal factor() const; + /** Returns a KReportUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ static KReportUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); /// Convert a unit symbol string into a KReportUnit /// @param symbol symbol to convert /// @param ok if set, it will be true if the unit was known, false if unknown static KReportUnit fromSymbol(const QString &symbol, bool *ok = 0); - /** Construction requires initialization. The factor is for variable factor units like pixel */ - explicit KReportUnit(Type unit = Point, qreal factor = 1.0) { - m_type = unit; - m_pixelConversion = factor; - } - - KReportUnit& operator=(Type unit) { - m_type = unit; m_pixelConversion = 1.0; return *this; - } - - bool operator!=(const KReportUnit &other) const { - return !operator==(other); - } - - bool operator==(const KReportUnit &other) const { - return m_type == other.m_type && - (m_type != Pixel || - qFuzzyCompare(m_pixelConversion, other.m_pixelConversion)); - } - - KReportUnit::Type type() const { - return m_type; - } - - void setFactor(qreal factor) { - m_pixelConversion = factor; - } /** * Prepare ptValue to be displayed in pt * This method will round to 0.001 precision */ - static qreal toPoint(qreal ptValue) { + static inline qreal toPoint(qreal ptValue) + { // No conversion, only rounding (to 0.001 precision) return floor(ptValue * 1000.0) / 1000.0; } /** * Prepare ptValue to be displayed in mm * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. */ - static qreal toMillimeter(qreal ptValue) { + static inline qreal toMillimeter(qreal ptValue) + { // "mm" values are rounded to 0.0001 millimeters return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; } /** * Prepare ptValue to be displayed in cm * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. */ - static qreal toCentimeter(qreal ptValue) { + static inline qreal toCentimeter(qreal ptValue) + { return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; } /** * Prepare ptValue to be displayed in dm * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. */ - static qreal toDecimeter(qreal ptValue) { + static inline qreal toDecimeter(qreal ptValue) + { return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; } /** * Prepare ptValue to be displayed in inch * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. */ - static qreal toInch(qreal ptValue) { + static inline qreal toInch(qreal ptValue) + { // "in" values are rounded to 0.00001 inches return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; } /** * Prepare ptValue to be displayed in pica * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. */ - static qreal toPica(qreal ptValue) { + static inline qreal toPica(qreal ptValue) + { // "pi" values are rounded to 0.00001 inches return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; } /** * Prepare ptValue to be displayed in cicero * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. */ - static qreal toCicero(qreal ptValue) { + static inline qreal toCicero(qreal ptValue) + { // "cc" values are rounded to 0.00001 inches return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; } @@ -242,13 +245,14 @@ /** * Equal to symbol(): returns the symbol string of the unit. */ - inline QString toString() const { + inline QString toString() const + { return symbol(); } - + private: - Type m_type; - qreal m_pixelConversion; + class Private; + Private * const d; }; #ifndef QT_NO_DEBUG_STREAM diff --git a/src/common/KReportUnit.cpp b/src/common/KReportUnit.cpp --- a/src/common/KReportUnit.cpp +++ b/src/common/KReportUnit.cpp @@ -27,6 +27,13 @@ #include #include +class Q_DECL_HIDDEN KReportUnit::Private +{ +public: + Type type; + qreal pixelConversion; +}; + // ensure the same order as in KReportUnit::Unit static const char* const unitNameList[KReportUnit::TypeCount] = { @@ -40,6 +47,50 @@ "px" }; +KReportUnit::KReportUnit(Type type, qreal factor) : d(new Private) +{ + d->type = type; + d->pixelConversion = factor; +} + +KReportUnit::KReportUnit(const KReportUnit& other) : d(new Private) +{ + d->type = other.type(); + d->pixelConversion = other.factor(); +} + +KReportUnit::~KReportUnit() +{ + delete d; +} + +bool KReportUnit::operator==(const KReportUnit& other) const +{ + return d->type == other.d->type && + (d->type != Pixel || + qFuzzyCompare(d->pixelConversion, other.d->pixelConversion)); +} + +bool KReportUnit::operator!=(const KReportUnit& other) const +{ + return !operator==(other); +} + + +KReportUnit& KReportUnit::operator=(Type type) +{ + d->type = type; + d->pixelConversion = 1.0; + return *this; +} + +KReportUnit & KReportUnit::operator=(const KReportUnit& other) +{ + d->type = other.type(); + d->pixelConversion = other.factor(); + return *this; +} + QString KReportUnit::unitDescription(KReportUnit::Type type) { switch (type) { @@ -111,7 +162,7 @@ int KReportUnit::indexInListForUi(ListOptions listOptions) const { - if ((listOptions&HidePixel) && (m_type == Pixel)) { + if ((listOptions&HidePixel) && (d->type == Pixel)) { return -1; } @@ -123,7 +174,7 @@ ++skipped; continue; } - if (typesInUi[i] == m_type) { + if (typesInUi[i] == d->type) { result = i - skipped; break; } @@ -134,7 +185,7 @@ qreal KReportUnit::toUserValue(qreal ptValue) const { - switch (m_type) { + switch (d->type) { case Millimeter: return toMillimeter(ptValue); case Centimeter: @@ -148,16 +199,16 @@ case Cicero: return toCicero(ptValue); case Pixel: - return ptValue * m_pixelConversion; + return ptValue * d->pixelConversion; case Point: default: return toPoint(ptValue); } } qreal KReportUnit::ptToUnit(qreal ptValue, const KReportUnit &unit) { - switch (unit.m_type) { + switch (unit.d->type) { case Millimeter: return POINT_TO_MM(ptValue); case Centimeter: @@ -171,7 +222,7 @@ case Cicero: return POINT_TO_CC(ptValue); case Pixel: - return ptValue * unit.m_pixelConversion; + return ptValue * unit.d->pixelConversion; case Point: default: return ptValue; @@ -185,7 +236,7 @@ qreal KReportUnit::fromUserValue(qreal value) const { - switch (m_type) { + switch (d->type) { case Millimeter: return MM_TO_POINT(value); case Centimeter: @@ -199,7 +250,7 @@ case Cicero: return CC_TO_POINT(value); case Pixel: - return value / m_pixelConversion; + return value / d->pixelConversion; case Point: default: return value; @@ -334,7 +385,7 @@ QString KReportUnit::symbol() const { - return QLatin1String(unitNameList[m_type]); + return QLatin1String(unitNameList[d->type]); } qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) @@ -383,4 +434,20 @@ return debug.space(); } + +void KReportUnit::setFactor(qreal factor) +{ + d->pixelConversion = factor; +} + +qreal KReportUnit::factor() const +{ + return d->pixelConversion; +} + +KReportUnit::Type KReportUnit::type() const +{ + return d->type; +} + #endif