diff --git a/libs/global/CMakeLists.txt b/libs/global/CMakeLists.txt index e8162c5e46..b5703b9c32 100644 --- a/libs/global/CMakeLists.txt +++ b/libs/global/CMakeLists.txt @@ -1,61 +1,62 @@ add_subdirectory( tests ) include(CheckFunctionExists) check_function_exists(backtrace HAVE_BACKTRACE) configure_file(config-debug.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-debug.h) option(HAVE_MEMORY_LEAK_TRACKER "Enable memory leak tracker (always disabled in release build)" OFF) option(HAVE_BACKTRACE_SUPPORT "Enable recording of backtrace in memory leak tracker" OFF) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-memory-leak-tracker.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-memory-leak-tracker.h) ### WRONG PLACE??? include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR} ) set(kritaglobal_LIB_SRCS kis_assert.cpp kis_debug.cpp kis_algebra_2d.cpp kis_memory_leak_tracker.cpp kis_shared.cpp kis_dom_utils.cpp kis_painting_tweaks.cpp KisHandlePainterHelper.cpp KisHandleStyle.cpp kis_signal_compressor.cpp kis_signal_compressor_with_param.cpp kis_thread_safe_signal_compressor.cpp kis_acyclic_signal_connector.cpp kis_latency_tracker.cpp KisQPainterStateSaver.cpp KisSharedThreadPoolAdapter.cpp KisSharedRunnable.cpp KisRollingMeanAccumulatorWrapper.cpp kis_config_notifier.cpp KisDeleteLaterWrapper.cpp KisUsageLogger.cpp KisFileUtils.cpp KisSignalMapper.cpp KisRegion.cpp + KoUnit.cpp ) add_library(kritaglobal SHARED ${kritaglobal_LIB_SRCS} ) generate_export_header(kritaglobal BASE_NAME kritaglobal) target_link_libraries(kritaglobal PUBLIC kritaversion Qt5::Concurrent Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Xml KF5::I18n ) set_target_properties(kritaglobal PROPERTIES VERSION ${GENERIC_KRITA_LIB_VERSION} SOVERSION ${GENERIC_KRITA_LIB_SOVERSION} ) install(TARGETS kritaglobal ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/libs/odf/KoUnit.cpp b/libs/global/KoUnit.cpp similarity index 98% rename from libs/odf/KoUnit.cpp rename to libs/global/KoUnit.cpp index 92938cdd3a..966803a973 100644 --- a/libs/odf/KoUnit.cpp +++ b/libs/global/KoUnit.cpp @@ -1,423 +1,421 @@ /* This file is part of the KDE project Copyright (C) 2001 David Faure Copyright (C) 2004, Nicolas GOUTTE Copyright 2012 Friedrich W. H. Kossebau This library 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 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KoUnit.h" #include #include #include -#include #include // ensure the same order as in KoUnit::Unit static const char* const unitNameList[KoUnit::TypeCount] = { "mm", "pt", "in", "cm", "dm", "pi", "cc", "px" }; QString KoUnit::unitDescription(KoUnit::Type type) { switch (type) { case KoUnit::Millimeter: return i18n("Millimeters (mm)"); case KoUnit::Centimeter: return i18n("Centimeters (cm)"); case KoUnit::Decimeter: return i18n("Decimeters (dm)"); case KoUnit::Inch: return i18n("Inches (in)"); case KoUnit::Pica: return i18n("Pica (pi)"); case KoUnit::Cicero: return i18n("Cicero (cc)"); case KoUnit::Point: return i18n("Points (pt)"); case KoUnit::Pixel: return i18n("Pixels (px)"); default: return i18n("Unsupported unit"); } } // grouped by units which are similar static const KoUnit::Type typesInUi[KoUnit::TypeCount] = { KoUnit::Millimeter, KoUnit::Centimeter, KoUnit::Decimeter, KoUnit::Inch, KoUnit::Pica, KoUnit::Cicero, KoUnit::Point, KoUnit::Pixel, }; QStringList KoUnit::listOfUnitNameForUi(ListOptions listOptions) { QStringList lst; for (int i = 0; i < KoUnit::TypeCount; ++i) { const Type type = typesInUi[i]; if ((type != Pixel) || ((listOptions & HideMask) == ListAll)) lst.append(unitDescription(type)); } return lst; } KoUnit KoUnit::fromListForUi(int index, ListOptions listOptions, qreal factor) { KoUnit::Type type = KoUnit::Point; if ((0 <= index) && (index < KoUnit::TypeCount)) { // iterate through all enums and skip the Pixel enum if needed for (int i = 0; i < KoUnit::TypeCount; ++i) { if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { ++index; continue; } if (i == index) { type = typesInUi[i]; break; } } } return KoUnit(type, factor); } int KoUnit::indexInListForUi(ListOptions listOptions) const { if ((listOptions&HidePixel) && (m_type == Pixel)) { return -1; } int result = -1; int skipped = 0; for (int i = 0; i < KoUnit::TypeCount; ++i) { if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { ++skipped; continue; } if (typesInUi[i] == m_type) { result = i - skipped; break; } } return result; } qreal KoUnit::toUserValueRounded(const qreal value) const { qreal userValue = toUserValuePrecise(value); qreal rounding = 1.0; switch (m_type) { case Pixel: return userValue; // no rounding for Pixel value case Millimeter: rounding = MM_ROUNDING; break; case Centimeter: rounding = CM_ROUNDING; break; case Decimeter: rounding = DM_ROUNDING; break; case Inch: rounding = IN_ROUNDING; break; case Pica: rounding = PI_ROUNDING; break; case Cicero: rounding = CC_ROUNDING; break; case Point: default: rounding = PT_ROUNDING; } return floor(userValue * rounding) / rounding; } qreal KoUnit::toUserValuePrecise(const qreal ptValue) const { switch (m_type) { case Millimeter: return POINT_TO_MM(ptValue); case Centimeter: return POINT_TO_CM(ptValue); case Decimeter: return POINT_TO_DM(ptValue); case Inch: return POINT_TO_INCH(ptValue); case Pica: return POINT_TO_PI(ptValue); case Cicero: return POINT_TO_CC(ptValue); case Pixel: return ptValue * m_pixelConversion; case Point: default: return ptValue; } } qreal KoUnit::toUserValue(qreal ptValue, bool rounding) const { if (rounding) { return toUserValueRounded(ptValue); } else { return toUserValuePrecise(ptValue); } } QString KoUnit::toUserStringValue(qreal ptValue) const { return QLocale().toString(toUserValue(ptValue)); } qreal KoUnit::fromUserValue(qreal value) const { switch (m_type) { case Millimeter: return MM_TO_POINT(value); case Centimeter: return CM_TO_POINT(value); case Decimeter: return DM_TO_POINT(value); case Inch: return INCH_TO_POINT(value); case Pica: return PI_TO_POINT(value); case Cicero: return CC_TO_POINT(value); case Pixel: return value / m_pixelConversion; case Point: default: return value; } } qreal KoUnit::fromUserValue(const QString &value, bool *ok) const { return fromUserValue(QLocale().toDouble(value, ok)); } qreal KoUnit::parseValue(const QString& _value, qreal defaultVal) { if (_value.isEmpty()) return defaultVal; QString value(_value.simplified()); value.remove(QLatin1Char(' ')); int firstLetter = -1; for (int i = 0; i < value.length(); ++i) { if (value.at(i).isLetter()) { if (value.at(i) == QLatin1Char('e')) continue; firstLetter = i; break; } } if (firstLetter == -1) return value.toDouble(); const QString symbol = value.mid(firstLetter); value.truncate(firstLetter); const qreal val = value.toDouble(); if (symbol == QLatin1String("pt")) return val; bool ok; KoUnit u = KoUnit::fromSymbol(symbol, &ok); if (ok) return u.fromUserValue(val); if (symbol == QLatin1String("m")) return DM_TO_POINT(val * 10.0); else if (symbol == QLatin1String("km")) return DM_TO_POINT(val * 10000.0); - warnOdf << "KoUnit::parseValue: Unit " << symbol << " is not supported, please report."; // TODO : add support for mi/ft ? return defaultVal; } KoUnit KoUnit::fromSymbol(const QString &symbol, bool *ok) { Type result = Point; if (symbol == QLatin1String("inch") /*compat*/) { result = Inch; if (ok) *ok = true; } else { if (ok) *ok = false; for (int i = 0; i < TypeCount; ++i) { if (symbol == QLatin1String(unitNameList[i])) { result = static_cast(i); if (ok) *ok = true; } } } return KoUnit(result); } qreal KoUnit::convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor) { qreal pt; switch (fromUnit.type()) { case Millimeter: pt = MM_TO_POINT(value); break; case Centimeter: pt = CM_TO_POINT(value); break; case Decimeter: pt = DM_TO_POINT(value); break; case Inch: pt = INCH_TO_POINT(value); break; case Pica: pt = PI_TO_POINT(value); break; case Cicero: pt = CC_TO_POINT(value); break; case Pixel: pt = value / factor; break; case Point: default: pt = value; } switch (toUnit.type()) { case Millimeter: return POINT_TO_MM(pt); case Centimeter: return POINT_TO_CM(pt); case Decimeter: return POINT_TO_DM(pt); case Inch: return POINT_TO_INCH(pt); case Pica: return POINT_TO_PI(pt); case Cicero: return POINT_TO_CC(pt); case Pixel: return pt * factor; case Point: default: return pt; } } QString KoUnit::symbol() const { return QLatin1String(unitNameList[m_type]); } qreal KoUnit::parseAngle(const QString& _value, qreal defaultVal) { if (_value.isEmpty()) return defaultVal; QString value(_value.simplified()); value.remove(QLatin1Char(' ')); int firstLetter = -1; for (int i = 0; i < value.length(); ++i) { if (value.at(i).isLetter()) { if (value.at(i) == QLatin1Char('e')) continue; firstLetter = i; break; } } if (firstLetter == -1) return value.toDouble(); const QString type = value.mid(firstLetter); value.truncate(firstLetter); const qreal val = value.toDouble(); if (type == QLatin1String("deg")) return val; else if (type == QLatin1String("rad")) return val * 180 / M_PI; else if (type == QLatin1String("grad")) return val * 0.9; return defaultVal; } qreal KoUnit::approxTransformScale(const QTransform &t) { return std::sqrt(qAbs(t.determinant())); } void KoUnit::adjustByPixelTransform(const QTransform &t) { m_pixelConversion *= approxTransformScale(t); } #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const KoUnit &unit) { #ifndef NDEBUG debug.nospace() << unit.symbol(); #else Q_UNUSED(unit); #endif return debug.space(); } #endif diff --git a/libs/odf/KoUnit.h b/libs/global/KoUnit.h similarity index 98% rename from libs/odf/KoUnit.h rename to libs/global/KoUnit.h index 60ced5e11c..b6d2c6031d 100644 --- a/libs/odf/KoUnit.h +++ b/libs/global/KoUnit.h @@ -1,240 +1,240 @@ /* This file is part of the KDE project Copyright (C) 1998, 1999 Reginald Stadlbauer Copyright (C) 1998, 1999 Torben Weis Copyright (C) 2004, Nicolas GOUTTE Copyright (C) 2010 Thomas Zander Copyright 2012 Friedrich W. H. Kossebau This library 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 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KOUNIT_H #define KOUNIT_H // Calligra -#include "kritaodf_export.h" +#include "kritaglobal_export.h" // Qt #include #include #include // std #include // for floor class QStringList; // 1 inch ^= 72 pt // 1 inch ^= 25.399956 mm (-pedantic ;p) // 1 pt = 1/12 pi // 1 pt ^= 0.0077880997 cc // 1 cc = 12 dd constexpr qreal POINT_TO_MM(qreal px) {return (px)*0.352777167;} constexpr qreal MM_TO_POINT(qreal mm) {return mm*2.83465058;} constexpr qreal POINT_TO_CM(qreal px) {return (px)*0.0352777167;} constexpr qreal CM_TO_POINT(qreal cm) {return (cm)*28.3465058;} constexpr qreal POINT_TO_DM(qreal px) {return (px)*0.00352777167;} constexpr qreal DM_TO_POINT(qreal dm) {return (dm)*283.465058;} constexpr qreal POINT_TO_INCH(qreal px) {return (px)*0.01388888888889;} constexpr qreal INCH_TO_POINT(qreal inch) {return (inch)*72.0;} constexpr qreal MM_TO_INCH(qreal mm) {return (mm)*0.039370147;} constexpr qreal INCH_TO_MM(qreal inch) {return (inch)*25.399956;} constexpr qreal POINT_TO_PI(qreal px) {return (px)*0.083333333;} constexpr qreal POINT_TO_CC(qreal px) {return (px)*0.077880997;} constexpr qreal PI_TO_POINT(qreal pi) {return (pi)*12;} constexpr qreal CC_TO_POINT(qreal cc) {return (cc)*12.840103;} static const qreal PT_ROUNDING {1000.0}; //static const qreal PX_ROUNDING {1000.0}; // pixel value is not to be rounded static const qreal CM_ROUNDING {10000.0}; static const qreal DM_ROUNDING {10000.0}; static const qreal MM_ROUNDING {10000.0}; static const qreal IN_ROUNDING {100000.0}; static const qreal PI_ROUNDING {100000.0}; // pico static const qreal CC_ROUNDING {100000.0}; // cicero /** * %Calligra stores everything in pt (using "qreal") internally. * 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. */ -class KRITAODF_EXPORT KoUnit +class KRITAGLOBAL_EXPORT KoUnit { public: /** Length units supported by Calligra. */ enum Type { Millimeter = 0, Point, ///< Postscript point, 1/72th of an Inco Inch, Centimeter, Decimeter, Pica, Cicero, Pixel, TypeCount ///< @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) /** Returns a KoUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ static KoUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); /// Convert a unit symbol string into a KoUnit /// @param symbol symbol to convert /// @param ok if set, it will be true if the unit was known, false if unknown static KoUnit fromSymbol(const QString &symbol, bool *ok = 0); /** Construction requires initialization. The factor is for variable factor units like pixel */ explicit KoUnit(Type unit = Point, qreal factor = 1.0) { m_type = unit; m_pixelConversion = factor; } KoUnit& operator=(Type unit) { m_type = unit; m_pixelConversion = 1.0; return *this; } bool operator!=(const KoUnit &other) const { return !operator==(other); } bool operator==(const KoUnit &other) const { return m_type == other.m_type && (m_type != Pixel || qFuzzyCompare(m_pixelConversion, other.m_pixelConversion)); } KoUnit::Type type() const { return m_type; } void setFactor(qreal factor) { m_pixelConversion = factor; } /** * convert the given value directly from one unit to another */ static qreal convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor = 1.0); /** * Convert the value @p ptValue to the unit and round it. * This method is meant to be used to display a value in a dialog. * \return the converted and rounded value */ qreal toUserValueRounded(const qreal value) const; /** * Convert the value @p ptValue to the unit (without rounding) * This method is meant to be used in complex calculations. * \return the converted value */ qreal toUserValuePrecise(const qreal ptValue) const; /** * Convert the value @p ptValue with or with rounding as indicated by @p rounding. * This method is a proxy to toUserValuePrecise and toUserValueRounded. * \return the value @p ptValue converted to unit */ qreal toUserValue(qreal ptValue, bool rounding = true) const; /// This method is the one to use to display a value in a dialog /// @return the value @p ptValue converted the unit and rounded, ready to be displayed QString toUserStringValue(qreal ptValue) const; /// This method is the one to use to read a value from a dialog /// @return the value converted to points for internal use qreal fromUserValue(qreal value) const; /// This method is the one to use to read a value from a dialog /// @param value value entered by the user /// @param ok if set, the pointed bool is set to true if the value could be /// converted to a qreal, and to false otherwise. /// @return the value converted to points for internal use qreal fromUserValue(const QString &value, bool *ok = 0) const; /// Get the description string of the given unit static QString unitDescription(KoUnit::Type type); /// Get the symbol string of the unit QString symbol() const; /// Returns the list of unit types for the UI, controlled with the given @p listOptions. static QStringList listOfUnitNameForUi(ListOptions listOptions = ListAll); /// 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; /// parse common %Calligra and Odf values, like "10cm", "5mm" to pt 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); QString toString() const { return symbol(); } /** * Get an approximate scale of a unit vector that was converted by * the transformation. * * Please note that exact values are guaranteed only for * combinations of Translate, Rotation and Uniform Scale * matrices. For combinations having shears and perspective the * value will be average for the point near CS origin. */ static qreal approxTransformScale(const QTransform &t); /** * Adjust the unit by pixel transformation applied to the * describing object. It multiplies the pixel coefficient by the * average scale of the matrix. */ void adjustByPixelTransform(const QTransform &t); private: Type m_type; qreal m_pixelConversion; }; #ifndef QT_NO_DEBUG_STREAM -KRITAODF_EXPORT QDebug operator<<(QDebug, const KoUnit &); +KRITAGLOBAL_EXPORT QDebug operator<<(QDebug, const KoUnit &); #endif Q_DECLARE_METATYPE(KoUnit) Q_DECLARE_OPERATORS_FOR_FLAGS(KoUnit::ListOptions) #endif diff --git a/libs/odf/CMakeLists.txt b/libs/odf/CMakeLists.txt index 1c626c62c4..e030002c8b 100644 --- a/libs/odf/CMakeLists.txt +++ b/libs/odf/CMakeLists.txt @@ -1,33 +1,32 @@ add_subdirectory( tests ) set(kritaodf_LIB_SRCS KoOdfManifestEntry.cpp KoDocumentInfo.cpp KoGenStyle.cpp KoGenStyles.cpp KoFontFace.cpp KoOdfLoadingContext.cpp KoOdfStylesReader.cpp KoOdfReadStore.cpp KoOdfWriteStore.cpp KoStyleStack.cpp KoOdfGraphicStyles.cpp KoDocumentBase.cpp KoEmbeddedDocumentSaver.cpp KoBorder.cpp KoShadowStyle.cpp - KoUnit.cpp KoElementReference.cpp OdfDebug.cpp ) add_library(kritaodf SHARED ${kritaodf_LIB_SRCS}) generate_export_header(kritaodf BASE_NAME kritaodf) -target_link_libraries(kritaodf kritaversion kritaplugin kritastore KF5::CoreAddons KF5::ConfigCore KF5::I18n Qt5::PrintSupport Qt5::Gui Qt5::Xml) +target_link_libraries(kritaodf kritaglobal kritaversion kritaplugin kritastore KF5::CoreAddons KF5::ConfigCore KF5::I18n Qt5::PrintSupport Qt5::Gui Qt5::Xml) set_target_properties(kritaodf PROPERTIES VERSION ${GENERIC_KRITA_LIB_VERSION} SOVERSION ${GENERIC_KRITA_LIB_SOVERSION} ) install(TARGETS kritaodf ${INSTALL_TARGETS_DEFAULT_ARGS} )