File indexing completed on 2024-04-21 04:52:02

0001 /*
0002     SPDX-FileCopyrightText: 2017 Nicolas Carion
0003     SPDX-FileCopyrightText: 2022 Julius Künzel <jk.kdedev@smartlab.uber.space>
0004     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include "definitions.h" //for QString hash function
0010 #include <QReadWriteLock>
0011 #include <QString>
0012 #include <memory>
0013 #include <mutex>
0014 #include <unordered_map>
0015 
0016 class RenderPresetModel;
0017 
0018 /** @class RenderPresetRepository
0019     @brief This class is used to read all the presets available to the user (MLT defaults, Kdenlive defaults and Custom ones).
0020     You can then query presets based on their name
0021     Note that this class is a Singleton, with Mutex protections to allow concurrent access.
0022  */
0023 class RenderPresetRepository
0024 {
0025 
0026 public:
0027     // Returns the instance of the Singleton
0028     static std::unique_ptr<RenderPresetRepository> &get();
0029 
0030     /** @brief Reloads all the presets from the disk */
0031     void refresh(bool fullRefresh = false);
0032 
0033     /** @brief Returns a list of all the pairs (description, path) of all the presets loaded */
0034     QVector<QString> getAllPresets() const;
0035 
0036     QStringList groupNames() const { return m_groups; };
0037 
0038     /** @brief Returns a preset model given the presets's @param name
0039      */
0040     std::unique_ptr<RenderPresetModel> &getPreset(const QString &name);
0041 
0042     /** @brief Returns true if the given preset exists in repository
0043      */
0044     bool presetExists(const QString &name) const;
0045 
0046     static QStringList acodecs()
0047     {
0048         checkCodecs();
0049         return m_acodecsList;
0050     };
0051     static QStringList vcodecs()
0052     {
0053         checkCodecs();
0054         return m_vcodecsList;
0055     };
0056     static QStringList supportedFormats()
0057     {
0058         checkCodecs();
0059         return m_supportedFormats;
0060     };
0061 
0062     /** @brief Saves given preset
0063      *  @returns The name of the saved preset
0064      */
0065     const QString savePreset(RenderPresetModel *profile, bool editMode = false, const QString &oldName = QString());
0066 
0067     /** @brief Delete a (custom) preset*/
0068     bool deletePreset(const QString &path, bool dontRefresh = false);
0069 
0070 protected:
0071     // Constructor is protected because class is a Singleton
0072     RenderPresetRepository();
0073     void parseFile(const QString &exportFile, bool editable);
0074     void parseMltPresets();
0075 
0076     static void checkCodecs(bool forceRefresh = false);
0077 
0078     static std::unique_ptr<RenderPresetRepository> instance;
0079     static std::once_flag m_onceFlag; // flag to create the repository only once;
0080 
0081     static std::vector<std::pair<int, QString>> colorProfiles;
0082 
0083     static QStringList m_acodecsList;
0084     static QStringList m_vcodecsList;
0085     static QStringList m_supportedFormats;
0086 
0087     mutable QReadWriteLock m_mutex;
0088 
0089     /** @brief map from the presets name to the instance of the preset.
0090      * @details We use unordered_map because QMap and QHash currently don't support
0091      * move insertion, hence inserting unique_ptr is impossible.
0092      */
0093     std::unordered_map<QString, std::unique_ptr<RenderPresetModel>> m_profiles;
0094     QStringList m_groups;
0095 };