aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/contentplugin.qdoc
blob: 3671309a6a2b9ec249252aa821377e5d4cc11f47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/****************************************************************************
**
** Copyright (C) 2015 Pelagicore AG
** Contact: http://www.qt.io/ or http://www.pelagicore.com/
**
** This file is part of the QmlLive tool.
**
** $QT_BEGIN_LICENSE:GPL3-PELAGICORE$
** Commercial License Usage
** Licensees holding valid commercial Pelagicore Application Manager
** licenses may use this file in accordance with the commercial license
** agreement provided with the Software or, alternatively, in accordance
** with the terms contained in a written agreement between you and
** Pelagicore. For licensing terms and conditions, contact us at:
** http://www.pelagicore.com.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3 requirements will be
** met: http://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/
/*!
    \example contentplugin
    \title ContentPlugin Example

    \brief The ContentPlugin example demonstrates how to write a
    Content Plugin for QmlLive

    \image contentplugin-example.png Screenshot of the Plugin in Action

    The Plugin written in this example displays Images like the
    builtin Imageviewer in QmlLive, but it shows the content rotated
    only works on *.png files.

    We will start by reviewing the interface defined in \c
    contentadapterinterface.h in the QmlLive source code. This
    interface can be used to add a new ContentAdapter to QmlLive.
    The ContentAdapter will be used to display any content you want
    to be handled not directly by the LiveRuntime like displaying a image.

    \snippet contentadapterinterface.h 0

    The \c ContentAdapterInterface class declares four functions. The first
    function \c canAdapt(const QUrl&) returns whether the plugin can display the
    given file or directory.

    The second function \c adapt(const QUrl& url, QDeclarativeContext* context)
    returns a custom QUrl which will be used by QmlLive to display the given QUrl.
    The returned QUrl always has to point to a qml file used to display the content.
    To be able to control the returned qml File \c context can be used to set custom
    Properties which will be exported to the qml File.

    canPreview() and preview() are used for generating preview thumbnails.
    We use the easiest implementation for this two methods.

   \section1 MyContentAdapterPlugin

   \snippet contentplugin/mycontentadapterplugin.h 0

   The MyContentAdapterPlugin implements the interface to QmlLive. It subclasses
   QObject and the needed \c ContentAdapterInterface.

   \snippet contentplugin/mycontentadapterplugin.h 1

   The \c Q_INTERFACES macro will be used to register the Plugin to Qt's Plugin
   System.


   To be called only for the right file type we have to overload the
   \c canAdapt(const QUrl&) function. We only want to be loaded for *.png files.
   That's why we check the file ending on the given url and return true when it's
   an png file.

   \snippet contentplugin/mycontentadapterplugin.cpp 0

   If the plugin accepts the file \c adapt(const QUrl& url, QDeclarativeContext* context)
   will be called. Here we export the path to the image to a special property in the
   context to be able to access the fileName from within our qml File.
   Afterwards we return a QUrl pointing to our qml File which is inside a ResourceFile.

   \snippet contentplugin/mycontentadapterplugin.cpp 1

   Now only the implementation of canPreview() and preview() are missing. Here we just
   add some dummy implementation because we don't want to add this functionality.

   \snippet contentplugin/mycontentadapterplugin.cpp 2

   \section1 plugin.qml

   The plugin.qml file is our ImageViewer. In it we create a \c Image Element and
   set the source to our exported Property \c imageSource. The rotation Property
   will be set to 180 to rotate the Image 180 degrees.

   \snippet contentplugin/plugin.qml 0

   \section1 The Resource File

   Because we don't want to fiddle around with paths we include our qml File into
   a Resource File. The most important thing in the Resource File is to use an
   unused Prefix. Otherwise our file can't be found because QmlLive also uses
   Resource Files. The best approach is to use the plugin name as prefix.
*/