1 #ifndef GRIDVIEW_H
2 #define GRIDVIEW_H
3 
4 #include <QAbstractItemView>
5 #include <QStyledItemDelegate>
6 
7 #define DEFAULT_GRID_ITEM_MARGIN 11
8 #define DEFAULT_GRID_ITEM_THUMBNAIL_ALIGNMENT "bottom"
9 #define DEFAULT_GRID_SPACING 7
10 #define DEFAULT_GRID_LAYOUT "centered"
11 
12 class GridItem;
13 
14 class ThumbnailDelegate : public QStyledItemDelegate
15 {
16    Q_OBJECT
17 
18 public:
19    ThumbnailDelegate(const GridItem &gridItem, QObject* parent = 0);
20    void paint(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex& index) const;
21 
22 private:
23    const GridItem &m_style;
24 };
25 
26 class GridView : public QAbstractItemView
27 {
28    Q_OBJECT
29 
30    Q_PROPERTY(QString layout READ getLayout WRITE setLayout DESIGNABLE true SCRIPTABLE true)
31    Q_PROPERTY(int spacing READ getSpacing WRITE setSpacing DESIGNABLE true SCRIPTABLE true)
32 
33 public:
34    enum ViewMode
35    {
36       Simple,
37       Centered,
38       Anchored
39    };
40 
41    GridView(QWidget *parent = 0);
~GridView()42    ~GridView() {}
43 
44    QModelIndex indexAt(const QPoint &point_) const;
45    QVector<QModelIndex> visibleIndexes() const;
46    QRect visualRect(const QModelIndex &index) const;
47    void setModel(QAbstractItemModel *model);
48    void scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint);
49    void setGridSize(const int newSize);
50    void setviewMode(ViewMode mode);
51    QString getLayout() const;
52    void setLayout(QString layout);
53    int getSpacing() const;
54    void setSpacing(const int spacing);
55 
56 signals:
57    void visibleItemsChangedMaybe() const;
58 
59 protected slots:
60    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());
61    void rowsInserted(const QModelIndex &parent, int start, int end);
62    void rowsRemoved(const QModelIndex &parent, int start, int end);
63    void updateGeometries();
64    void reset();
65 
66 protected:
67    QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers);
68    QRegion visualRegionForSelection(const QItemSelection &selection) const;
isIndexHidden(const QModelIndex &)69    bool isIndexHidden(const QModelIndex&) const { return false; }
70    int horizontalOffset() const;
71    int verticalOffset() const;
72    void scrollContentsBy(int dx, int dy);
73    void setSelection(const QRect &rect, QFlags<QItemSelectionModel::SelectionFlag> flags);
74    void paintEvent(QPaintEvent*);
75    void resizeEvent(QResizeEvent*);
76 
77 private:
78    QRectF viewportRectForRow(int row) const;
79    void calculateRectsIfNecessary() const;
80    void refresh();
81 
82    int m_size = 255;
83    int m_spacing = DEFAULT_GRID_SPACING;
84    QVector<QModelIndex> m_visibleIndexes;
85    ViewMode m_viewMode = Centered;
86    mutable int m_idealHeight;
87    mutable QHash<int, QRectF> m_rectForRow;
88    mutable bool m_hashIsDirty;
89 };
90 
91 #endif
92