From a725ce21815cd28d5ab6a0f05800d4d313dc203f Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 14 Oct 2015 01:48:52 +0200 Subject: [PATCH] refs #478 refs #488, utils / free functions * seed for random numbers per thread * added function to get all user metatypes as string * added new read file function per path/name * removed DBus operators for pixmap * JSON operators for byte array * util function for JSON string --- src/blackmisc/blackmiscfreefunctions.cpp | 35 ++++++++++++++++++------ src/blackmisc/blackmiscfreefunctions.h | 8 ++---- src/blackmisc/containerbase.h | 13 +++++++++ src/blackmisc/dbus.cpp | 31 --------------------- src/blackmisc/dbus.h | 11 -------- src/blackmisc/fileutilities.cpp | 5 ++++ src/blackmisc/fileutilities.h | 3 ++ src/blackmisc/json.cpp | 32 ++++++++++++++++++++-- src/blackmisc/json.h | 19 +++++++++++++ src/blackmisc/math/mathutils.cpp | 16 +++++++++++ src/blackmisc/math/mathutils.h | 5 +--- src/blackmisc/pixmap.cpp | 1 - src/blackmisc/pixmap.h | 2 +- src/blackmisc/valueobject.h | 3 ++ 14 files changed, 120 insertions(+), 64 deletions(-) delete mode 100644 src/blackmisc/dbus.cpp diff --git a/src/blackmisc/blackmiscfreefunctions.cpp b/src/blackmisc/blackmiscfreefunctions.cpp index 6ae6b823c..bbcf05e8a 100644 --- a/src/blackmisc/blackmiscfreefunctions.cpp +++ b/src/blackmisc/blackmiscfreefunctions.cpp @@ -267,13 +267,13 @@ size_t BlackMisc::heapSizeOf(const QMetaObject &metaObject) size_t BlackMisc::heapSizeOf(const QMetaType &) { - qDebug() << "heapSizeOf not supported by your compiler toolchain"; + // qDebug() << "heapSizeOf not supported by your compiler toolchain"; return 0; } size_t BlackMisc::heapSizeOf(const QMetaObject &) { - qDebug() << "heapSizeOf not supported by your compiler toolchain"; + // qDebug() << "heapSizeOf not supported by your compiler toolchain"; return 0; } @@ -281,14 +281,31 @@ size_t BlackMisc::heapSizeOf(const QMetaObject &) void BlackMisc::displayAllUserMetatypesTypes(QTextStream &out) { + out << getAllUserMetatypesTypes(); +} +QString BlackMisc::getAllUserMetatypesTypes(const QString &separator) +{ + int fails = 0; + QString meta; for (int mt = QMetaType::User; mt < QMetaType::User + 1000; mt++) { - if (!QMetaType::isRegistered(mt)) { continue; } + if (!QMetaType::isRegistered(mt)) + { + fails++; + // normally a consecutive order of metatypes, we allow a space before we break + if (fails > 3) { return meta; } + continue; + } QMetaType metaType(mt); - out << "type: " << mt << " name:" << QMetaType::typeName(mt) << " | " - << QMetaType::sizeOf(mt) << " / " << BlackMisc::heapSizeOf(metaType) << endl; + meta = meta. + append("type: ").append(QString::number(mt)). + append(" name: ").append(QMetaType::typeName(mt)). + append(" | ").append(QString::number(QMetaType::sizeOf(mt))). + append(" / ").append(QString::number(BlackMisc::heapSizeOf(metaType))). + append(separator); } + return meta; } const QString &BlackMisc::localHostName() @@ -369,12 +386,13 @@ QString BlackMisc::boolToTrueFalse(bool v, bool i18n) bool BlackMisc::stringToBool(const QString &string) { - if (string.isEmpty()) { return false; } - QChar c = string.at(0).toLower(); + QString s(string.trimmed().toLower()); + if (s.isEmpty()) { return false; } + QChar c = s.at(0); // explicit values if (c == '1' || c == 't' || c == 'y' || c == 'x') { return true; } - if (c == '0' || c == 'f' || c == 'n' || c == ' ') { return false; } + if (c == '0' || c == 'f' || c == 'n' || c == '_') { return false; } return false; } @@ -439,7 +457,6 @@ bool BlackMisc::pngByteArrayToPixmapRef(const QByteArray &array, QPixmap &pixmap return s; } - QString BlackMisc::pixmapToPngHexString(const QPixmap &pixmap) { QByteArray ba; diff --git a/src/blackmisc/blackmiscfreefunctions.h b/src/blackmisc/blackmiscfreefunctions.h index 9d7734f85..db0480183 100644 --- a/src/blackmisc/blackmiscfreefunctions.h +++ b/src/blackmisc/blackmiscfreefunctions.h @@ -35,11 +35,6 @@ inline void initBlackMiscResources() // cannot be declared within namespace, see docu // hence BlackMisc::initResources() calls this inline function Q_INIT_RESOURCE(blackmisc); - - // set seed for random number if ever used - // that is needed only once on application startup - QTime time = QTime::currentTime(); - qsrand((uint)time.msec()); } //! Free functions in BlackMisc @@ -258,6 +253,9 @@ namespace BlackMisc //! \remarks Used in order to debug code, do not remove BLACKMISC_EXPORT void displayAllUserMetatypesTypes(QTextStream &out); + //! Get all user metatypes + BLACKMISC_EXPORT QString getAllUserMetatypesTypes(const QString &separator = "\n"); + /*! * \brief Calculate a single hash value based on a list of individual hash values * \param values diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index cc4e665a1..447272716 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -131,6 +131,13 @@ namespace BlackMisc return json; } + //! Convenience function JSON as string + QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const + { + QJsonDocument jsonDoc(toJson()); + return jsonDoc.toJson(format); + } + //! \copydoc CValueObject::convertFromJson void convertFromJson(const QJsonObject &json) { @@ -144,6 +151,12 @@ namespace BlackMisc } } + //! Assign from JSON object string + void convertFromJson(const QString &jsonString) + { + convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString)); + } + //! \copydoc BlackMisc::CValueObject::convertToQString QString convertToQString(bool i18n = false) const { diff --git a/src/blackmisc/dbus.cpp b/src/blackmisc/dbus.cpp deleted file mode 100644 index 8d87e2bba..000000000 --- a/src/blackmisc/dbus.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright (C) 2015 - * swift project Community / Contributors - * - * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level - * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, - * including this file, may be copied, modified, propagated, or distributed except according to the terms - * contained in the LICENSE file. - */ - -#include "blackmisc/dbus.h" -#include "blackmisc/blackmiscfreefunctions.h" - -const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap) -{ - QByteArray ba; - argument.beginStructure(); - argument >> ba; - BlackMisc::pngByteArrayToPixmapRef(ba, pixmap); - argument.endStructure(); - return argument; -} - -QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap) -{ - QByteArray ba; - BlackMisc::pixmapToPngByteArray(pixmap, ba); - argument.beginStructure(); - argument << ba; - argument.endStructure(); - return argument; -} diff --git a/src/blackmisc/dbus.h b/src/blackmisc/dbus.h index c4a647ba3..402651bd5 100644 --- a/src/blackmisc/dbus.h +++ b/src/blackmisc/dbus.h @@ -21,7 +21,6 @@ namespace BlackMisc { namespace Mixin { - /*! * CRTP class template which will generate marshalling operators for a derived class with its own marshalling implementation. * @@ -124,14 +123,4 @@ const QDBusArgument &operator >>(const QDBusArgument &arg, E &value) return arg; } -/*! - * Non member non-friend streaming for QPixmap - */ -const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap); - -/*! - * Non member non-friend streaming for QPixmap - */ -QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap); - #endif // guard diff --git a/src/blackmisc/fileutilities.cpp b/src/blackmisc/fileutilities.cpp index cfcf9e8bd..799a2a8cf 100644 --- a/src/blackmisc/fileutilities.cpp +++ b/src/blackmisc/fileutilities.cpp @@ -36,6 +36,11 @@ namespace BlackMisc return content; } + QString CFileUtils::readFileToString(const QString &filePath, const QString &fileName) + { + return readFileToString(appendFilePaths(filePath, fileName)); + } + bool CFileUtils::writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath) { if (fileNameAndPath.isEmpty()) { return false; } diff --git a/src/blackmisc/fileutilities.h b/src/blackmisc/fileutilities.h index 19c32422d..22b0c2ff1 100644 --- a/src/blackmisc/fileutilities.h +++ b/src/blackmisc/fileutilities.h @@ -30,6 +30,9 @@ namespace BlackMisc //! Read file into string static QString readFileToString(const QString &fileNameAndPath); + //! Read file into string + static QString readFileToString(const QString &filePath, const QString &fileName); + //! Write string to text file in background static bool writeStringToFileInBackground(const QString &content, const QString &fileNameAndPath); diff --git a/src/blackmisc/json.cpp b/src/blackmisc/json.cpp index ebaae7ec9..93443560a 100644 --- a/src/blackmisc/json.cpp +++ b/src/blackmisc/json.cpp @@ -65,6 +65,20 @@ const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value) return json; } +const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value) +{ + const QString hex(json.toString()); + BlackMisc::pngHexStringToPixmapRef(hex, value); + return json; +} + +const QJsonValue &operator >>(const QJsonValue &json, QByteArray &value) +{ + const QString hex(json.toString()); + value = BlackMisc::byteArrayFromHexString(hex); + return json; +} + const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value) { value = json.toInt(); @@ -126,10 +140,10 @@ const QJsonValueRef &operator >>(const QJsonValueRef &json, QPixmap &value) return json; } -const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value) +const QJsonValueRef &operator >>(const QJsonValueRef &json, QByteArray &value) { const QString hex(json.toString()); - BlackMisc::pngHexStringToPixmapRef(hex, value); + value = BlackMisc::byteArrayFromHexString(hex); return json; } @@ -194,6 +208,13 @@ QJsonArray &operator<<(QJsonArray &json, const QPixmap &value) return json; } +QJsonArray &operator<<(QJsonArray &json, const QByteArray &value) +{ + QString pm(BlackMisc::byteArrayFromHexString(value)); + json.append(QJsonValue(pm)); + return json; +} + QJsonObject &operator<<(QJsonObject &json, const std::pair &value) { json.insert(value.first, QJsonValue(value.second)); @@ -255,6 +276,13 @@ QJsonObject &operator<<(QJsonObject &json, const std::pair &value) +{ + QString pm(BlackMisc::bytesToHexString(value.second)); + json.insert(value.first, pm); + return json; +} + QJsonObject BlackMisc::Json::jsonObjectFromString(const QString &json) { if (json.isEmpty()) { return QJsonObject();} diff --git a/src/blackmisc/json.h b/src/blackmisc/json.h index 3019e041c..1ded4a826 100644 --- a/src/blackmisc/json.h +++ b/src/blackmisc/json.h @@ -16,12 +16,14 @@ #include "blackmisc/tuple.h" #include "blackmisc/inheritance_traits.h" #include +#include #include #include #include #include #include #include +#include #include /*! @@ -41,6 +43,7 @@ BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, double &v BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, bool &value); BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QDateTime &value); BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value); +BLACKMISC_EXPORT const QJsonValue &operator >>(const QJsonValue &json, QByteArray &value); BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, int &value); BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, qlonglong &value); BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, qulonglong &value); @@ -51,6 +54,7 @@ BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, dou BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, bool &value); BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QDateTime &value); BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QPixmap &value); +BLACKMISC_EXPORT const QJsonValueRef &operator >>(const QJsonValueRef &json, QByteArray &value); //! @} @@ -98,6 +102,7 @@ BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const double value); BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const bool value); BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QDateTime &value); BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QPixmap &value); +BLACKMISC_EXPORT QJsonArray &operator<<(QJsonArray &json, const QByteArray &value); //! @} //! \name Streaming operators for QJsonObject (from value) @@ -113,6 +118,7 @@ BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair &value); BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair &value); BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair &value); +BLACKMISC_EXPORT QJsonObject &operator<<(QJsonObject &json, const std::pair &value); //! @} namespace BlackMisc @@ -198,6 +204,13 @@ namespace BlackMisc return Json::appendJsonObject(json, baseToJson(static_cast *>(derived()))); } + //! Convenience function JSON as string + QString toJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Indented) const + { + QJsonDocument jsonDoc(toJson()); + return jsonDoc.toJson(format); + } + //! Assign from JSON object void convertFromJson(const QJsonObject &json) { @@ -205,6 +218,12 @@ namespace BlackMisc BlackMisc::deserializeJson(json, Private::EncapsulationBreaker::toMetaTuple(*derived())); } + //! Assign from JSON object string + void convertFromJson(const QString &jsonString) + { + convertFromJson(BlackMisc::Json::jsonObjectFromString(jsonString)); + } + private: const Derived *derived() const { return static_cast(this); } Derived *derived() { return static_cast(this); } diff --git a/src/blackmisc/math/mathutils.cpp b/src/blackmisc/math/mathutils.cpp index 0794d2084..8fe8e957b 100644 --- a/src/blackmisc/math/mathutils.cpp +++ b/src/blackmisc/math/mathutils.cpp @@ -8,6 +8,8 @@ */ #include "blackmisc/math/mathutils.h" +#include +#include #include #include @@ -77,5 +79,19 @@ namespace BlackMisc return (result >= 0.0) ? result : result + 360.0; } + int CMathUtils::randomInteger(int low, int high) + { + static QThreadStorage seeds; + if (!seeds.hasLocalData()) + { + // seed is per thread! + uint seed = static_cast(QTime::currentTime().msec()); + qsrand(seed); + seeds.setLocalData(seed); + } + int r(qrand()); + return r % ((high + 1) - low) + low; + } + } // namespace } // namespace diff --git a/src/blackmisc/math/mathutils.h b/src/blackmisc/math/mathutils.h index c59c877e2..291af43bd 100644 --- a/src/blackmisc/math/mathutils.h +++ b/src/blackmisc/math/mathutils.h @@ -100,10 +100,7 @@ namespace BlackMisc static double normalizeDegrees(double degrees); //! Random number between low and high - static int randomInteger(int low, int high) - { - return qrand() % ((high + 1) - low) + low; - } + static int randomInteger(int low, int high); }; } // namespace diff --git a/src/blackmisc/pixmap.cpp b/src/blackmisc/pixmap.cpp index ca04c2418..697e1d8c2 100644 --- a/src/blackmisc/pixmap.cpp +++ b/src/blackmisc/pixmap.cpp @@ -13,7 +13,6 @@ namespace BlackMisc { - CPixmap::CPixmap() = default; CPixmap::CPixmap(const QPixmap &pixmap) : m_pixmap(pixmap), m_hasCachedPixmap(true) diff --git a/src/blackmisc/pixmap.h b/src/blackmisc/pixmap.h index c191c722b..840d52674 100644 --- a/src/blackmisc/pixmap.h +++ b/src/blackmisc/pixmap.h @@ -66,7 +66,7 @@ namespace BlackMisc } // namespace BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CPixmap, ( - attr(o.m_array, flags()) + attr(o.m_array) )) Q_DECLARE_METATYPE(BlackMisc::CPixmap) diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index f9198ca59..f0b525e34 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -112,6 +112,9 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::JsonByTuple::toJson using Mixin::JsonByTuple::toJson; + //! \copydoc BlackMisc::Mixin::JsonByTuple::toJsonString + using Mixin::JsonByTuple::toJsonString; + //! \copydoc BlackMisc::Mixin::JsonByTuple::convertFromJson using Mixin::JsonByTuple::convertFromJson;