Issue #77 Use Qt facilites instead of own implementations

This commit is contained in:
Mat Sutcliffe
2020-08-24 00:53:04 +01:00
parent b7f6b06491
commit ac454ebf3a
7 changed files with 13 additions and 55 deletions

View File

@@ -7,7 +7,6 @@
*/
#include "blackmisc/imageutils.h"
#include "blackmisc/stringutils.h"
#include <QBuffer>
#include <QIODevice>
@@ -41,20 +40,20 @@ QString BlackMisc::pixmapToPngHexString(const QPixmap &pixmap)
QByteArray ba;
bool s = pixmapToPngByteArray(pixmap, ba);
if (!s) { return QString(); }
return bytesToHexString(ba);
return ba.toHex();
}
QPixmap BlackMisc::pngHexStringToPixmap(const QString &hexString)
{
if (hexString.isEmpty()) { return QPixmap(); }
QByteArray ba(byteArrayFromHexString(hexString));
QByteArray ba(QByteArray::fromHex(hexString.toLatin1()));
return pngByteArrayToPixmap(ba);
}
bool BlackMisc::pngHexStringToPixmapRef(const QString &hexString, QPixmap &pixmap)
{
if (hexString.isEmpty()) { return false; }
QByteArray ba(byteArrayFromHexString(hexString));
QByteArray ba(QByteArray::fromHex(hexString.toLatin1()));
return pngByteArrayToPixmapRef(ba, pixmap);
}

View File

@@ -8,7 +8,6 @@
#include "blackmisc/json.h"
#include "blackmisc/imageutils.h"
#include "blackmisc/stringutils.h"
#include <QDateTime>
#include <QJsonDocument>
@@ -95,7 +94,7 @@ const QJsonValue &operator >>(const QJsonValue &json, QPixmap &value)
const QJsonValue &operator >>(const QJsonValue &json, QByteArray &value)
{
const QString hex(json.toString());
value = BlackMisc::byteArrayFromHexString(hex);
value = QByteArray::fromHex(hex.toLatin1());
return json;
}
@@ -175,7 +174,7 @@ QJsonValueRef operator >>(QJsonValueRef json, QPixmap &value)
QJsonValueRef operator >>(QJsonValueRef json, QByteArray &value)
{
const QString hex(json.toString());
value = BlackMisc::byteArrayFromHexString(hex);
value = QByteArray::fromHex(hex.toLatin1());
return json;
}
@@ -248,7 +247,7 @@ QJsonArray &operator<<(QJsonArray &json, const QPixmap &value)
QJsonArray &operator<<(QJsonArray &json, const QByteArray &value)
{
QString pm(BlackMisc::byteArrayFromHexString(value));
QString pm(QByteArray::fromHex(value));
json.append(QJsonValue(pm));
return json;
}
@@ -328,7 +327,7 @@ QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QPixma
QJsonObject &operator<<(QJsonObject &json, const std::pair<QString, const QByteArray &> &value)
{
QString pm(BlackMisc::bytesToHexString(value.second));
QString pm(value.second.toHex());
json.insert(value.first, pm);
return json;
}
@@ -408,8 +407,7 @@ QJsonObject &operator<<(QJsonObject &json, const std::pair<CExplicitLatin1String
QJsonObject &operator<<(QJsonObject &json, const std::pair<CExplicitLatin1String, const QByteArray &> &value)
{
QString pm(BlackMisc::bytesToHexString(value.second));
json[value.first] = pm;
json[value.first] = QString(value.second.toHex());
return json;
}

View File

@@ -147,34 +147,6 @@ namespace BlackMisc
return QString(d, '0') + hex;
}
QString bytesToHexString(const QByteArray &bytes)
{
QString h;
for (int i = 0; i < bytes.size(); i++)
{
const int b = static_cast<int>(bytes.at(i));
h += intToHex(b, 2);
}
return h;
}
QByteArray byteArrayFromHexString(const QString &hexString)
{
QByteArray ba;
int pos = 0;
while (pos + 1 < hexString.length())
{
bool ok;
const QString h = hexString.mid(pos, 2);
int hex = h.toInt(&ok, 16);
Q_ASSERT_X(ok, Q_FUNC_INFO, "Invalid hex");
if (!ok) { return QByteArray(); }
ba.push_back(static_cast<char>(hex));
pos += 2;
}
return ba;
}
QString stripDesignatorFromCompleterString(const QString &candidate)
{
const QString s(candidate.trimmed().toUpper());

View File

@@ -247,12 +247,6 @@ namespace BlackMisc
//! Replace dot '.' by locale decimal point
BLACKMISC_EXPORT QString dotToLocaleDecimalPoint(const QString &input);
//! Int to hex value (per byte, 2 digits)
BLACKMISC_EXPORT QString bytesToHexString(const QByteArray &bytes);
//! Byte array from hex value string per byte, 2 digits
BLACKMISC_EXPORT QByteArray byteArrayFromHexString(const QString &hexString);
//! Strip a designator from a combined string
BLACKMISC_EXPORT QString stripDesignatorFromCompleterString(const QString &candidate);

View File

@@ -53,12 +53,7 @@ namespace BlackMisc
if (!object) { return false; }
if (CThreadUtils::isCurrentThreadObjectThread(object)) { return false; }
QPointer<QObject> myself(object);
QTimer::singleShot(0, object, [ = ]
{
if (!myself) { return; }
callFunct();
});
QMetaObject::invokeMethod(object, callFunct);
return true;
}
} // ns