Skip to content
Snippets Groups Projects
Commit 4f7618ee authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee
Browse files

collection: Add a CollectionModel proxy to get the manageable collections

Refs #68098
parent c3b88d14
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
//Qt //Qt
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtCore/QSortFilterProxyModel>
//Ring //Ring
#include "collectionmanagerinterface.h" #include "collectionmanagerinterface.h"
...@@ -28,7 +29,17 @@ ...@@ -28,7 +29,17 @@
CollectionModel* CollectionModelPrivate::m_spInstance = nullptr; CollectionModel* CollectionModelPrivate::m_spInstance = nullptr;
CollectionModelPrivate::CollectionModelPrivate(CollectionModel* parent) : QObject(parent),q_ptr(parent) class ManageableCollectionProxy : public QSortFilterProxyModel
{
public:
ManageableCollectionProxy(QAbstractItemModel* parent) : QSortFilterProxyModel(parent)
{
setSourceModel(parent);
}
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
};
CollectionModelPrivate::CollectionModelPrivate(CollectionModel* parent) : QObject(parent),q_ptr(parent),m_pManageableProxy(nullptr)
{} {}
CollectionModel::CollectionModel(QObject* parent) : QAbstractTableModel(parent), d_ptr(new CollectionModelPrivate(this)) CollectionModel::CollectionModel(QObject* parent) : QAbstractTableModel(parent), d_ptr(new CollectionModelPrivate(this))
...@@ -62,11 +73,28 @@ CollectionModel* CollectionModel::instance() ...@@ -62,11 +73,28 @@ CollectionModel* CollectionModel::instance()
QHash<int,QByteArray> CollectionModel::roleNames() const QHash<int,QByteArray> CollectionModel::roleNames() const
{ {
static QHash<int, QByteArray> roles = QAbstractItemModel::roleNames(); static QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
/*static bool initRoles = false; static bool initRoles = false;
if (!initRoles) { if (!initRoles) {
initRoles = true; initRoles = true;
roles[static_cast<int>(Role::count ) ] = "count" ;
roles[static_cast<int>(Role::supportNone ) ] = "supportNone" ;
roles[static_cast<int>(Role::supportLoad ) ] = "supportLoad" ;
roles[static_cast<int>(Role::supportSave ) ] = "supportSave" ;
roles[static_cast<int>(Role::supportEdit ) ] = "supportEdit" ;
roles[static_cast<int>(Role::supportProbe ) ] = "supportProbe" ;
roles[static_cast<int>(Role::supportAdd ) ] = "supportAdd" ;
roles[static_cast<int>(Role::supportSave_all ) ] = "supportSave_all" ;
roles[static_cast<int>(Role::supportClear ) ] = "supportClear" ;
roles[static_cast<int>(Role::supportRemove ) ] = "supportRemove" ;
roles[static_cast<int>(Role::supportExport ) ] = "supportExport" ;
roles[static_cast<int>(Role::supportImport ) ] = "supportImport" ;
roles[static_cast<int>(Role::isEnableable ) ] = "isEnableable" ;
roles[static_cast<int>(Role::isDisableable ) ] = "isDisableable" ;
roles[static_cast<int>(Role::isManageable ) ] = "isManageable" ;
roles[static_cast<int>(Role::hasManageableChildren) ] = "hasManageableChildren" ;
}
}*/
return roles; return roles;
} }
...@@ -86,20 +114,31 @@ QVariant CollectionModel::data (const QModelIndex& idx, int role) const ...@@ -86,20 +114,31 @@ QVariant CollectionModel::data (const QModelIndex& idx, int role) const
case Qt::DecorationRole: case Qt::DecorationRole:
return item->collection->icon(); return item->collection->icon();
break; break;
case Qt::UserRole: case Qt::UserRole: // and Role::count
return 'x'+QString::number(item->collection->size()); return 'x'+QString::number(item->collection->size());
break; break;
case Qt::CheckStateRole: { case Qt::CheckStateRole: {
if (ItemModelStateSerializationDelegate::instance()) //TODO do better than that if (ItemModelStateSerializationDelegate::instance()) //TODO do better than that
return ItemModelStateSerializationDelegate::instance()->isChecked(item->collection)?Qt::Checked:Qt::Unchecked; return ItemModelStateSerializationDelegate::instance()->isChecked(item->collection)?Qt::Checked:Qt::Unchecked;
} }
case static_cast<int>(Role::hasManageableChildren):
return item->manageableCount ? true : false;
break;
}; };
//Retro-map to the SupportedFeatures //WARNING if SupportedFeatures change, this need to be updated
if (role > Qt::UserRole && role <= Qt::UserRole+14) {
return (bool) (role == Qt::UserRole+1 ? true : item->collection->supportedFeatures() & (0x01 << (role - Qt::UserRole - 2)));
}
} }
else { else {
switch(role) { switch(role) {
case Qt::DisplayRole: case Qt::DisplayRole:
return item->m_AltName; return item->m_AltName;
break; break;
case static_cast<int>(Role::hasManageableChildren):
return item->manageableCount ? true : false;
break;
} }
} }
} }
...@@ -317,9 +356,36 @@ void CollectionModelPrivate::registerNew(CollectionInterface* col) ...@@ -317,9 +356,36 @@ void CollectionModelPrivate::registerNew(CollectionInterface* col)
par->m_Children << item; par->m_Children << item;
q_ptr->endInsertRows(); q_ptr->endInsertRows();
//Make sure the manageable proxy get noticed things changed
if (col->supportedFeatures() & CollectionInterface::SupportedFeatures::MANAGEABLE) {
item->manageableCount++;
while(par) {
par->manageableCount++;
const QModelIndex& idx = q_ptr->createIndex(par->row,0,par);
emit q_ptr->dataChanged(idx,idx);
par = par->parent;
}
}
item->collection = col ; item->collection = col ;
m_hBackendsNodes[col] = item; m_hBackendsNodes[col] = item;
} }
bool ManageableCollectionProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
CollectionModelPrivate::ProxyItem* item = static_cast<CollectionModelPrivate::ProxyItem*>(sourceModel()->index(source_row,0,source_parent).internalPointer());
return item ? item->manageableCount : false;
}
/**
* Filter the CollectionModel to only keep the manageable collections. Those
* collections can be exposed and configured in the UI
*/
QAbstractItemModel* CollectionModel::manageableCollections() const
{
if (!d_ptr->m_pManageableProxy)
d_ptr->m_pManageableProxy = new ManageableCollectionProxy(const_cast<CollectionModel*>(this));
return d_ptr->m_pManageableProxy;
}
#include <collectionmodel.moc> #include <collectionmodel.moc>
...@@ -39,6 +39,28 @@ class LIB_EXPORT CollectionModel : public QAbstractTableModel ...@@ -39,6 +39,28 @@ class LIB_EXPORT CollectionModel : public QAbstractTableModel
friend class CollectionManagerInterfaceBase; friend class CollectionManagerInterfaceBase;
/**
* Imported from CollectionInterface::SupportedFeatures
*/
enum class Role {
count = Qt::UserRole,
supportNone ,
supportLoad ,
supportSave ,
supportEdit ,
supportProbe ,
supportAdd ,
supportSave_all ,
supportClear ,
supportRemove ,
supportExport ,
supportImport ,
isEnableable ,
isDisableable ,
isManageable ,
hasManageableChildren,
};
public: public:
explicit CollectionModel(QObject* parent = nullptr); explicit CollectionModel(QObject* parent = nullptr);
virtual ~CollectionModel(); virtual ~CollectionModel();
...@@ -55,6 +77,8 @@ public: ...@@ -55,6 +77,8 @@ public:
CollectionInterface* collectionAt(const QModelIndex& index); CollectionInterface* collectionAt(const QModelIndex& index);
QAbstractItemModel* manageableCollections() const;
void addExtension(CollectionExtensionInterface* extension); void addExtension(CollectionExtensionInterface* extension);
bool save(); bool save();
......
...@@ -92,6 +92,7 @@ CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const ...@@ -92,6 +92,7 @@ CollectionInterface::SupportedFeatures NumberCategory::supportedFeatures() const
{ {
return (CollectionInterface::SupportedFeatures) ( return (CollectionInterface::SupportedFeatures) (
CollectionInterface::SupportedFeatures::NONE | CollectionInterface::SupportedFeatures::NONE |
CollectionInterface::SupportedFeatures::MANAGEABLE |
CollectionInterface::SupportedFeatures::LOAD ); CollectionInterface::SupportedFeatures::LOAD );
} }
......
...@@ -18,10 +18,13 @@ ...@@ -18,10 +18,13 @@
#ifndef COLLECTIONMODELPRIVATE_H #ifndef COLLECTIONMODELPRIVATE_H
#define COLLECTIONMODELPRIVATE_H #define COLLECTIONMODELPRIVATE_H
//Qt
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QVector> #include <QtCore/QVector>
#include <QtCore/QHash> #include <QtCore/QHash>
class QAbstractItemModel;
//Ring
class CollectionInterface; class CollectionInterface;
class CollectionModel; class CollectionModel;
class CollectionExtensionInterface; class CollectionExtensionInterface;
...@@ -38,13 +41,14 @@ public: ...@@ -38,13 +41,14 @@ public:
* index have to be implemented. * index have to be implemented.
*/ */
struct ProxyItem { struct ProxyItem {
ProxyItem() : parent(nullptr),col(1),row(0),collection(nullptr){} ProxyItem() : parent(nullptr),col(1),row(0),collection(nullptr),manageableCount(0){}
int row; int row;
int col; int col;
CollectionInterface* collection; CollectionInterface* collection;
ProxyItem* parent; ProxyItem* parent;
QVector<ProxyItem*> m_Children; QVector<ProxyItem*> m_Children;
QString m_AltName; QString m_AltName;
int manageableCount;
}; };
QHash<CollectionInterface*,ProxyItem*> m_hBackendsNodes; QHash<CollectionInterface*,ProxyItem*> m_hBackendsNodes;
...@@ -52,6 +56,7 @@ public: ...@@ -52,6 +56,7 @@ public:
QVector<CollectionExtensionInterface*> m_lExtensions; QVector<CollectionExtensionInterface*> m_lExtensions;
QHash<QString,ProxyItem*> m_hCategories; QHash<QString,ProxyItem*> m_hCategories;
static CollectionModel* m_spInstance; static CollectionModel* m_spInstance;
QAbstractItemModel* m_pManageableProxy;
//Helper //Helper
void registerNew(CollectionInterface* col); void registerNew(CollectionInterface* col);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment