Advertisement
VRonin

FlaggableProxy

Oct 5th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // flaggableproxy.h
  2.  
  3. #ifndef FLAGGABLEPROXY_H
  4. #define FLAGGABLEPROXY_H
  5.  
  6. #include <QIdentityProxyModel>
  7.  
  8. class FlaggableProxy : public QIdentityProxyModel
  9. {
  10.     Q_OBJECT
  11.  
  12. public:
  13.     explicit FlaggableProxy(QObject *parent=nullptr);
  14.     virtual ~FlaggableProxy() = default;
  15.     virtual Qt::ItemFlags flags(const QModelIndex & index) const override;
  16.     virtual void setFlagForColumn(qint32 col, Qt::ItemFlags val);
  17.     virtual void removeFlagForColumn(qint32 col);
  18.     virtual Qt::ItemFlags flagsForColumn(qint32 col) const;
  19.     virtual void setFlagForRow(qint32 row, Qt::ItemFlags val);
  20.     virtual void removeFlagForRow(qint32 row);
  21.     virtual Qt::ItemFlags flagsForRow(qint32 row) const;
  22.     virtual void setFlag(qint32 row, qint32 col, Qt::ItemFlags val);
  23.     virtual void removeFlag(qint32 row, qint32 col);
  24.     virtual Qt::ItemFlags getFlag(qint32 row, qint32 col)const;
  25.     virtual void clearFlags();
  26.  
  27. private:
  28.     QHash<qint64, Qt::ItemFlags> m_flagStorage;
  29.     QHash<qint32, Qt::ItemFlags> m_rowFlagStorage;
  30.     QHash<qint32, Qt::ItemFlags> m_colFlagStorage;
  31. };
  32.  
  33. #endif // FLAGGABLEPROXY_H
  34.  
  35. // flaggableproxy.cpp
  36.  
  37. #include "flaggableproxy.h"
  38.  
  39. FlaggableProxy::FlaggableProxy(QObject *parent)
  40.     : QIdentityProxyModel(parent)
  41. {
  42.  
  43. }
  44.  
  45.  
  46. Qt::ItemFlags FlaggableProxy::flags(const QModelIndex & idx) const
  47. {
  48.     const QModelIndex index = mapToSource(idx);
  49.     const qint64 hashIndex = (static_cast<qint64>(index.row()) << 32) | static_cast<qint64>(index.column());        
  50.     if (m_flagStorage.contains(hashIndex))
  51.         return m_flagStorage.value(hashIndex);
  52.     if (m_rowFlagStorage.contains(index.row()))
  53.         return m_rowFlagStorage.value(index.row());
  54.     if (m_colFlagStorage.contains(index.column()))
  55.         return m_colFlagStorage.value(index.column());
  56.     return QIdentityProxyModel::flags(idx);
  57. }
  58.  
  59. void FlaggableProxy::setFlagForColumn(qint32 col, Qt::ItemFlags val)
  60. {
  61.     m_colFlagStorage[col] = val;
  62. }
  63.  
  64. void FlaggableProxy::removeFlagForColumn(qint32 col)
  65. {
  66.     m_colFlagStorage.remove(col);
  67. }
  68.  
  69. Qt::ItemFlags FlaggableProxy::flagsForColumn(qint32 col) const
  70. {
  71.     return m_colFlagStorage.value(col, QIdentityProxyModel::flags(index(0, col)));
  72. }
  73.  
  74. void FlaggableProxy::setFlagForRow(qint32 row, Qt::ItemFlags val)
  75. {
  76.     m_rowFlagStorage[row] = val;
  77. }
  78.  
  79. void FlaggableProxy::removeFlagForRow(qint32 row)
  80. {
  81.     m_colFlagStorage.remove(row);
  82. }
  83.  
  84. Qt::ItemFlags FlaggableProxy::flagsForRow(qint32 row) const
  85. {
  86.     return m_rowFlagStorage.value(row, QIdentityProxyModel::flags(index(row, 0)));
  87. }
  88.  
  89. void FlaggableProxy::setFlag(qint32 row, qint32 col, Qt::ItemFlags val)
  90. {
  91.     const qint64 hashIndex = (static_cast<qint64>(row) << 32) | static_cast<qint64>(col);
  92.     m_flagStorage[hashIndex] = val;
  93. }
  94.  
  95. void FlaggableProxy::removeFlag(qint32 row, qint32 col)
  96. {
  97.     const qint64 hashIndex = (static_cast<qint64>(row) << 32) | static_cast<qint64>(col);
  98.     m_flagStorage.remove(hashIndex);
  99. }
  100.  
  101. Qt::ItemFlags FlaggableProxy::getFlag(qint32 row, qint32 col) const
  102. {
  103.     const qint64 hashIndex = (static_cast<qint64>(row) << 32) | static_cast<qint64>(col);
  104.     return m_flagStorage.value(hashIndex, QIdentityProxyModel::flags(index(row, col)));
  105. }
  106.  
  107. void FlaggableProxy::clearFlags()
  108. {
  109.     m_flagStorage.clear();
  110.     m_rowFlagStorage.clear();
  111.     m_colFlagStorage.clear();
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement