assetparametermodel.hpp 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/***************************************************************************
 *   Copyright (C) 2017 by Nicolas Carion                                  *
 *   This file is part of Kdenlive. See www.kdenlive.org.                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3 or any later version accepted by the       *
 *   membership of KDE e.V. (or its successor approved  by the membership  *
 *   of KDE e.V.), which shall act as a proxy defined in Section 14 of     *
 *   version 3 of the license.                                             *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#ifndef ASSETPARAMETERMODEL_H
#define ASSETPARAMETERMODEL_H

Nicolas Carion's avatar
Nicolas Carion committed
25 26
#include "definitions.h"
#include "klocalizedstring.h"
27
#include <QAbstractListModel>
28
#include <QDomElement>
29
#include <QUndoCommand>
30 31
#include <unordered_map>

32
#include <memory>
Nicolas Carion's avatar
Nicolas Carion committed
33
#include <mlt++/MltProperties.h>
34

35
class KeyframeModelList;
36
/* @brief This class is the model for a list of parameters.
Nicolas Carion's avatar
Nicolas Carion committed
37 38
   The behaviour of a transition or an effect is typically  controlled by several parameters. This class exposes this parameters as a list that can be rendered
   using the relevant widgets.
39
   Note that internally parameters are not sorted in any ways, because some effects like sox need a precise order
40 41 42

 */

Nicolas Carion's avatar
Nicolas Carion committed
43
enum class ParamType {
44 45 46 47
    Double,
    List,
    Bool,
    Switch,
48
    RestrictedAnim, // animated 1 dimensional param with linear support only
49
    Animated,
50
    AnimatedRect,
51 52
    Geometry,
    Addedgeometry,
53
    KeyframeParam,
54
    Color,
55
    ColorWheel,
56 57 58 59 60 61 62 63 64 65 66
    Position,
    Curve,
    Bezier_spline,
    Roto_spline,
    Wipe,
    Url,
    Keywords,
    Fontfamily,
    Filterjob,
    Readonly
};
67
Q_DECLARE_METATYPE(ParamType)
68
class AssetParameterModel : public QAbstractListModel, public enable_shared_from_this_virtual<AssetParameterModel>
69 70 71 72
{
    Q_OBJECT

public:
73
    explicit AssetParameterModel(Mlt::Properties *asset, const QDomElement &assetXml, const QString &assetId, ObjectId ownerId, QObject *parent = nullptr);
74
    virtual ~AssetParameterModel();
Nicolas Carion's avatar
Nicolas Carion committed
75 76 77 78 79 80 81 82 83 84
    enum {
        NameRole = Qt::UserRole + 1,
        TypeRole,
        CommentRole,
        MinRole,
        MaxRole,
        DefaultRole,
        SuffixRole,
        DecimalsRole,
        ValueRole,
85
        AlphaRole,
Nicolas Carion's avatar
Nicolas Carion committed
86 87 88
        ListValuesRole,
        ListNamesRole,
        FactorRole,
89
        FilterRole,
90
        ScaleRole,
Nicolas Carion's avatar
Nicolas Carion committed
91 92
        OpacityRole,
        InRole,
93 94 95
        OutRole,
        ParentInRole,
        ParentDurationRole
Nicolas Carion's avatar
Nicolas Carion committed
96
    };
97

98
    /* @brief Returns the id of the asset represented by this object */
99
    QString getAssetId() const;
100

101 102
    /* @brief Set the parameter with given name to the given value
     */
103
    Q_INVOKABLE void setParameter(const QString &name, const QString &value, bool update = true);
104
    void setParameter(const QString &name, const int value, bool update = true);
105
    Q_INVOKABLE void setParameter(const QString &name, double &value);
106

107
    /* @brief Return all the parameters as pairs (parameter name, parameter value) */
Nicolas Carion's avatar
Nicolas Carion committed
108
    QVector<QPair<QString, QVariant>> getAllParameters() const;
109 110 111 112

    /* @brief Sets the value of a list of parameters
       @param params contains the pairs (parameter name, parameter value)
     */
Nicolas Carion's avatar
Nicolas Carion committed
113
    void setParameters(const QVector<QPair<QString, QVariant>> &params);
114

115 116 117 118
    /* Which monitor is attached to this asset (clip/project)
    */
    Kdenlive::MonitorId monitorId;

119 120
    QVariant data(const QModelIndex &index, int role) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
121 122 123

    /* @brief Returns the id of the actual object associated with this asset */
    ObjectId getOwnerId() const;
124

125 126 127
    /* @brief Returns the keyframe model associated with this asset
       Return empty ptr if there is no keyframable parameter in the asset or if prepareKeyframes was not called
     */
128
    Q_INVOKABLE std::shared_ptr<KeyframeModelList> getKeyframeModel();
129 130 131

    /* @brief Must be called before using the keyframes of this model */
    void prepareKeyframes();
132
    void resetAsset(Mlt::Properties *asset);
133

134
protected:
135
    /* @brief Helper function to retrieve the type of a parameter given the string corresponding to it*/
Nicolas Carion's avatar
Nicolas Carion committed
136
    static ParamType paramTypeFromStr(const QString &type);
137

138 139
    static QString getDefaultKeyframes(int start, const QString &defaultValue, bool linearOnly);

140
    /* @brief Helper function to get an attribute from a dom element, given its name.
141 142
       The function additionally parses following keywords:
       - %width and %height that are replaced with profile's height and width.
143
       If keywords are found, mathematical operations are supported for double type params. For example "%width -1" is a valid value.
144
    */
145
    static QVariant parseAttribute(const QString &attribute, const QDomElement &element, QVariant defaultValue = QVariant());
146 147 148 149 150 151

    /* @brief Helper function to register one more parameter that is keyframable.
       @param index is the index corresponding to this parameter
    */
    void addKeyframeParam(const QModelIndex index);

Nicolas Carion's avatar
Nicolas Carion committed
152 153
    struct ParamRow
    {
154 155 156
        ParamType type;
        QDomElement xml;
        QVariant value;
157
        QString name;
158 159 160
    };

    QDomElement m_xml;
161
    QString m_assetId;
162
    ObjectId m_ownerId;
163
    std::vector<QString> m_paramOrder;                   // Keep track of parameter order, important for sox
Nicolas Carion's avatar
Nicolas Carion committed
164 165
    std::unordered_map<QString, ParamRow> m_params;      // Store all parameters by name
    std::unordered_map<QString, QVariant> m_fixedParams; // We store values of fixed parameters aside
166
    QVector<QString> m_rows;                             // We store the params name in order of parsing. The order is important (cf some effects like sox)
167 168

    std::unique_ptr<Mlt::Properties> m_asset;
169 170

    std::shared_ptr<KeyframeModelList> m_keyframes;
171 172 173

signals:
    void modelChanged();
174
    void compositionTrackChanged();
175
    void replugEffect(std::shared_ptr<AssetParameterModel> asset);
176
    void rebuildEffect(std::shared_ptr<AssetParameterModel> asset);
177 178 179
};

#endif