mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 20:15:35 +08:00
refs #452, misc. functions for pixmap and hex value handling
This commit is contained in:
committed by
Mathew Sutcliffe
parent
38c92c5676
commit
e6eac2333d
@@ -357,3 +357,86 @@ bool BlackMisc::stringToBool(const QString &string)
|
||||
if (c == '0' || c == 'f' || c == 'n' || c == ' ') { return false; }
|
||||
return false;
|
||||
}
|
||||
|
||||
QString BlackMisc::intToHex(int value, int digits)
|
||||
{
|
||||
QString hex(QString::number(value, 16).toUpper());
|
||||
int l = hex.length();
|
||||
if (l >= digits) { return hex.right(digits); }
|
||||
int d = digits - l;
|
||||
return QString(d, '0') + hex;
|
||||
}
|
||||
|
||||
bool BlackMisc::pixmapToPngByteArray(const QPixmap &pixmap, QByteArray &array)
|
||||
{
|
||||
QBuffer buffer(&array);
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
bool s = pixmap.save(&buffer, "PNG");
|
||||
buffer.close();
|
||||
return s;
|
||||
}
|
||||
|
||||
QString BlackMisc::bytesToHexString(const QByteArray &bytes)
|
||||
{
|
||||
QString h;
|
||||
for (int i = 0; i < bytes.size(); i++)
|
||||
{
|
||||
h.append(static_cast<int>(bytes.at(i)));
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
QByteArray BlackMisc::byteArrayFromHexString(const QString &hexString)
|
||||
{
|
||||
QByteArray ba;
|
||||
int pos = 0;
|
||||
while (pos + 1 < hexString.length())
|
||||
{
|
||||
bool ok;
|
||||
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;
|
||||
}
|
||||
|
||||
QPixmap BlackMisc::pngByteArrayToPixmap(const QByteArray &array)
|
||||
{
|
||||
if (array.isEmpty()) { return QPixmap(); }
|
||||
QPixmap p;
|
||||
bool s = p.loadFromData(array, "PNG");
|
||||
return s ? p : QPixmap();
|
||||
}
|
||||
|
||||
bool BlackMisc::pngByteArrayToPixmapRef(const QByteArray &array, QPixmap &pixmap)
|
||||
{
|
||||
if (array.isEmpty()) { return false; }
|
||||
bool s = pixmap.loadFromData(array, "PNG");
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
QString BlackMisc::pixmapToPngHexString(const QPixmap &pixmap)
|
||||
{
|
||||
QByteArray ba;
|
||||
bool s = pixmapToPngByteArray(pixmap, ba);
|
||||
if (!s) { return QString(); }
|
||||
return bytesToHexString(ba);
|
||||
}
|
||||
|
||||
QPixmap BlackMisc::pngHexStringToPixmap(const QString &hexString)
|
||||
{
|
||||
if (hexString.isEmpty()) { return QPixmap(); }
|
||||
QByteArray ba(byteArrayFromHexString(hexString));
|
||||
return pngByteArrayToPixmap(ba);
|
||||
}
|
||||
|
||||
bool BlackMisc::pngHexStringToPixmapRef(const QString &hexString, QPixmap &pixmap)
|
||||
{
|
||||
if (hexString.isEmpty()) { return false; }
|
||||
QByteArray ba(byteArrayFromHexString(hexString));
|
||||
return pngByteArrayToPixmapRef(ba, pixmap);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,11 @@ 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
|
||||
@@ -328,6 +333,33 @@ namespace BlackMisc
|
||||
//! Merges an incremental json object into an existing one
|
||||
BLACKMISC_EXPORT QJsonObject applyIncrementalObject(const QJsonObject &previousObject, const QJsonObject &incrementalObject);
|
||||
|
||||
//! Int to hex value
|
||||
BLACKMISC_EXPORT QString intToHex(int value, int digits = 2);
|
||||
|
||||
//! 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);
|
||||
|
||||
//! Pixmap to PNG byte array
|
||||
BLACKMISC_EXPORT bool pixmapToPngByteArray(const QPixmap &pixmap, QByteArray &array);
|
||||
|
||||
//! Pixmap from PNG byte array
|
||||
BLACKMISC_EXPORT QPixmap pngByteArrayToPixmap(const QByteArray &array);
|
||||
|
||||
//! Pixmap from PNG byte array
|
||||
BLACKMISC_EXPORT bool pngByteArrayToPixmapRef(const QByteArray &array, QPixmap &pixmap);
|
||||
|
||||
//! Pixmap as HEX string (for PNG image)
|
||||
BLACKMISC_EXPORT QString pixmapToPngHexString(const QPixmap &pixmap);
|
||||
|
||||
//! Hex encoded pixmap string to Pixmap
|
||||
BLACKMISC_EXPORT QPixmap pngHexStringToPixmap(const QString &hexString);
|
||||
|
||||
//! Hex encoded pixmap string to Pixmap
|
||||
BLACKMISC_EXPORT bool pngHexStringToPixmapRef(const QString &hexString, QPixmap &pixmap);
|
||||
|
||||
} // BlackMisc
|
||||
|
||||
#endif // guard
|
||||
|
||||
Reference in New Issue
Block a user