#include <stdlib.h>
#include <qglobal.h>
#include <QByteArray>
#include <QString>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QLibraryInfo>
#include <QUrl>
#include <QMessageBox>
#include <QTextCodec>
#include <QDateTime>
#include <QRect>
#include <QIcon>
#include <QColor>
#include <QBrush>
#include <QPushButton>
#include <QApplication>
#include <QDesktopWidget>
#include <QDomDocument>
#include <QTextFormat>
#include "klfutil.h"
#include "klfstyle.h"
Go to the source code of this file.
Macros | |
#define | KLF_BRUSH_STYLE(sty) { Qt::sty##Pattern, #sty } |
#define | KLF_TEXT_FORMAT_FORMAT(fmt) { QTextFormat::fmt##Format, #fmt "Format" } |
#define | KLF_TEXT_FORMAT_PROP(p, type) { QTextFormat::p, #p, #type } |
#define | RX_INT "-?\\d+" |
#define | RX_COORD_SEP "\\s*(?:[,;]|\\s)\\s*" |
#define | RX_SIZE_SEP "\\s*(?:[,;x]|\\s)\\s*" |
Functions | |
KLF_EXPORT bool | klfEnsureDir (const QString &dir) |
Ensure existence of a directory. More... | |
static QMap< QString, QString > | klf_url_query_items_map (const QUrl &url, const QStringList &interestQueryItems) |
KLF_EXPORT uint | klfUrlCompare (const QUrl &url1, const QUrl &url2, uint interestFlags, const QStringList &interestQueryItems) |
Compares two URLs and returns some flags as to how they differ. More... | |
KLF_EXPORT bool | klfMatch (const QVariant &testForHitCandidateValue, const QVariant &queryValue, Qt::MatchFlags flags, const QString &queryStringCache) |
Generalized value matching. More... | |
static bool | klf_is_hex_char (char c) |
KLF_EXPORT QByteArray | klfDataToEscaped (const QByteArray &value_ba) |
KLF_EXPORT QByteArray | klfEscapedToData (const QByteArray &data) |
static QByteArray | encaps_list (const QList< QByteArray > &list) |
static QByteArray | encaps_map (const QList< QPair< QByteArray, QByteArray > > §ions, bool ignore_empty_values=false) |
static QList< QByteArray > | decaps_list (const QByteArray &ba_data) |
static QList< QPair < QByteArray, QByteArray > > | decaps_map (const QByteArray &ba_data, bool allow_empty_values=false) |
static QDomElement | make_xml_wrapper (const QString &rootname) |
static QDomElement | parse_xml_wrapper (const QByteArray &xmldata, const QString &shouldBeRootName) |
KLF_EXPORT QByteArray | klfSaveVariantToText (const QVariant &value, bool saveListAndMapsAsXML) |
KLF_EXPORT QVariant | klfLoadVariantFromText (const QByteArray &stringdata, const char *dataTypeName, const char *listOrMapDataTypeName) |
KLF_EXPORT QDomElement | klfSaveVariantMapToXML (const QVariantMap &vmap, QDomElement baseNode) |
Lossless save of full map to XML with type information. More... | |
KLF_EXPORT QVariantMap | klfLoadVariantMapFromXML (const QDomElement &xmlNode) |
Load a map saved with klfSaveVariantMapToXML() More... | |
KLF_EXPORT QDomElement | klfSaveVariantListToXML (const QVariantList &vlist, QDomElement baseNode) |
Lossless save of full list to XML with type information. More... | |
KLF_EXPORT QVariantList | klfLoadVariantListFromXML (const QDomElement &xmlNode) |
Load a list saved with klfSaveVariantListToXML() More... | |
KLF_EXPORT QString | klfPrefixedPath (const QString &path, const QString &reference) |
Returns absolute path to path as seen from reference . More... | |
KLF_EXPORT QString | klfUrlLocalFilePath (const QUrl &url) |
Variables | |
struct { | |
int brushStyle | |
const char * style | |
} | klf_brush_styles [] |
struct { | |
int formatId | |
const char * format | |
} | klf_text_format_formats [] |
struct { | |
int propId | |
const char * key | |
const char * type | |
} | klf_text_format_props [] |
struct { | |
const char * keyword | |
int propId | |
QVariant fixed_value | |
} | klf_text_format_keywords [] |
#define KLF_BRUSH_STYLE | ( | sty | ) | { Qt::sty##Pattern, #sty } |
Definition at line 208 of file klfutil.cpp.
#define KLF_TEXT_FORMAT_FORMAT | ( | fmt | ) | { QTextFormat::fmt##Format, #fmt "Format" } |
Definition at line 233 of file klfutil.cpp.
Definition at line 248 of file klfutil.cpp.
#define RX_COORD_SEP "\\s*(?:[,;]|\\s)\\s*" |
Referenced by klfLoadVariantFromText().
#define RX_INT "-?\\d+" |
Referenced by klfLoadVariantFromText().
#define RX_SIZE_SEP "\\s*(?:[,;x]|\\s)\\s*" |
Referenced by klfLoadVariantFromText().
|
static |
Definition at line 384 of file klfutil.cpp.
References QList::append(), QByteArray::isEmpty(), klfDbg, QByteArray::size(), and QByteArray::trimmed().
Referenced by klfLoadVariantFromText().
|
static |
Definition at line 436 of file klfutil.cpp.
References QByteArray::contains(), KLF_FUNC_NAME, QByteArray::size(), and QByteArray::trimmed().
Referenced by klfLoadVariantFromText().
|
static |
Definition at line 337 of file klfutil.cpp.
References QByteArray::replace(), and QList::size().
Referenced by klfSaveVariantToText().
|
static |
Definition at line 355 of file klfutil.cpp.
References QByteArray::isEmpty(), key, and QByteArray::replace().
Referenced by klfSaveVariantToText().
|
inlinestatic |
Definition at line 201 of file klfutil.cpp.
Referenced by klfEscapedToData(), and klfLoadVariantFromText().
|
static |
Definition at line 73 of file klfutil.cpp.
References QStringList::contains(), QPair::first(), QUrl::queryItems(), QPair::second(), and QList::size().
Referenced by klfUrlCompare().
KLF_EXPORT QByteArray klfDataToEscaped | ( | const QByteArray & | data | ) |
Escapes every character in data
that is not in the range 32-126 (included) as \xHH whith HH the hex code of the character. Backslashes are replaced by double-backslashes.
Definition at line 278 of file klfutil.cpp.
References QString::arg(), QByteArray::constData(), and QByteArray::size().
Referenced by klfSaveVariantToText().
KLF_EXPORT bool klfEnsureDir | ( | const QString & | dir | ) |
Ensure existence of a directory.
Definition at line 52 of file klfutil.cpp.
References QDir::mkpath(), and QFile::setPermissions().
KLF_EXPORT QByteArray klfEscapedToData | ( | const QByteArray & | escaped | ) |
Performs the exact inverse of klfDataToEscaped().
Definition at line 297 of file klfutil.cpp.
References klf_is_hex_char(), QByteArray::mid(), and QByteArray::size().
Referenced by klfLoadVariantFromText().
KLF_EXPORT QVariant klfLoadVariantFromText | ( | const QByteArray & | string, |
const char * | dataTypeName, | ||
const char * | listOrMapTypeName = NULL |
||
) |
Loads the value stored in string
into a variant of data type dataTypeName
. The string is parsed and the returned variant will by of the given type name, or invalid if the string could not be parsed.
string
.Example use: to save/store settings values in QSettings in a human-read/writable format.
If dataTypeName
is a variant list or map, then please specify the type of the values in the list or map (this function assumes all objects in list or map have the same type). As a special case, you can pass "XML"
to load the list or map data with klfLoadVariantListFromXML() or klfLoadVariantMapFromXML(), which enables you to save arbitrary combination of types.
See also klfSaveVariantToText().
Definition at line 832 of file klfutil.cpp.
References QList::begin(), KLFStyle::bg_color, QRegExp::capturedTexts(), QString::compare(), QByteArray::constData(), decaps_list(), decaps_map(), KLFStyle::dpi, QList::end(), KLFStyle::fg_color, QPair::first(), fixed_value, format, QString::fromLatin1(), QString::fromLocal8Bit(), QDateTime::fromString(), QDate::fromString(), QTime::fromString(), QString::fromUtf8(), QRegExp::indexIn(), QByteArray::indexOf(), QByteArray::isEmpty(), QString::isEmpty(), QList::isEmpty(), QChar::isSpace(), QTime::isValid(), QDateTime::isValid(), QDate::isValid(), key, keyword, klf_brush_styles, KLF_DEBUG_TIME_BLOCK, KLF_FUNC_NAME, klf_is_hex_char(), klf_text_format_formats, klf_text_format_keywords, klf_text_format_props, klfDbg, klfEscapedToData(), klfLoadVariantFromText(), klfLoadVariantListFromXML(), klfLoadVariantMapFromXML(), KLFStyle::mathmode, QByteArray::mid(), KLFStyle::name, QVariant::nameToType(), parse_xml_wrapper(), QRegExp::pattern(), KLFStyle::preamble, propId, QByteArray::remove(), RX_COORD_SEP, RX_INT, RX_SIZE_SEP, QColor::setAlpha(), QRegExp::setMinimal(), QColor::setNamedColor(), QTextFormat::setProperty(), QFont::setStyle(), QByteArray::size(), QList::size(), QByteArray::startsWith(), QString::startsWith(), style, QList::takeFirst(), QByteArray::toDouble(), QByteArray::toInt(), QVariant::toInt(), QString::toInt(), QByteArray::toLong(), QByteArray::toLongLong(), QVariant::toMap(), QByteArray::toShort(), QVariant::toString(), QByteArray::toUInt(), QByteArray::toULong(), QByteArray::toULongLong(), QByteArray::toUShort(), QByteArray::trimmed(), type, and QVariant::value().
Referenced by klfLoadVariantFromText(), klfLoadVariantListFromXML(), and klfLoadVariantMapFromXML().
KLF_EXPORT QVariantList klfLoadVariantListFromXML | ( | const QDomElement & | xmlNode | ) |
Load a list saved with klfSaveVariantListToXML()
Definition at line 1525 of file klfutil.cpp.
References QDomElement::attribute(), QDomNode::isNull(), KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klfLoadVariantFromText(), klfLoadVariantListFromXML(), klfLoadVariantMapFromXML(), QDomNode::nextSibling(), QDomNode::nodeType(), QDomElement::text(), QDomNode::toElement(), and QString::toLatin1().
Referenced by klfLoadVariantFromText(), klfLoadVariantListFromXML(), and klfLoadVariantMapFromXML().
KLF_EXPORT QVariantMap klfLoadVariantMapFromXML | ( | const QDomElement & | xmlNode | ) |
Load a map saved with klfSaveVariantMapToXML()
Definition at line 1443 of file klfutil.cpp.
References QByteArray::constData(), QDomNode::isNull(), key, KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klfDbg, klfLoadVariantFromText(), klfLoadVariantListFromXML(), klfLoadVariantMapFromXML(), QDomNode::nextSibling(), QDomNode::nodeName(), QDomNode::nodeType(), and QDomNode::toElement().
Referenced by klfLoadVariantFromText(), klfLoadVariantListFromXML(), and klfLoadVariantMapFromXML().
KLF_EXPORT bool klfMatch | ( | const QVariant & | testForHitCandidateValue, |
const QVariant & | queryValue, | ||
Qt::MatchFlags | flags, | ||
const QString & | queryStringCache = QString() |
||
) |
Generalized value matching.
This function tests to see if the value testForHitCandidateValue
matches the value queryValue
according to the match flags flags
.
If you call this function repeatedly with the same queryValue
, the query value may be converted (unnecessarily) repeatedly to a string with queryValue.toString()
. To optimize this, you may cache that string and pass each time the string representation for the queryValue
as parameter to queryStringCache
. If however a null string is passed, the conversion is performed automatically.
Definition at line 156 of file klfutil.cpp.
References QString::compare(), QString::contains(), QString::endsWith(), QString::isNull(), QString::startsWith(), and QVariant::toString().
Returns absolute path to path
as seen from reference
.
If path
is absolute, then path
is returned as is. Otherwise, an absolute path constructed by concatenating path
to reference
is returned.
If reference
is empty, then the reference is considered to be the application's location, see QCoreApplication::applicationDirPath().
Definition at line 1561 of file klfutil.cpp.
References QCoreApplication::applicationDirPath(), QString::endsWith(), QString::isEmpty(), KLF_DEBUG_TEE, and klfDbg.
KLF_EXPORT QDomElement klfSaveVariantListToXML | ( | const QVariantList & | vlist, |
QDomElement | baseNode | ||
) |
Lossless save of full list to XML with type information.
Definition at line 1498 of file klfutil.cpp.
References QDomDocument::createElement(), QDomDocument::createTextNode(), QString::fromLatin1(), QString::fromLocal8Bit(), klfSaveVariantListToXML(), klfSaveVariantMapToXML(), klfSaveVariantToText(), QDomElement::setAttribute(), QVariant::toList(), QVariant::toMap(), and QVariant::typeName().
Referenced by klfSaveVariantListToXML(), klfSaveVariantMapToXML(), and klfSaveVariantToText().
KLF_EXPORT QDomElement klfSaveVariantMapToXML | ( | const QVariantMap & | vmap, |
QDomElement | baseNode | ||
) |
Lossless save of full map to XML with type information.
Definition at line 1410 of file klfutil.cpp.
References QDomDocument::createElement(), QDomDocument::createTextNode(), QString::fromLocal8Bit(), key, klfSaveVariantListToXML(), klfSaveVariantMapToXML(), klfSaveVariantToText(), QDomElement::setAttribute(), QVariant::toList(), QVariant::toMap(), QVariant::typeName(), and QVariant::value().
Referenced by klfSaveVariantListToXML(), klfSaveVariantMapToXML(), and klfSaveVariantToText().
KLF_EXPORT QByteArray klfSaveVariantToText | ( | const QVariant & | value, |
bool | saveListAndMapsAsXML = false |
||
) |
Saves the variant value
into a string, stored in Local 8-bit encoding text in QByteArray. The saved string is both human and machine readable, ie. the exact string can be recast again to a variant with klfLoadVariantFromText().
This function is aware of various QVariant formats, however maybe not all of them. The unknown formats are stored machine-readable only, by sending the variant in a datastream, and protecting the special characters from encoding artifacts (ascii chars only, proper escape sequences).
If saveListAndMapsAsXML
is FALSE (the default), then variant-lists and -maps are saved in a format like "[element-1,element-2,...]"
or "{key1=value1,key2=value2,....}"
, assuming that all elements are of the same QVariant type. If saveListAndMapsAsXML
is TRUE, then variant lists and maps are saved with klfSaveVariantListToXML() and klfSaveVariantMapToXML(), which enables you to save arbitrary combination of types.
Definition at line 519 of file klfutil.cpp.
References QColor::alpha(), QList::append(), QString::arg(), QMap::begin(), KLFStyle::bg_color, QColor::blue(), brushStyle, QTextCodec::canEncode(), QTextCodec::codecForLocale(), QBrush::color(), QByteArray::constData(), KLFStyle::dpi, encaps_list(), encaps_map(), QMap::end(), QFont::family(), KLFStyle::fg_color, fixed_value, format, formatId, QTextCodec::fromUnicode(), QVariant::fromValue(), QColor::green(), QRect::height(), QSize::height(), QByteArray::isNull(), QVariant::isNull(), QVariant::isValid(), key, klf_brush_styles, KLF_FUNC_NAME, klf_text_format_formats, klf_text_format_keywords, klf_text_format_props, klfDataToEscaped(), klfDbg, klfSaveVariantListToXML(), klfSaveVariantMapToXML(), klfSaveVariantToText(), QRect::left(), QString::length(), make_xml_wrapper(), KLFStyle::mathmode, QBrush::matrix(), QString::mid(), KLFStyle::name, QString::number(), KLFStyle::preamble, QTextFormat::properties(), propId, QColor::red(), QString::replace(), QList::size(), QByteArray::size(), QByteArray::startsWith(), QFont::style(), style, QVariant::toBool(), QVariant::toChar(), QVariant::toList(), QVariant::toMap(), QRect::top(), QVariant::toPoint(), QVariant::toRect(), QVariant::toSize(), QVariant::toString(), QVariant::toStringList(), QVariant::toUrl(), QTextFormat::type(), QVariant::type(), type, QVariant::typeName(), QChar::unicode(), QVariant::value(), QFont::weight(), QRect::width(), QSize::width(), QPoint::x(), and QPoint::y().
Referenced by klfSaveVariantListToXML(), klfSaveVariantMapToXML(), and klfSaveVariantToText().
KLF_EXPORT uint klfUrlCompare | ( | const QUrl & | url1, |
const QUrl & | url2, | ||
uint | interestFlags = 0xffffffff , |
||
const QStringList & | interestQueryItems = QStringList() |
||
) |
Compares two URLs and returns some flags as to how they differ.
The return value is an binary-OR'ed value of flags given in KlfUrlCompareFlag.
If the interestFlag
parameter is set, only the tests that are given in interestFlags
are performed. The returned flags are those flags set in interestFlags
that are true.
If the interestQueryItems
is set, all query items other than those specified in interestQueryItems
are ignored. If interestQueryItems
is an empty list, no query items are ignored, they are all taken into account.
Definition at line 89 of file klfutil.cpp.
References KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klf_url_query_items_map(), klfDbg, klfMapIsIncludedIn(), KlfUrlCompareBaseEqual, KlfUrlCompareEqual, klfUrlCompareFlagIgnoreQueryItemValueCase, KlfUrlCompareLessSpecific, KlfUrlCompareMoreSpecific, and QUrl::setQueryItems().
Returns the file path represented in url
, interpreted as an (absolute) path to a local file.
On windows, this ensures that there is no slash preceeding the drive letter, eg. fixes "/C:/..." to "C:/...", but keeps forward-slashes.
Definition at line 1583 of file klfutil.cpp.
References QString::mid(), QUrl::path(), and QString::startsWith().
|
static |
Definition at line 497 of file klfutil.cpp.
References QDomDocument::createElement().
Referenced by klfSaveVariantToText().
|
static |
Definition at line 505 of file klfutil.cpp.
References QDomDocument::documentElement(), KLF_ASSERT_CONDITION, and QDomDocument::setContent().
Referenced by klfLoadVariantFromText().
int brushStyle |
Definition at line 211 of file klfutil.cpp.
Referenced by klfSaveVariantToText().
QVariant fixed_value |
Definition at line 266 of file klfutil.cpp.
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
const char* format |
Definition at line 236 of file klfutil.cpp.
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
int formatId |
Definition at line 236 of file klfutil.cpp.
Referenced by klfSaveVariantToText().
const char* key |
Definition at line 251 of file klfutil.cpp.
Referenced by encaps_map(), klfLoadVariantFromText(), klfLoadVariantMapFromXML(), klfSaveVariantMapToXML(), and klfSaveVariantToText().
const char* keyword |
Definition at line 266 of file klfutil.cpp.
Referenced by klfLoadVariantFromText().
struct { ... } klf_brush_styles[] |
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
struct { ... } klf_text_format_formats[] |
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
struct { ... } klf_text_format_keywords[] |
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
struct { ... } klf_text_format_props[] |
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().
int propId |
Definition at line 251 of file klfutil.cpp.
Referenced by klfLoadVariantFromText(), klfSaveVariantToText(), KLFPropertizedObject::loadProperty(), KLFPropertizedObject::property(), and KLFPropertizedObject::setProperty().
const char* style |
Definition at line 211 of file klfutil.cpp.
Referenced by klfLoadVariantFromText(), klfSaveVariantToText(), KLFPixmapButton::sizeHint(), and KLFColorChooser::sizeHint().
const char* type |
Definition at line 251 of file klfutil.cpp.
Referenced by klfLoadVariantFromText(), and klfSaveVariantToText().