File indexing completed on 2024-05-05 16:07:09

0001 /*
0002     SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDECLARATIVE_H
0008 #define KDECLARATIVE_H
0009 
0010 #include <QQmlEngine>
0011 
0012 #include <kdeclarative/kdeclarative_export.h>
0013 
0014 #include <QStringList>
0015 
0016 class QQmlEngine;
0017 
0018 namespace KDeclarative
0019 {
0020 class KDeclarativePrivate;
0021 
0022 /**
0023  * @class KDeclarative::KDeclarative kdeclarative.h <KDeclarative/KDeclarative>
0024  *
0025  * The KDeclarative class is used to manipulate the QQmlEngine instance used by
0026  * the application and to get some information about the platform,
0027  * that influences the behavior of the QML components.
0028  *
0029  * In order to use it, you will need a pointer to a QQmlEngine, and call
0030  * `setupEngine(engine)` at least once on the engine.
0031  * @code
0032  *     KDeclarative::KDeclarative::setupEngine(engine);  // if not done elsewhere
0033  * @endcode
0034  *
0035  * To setup integration with KI18n's translation methods and thus being able to call
0036  * i18n() from anywhere in your QML code., you set a KLocalizedContext from Ki18n
0037  * directly like this (since KF 5.17):
0038  * @code
0039  *     KLocalizedContext *localizedContextObject = new KLocalizedContext(engine);
0040  *     // if not using the global application domain, set custom domain name
0041  *     localizedContextObject->setTranslationDomain(QStringLiteral("mydomainname"));
0042  *     engine->rootContext()->setContextObject(localizedContextObject);
0043  * @endcode
0044  *
0045  * In case your code should work with KF versions before 5.17, use these deprecated
0046  * calls instead:
0047  * @code
0048  *     KDeclarative::KDeclarative decl;
0049  *     decl.setDeclarativeEngine(engine);
0050  *     // if not using the global application domain, set custom domain name
0051  *     decl.setTranslationDomain(QStringLiteral("mydomainname"));
0052  *     decl.setupContext();
0053  * @endcode
0054  *
0055  * @deprecated since 5.100.
0056  *
0057  * To set up translations use KLocalizedContext instead.
0058  *
0059  * Set the KQuickIconProvider from KIconThemes and the QQmlNetworkAccessManagerFactory creating a KIO::Integration::AccessManager
0060  * manually up if needed
0061  *
0062  */
0063 class KDECLARATIVE_EXPORT KDeclarative
0064 {
0065 public:
0066 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 100)
0067     KDECLARATIVE_DEPRECATED_VERSION(5, 100, "See class API docs")
0068     explicit KDeclarative();
0069 #endif
0070     ~KDeclarative();
0071 
0072     KDeclarative(const KDeclarative &) = delete;
0073     KDeclarative &operator=(const KDeclarative &) = delete;
0074 
0075 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 0)
0076     /**
0077      * @deprecated since 5.0. The method is a no-op now, any call can be simply removed.
0078      */
0079     KDECLARATIVE_DEPRECATED_VERSION(5, 0, "Is a no-op")
0080     void initialize();
0081 #endif
0082 
0083 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 45)
0084     /**
0085      * Call this after setDeclarativeEngine to set the i18n global functions, the runtime platform, etc
0086      *
0087      * @deprecated since 5.45 use setupContext() and setupEngine()
0088      */
0089     KDECLARATIVE_DEPRECATED_VERSION(5, 45, "Call setupContext() and setupEngine() independently")
0090     void setupBindings();
0091 #endif
0092 
0093 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 75)
0094     /**
0095      * Call this after setDeclarativeEngine to set the i18n global functions.
0096      *
0097      * @sa setupEngine
0098      * @since 5.45
0099      * @deprecated Since 5.17 set KLocalizedContext directly, see documentation in KI18n::KLocalizedContext
0100      */
0101     KDECLARATIVE_DEPRECATED_VERSION_BELATED(5, 75, 5, 17, "set KLocalizedContext directly, see documentation in KI18n::KLocalizedContext")
0102     void setupContext();
0103 #endif
0104 
0105 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 100)
0106     /**
0107      * Assign a specific QQmlEngine to be used in this KDeclarative.
0108      *
0109      * A KDeclarative object works with a specific QQmlEngine. There
0110      * is no default engine, so you **must** call this function with a
0111      * non-null pointer to an engine before calling setupBindings()
0112      * or setupContext(), which set properties on the engine.
0113      *
0114      * The KDeclarative object does not take ownership of the engine.
0115      *
0116      * @param engine the engine to use in this KDeclarative object
0117      * @sa setupContext(), setupEngine()
0118      * @since 5.0
0119      */
0120     void setDeclarativeEngine(QQmlEngine *engine);
0121 #endif
0122 
0123 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 100)
0124     /**
0125      * @return the engine assigned to this KDeclarative.
0126      *         The engine may be a @c nullptr . No ownership is transferred.
0127      * @sa setDeclarativeEngine(), setupEngine()
0128      * @since 5.0
0129      */
0130     QQmlEngine *declarativeEngine() const;
0131 #endif
0132 
0133 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 75)
0134     /**
0135      * Call this method before calling setupContext to install a translation domain for all
0136      * i18n global functions. If a translation domain is set all i18n calls delegate to the
0137      * matching i18nd calls with the provided translation domain.
0138      *
0139      * The translationDomain affects all i18n calls including those from imports. Because of
0140      * that modules intended to be used as imports should prefer the i18nd variants and set
0141      * the translation domain explicitly in each call.
0142      *
0143      * This method is only required if your declarative usage is inside a library. If it's
0144      * in an application there is no need to set the translation domain as the application's
0145      * domain can be used.
0146      *
0147      * @param translationDomain The translation domain to be used for i18n calls.
0148      * @since 5.0
0149      * @deprecated Since 5.17 use KLocalizedContext::setTranslationDomain
0150      */
0151     KDECLARATIVE_DEPRECATED_VERSION_BELATED(5, 75, 5, 17, "set via KLocalizedContext::setTranslationDomain")
0152     void setTranslationDomain(const QString &translationDomain);
0153 #endif
0154 
0155 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 75)
0156     /**
0157      * @return the translation domain for the i18n calls done in this QML engine
0158      * @since 5.0
0159      * @deprecated Since 5.17 use KLocalizedContext::translationDomain
0160      */
0161     KDECLARATIVE_DEPRECATED_VERSION_BELATED(5, 75, 5, 17, "available via KLocalizedContext::translationDomain")
0162     QString translationDomain() const;
0163 #endif
0164 
0165 #if KDECLARATIVE_ENABLE_DEPRECATED_SINCE(5, 98)
0166     /**
0167      * This method must be called very early at startup time to ensure the
0168      * QQuickDebugger is enabled. Ideally it should be called in main(),
0169      * after command-line options are defined.
0170      * @since 5.0
0171      * @deprecated Since 5.98, deprecated for lack of usage. Set the debugger manually up
0172      */
0173     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "Deprecated for lack of usage. Set the debugger manually up")
0174     static void setupQmlJsDebugger();
0175 
0176     /**
0177      * @return the runtime platform, e.g. "desktop" or "tablet, touch". The first entry/ies in
0178      *         the list relate to the platform formfactor and the last is the input method
0179      *         specialization. If the string is empty, there is no specified runtime platform
0180      *         and a traditional desktop environment may be assumed
0181      * @since 4.10
0182      * @deprecated Since 5.98, use KRuntimePlatform::runtimePlatform instead
0183      */
0184     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "Use KRuntimePlatform::runtimePlatform instead")
0185     static QStringList runtimePlatform();
0186 
0187     /**
0188      * Sets the runtime platform from now on, globally to the process.
0189      * Already loaded QML components won't be affected.
0190      * @since 5.0
0191      * @deprecated Since 5.98, deprecated for lack of usage, set PLASMA_PLATFORM ENV variable instead
0192      */
0193     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "Deprecated for lack of usage, set PLASMA_PLATFORM ENV variable instead")
0194     static void setRuntimePlatform(const QStringList &platform);
0195 
0196     /**
0197      * @return the QML components target, based on the runtime platform. e.g. touch or desktop
0198      * @since 4.10
0199      * @deprecated Since 5.98, deprecated for lack of usage, use last value from KRuntimePlatform::runtimePlatform instead
0200      */
0201     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "Deprecated for lack of usage, use last value from KRuntimePlatform::runtimePlatform instead")
0202     static QString componentsTarget();
0203 
0204     /**
0205      * @return the default components target; can be used to compare against the returned value
0206      *         from @see componentsTarget()
0207      * @since 4.10
0208      * @deprecated Since 5.98, deprecated for lack of usage, use "desktop" as hardcoded value instead
0209      */
0210     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "Deprecated for lack of usage, use \"desktop\" as hardcoded value instead")
0211     static QString defaultComponentsTarget();
0212 
0213     /**
0214      * Setup a QML engine for use with any KDeclarative object.
0215      *
0216      * This needs to be done only once per QQmlEngine instance. An
0217      * engine that is shared between KDeclarative objects only needs
0218      * to be setup once. The engine is setup for the component target
0219      * (runtime platform) that is configured at the time setupEngine()
0220      * is called. The following things are added to the engine:
0221      * - a KIOAccessManagerFactory, replacing any existing stock QQmlNetworkAccessManagerFactory
0222      * - a QML icon provider, enabling the Image {} element to load images using the scheme "image:/"
0223      *
0224      * @param engine the engine to setup
0225      * @sa setupContext(), componentsTarget()
0226      * @since 5.45
0227      * @deprecated Since 5.98, set the KQuickIconProvider from KIconThemes and the QQmlNetworkAccessManagerFactory creating a KIO::Integration::AccessManager
0228      * manually up if needed
0229      */
0230     KDECLARATIVE_DEPRECATED_VERSION(5, 98, "See API docs")
0231     static void setupEngine(QQmlEngine *engine);
0232 #endif
0233 
0234 private:
0235     KDeclarativePrivate *const d;
0236     friend class QmlObject;
0237 };
0238 
0239 }
0240 
0241 #endif