Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // addtablerolesproxy.h
- #ifndef AddTableRolesProxy_h__
- #define AddTableRolesProxy_h__
- #include <QIdentityProxyModel>
- #include <QSet>
- /*!
- \brief Adds support for additional roles in flat models
- \details This proxy adds support for other roles to models like QStringListModel that do not have it
- use setBaseRoles() to determine which roles should be handled by the base model instead than this proxy. By default Qt::EditRole and Qt::DisplayRole are handled by the base directly
- \warning This proxy only support indexes without parents, child items are passed directly to the base model regardless of the role.
- */
- class AddTableRolesProxyPrivate;
- class AddTableRolesProxy : public QIdentityProxyModel
- {
- Q_OBJECT
- Q_DECLARE_PRIVATE(AddTableRolesProxy)
- public:
- explicit AddTableRolesProxy(QObject* parent = nullptr);
- virtual ~AddTableRolesProxy();
- virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
- virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
- //! Roles handled by the source model
- const QSet<int>& baseRoles() const;
- //! Set the roles handled by the source model
- void setBaseRoles(const QSet<int>& val);
- private:
- AddTableRolesProxyPrivate* d_ptr;
- };
- #endif // AddTableRolesProxy_h__
- //////////////////////////////////////////////////////////////////////////
- // addtablerolesproxy.cpp
- #include "AddTableRolesProxy.h"
- #include <memory>
- #include <QHash>
- class AddTableRolesProxyPrivate
- {
- Q_DECLARE_PUBLIC(AddTableRolesProxy)
- AddTableRolesProxyPrivate(AddTableRolesProxy* q);
- AddTableRolesProxyPrivate() = delete;
- AddTableRolesProxyPrivate(const AddTableRolesProxyPrivate&) = delete;
- AddTableRolesProxy* q_ptr;
- QHash<qint64, std::shared_ptr< QHash<int, QVariant> > > m_otherRoles;
- QSet<int> m_baseRoles;
- };
- AddTableRolesProxyPrivate::AddTableRolesProxyPrivate(AddTableRolesProxy* q)
- :q_ptr(q)
- , m_baseRoles({ Qt::EditRole, Qt::DisplayRole })
- {
- Q_ASSERT(q);
- }
- AddTableRolesProxy::AddTableRolesProxy(QObject* parent)
- :QIdentityProxyModel(parent)
- , d_ptr(new AddTableRolesProxyPrivate(this))
- {}
- AddTableRolesProxy::~AddTableRolesProxy()
- {
- delete d_ptr;
- }
- bool AddTableRolesProxy::setData(const QModelIndex &index, const QVariant &value, int role )
- {
- Q_D(AddTableRolesProxy);
- if (d->m_baseRoles.contains(role))
- return QIdentityProxyModel::setData(index, value, role);
- if (index.parent().isValid())
- return QIdentityProxyModel::setData(index, value, role); // Trees are not supported
- if (!index.isValid())
- return false;
- Q_ASSERT(index.model() == this);
- if (index.row() < 0 || index.row() > rowCount() || index.column() < 0 || index.column() > columnCount())
- return false;
- const qint64 rowColIdx = (static_cast<qint64>(index.row()) << 32) | static_cast<qint64>(index.column());
- auto roleIter = d->m_otherRoles.find(rowColIdx);
- if (roleIter == d->m_otherRoles.end())
- roleIter = d->m_otherRoles.insert(rowColIdx, std::make_shared<QHash<int, QVariant> >());
- const auto valueIter = roleIter.value()->find(role);
- if (valueIter == roleIter.value()->end()) {
- roleIter.value()->insert(role, value);
- }
- else {
- if (valueIter.value() == value)
- return true;
- valueIter.value() = value;
- }
- emit dataChanged(index, index, QVector<int>() << role);
- return true;
- }
- QVariant AddTableRolesProxy::data(const QModelIndex &index, int role) const
- {
- Q_D(const AddTableRolesProxy);
- if (d->m_baseRoles.contains(role))
- return QIdentityProxyModel::data(index, role);
- if (index.parent().isValid())
- return QIdentityProxyModel::data(index, role); // Trees are not supported
- if (!index.isValid())
- return QVariant();
- Q_ASSERT(index.model() == this);
- if (index.row() < 0 || index.row() > rowCount() || index.column() < 0 || index.column() > columnCount())
- return QVariant();
- const qint64 rowColIdx = (static_cast<qint64>(index.row()) << 32) | static_cast<qint64>(index.column());
- const auto roleIter = d->m_otherRoles.constFind(rowColIdx);
- if (roleIter == d->m_otherRoles.constEnd())
- return QVariant();
- return roleIter.value()->value(role, QVariant());
- }
- const QSet<int>& AddTableRolesProxy::baseRoles() const
- {
- Q_D(const AddTableRolesProxy);
- return d->m_baseRoles;
- }
- void AddTableRolesProxy::setBaseRoles(const QSet<int>& val)
- {
- Q_D(AddTableRolesProxy);
- d->m_baseRoles = val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement