Advertisement
VRonin

ColumnProxy

Jun 27th, 2016
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////
  2. // ColumnsToRolesProxy.h
  3. //////////////////////////////////////////////////////////////////////////
  4. #ifndef COLUMNSTOROLESPROXY_H
  5. #define COLUMNSTOROLESPROXY_H
  6.  
  7. #include <QIdentityProxyModel>
  8.  
  9. class ColumnsToRolesProxy : public QIdentityProxyModel
  10. {
  11.     Q_OBJECT
  12.  
  13. public:
  14.     ColumnsToRolesProxy(QObject *parent=nullptr);
  15.     virtual ~ColumnsToRolesProxy() = default;
  16.     virtual QHash<int, QByteArray> roleNames() const override;
  17.     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  18.     virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
  19.     virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  20.     enum{
  21.         FirstColumnShift = 10
  22.     };
  23. };
  24.  
  25. #endif // COLUMNSTOROLESPROXY_H
  26.  
  27. //////////////////////////////////////////////////////////////////////////
  28. // ColumnsToRolesProxy.cpp
  29. //////////////////////////////////////////////////////////////////////////
  30. #include "ColumnsToRolesProxy.h"
  31.  
  32. ColumnsToRolesProxy::ColumnsToRolesProxy(QObject *parent)
  33.     : QIdentityProxyModel(parent)
  34. {}
  35.  
  36. QHash<int, QByteArray> ColumnsToRolesProxy::roleNames() const
  37. {
  38.     QHash<int, QByteArray> result = QIdentityProxyModel::roleNames();
  39.     QList<int> baseRoles = result.keys();
  40.     baseRoles.removeAll(Qt::DisplayRole);
  41.     for (int i = 0; i < sourceModel()->columnCount();++i){
  42.         result[(i << FirstColumnShift) | Qt::DisplayRole] = sourceModel()->headerData(i, Qt::Horizontal, Qt::DisplayRole).toByteArray();
  43.         for (auto j = baseRoles.constBegin(); j != baseRoles.constEnd(); ++j) {
  44.             result.insert((i << FirstColumnShift) | *j, "col" + QByteArray::number(i) + result.value(*j));
  45.         }
  46.     }
  47.  
  48.     return result;
  49. }
  50.  
  51. QVariant ColumnsToRolesProxy::data(const QModelIndex &index, int role) const
  52. {
  53.     return sourceModel()->data(sourceModel()->index(index.row(), role >> FirstColumnShift, index.parent()), role & ((1 << FirstColumnShift) - 1));
  54. }
  55.  
  56. bool ColumnsToRolesProxy::setData(const QModelIndex &index, const QVariant &value, int role)
  57. {
  58.     return sourceModel()->setData(sourceModel()->index(index.row(), role >> FirstColumnShift, index.parent()), value, role & ((1 << FirstColumnShift) - 1));
  59. }
  60.  
  61. int ColumnsToRolesProxy::columnCount(const QModelIndex &parent) const
  62. {
  63.     return qMin(1, QIdentityProxyModel::columnCount(parent));
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement