Commit d20dc1f9 authored by Volker Krause's avatar Volker Krause
Browse files

Util does not need to be a QObject, a Q_GADGET is sufficient

parent 903a7fe4
......@@ -174,10 +174,7 @@ void registerApplicationSingletons()
REGISTER_SINGLETON_GADGET_FACTORY(NavigationController)
REGISTER_SINGLETON_GADGET_FACTORY(NotificationConfigController)
REGISTER_SINGLETON_GADGET_FACTORY(PublicTransport)
qmlRegisterSingletonType<Util>("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine*) -> QObject*{
return new Util;
});
REGISTER_SINGLETON_GADGET_FACTORY(Util)
}
#undef REGISTER_SINGLETON_INSTANCE
......
......@@ -22,14 +22,7 @@
using namespace KItinerary;
Util::Util(QObject* parent)
: QObject(parent)
{
}
Util::~Util() = default;
QDateTime Util::dateTimeStripTimezone(const QVariant& obj, const QString& propertyName) const
QDateTime Util::dateTimeStripTimezone(const QVariant& obj, const QString& propertyName)
{
auto dt = JsonLdDocument::readProperty(obj, propertyName.toUtf8().constData()).toDateTime();
if (!dt.isValid()) {
......@@ -40,7 +33,7 @@ QDateTime Util::dateTimeStripTimezone(const QVariant& obj, const QString& proper
return dt;
}
QVariant Util::setDateTimePreserveTimezone(const QVariant &obj, const QString& propertyName, QDateTime value) const
QVariant Util::setDateTimePreserveTimezone(const QVariant &obj, const QString& propertyName, QDateTime value)
{
if (!value.isValid()) {
qCDebug(Log) << "Invalid date passed for property" << propertyName;
......@@ -56,7 +49,7 @@ QVariant Util::setDateTimePreserveTimezone(const QVariant &obj, const QString& p
return o;
}
bool Util::isRichText(const QString &text) const
bool Util::isRichText(const QString &text)
{
auto idx = text.indexOf(QLatin1Char('<'));
if (idx >= 0 && idx < text.size() - 2) {
......@@ -65,7 +58,7 @@ bool Util::isRichText(const QString &text) const
return false;
}
QString Util::textToHtml(const QString& text) const
QString Util::textToHtml(const QString& text)
{
if (isRichText(text)) {
return text;
......@@ -73,14 +66,14 @@ QString Util::textToHtml(const QString& text) const
return KTextToHTML::convertToHtml(text, KTextToHTML::ConvertPhoneNumbers | KTextToHTML::PreserveSpaces);
}
void Util::sortModel(QObject *model, int column, Qt::SortOrder sortOrder) const
void Util::sortModel(QObject *model, int column, Qt::SortOrder sortOrder)
{
if (auto qaim = qobject_cast<QAbstractItemModel*>(model)) {
qaim->sort(column, sortOrder);
}
}
float Util::svgAspectRatio(const QString &svgFilePath) const
float Util::svgAspectRatio(const QString &svgFilePath)
{
if (svgFilePath.isEmpty()) {
return 1.0f;
......
......@@ -7,38 +7,35 @@
#ifndef UTIL_H
#define UTIL_H
#include <QObject>
#include <qobjectdefs.h>
class QDateTime;
class QVariant;
/** Misc utilities. */
class Util : public QObject
class Util
{
Q_OBJECT
Q_GADGET
public:
explicit Util(QObject *parent = nullptr);
~Util() override;
// workarounds for JS not preserving timezones
/** Read a QDateTime property with the timezone stripped off. */
Q_INVOKABLE QDateTime dateTimeStripTimezone(const QVariant &obj, const QString &propertyName) const;
Q_INVOKABLE static QDateTime dateTimeStripTimezone(const QVariant &obj, const QString &propertyName);
/** Set a QDateTime property preserving the timezone of the current value. */
Q_INVOKABLE QVariant setDateTimePreserveTimezone(const QVariant &obj, const QString &propertyName, QDateTime value) const;
Q_INVOKABLE static QVariant setDateTimePreserveTimezone(const QVariant &obj, const QString &propertyName, QDateTime value);
/** Checks whether @p text is rich-text. */
Q_INVOKABLE bool isRichText(const QString &text) const;
Q_INVOKABLE static bool isRichText(const QString &text);
/** Convert links, email address and phone numbers in the given text to HTML links. */
Q_INVOKABLE QString textToHtml(const QString &text) const;
Q_INVOKABLE static QString textToHtml(const QString &text);
/** Execute the non-exported sort() method on a QAbstractItemModel. */
Q_INVOKABLE void sortModel(QObject *model, int column, Qt::SortOrder sortOrder) const;
Q_INVOKABLE static void sortModel(QObject *model, int column, Qt::SortOrder sortOrder);
/** Determine the aspect ratio of an SVG file.
* This is a dirty workaround for the problem that Kirigami.Icon has its implicit size hardcoded to 32x32...
*/
Q_INVOKABLE float svgAspectRatio(const QString &svgFilePath) const;
Q_INVOKABLE static float svgAspectRatio(const QString &svgFilePath);
};
#endif // UTIL_H
......@@ -58,14 +58,14 @@ int main(int argc, char **argv)
req.setIncludeIntermediateStops(true);
qmlRegisterType<QSortFilterProxyModel>("org.kde.itinerary", 1, 0, "SortFilterProxyModel");
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Localizer", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { \
return engine->toScriptValue(Localizer()); \
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Localizer", [](QQmlEngine*, QJSEngine *engine) -> QJSValue {
return engine->toScriptValue(Localizer());
});
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "PublicTransport", [](QQmlEngine*, QJSEngine *engine) -> QJSValue { \
return engine->toScriptValue(PublicTransport()); \
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "PublicTransport", [](QQmlEngine*, QJSEngine *engine) -> QJSValue {
return engine->toScriptValue(PublicTransport());
});
qmlRegisterSingletonType<Util>("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine*) -> QObject*{
return new Util;
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine *engine) -> QJSValue {
return engine->toScriptValue(Util());
});
QQmlApplicationEngine engine;
......
......@@ -47,8 +47,8 @@ int main(int argc, char **argv)
qmlRegisterUncreatableType<KPkPass::Field>("org.kde.pkpass", 1, 0, "Field", {});
qmlRegisterUncreatableType<KPkPass::Pass>("org.kde.pkpass", 1, 0, "Pass", {});
qmlRegisterUncreatableType<KPkPass::BoardingPass>("org.kde.pkpass", 1, 0, "BoardingPass", {});
qmlRegisterSingletonType<Util>("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine*) -> QObject*{
return new Util;
qmlRegisterSingletonType("org.kde.itinerary", 1, 0, "Util", [](QQmlEngine*, QJSEngine *engine) -> QJSValue {
return engine->toScriptValue(Util());
});
QQmlApplicationEngine engine;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment