Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 6266d020 authored by Nicolas Carion's avatar Nicolas Carion

Abstact away effecttreemodel

parent 9c0574de
set(kdenlive_SRCS
${kdenlive_SRCS}
assets/assetlist/view/qmltypes/asseticonprovider.cpp
assets/assetlist/model/assetfilter.cpp
assets/assetlist/model/assettreemodel.cpp
PARENT_SCOPE)
/***************************************************************************
* 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/>. *
***************************************************************************/
#include "assetfilter.hpp"
#include "assettreemodel.hpp"
#include "abstractmodel/treeitem.hpp"
AssetFilter::AssetFilter(QObject *parent)
: QSortFilterProxyModel(parent)
{
m_name_enabled = false;
}
void AssetFilter::setFilterName(bool enabled, const QString& pattern)
{
m_name_enabled = enabled;
m_name_value = pattern;
invalidateFilter();
}
bool AssetFilter::filterName(TreeItem* item) const
{
if (!m_name_enabled) {
return true;
}
QString itemText = item->data(AssetTreeModel::nameCol).toString();
itemText = itemText.normalized(QString::NormalizationForm_D).remove(QRegExp(QStringLiteral("[^a-zA-Z0-9\\s]")));
QString patt = m_name_value.normalized(QString::NormalizationForm_D).remove(QRegExp(QStringLiteral("[^a-zA-Z0-9\\s]")));
return itemText.contains(patt, Qt::CaseInsensitive);
}
bool AssetFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
if (sourceParent == QModelIndex()) {
//In that case, we have a category. We hide it if it does not have children.
QModelIndex category = sourceModel()->index(sourceRow, 0, sourceParent);
bool accepted = false;
for (int i = 0; i < sourceModel()->rowCount(category) && !accepted; ++i) {
accepted = filterAcceptsRow(i, category);
}
return accepted;
}
QModelIndex row = sourceModel()->index(sourceRow, 0, sourceParent);
TreeItem *item = static_cast<TreeItem*>(row.internalPointer());
return applyAll(item);
}
bool AssetFilter::isVisible(const QModelIndex &sourceIndex)
{
auto parent = sourceModel()->parent(sourceIndex);
return filterAcceptsRow(sourceIndex.row(), parent);
}
bool AssetFilter::applyAll(TreeItem* item) const
{
return filterName(item);
}
/***************************************************************************
* 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 ASSETFILTER_H
#define ASSETFILTER_H
#include <QSortFilterProxyModel>
#include <memory>
#include "effects/effectsrepository.hpp"
/* @brief This class is used as a proxy model to filter an asset list based on given criterion (name, ...)
*/
class TreeItem;
class AssetFilter : public QSortFilterProxyModel
{
Q_OBJECT
public:
AssetFilter(QObject *parent = nullptr);
/* @brief Manage the name filter
@param enabled whether to enable this filter
@param pattern to match against effects' names
*/
void setFilterName(bool enabled, const QString& pattern);
/** @brief Returns true if the ModelIndex in the source model is visible after filtering
*/
bool isVisible(const QModelIndex &sourceIndex);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool filterName(TreeItem* item) const;
/* @brief Apply all filter and returns true if the object should be kept after filtering */
virtual bool applyAll(TreeItem *item) const;
bool m_name_enabled;
QString m_name_value;
};
#endif
/***************************************************************************
* 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/>. *
***************************************************************************/
#include "assettreemodel.hpp"
#include "effects/effectsrepository.hpp"
#include "abstractmodel/treeitem.hpp"
int AssetTreeModel::nameCol = 0;
int AssetTreeModel::idCol = 1;
int AssetTreeModel::typeCol = 2;
int AssetTreeModel::favCol = 3;
AssetTreeModel::AssetTreeModel(QObject *parent)
: AbstractTreeModel(parent)
{
}
QHash<int, QByteArray> AssetTreeModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[NameRole] = "name";
return roles;
}
QString AssetTreeModel::getName(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item->depth() == 1) {
return item->data(0).toString();
} else {
return item->data(AssetTreeModel::nameCol).toString();
}
}
QString AssetTreeModel::getDescription(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item->depth() == 1) {
return QString();
} else {
auto id = item->data(AssetTreeModel::idCol).toString();
return EffectsRepository::get()->getDescription(id);
}
}
QVariant AssetTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if(role == IdRole) {
if (item->depth() == 1) {
return "root";
} else {
return item->data(AssetTreeModel::idCol);
}
}
if (role != NameRole) {
return QVariant();
}
return item->data(index.column());
}
/***************************************************************************
* 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 ASSETTREEMODEL_H
#define ASSETTREEMODEL_H
#include "abstractmodel/abstracttreemodel.hpp"
#include <QHash>
#include <QIcon>
/* @brief This class represents an effect hierarchy to be displayed as a tree
*/
class TreeItem;
class AssetTreeModel : public AbstractTreeModel
{
public:
explicit AssetTreeModel(QObject *parent = 0);
enum {
IdRole = Qt::UserRole + 1,
NameRole
};
//Helper function to retrieve name
QString getName(const QModelIndex& index) const;
//Helper function to retrieve description
QString getDescription(const QModelIndex& index) const;
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
// for convenience, we store the column of each data field
static int nameCol, idCol, favCol, typeCol;
protected:
};
#endif
......@@ -21,32 +21,13 @@
#include "effectfilter.hpp"
#include "effecttreemodel.hpp"
#include "assets/assetlist/model/assettreemodel.hpp"
#include "abstractmodel/treeitem.hpp"
EffectFilter::EffectFilter(QObject *parent)
: QSortFilterProxyModel(parent)
: AssetFilter(parent)
{
m_name_enabled = m_type_enabled = false;
}
void EffectFilter::setFilterName(bool enabled, const QString& pattern)
{
m_name_enabled = enabled;
m_name_value = pattern;
invalidateFilter();
}
bool EffectFilter::filterName(TreeItem* item) const
{
if (!m_name_enabled) {
return true;
}
QString itemText = item->data(EffectTreeModel::nameCol).toString();
itemText = itemText.normalized(QString::NormalizationForm_D).remove(QRegExp(QStringLiteral("[^a-zA-Z0-9\\s]")));
QString patt = m_name_value.normalized(QString::NormalizationForm_D).remove(QRegExp(QStringLiteral("[^a-zA-Z0-9\\s]")));
return itemText.contains(patt, Qt::CaseInsensitive);
m_type_enabled = false;
}
void EffectFilter::setFilterType(bool enabled, EffectType type)
......@@ -62,29 +43,11 @@ bool EffectFilter::filterType(TreeItem* item) const
if (!m_type_enabled) {
return true;
}
EffectType itemType = item->data(EffectTreeModel::typeCol).value<EffectType>();
EffectType itemType = item->data(AssetTreeModel::typeCol).value<EffectType>();
return itemType == m_type_value;
}
bool EffectFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
bool EffectFilter::applyAll(TreeItem* item) const
{
if (sourceParent == QModelIndex()) {
//In that case, we have a category. We hide it if it does not have children.
QModelIndex category = sourceModel()->index(sourceRow, 0, sourceParent);
bool accepted = false;
for (int i = 0; i < sourceModel()->rowCount(category) && !accepted; ++i) {
accepted = filterAcceptsRow(i, category);
}
return accepted;
}
QModelIndex row = sourceModel()->index(sourceRow, 0, sourceParent);
TreeItem *item = static_cast<TreeItem*>(row.internalPointer());
return filterName(item) && filterType(item);
}
bool EffectFilter::isVisible(const QModelIndex &sourceIndex)
{
auto parent = sourceModel()->parent(sourceIndex);
return filterAcceptsRow(sourceIndex.row(), parent);
}
......@@ -24,43 +24,27 @@
#include <QSortFilterProxyModel>
#include <memory>
#include "effects/effectsrepository.hpp"
#include "assets/assetlist/model/assetfilter.hpp"
class TreeItem;
/* @brief This class is used as a proxy model to filter the effect tree based on given criterion (name, type)
/* @brief This class is used as a proxy model to filter the effect tree based on given criterion (name, type).
It simply adds a filter of type
*/
class EffectFilter : public QSortFilterProxyModel
class EffectFilter : public AssetFilter
{
Q_OBJECT
public:
EffectFilter(QObject *parent = nullptr);
/* @brief Manage the name filter
@param enabled whether to enable this filter
@param pattern to match against effects' names
*/
void setFilterName(bool enabled, const QString& pattern);
/* @brief Manage the type filter
@param enabled whether to enable this filter
@param type Effect type to display
*/
void setFilterType(bool enabled, EffectType type);
/** @brief Returns true if the ModelIndex in the source model is visible after filtering
*/
bool isVisible(const QModelIndex &sourceIndex);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
private:
bool filterName(TreeItem* item) const;
bool filterType(TreeItem* item) const;
bool m_name_enabled;
QString m_name_value;
bool applyAll(TreeItem *item) const override;
bool m_type_enabled;
EffectType m_type_value;
......
......@@ -32,13 +32,8 @@
#include <QDebug>
int EffectTreeModel::nameCol = 0;
int EffectTreeModel::idCol = 1;
int EffectTreeModel::typeCol = 2;
int EffectTreeModel::favCol = 3;
EffectTreeModel::EffectTreeModel(const QString &categoryFile, QObject *parent)
: AbstractTreeModel(parent)
: AssetTreeModel(parent)
{
QList<QVariant> rootData;
rootData << "Name" << "ID" << "Type" << "isFav";
......@@ -92,59 +87,3 @@ EffectTreeModel::EffectTreeModel(const QString &categoryFile, QObject *parent)
}
}
QHash<int, QByteArray> EffectTreeModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[NameRole] = "name";
return roles;
}
QString EffectTreeModel::getName(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item->depth() == 1) {
return item->data(0).toString();
} else {
return item->data(EffectTreeModel::nameCol).toString();
}
}
QString EffectTreeModel::getDescription(const QModelIndex& index) const
{
if (!index.isValid()) {
return QString();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item->depth() == 1) {
return QString();
} else {
auto id = item->data(EffectTreeModel::idCol).toString();
return EffectsRepository::get()->getDescription(id);
}
}
QVariant EffectTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if(role == IdRole) {
if (item->depth() == 1) {
return "root";
} else {
return item->data(EffectTreeModel::idCol);
}
}
if (role != NameRole) {
return QVariant();
}
return item->data(index.column());
}
......@@ -23,35 +23,19 @@
#define EFFECTTREEMODEL_H
#include "abstractmodel/abstracttreemodel.hpp"
#include "assets/assetlist/model/assettreemodel.hpp"
#include <QHash>
#include <QIcon>
/* @brief This class represents an effect hierarchy to be displayed as a tree
*/
class TreeItem;
class EffectTreeModel : public AbstractTreeModel
class EffectTreeModel : public AssetTreeModel
{
Q_OBJECT
public:
explicit EffectTreeModel(const QString &categoryFile, QObject *parent = 0);
enum {
IdRole = Qt::UserRole + 1,
NameRole
};
//Helper function to retrieve name
QString getName(const QModelIndex& index) const;
//Helper function to retrieve description
QString getDescription(const QModelIndex& index) const;
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
// for convenience, we store the column of each data field
static int nameCol, idCol, favCol, typeCol;
protected:
};
......
......@@ -25,6 +25,7 @@
#include <QQuickWidget>
#include <memory>
#include "effects/effectsrepository.hpp"
#include "../model/effecttreemodel.hpp"
#include "assets/assetlist/view/qmltypes/asseticonprovider.hpp"
/* @brief This class is a widget that display the list of available effects
......
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