Commit 24d710f7 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Updated logic for effect favorites.

They now appear in a folder above all other catagories and can be added / removed with right click menu
parent 3405e22f
......@@ -57,9 +57,6 @@ public:
/* @brief Return description of asset */
QString getDescription(const QString &assetId) const;
/* @brief Set an asset as favorite (or not)*/
virtual void setFavorite(const QString &assetId, bool favorite) = 0;
/* @brief Returns a DomElement representing the asset's properties */
QDomElement getXml(const QString &assetId) const;
......@@ -79,7 +76,6 @@ protected:
void init();
virtual Mlt::Properties *retrieveListFromMlt() const = 0;
virtual void parseFavorites() = 0;
/* @brief Parse some info from a mlt structure
@param res Datastructure to fill
......@@ -113,7 +109,6 @@ protected:
std::unordered_map<QString, Info> m_assets;
QSet<QString> m_blacklist;
QSet<QString> m_favorites;
};
#include "abstractassetsrepository.ipp"
......
......@@ -44,7 +44,6 @@ template <typename AssetType> void AbstractAssetsRepository<AssetType>::init()
// Parse effects blacklist
parseBlackList(assetBlackListPath());
parseFavorites();
// Retrieve the list of MLT's available assets.
QScopedPointer<Mlt::Properties> assets(retrieveListFromMlt());
......
......@@ -25,6 +25,7 @@
#include "abstractmodel/treeitem.hpp"
#include "assettreemodel.hpp"
#include <utility>
#include <klocalizedstring.h>
AssetFilter::AssetFilter(QObject *parent)
: QSortFilterProxyModel(parent)
......@@ -45,6 +46,17 @@ void AssetFilter::setFilterName(bool enabled, const QString &pattern)
}
}
bool AssetFilter::lessThan(const QModelIndex &left,
const QModelIndex &right) const
{
QString leftData = sourceModel()->data(left).toString();
QString rightData = sourceModel()->data(right).toString();
if (rightData == i18n("Favorites")) {
return false;
}
return QString::localeAwareCompare(leftData, rightData) < 0;
}
bool AssetFilter::filterName(const std::shared_ptr<TreeItem> &item) const
{
if (!m_name_enabled) {
......
......@@ -59,7 +59,7 @@ public:
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
bool filterName(const std::shared_ptr<TreeItem> &item) const;
/* @brief Apply all filter and returns true if the object should be kept after filtering */
virtual bool applyAll(std::shared_ptr<TreeItem> item) const;
......
......@@ -67,28 +67,6 @@ bool AssetTreeModel::isFavorite(const QModelIndex &index) const
return item->dataColumn(AssetTreeModel::favCol).toBool();
}
void AssetTreeModel::setFavorite(const QModelIndex &index, bool favorite, bool isEffect)
{
if (!index.isValid()) {
return;
}
std::shared_ptr<TreeItem> item = getItemById((int)index.internalId());
if (isEffect && item->depth() == 1) {
return;
}
item->setData(AssetTreeModel::favCol, favorite);
auto id = item->dataColumn(AssetTreeModel::idCol).toString();
if (isEffect) {
if (EffectsRepository::get()->exists(id)) {
EffectsRepository::get()->setFavorite(id, favorite);
}
} else {
if (TransitionsRepository::get()->exists(id)) {
TransitionsRepository::get()->setFavorite(id, favorite);
}
}
}
QString AssetTreeModel::getDescription(const QModelIndex &index) const
{
if (!index.isValid()) {
......@@ -121,6 +99,7 @@ QVariant AssetTreeModel::data(const QModelIndex &index, int role) const
case FavoriteRole:
return item->dataColumn(AssetTreeModel::favCol);
case NameRole:
case Qt::DisplayRole:
return item->dataColumn(index.column());
default:
return QVariant();
......
......@@ -44,10 +44,10 @@ public:
QString getDescription(const QModelIndex &index) const;
// Helper function to retrieve if an effect is categorized as favorite
bool isFavorite(const QModelIndex &index) const;
void setFavorite(const QModelIndex &index, bool favorite, bool isEffect);
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
virtual void reloadAssetMenu(QMenu *effectsMenu, KActionCategory *effectActions) = 0;
virtual void setFavorite(const QModelIndex &index, bool favorite, bool isEffect) = 0;
// for convenience, we store the column of each data field
static int nameCol, idCol, favCol, typeCol;
......
......@@ -76,6 +76,7 @@ bool AssetListWidget::isFavorite(const QModelIndex &index) const
void AssetListWidget::setFavorite(const QModelIndex &index, bool favorite, bool isEffect)
{
m_model->setFavorite(m_proxyModel->mapToSource(index), favorite, isEffect);
m_proxyModel->sort(0);
}
QString AssetListWidget::getDescription(const QModelIndex &index) const
......
......@@ -43,11 +43,7 @@ EffectTreeModel::EffectTreeModel(QObject *parent)
std::shared_ptr<EffectTreeModel> EffectTreeModel::construct(const QString &categoryFile, QObject *parent)
{
std::shared_ptr<EffectTreeModel> self(new EffectTreeModel(parent));
QList<QVariant> rootData;
rootData << "Name"
<< "ID"
<< "Type"
<< "isFav";
QList<QVariant> rootData {"Name", "ID", "Type", "isFav"};
self->rootItem = TreeItem::construct(rootData, self, true);
QHash<QString, std::shared_ptr<TreeItem>> effectCategory; // category in which each effect should land.
......@@ -62,7 +58,9 @@ std::shared_ptr<EffectTreeModel> EffectTreeModel::construct(const QString &categ
file.close();
QDomNodeList groups = doc.documentElement().elementsByTagName(QStringLiteral("group"));
// Create favorite group
auto groupFav = self->rootItem->appendChild(QList<QVariant>{i18n("Favorites"), QStringLiteral("root")});
auto groupFav = self->rootItem->appendChild(QList<QVariant>{i18n("Favorites"), QStringLiteral("root"), -1});
effectCategory[QStringLiteral("kdenlive:favorites")] = groupFav;
auto groupLegacy = self->rootItem->appendChild(QList<QVariant>{i18n("Legacy"), QStringLiteral("root")});
......@@ -72,14 +70,9 @@ std::shared_ptr<EffectTreeModel> EffectTreeModel::construct(const QString &categ
continue;
}
QStringList list = groups.at(i).toElement().attribute(QStringLiteral("list")).split(QLatin1Char(','), QString::SkipEmptyParts);
auto groupItem = self->rootItem->appendChild(QList<QVariant>{groupName, QStringLiteral("root")});
for (const QString &effect : list) {
if (KdenliveSettings::favorite_effects().contains(effect)) {
effectCategory[effect] = groupFav;
} else {
effectCategory[effect] = groupItem;
}
effectCategory[effect] = groupItem;
}
}
// We also create "Misc", "Audio" and "Custom" categories
......@@ -95,6 +88,7 @@ std::shared_ptr<EffectTreeModel> EffectTreeModel::construct(const QString &categ
// We parse effects
auto allEffects = EffectsRepository::get()->getNames();
QString favCategory = QStringLiteral("kdenlive:favorites");
for (const auto &effect : allEffects) {
if (!KdenliveSettings::gpu_accel() && effect.first.contains(QLatin1String("movit."))) {
continue;
......@@ -114,7 +108,10 @@ std::shared_ptr<EffectTreeModel> EffectTreeModel::construct(const QString &categ
// we create the data list corresponding to this profile
bool isFav = KdenliveSettings::favorite_effects().contains(effect.first);
//qDebug() << effect.second << effect.first << "in " << targetCategory->dataColumn(0).toString();
QList<QVariant> data {effect.second, effect.first, QVariant::fromValue(type), isFav};
QList<QVariant> data {effect.second, effect.first, QVariant::fromValue(type), isFav, targetCategory->row()};
if (KdenliveSettings::favorite_effects().contains(effect.first) && effectCategory.contains(favCategory)) {
targetCategory = effectCategory[favCategory];
}
targetCategory->appendChild(data);
}
return self;
......@@ -149,3 +146,36 @@ void EffectTreeModel::reloadAssetMenu(QMenu *effectsMenu, KActionCategory *effec
}
}
}
void EffectTreeModel::setFavorite(const QModelIndex &index, bool favorite, bool isEffect)
{
if (!index.isValid()) {
return;
}
std::shared_ptr<TreeItem> item = getItemById((int)index.internalId());
if (isEffect && item->depth() == 1) {
return;
}
item->setData(AssetTreeModel::favCol, favorite);
auto id = item->dataColumn(AssetTreeModel::idCol).toString();
if (!EffectsRepository::get()->exists(id)) {
qDebug()<<"Trying to reparent unknown asset: "<<id;
return;
}
int max = rootItem->childCount();
QStringList favs = KdenliveSettings::favorite_effects();
if (!favorite) {
int ix = item->dataColumn(4).toInt();
item->changeParent(rootItem->child(ix));
favs.removeAll(id);
} else {
for (int i = 0; i < max; i++) {
if (rootItem->child(i)->dataColumn(2).toInt() == -1) {
item->changeParent(rootItem->child(i));
break;
}
}
favs << id;
}
KdenliveSettings::setFavorite_effects(favs);
}
......@@ -39,7 +39,7 @@ public:
static std::shared_ptr<EffectTreeModel> construct(const QString &categoryFile, QObject *parent);
void reloadEffect(const QString &path);
void reloadAssetMenu(QMenu *effectsMenu, KActionCategory *effectActions) override;
void setFavorite(const QModelIndex &index, bool favorite, bool isEffect) override;
protected:
std::shared_ptr<TreeItem> m_customCategory;
};
......
......@@ -61,22 +61,6 @@ Mlt::Properties *EffectsRepository::retrieveListFromMlt() const
return pCore->getMltRepository()->filters();
}
void EffectsRepository::parseFavorites()
{
m_favorites = KdenliveSettings::favorite_effects().toSet();
}
void EffectsRepository::setFavorite(const QString &id, bool favorite)
{
Q_ASSERT(exists(id));
if (favorite) {
m_favorites << id;
} else {
m_favorites.remove(id);
}
KdenliveSettings::setFavorite_effects(QStringList::fromSet(m_favorites));
}
Mlt::Properties *EffectsRepository::getMetadata(const QString &effectId)
{
return pCore->getMltRepository()->metadata(filter_type, effectId.toLatin1().data());
......
......@@ -49,7 +49,6 @@ public:
std::unique_ptr<Mlt::Filter> getEffect(const QString &effectId) const;
/* @brief returns true if an effect exists in MLT (bypasses the blacklist/metadata parsing) */
bool hasInternalEffect(const QString &effectId) const;
void setFavorite(const QString &id, bool favorite) override;
QPair<QString, QString> reloadCustom(const QString &path);
protected:
......@@ -59,9 +58,6 @@ protected:
/* Retrieves the list of all available effects from Mlt*/
Mlt::Properties *retrieveListFromMlt() const override;
/* Retrieves the list of favorite effects */
void parseFavorites() override;
/* @brief Retrieves additional info about effects from a custom XML file
The resulting assets are stored in customAssets
*/
......
......@@ -36,11 +36,7 @@ TransitionTreeModel::TransitionTreeModel(QObject *parent)
std::shared_ptr<TransitionTreeModel> TransitionTreeModel::construct(bool flat, QObject *parent)
{
std::shared_ptr<TransitionTreeModel> self(new TransitionTreeModel(parent));
QList<QVariant> rootData;
rootData << "Name"
<< "ID"
<< "Type"
<< "isFav";
QList<QVariant> rootData {"Name", "ID", "Type", "isFav"};
self->rootItem = TreeItem::construct(rootData, self, true);
// We create categories, if requested
......@@ -94,3 +90,26 @@ void TransitionTreeModel::reloadAssetMenu(QMenu *effectsMenu, KActionCategory *e
}
}
}
void TransitionTreeModel::setFavorite(const QModelIndex &index, bool favorite, bool isEffect)
{
if (!index.isValid()) {
return;
}
std::shared_ptr<TreeItem> item = getItemById((int)index.internalId());
if (isEffect && item->depth() == 1) {
return;
}
item->setData(AssetTreeModel::favCol, favorite);
auto id = item->dataColumn(AssetTreeModel::idCol).toString();
QStringList favs = KdenliveSettings::favorite_effects();
if (favorite) {
favs << id;
} else {
favs.removeAll(id);
}
KdenliveSettings::setFavorite_effects(favs);
/*if (TransitionsRepository::get()->exists(id)) {
TransitionsRepository::get()->setFavorite(id, favorite);
}*/
}
......@@ -38,6 +38,7 @@ public:
// if flat = true, then the categories are not created
static std::shared_ptr<TransitionTreeModel> construct(bool flat = false, QObject *parent = nullptr);
void reloadAssetMenu(QMenu *effectsMenu, KActionCategory *effectActions) override;
void setFavorite(const QModelIndex &index, bool favorite, bool isEffect) override;
protected:
};
......
......@@ -60,22 +60,6 @@ Mlt::Properties *TransitionsRepository::retrieveListFromMlt() const
return pCore->getMltRepository()->transitions();
}
void TransitionsRepository::parseFavorites()
{
m_favorites = KdenliveSettings::favorite_transitions().toSet();
}
void TransitionsRepository::setFavorite(const QString &id, bool favorite)
{
Q_ASSERT(exists(id));
if (favorite) {
m_favorites << id;
} else {
m_favorites.remove(id);
}
KdenliveSettings::setFavorite_transitions(QStringList::fromSet(m_favorites));
}
Mlt::Properties *TransitionsRepository::getMetadata(const QString &assetId)
{
return pCore->getMltRepository()->metadata(transition_type, assetId.toLatin1().data());
......
......@@ -52,7 +52,6 @@ public:
/* @brief Returns the id of the transition to be used for compositing */
const QString getCompositingTransition();
void setFavorite(const QString &id, bool favorite) override;
protected:
// Constructor is protected because class is a Singleton
......@@ -61,9 +60,6 @@ protected:
/* Retrieves the list of all available effects from Mlt*/
Mlt::Properties *retrieveListFromMlt() const override;
/* Retrieves the list of favorite effects */
void parseFavorites() override;
/* @brief Retrieves additional info about effects from a custom XML file
The resulting assets are stored in customAssets
*/
......
Markdown is supported
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