diff --git a/src/common/KReportDesign_p.h b/src/common/KReportDesign_p.h --- a/src/common/KReportDesign_p.h +++ b/src/common/KReportDesign_p.h @@ -32,13 +32,14 @@ class QDomElement; class KReportPluginInterface; -static const bool DEFAULT_SHOW_GRID = true; -static const bool DEFAULT_SNAP_TO_GRID = true; -static const int DEFAULT_GRID_DIVISIONS = 4; -static const KReportUnit DEFAULT_UNIT = KReportUnit(KReportUnit::Centimeter); -static const int DEFAULT_PAGE_MARGIN = CM_TO_POINT(1.0); -static const QPageSize::PageSizeId DEFAULT_PAGE_SIZE = QPageSize::A4; -static const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION = QPageLayout::Landscape; +extern const bool DEFAULT_SHOW_GRID; +extern const bool DEFAULT_SNAP_TO_GRID; +extern const int DEFAULT_GRID_DIVISIONS; +extern const KReportUnit::Type DEFAULT_UNIT_TYPE; +extern const KReportUnit DEFAULT_UNIT; +extern const int DEFAULT_PAGE_MARGIN; +extern const QPageSize::PageSizeId DEFAULT_PAGE_SIZE; +extern const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION; class Q_DECL_HIDDEN KReportDesign::Private { diff --git a/src/common/KReportDesign_p.cpp b/src/common/KReportDesign_p.cpp --- a/src/common/KReportDesign_p.cpp +++ b/src/common/KReportDesign_p.cpp @@ -28,6 +28,15 @@ #include #include +const bool DEFAULT_SHOW_GRID = true; +const bool DEFAULT_SNAP_TO_GRID = true; +const int DEFAULT_GRID_DIVISIONS = 4; +const KReportUnit::Type DEFAULT_UNIT_TYPE = KReportUnit::Type::Centimeter; +const KReportUnit DEFAULT_UNIT(DEFAULT_UNIT_TYPE); +const int DEFAULT_PAGE_MARGIN = CM_TO_POINT(1.0); +const QPageSize::PageSizeId DEFAULT_PAGE_SIZE = QPageSize::A4; +const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION = QPageLayout::Landscape; + KReportDesign::Private::Private(KReportDesign *design) : q(design) , showGrid(DEFAULT_SHOW_GRID) @@ -345,9 +354,8 @@ snapToGrid = KReportUtils::attr(el, "report:grid-snap", DEFAULT_SNAP_TO_GRID); gridDivisions = KReportUtils::attr(el, "report:grid-divisions", DEFAULT_GRID_DIVISIONS); const QString pageUnitString = KReportUtils::attr(el, "report:page-unit", QString()); - bool found; - pageUnit = KReportUnit::fromSymbol(pageUnitString, &found); - if (!found) { + pageUnit = KReportUnit(KReportUnit::symbolToType(pageUnitString)); + if (!pageUnit.isValid()) { pageUnit = DEFAULT_UNIT; if (!pageUnitString.isEmpty()) { qWarning() << "Invalid page unit" << pageUnitString << "specified in" << name diff --git a/src/common/KReportItemBase.cpp b/src/common/KReportItemBase.cpp --- a/src/common/KReportItemBase.cpp +++ b/src/common/KReportItemBase.cpp @@ -101,7 +101,7 @@ { qDebug() << "Setting page unit to: " << u.symbol(); d->positionProperty->setOption("unit", u.symbol()); - d->sizeProperty->setOption("unit", u.symbol()); + d->sizeProperty->setOption("unit", u.symbol()); } int KReportItemBase::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset, diff --git a/src/common/KReportSectionData.cpp b/src/common/KReportSectionData.cpp --- a/src/common/KReportSectionData.cpp +++ b/src/common/KReportSectionData.cpp @@ -102,7 +102,7 @@ KReportDesigner::addMetaProperties(m_set, tr("Section", "Report section"), QLatin1String("kreport-section-element")); - m_height = new KProperty("height", KReportUnit(KReportUnit::Centimeter).fromUserValue(2.0), tr("Height")); + m_height = new KProperty("height", KReportUnit(KReportUnit::Type::Centimeter).fromUserValue(2.0), tr("Height")); m_backgroundColor = new KProperty("background-color", QColor(Qt::white), tr("Background Color")); m_height->setOption("unit", QLatin1String("cm")); if (!elemSource.isNull()) diff --git a/src/common/KReportUnit.h b/src/common/KReportUnit.h --- a/src/common/KReportUnit.h +++ b/src/common/KReportUnit.h @@ -4,6 +4,7 @@ Copyright (C) 2004, Nicolas GOUTTE Copyright (C) 2010 Thomas Zander Copyright 2012 Friedrich W. H. Kossebau + Copyright (C) 2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -26,14 +27,13 @@ #include // for floor +#include #include #include #include #include "kreport_export.h" -class QStringList; - // 1 inch ^= 72 pt // 1 inch ^= 25.399956 mm (-pedantic ;p) // 1 pt = 1/12 pi @@ -60,65 +60,93 @@ * When displaying a value to the user, the value is converted to the user's unit * of choice, and rounded to a reasonable precision to avoid 0.999999 * - * For implementing the selection of a unit type in the UI use the *ForUi() methods. - * They ensure the same order of the unit types in all places, with the order not - * bound to the order in the enum (so ABI-compatible extension is possible) and - * with the order and scope of listed types controlled by the @c ListOptions parameter. + * For implementing the selection of a unit type in the UI use the allTypes() method. + * it ensure the same order of the unit types in all places, with the order not + * bound to the order in the enum so ABI-compatible extension is possible. */ class KREPORT_EXPORT KReportUnit { + Q_DECLARE_TR_FUNCTIONS(KReportUnit) public: /** Length units supported by %KReport. */ - enum Type { - Millimeter = 0, - Point, ///< Postscript point, 1/72th of an Inco - Inch, + enum class Type { + Invalid, + Millimeter, Centimeter, Decimeter, + Inch, Pica, Cicero, + Point, ///< Postscript point, 1/72th of an Inco Pixel, - TypeCount ///< @internal + Last = Pixel ///< internal }; - /// Used to control the scope of the unit types listed in the UI - enum ListOption { - ListAll = 0, - HidePixel = 1, - HideMask = HidePixel - }; - Q_DECLARE_FLAGS(ListOptions, ListOption) + /** + * @brief Constructs invalid unit + * + * @since 3.1 + */ + KReportUnit(); + + /** Construct unit with given type and factor. */ + explicit KReportUnit(Type type, qreal factor = 1.0); - /** 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; + /** + * @brief Returns true if type of this unit is valid + * + * @since 3.1 + */ + bool isValid() const; + + /** + * @brief Returns list of all supported types (without Invalid) + * + * @since 3.1 + */ + static QList allTypes(); + + /** Returns the type of this unit */ KReportUnit::Type type() const; + /** + * @brief Returns (translated) description string for type of this unit + * + * @since 3.1 + */ + QString description() const; + + /** + * @brief Returns (translated) description string for given unit type + * + * @since 3.1 + */ + static QString description(KReportUnit::Type type); + + /** + * @brief Returns the list of (translated) description strings for given list of types + * + * @since 3.1 + */ + static QStringList descriptions(const QList &types); + 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 = nullptr); + qreal factor() const; /** * Prepare ptValue to be displayed in pt @@ -193,7 +221,6 @@ */ static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor = 1.0); - /** * This method is the one to use to display a value in a dialog * \return the value @p ptValue converted to unit and rounded, ready to be displayed @@ -222,34 +249,46 @@ /// @return the value converted to points for internal use qreal fromUserValue(const QString &value, bool *ok = nullptr) const; - /// Get the description string of the given unit - static QString unitDescription(KReportUnit::Type type); + //! Returns the symbol string of given unit type + //! Symbol for Invalid type is empty string. + static QString symbol(KReportUnit::Type type); - /// Get the symbol string of the unit + //! Returns the symbol string of the unit + //! Symbol for Invalid type is empty string. QString symbol() const; - /// Returns the list of unit types for the UI, controlled with the given @p listOptions. - static QStringList listOfUnitNameForUi(ListOptions listOptions = ListAll); + /** + * Equal to symbol(): returns the symbol string of the unit. + */ + inline QString toString() const + { + return symbol(); + } + + /** + * @brief Returns a unit symbol string to type + * + * @param symbol symbol to convert, must be lowercase + * @return Invalid type for unsupported symbol + * + * @since 3.1 + */ + static KReportUnit::Type symbolToType(const QString &symbol); - /// Get the index of this unit in the list of unit types for the UI, - /// if it is controlled with the given @p listOptions. - int indexInListForUi(ListOptions listOptions = ListAll) const; + /** + * @brief Returns the list of unit symbols for the given types + * + * @since 3.1 + */ + static QStringList symbols(const QList &types); /// Parses common %KReport and ODF values, like "10cm", "5mm" to pt. /// If no unit is specified, pt is assumed. static qreal parseValue(const QString &value, qreal defaultVal = 0.0); /// parse an angle to its value in degrees static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); - /** - * Equal to symbol(): returns the symbol string of the unit. - */ - inline QString toString() const - { - return symbol(); - } - private: class Private; Private * const d; @@ -260,6 +299,5 @@ #endif Q_DECLARE_METATYPE(KReportUnit) -Q_DECLARE_OPERATORS_FOR_FLAGS(KReportUnit::ListOptions) #endif diff --git a/src/common/KReportUnit.cpp b/src/common/KReportUnit.cpp --- a/src/common/KReportUnit.cpp +++ b/src/common/KReportUnit.cpp @@ -2,6 +2,7 @@ Copyright (C) 2001 David Faure Copyright (C) 2004, Nicolas GOUTTE Copyright 2012 Friedrich W. H. Kossebau + Copyright (C) 2017 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -30,24 +31,54 @@ class Q_DECL_HIDDEN KReportUnit::Private { public: - Type type; + KReportUnit::Type type; qreal pixelConversion; }; -// ensure the same order as in KReportUnit::Unit -static const char* const unitNameList[KReportUnit::TypeCount] = +// unit types +// Note: ensure the same order as in KReportUnit::Unit +static QList s_unitTypes = { + KReportUnit::Type::Millimeter, + KReportUnit::Type::Centimeter, + KReportUnit::Type::Decimeter, + KReportUnit::Type::Inch, + KReportUnit::Type::Pica, + KReportUnit::Type::Cicero, + KReportUnit::Type::Point, + KReportUnit::Type::Pixel +}; +static int firstUnitIndex() +{ + return static_cast(KReportUnit::Type::Invalid) + 1; +} + +static int lastUnitIndex() { + return static_cast(KReportUnit::Type::Last); // without Invalid +} + +// unit symbols +// Note: ensure the same order as in KReportUnit::Unit +static const char* const s_unitSymbols[] = +{ + nullptr, "mm", - "pt", - "in", "cm", "dm", + "in", "pi", "cc", + "pt", "px" }; -KReportUnit::KReportUnit(Type type, qreal factor) : d(new Private) +KReportUnit::KReportUnit() : d(new Private) +{ + d->type = Type::Invalid; + d->pixelConversion = 1.0; +} + +KReportUnit::KReportUnit(Type type, qreal factor) : d(new Private) { d->type = type; d->pixelConversion = factor; @@ -67,7 +98,7 @@ bool KReportUnit::operator==(const KReportUnit& other) const { return d->type == other.d->type && - (d->type != Pixel || + (d->type != Type::Pixel || qFuzzyCompare(d->pixelConversion, other.d->pixelConversion)); } @@ -91,139 +122,99 @@ return *this; } -QString KReportUnit::unitDescription(KReportUnit::Type type) +//static +QList KReportUnit::allTypes() { - switch (type) { - case KReportUnit::Millimeter: - return QCoreApplication::translate("KReportUnit", "Millimeters (mm)"); - case KReportUnit::Centimeter: - return QCoreApplication::translate("KReportUnit", "Centimeters (cm)"); - case KReportUnit::Decimeter: - return QCoreApplication::translate("KReportUnit", "Decimeters (dm)"); - case KReportUnit::Inch: - return QCoreApplication::translate("KReportUnit", "Inches (in)"); - case KReportUnit::Pica: - return QCoreApplication::translate("KReportUnit", "Pica (pi)"); - case KReportUnit::Cicero: - return QCoreApplication::translate("KReportUnit", "Cicero (cc)"); - case KReportUnit::Point: - return QCoreApplication::translate("KReportUnit", "Points (pt)"); - case KReportUnit::Pixel: - return QCoreApplication::translate("KReportUnit", "Pixels (px)"); - default: - return QCoreApplication::translate("KReportUnit", "Unsupported unit"); - } + return s_unitTypes; } -// grouped by units which are similar -static const KReportUnit::Type typesInUi[KReportUnit::TypeCount] = -{ - KReportUnit::Millimeter, - KReportUnit::Centimeter, - KReportUnit::Decimeter, - KReportUnit::Inch, - KReportUnit::Pica, - KReportUnit::Cicero, - KReportUnit::Point, - KReportUnit::Pixel, -}; - -QStringList KReportUnit::listOfUnitNameForUi(ListOptions listOptions) +//static +QString KReportUnit::description(KReportUnit::Type type) { - QStringList lst; - for (int i = 0; i < KReportUnit::TypeCount; ++i) { - const Type type = typesInUi[i]; - if ((type != Pixel) || ((listOptions & HideMask) == ListAll)) - lst.append(unitDescription(type)); + switch (type) { + case KReportUnit::Type::Invalid: + return tr("Invalid"); + case KReportUnit::Type::Millimeter: + return tr("Millimeters (mm)"); + case KReportUnit::Type::Centimeter: + return tr("Centimeters (cm)"); + case KReportUnit::Type::Decimeter: + return tr("Decimeters (dm)"); + case KReportUnit::Type::Inch: + return tr("Inches (in)"); + case KReportUnit::Type::Pica: + return tr("Pica (pi)"); + case KReportUnit::Type::Cicero: + return tr("Cicero (cc)"); + case KReportUnit::Type::Point: + return tr("Points (pt)"); + case KReportUnit::Type::Pixel: + return tr("Pixels (px)"); + default: + return tr("Unsupported unit"); } - return lst; } -KReportUnit KReportUnit::fromListForUi(int index, ListOptions listOptions, qreal factor) +QString KReportUnit::description() const { - KReportUnit::Type type = KReportUnit::Point; - - if ((0 <= index) && (index < KReportUnit::TypeCount)) { - // iterate through all enums and skip the Pixel enum if needed - for (int i = 0; i < KReportUnit::TypeCount; ++i) { - if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { - ++index; - continue; - } - if (i == index) { - type = typesInUi[i]; - break; - } - } - } - - return KReportUnit(type, factor); + return KReportUnit::description(type()); } -int KReportUnit::indexInListForUi(ListOptions listOptions) const +QStringList KReportUnit::descriptions(const QList &types) { - if ((listOptions&HidePixel) && (d->type == Pixel)) { - return -1; + QStringList result; + for (Type t : types) { + result.append(description(t)); } - - int result = -1; - - int skipped = 0; - for (int i = 0; i < KReportUnit::TypeCount; ++i) { - if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { - ++skipped; - continue; - } - if (typesInUi[i] == d->type) { - result = i - skipped; - break; - } - } - return result; } qreal KReportUnit::toUserValue(qreal ptValue) const { switch (d->type) { - case Millimeter: + case Type::Invalid: + kreportWarning() << "Conversion for Invalid type not supported"; + return -1.0; + case Type::Millimeter: return toMillimeter(ptValue); - case Centimeter: + case Type::Centimeter: return toCentimeter(ptValue); - case Decimeter: + case Type::Decimeter: return toDecimeter(ptValue); - case Inch: + case Type::Inch: return toInch(ptValue); - case Pica: + case Type::Pica: return toPica(ptValue); - case Cicero: + case Type::Cicero: return toCicero(ptValue); - case Pixel: + case Type::Pixel: return ptValue * d->pixelConversion; - case Point: + case Type::Point: default: return toPoint(ptValue); } } qreal KReportUnit::ptToUnit(qreal ptValue, const KReportUnit &unit) { switch (unit.d->type) { - case Millimeter: + case Type::Invalid: + return -1.0; + case Type::Millimeter: return POINT_TO_MM(ptValue); - case Centimeter: + case Type::Centimeter: return POINT_TO_CM(ptValue); - case Decimeter: + case Type::Decimeter: return POINT_TO_DM(ptValue); - case Inch: + case Type::Inch: return POINT_TO_INCH(ptValue); - case Pica: + case Type::Pica: return POINT_TO_PI(ptValue); - case Cicero: + case Type::Cicero: return POINT_TO_CC(ptValue); - case Pixel: + case Type::Pixel: return ptValue * unit.d->pixelConversion; - case Point: + case Type::Point: default: return ptValue; } @@ -237,28 +228,37 @@ qreal KReportUnit::fromUserValue(qreal value) const { switch (d->type) { - case Millimeter: + case Type::Invalid: + return -1.0; + case Type::Millimeter: return MM_TO_POINT(value); - case Centimeter: + case Type::Centimeter: return CM_TO_POINT(value); - case Decimeter: + case Type::Decimeter: return DM_TO_POINT(value); - case Inch: + case Type::Inch: return INCH_TO_POINT(value); - case Pica: + case Type::Pica: return PI_TO_POINT(value); - case Cicero: + case Type::Cicero: return CC_TO_POINT(value); - case Pixel: + case Type::Pixel: return value / d->pixelConversion; - case Point: + case Type::Point: default: return value; } } qreal KReportUnit::fromUserValue(const QString &value, bool *ok) const { + if (d->type == Type::Invalid) { + kreportWarning() << "Conversion from Invalid type not supported"; + if (ok) { + *ok = false; + } + return -1.0; + } return fromUserValue(QLocale::system().toDouble(value, ok)); } @@ -293,9 +293,10 @@ if (symbol == "pt" || symbol.isEmpty()) return val; - KReportUnit u = KReportUnit::fromSymbol(QLatin1String(symbol), &ok); - if (ok) + KReportUnit u(KReportUnit::symbolToType(QLatin1String(symbol))); + if (u.isValid()) { return u.fromUserValue(val); + } if (symbol == "m") return DM_TO_POINT(val * 10.0); @@ -307,85 +308,98 @@ return defaultVal; } -KReportUnit KReportUnit::fromSymbol(const QString &symbol, bool *ok) +//static +QString KReportUnit::symbol(KReportUnit::Type type) { - Type result = Point; + return QLatin1String(s_unitSymbols[static_cast(type)]); +} + +//static +KReportUnit::Type KReportUnit::symbolToType(const QString &symbol) +{ + Type result = Type::Invalid; if (symbol == QLatin1String("inch") /*compat*/) { - result = Inch; - if (ok) - *ok = true; + result = Type::Inch; } else { - if (ok) - *ok = false; - - for (int i = 0; i < TypeCount; ++i) { - if (symbol == QLatin1String(unitNameList[i])) { + for (int i = firstUnitIndex(); i <= lastUnitIndex(); ++i) { + if (symbol == QLatin1String(s_unitSymbols[i])) { result = static_cast(i); - if (ok) - *ok = true; + break; } } } + return result; +} - return KReportUnit(result); +//static +QStringList KReportUnit::symbols(const QList &types) +{ + QStringList result; + for (Type t : types) { + result.append(symbol(t)); + } + return result; } qreal KReportUnit::convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor) { qreal pt; switch (fromUnit.type()) { - case Millimeter: + case Type::Invalid: + pt = -1.0; + break; + case Type::Millimeter: pt = MM_TO_POINT(value); break; - case Centimeter: + case Type::Centimeter: pt = CM_TO_POINT(value); break; - case Decimeter: + case Type::Decimeter: pt = DM_TO_POINT(value); break; - case Inch: + case Type::Inch: pt = INCH_TO_POINT(value); break; - case Pica: + case Type::Pica: pt = PI_TO_POINT(value); break; - case Cicero: + case Type::Cicero: pt = CC_TO_POINT(value); break; - case Pixel: + case Type::Pixel: pt = value / factor; break; - case Point: + case Type::Point: default: pt = value; } switch (toUnit.type()) { - case Millimeter: + case Type::Millimeter: return POINT_TO_MM(pt); - case Centimeter: + case Type::Centimeter: return POINT_TO_CM(pt); - case Decimeter: + case Type::Decimeter: return POINT_TO_DM(pt); - case Inch: + case Type::Inch: return POINT_TO_INCH(pt); - case Pica: + case Type::Pica: return POINT_TO_PI(pt); - case Cicero: + case Type::Cicero: return POINT_TO_CC(pt); - case Pixel: + case Type::Pixel: return pt * factor; - case Point: + case Type::Invalid: + case Type::Point: default: return pt; } - } QString KReportUnit::symbol() const { - return QLatin1String(unitNameList[d->type]); + return QLatin1String(s_unitSymbols[static_cast(d->type)]); } qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) @@ -427,12 +441,15 @@ QDebug operator<<(QDebug debug, const KReportUnit &unit) { #ifndef NDEBUG - debug.nospace() << unit.symbol(); + if (unit.isValid()) { + debug.nospace() << QString::fromLatin1("Unit(%1, %2)").arg(unit.symbol()).arg(unit.factor()); + } else { + debug.nospace() << QString::fromLatin1("Unit(Invalid)"); + } #else Q_UNUSED(unit); #endif return debug.space(); - } void KReportUnit::setFactor(qreal factor) @@ -450,4 +467,9 @@ return d->type; } +bool KReportUnit::isValid() const +{ + return d->type != KReportUnit::Type::Invalid; +} + #endif diff --git a/src/wrtembed/KReportDesigner.cpp b/src/wrtembed/KReportDesigner.cpp --- a/src/wrtembed/KReportDesigner.cpp +++ b/src/wrtembed/KReportDesigner.cpp @@ -18,23 +18,24 @@ */ #include "KReportDesigner.h" +#include "KReportDesign_p.h" +#include "KReportDesignerItemLine.h" #include "KReportDesignerSection.h" +#include "KReportDesignerSectionDetail.h" +#include "KReportDesignerSectionDetailGroup.h" #include "KReportDesignerSectionScene.h" #include "KReportDesignerSectionView.h" -#include "KReportDesignerSectionDetailGroup.h" -#include "KReportPropertiesButton.h" -#include "KReportSectionEditor.h" -#include "KReportDesignerSectionDetail.h" -#include "KReportDesignerItemLine.h" -#include "KReportRuler_p.h" -#include "KReportZoomHandler_p.h" #include "KReportPageSize.h" -#include "KReportUtils_p.h" -#include "KReportUtils.h" #include "KReportPluginInterface.h" #include "KReportPluginManager.h" -#include "KReportSection.h" #include "KReportPluginMetaData.h" +#include "KReportPropertiesButton.h" +#include "KReportRuler_p.h" +#include "KReportSection.h" +#include "KReportSectionEditor.h" +#include "KReportUtils.h" +#include "KReportUtils_p.h" +#include "KReportZoomHandler_p.h" #include "kreport_debug.h" #ifdef KREPORT_SCRIPTING #include "KReportScriptSource.h" @@ -215,8 +216,6 @@ d->pageButton = new KReportPropertiesButton(this); - d->hruler->setUnit(KReportUnit(KReportUnit::Centimeter)); - d->grid->addWidget(d->pageButton, 0, 0); d->grid->addWidget(d->hruler, 0, 1); d->grid->addLayout(d->vboxlayout, 1, 0, 1, 2); @@ -737,28 +736,22 @@ d->orientation = new KProperty("print-orientation", listData, QLatin1String("portrait"), tr("Page Orientation")); - QStringList keys; - QStringList strings = KReportUnit::listOfUnitNameForUi(KReportUnit::HidePixel); - QString unit; - foreach(const QString &un, strings) { - unit = un.mid(un.indexOf(QLatin1String("(")) + 1, 2); - keys << unit; - } - - listData = new KPropertyListData(keys, strings); + QList types(KReportUnit::allTypes()); + types.removeOne(KReportUnit::Type::Pixel); + listData = new KPropertyListData(KReportUnit::symbols(types), KReportUnit::descriptions(types)); d->unit = new KProperty("page-unit", listData, QLatin1String("cm"), tr("Page Unit")); d->showGrid = new KProperty("grid-visible", true, tr("Show Grid")); d->gridSnap = new KProperty("grid-snap", true, tr("Snap to Grid")); d->gridDivisions = new KProperty("grid-divisions", 4, tr("Grid Divisions")); - d->leftMargin = new KProperty("margin-left", KReportUnit(KReportUnit::Centimeter).fromUserValue(1.0), + d->leftMargin = new KProperty("margin-left", KReportUnit(KReportUnit::Type::Centimeter).fromUserValue(1.0), tr("Left Margin"), tr("Left Margin"), KProperty::Double); - d->rightMargin = new KProperty("margin-right", KReportUnit(KReportUnit::Centimeter).fromUserValue(1.0), + d->rightMargin = new KProperty("margin-right", KReportUnit(KReportUnit::Type::Centimeter).fromUserValue(1.0), tr("Right Margin"), tr("Right Margin"), KProperty::Double); - d->topMargin = new KProperty("margin-top", KReportUnit(KReportUnit::Centimeter).fromUserValue(1.0), + d->topMargin = new KProperty("margin-top", KReportUnit(KReportUnit::Type::Centimeter).fromUserValue(1.0), tr("Top Margin"), tr("Top Margin"), KProperty::Double); - d->bottomMargin = new KProperty("margin-bottom", KReportUnit(KReportUnit::Centimeter).fromUserValue(1.0), + d->bottomMargin = new KProperty("margin-bottom", KReportUnit(KReportUnit::Type::Centimeter).fromUserValue(1.0), tr("Bottom Margin"), tr("Bottom Margin"), KProperty::Double); d->leftMargin->setOption("unit", QLatin1String("cm")); d->rightMargin->setOption("unit", QLatin1String("cm")); @@ -903,17 +896,9 @@ KReportUnit KReportDesigner::pageUnit() const { - QString u; - bool found; - - u = d->unit->value().toString(); - - KReportUnit unit = KReportUnit::fromSymbol(u, &found); - if (!found) { - unit = KReportUnit(KReportUnit::Centimeter); - } - - return unit; + const QString symbol = d->unit->value().toString(); + KReportUnit unit(KReportUnit::symbolToType(symbol)); + return unit.isValid() ? unit : DEFAULT_UNIT; } void KReportDesigner::setGridOptions(bool vis, int div) diff --git a/src/wrtembed/KReportDesignerSectionScene.cpp b/src/wrtembed/KReportDesignerSectionScene.cpp --- a/src/wrtembed/KReportDesignerSectionScene.cpp +++ b/src/wrtembed/KReportDesignerSectionScene.cpp @@ -36,12 +36,12 @@ if (m_unit.type() != m_rd->pageUnit().type()) { m_unit = m_rd->pageUnit(); - if (m_unit.type() == KReportUnit::Cicero || - m_unit.type() == KReportUnit::Pica || - m_unit.type() == KReportUnit::Millimeter) { + if (m_unit.type() == KReportUnit::Type::Cicero || + m_unit.type() == KReportUnit::Type::Pica || + m_unit.type() == KReportUnit::Type::Millimeter) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY; - } else if (m_unit.type() == KReportUnit::Point) { + } else if (m_unit.type() == KReportUnit::Type::Point) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY; } else { @@ -64,12 +64,12 @@ if (m_rd->propertySet()->property("grid-visible").value().toBool()) { if (m_unit.type() != m_rd->pageUnit().type()) { m_unit = m_rd->pageUnit(); - if (m_unit.type() == KReportUnit::Cicero || - m_unit.type() == KReportUnit::Pica || - m_unit.type() == KReportUnit::Millimeter) { + if (m_unit.type() == KReportUnit::Type::Cicero || + m_unit.type() == KReportUnit::Type::Pica || + m_unit.type() == KReportUnit::Type::Millimeter) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY; - } else if (m_unit.type() == KReportUnit::Point) { + } else if (m_unit.type() == KReportUnit::Type::Point) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY; } else { @@ -155,12 +155,12 @@ //! @todo Again? Copy&Paste error? if (m_unit.type() != m_rd->pageUnit().type()) { m_unit = m_rd->pageUnit(); - if (m_unit.type() == KReportUnit::Cicero || - m_unit.type() == KReportUnit::Pica || - m_unit.type() == KReportUnit::Millimeter) { + if (m_unit.type() == KReportUnit::Type::Cicero || + m_unit.type() == KReportUnit::Type::Pica || + m_unit.type() == KReportUnit::Type::Millimeter) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY; - } else if (m_unit.type() == KReportUnit::Point) { + } else if (m_unit.type() == KReportUnit::Type::Point) { m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX; m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY; } else { diff --git a/src/wrtembed/KReportRuler_p.h b/src/wrtembed/KReportRuler_p.h --- a/src/wrtembed/KReportRuler_p.h +++ b/src/wrtembed/KReportRuler_p.h @@ -55,6 +55,7 @@ }; /// The ruler's unit + /// Default is Centimeter. KReportUnit unit() const; /// The length of the ruler in points (pt) diff --git a/src/wrtembed/KReportRuler_p.cpp b/src/wrtembed/KReportRuler_p.cpp --- a/src/wrtembed/KReportRuler_p.cpp +++ b/src/wrtembed/KReportRuler_p.cpp @@ -22,6 +22,7 @@ */ #include "KReportRuler_p.h" +#include "KReportDesign_p.h" #include "KReportZoomHandler_p.h" #include @@ -867,7 +868,7 @@ KReportRuler::Private::Private(KReportRuler *parent, const KReportZoomHandler &zoomHandler, Qt::Orientation o) - : unit(KReportUnit(KReportUnit::Point)), + : unit(DEFAULT_UNIT), orientation(o), viewConverter(&zoomHandler), offset(0), @@ -912,15 +913,15 @@ qreal KReportRuler::Private::numberStepForUnit() const { switch(unit.type()) { - case KReportUnit::Inch: - case KReportUnit::Centimeter: - case KReportUnit::Decimeter: - case KReportUnit::Millimeter: + case KReportUnit::Type::Inch: + case KReportUnit::Type::Centimeter: + case KReportUnit::Type::Decimeter: + case KReportUnit::Type::Millimeter: return 1.0; - case KReportUnit::Pica: - case KReportUnit::Cicero: + case KReportUnit::Type::Pica: + case KReportUnit::Type::Cicero: return 10.0; - case KReportUnit::Point: + case KReportUnit::Type::Point: default: return 100.0; }