1 /*
2  * Copyright (C) 2012 - 2015  Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19 
20 
21 #ifndef FM_FOLDERVIEW_P_H
22 #define FM_FOLDERVIEW_P_H
23 
24 #include <QListView>
25 #include <QTreeView>
26 #include <QMouseEvent>
27 #include "folderview.h"
28 
29 class QTimer;
30 
31 namespace Fm {
32 
33 // override these classes for implementing FolderView
34 class FolderViewListView : public QListView {
35   Q_OBJECT
36 public:
37   friend class FolderView;
38   FolderViewListView(QWidget* parent = nullptr);
39   ~FolderViewListView() override;
40   void startDrag(Qt::DropActions supportedActions) override;
41   void mousePressEvent(QMouseEvent* event) override;
42   void mouseMoveEvent(QMouseEvent* event) override;
43   void mouseReleaseEvent(QMouseEvent* event) override;
44   void mouseDoubleClickEvent(QMouseEvent* event) override;
45   void dragEnterEvent(QDragEnterEvent* event) override;
46   void dragMoveEvent(QDragMoveEvent* e) override;
47   void dragLeaveEvent(QDragLeaveEvent* e) override;
48   void dropEvent(QDropEvent* e) override;
49 
50   QModelIndex indexAt(const QPoint & point) const override;
51 
setPositionForIndex(const QPoint & position,const QModelIndex & index)52   inline void setPositionForIndex(const QPoint & position, const QModelIndex & index) {
53     QListView::setPositionForIndex(position, index);
54   }
55 
rectForIndex(const QModelIndex & index)56   inline QRect rectForIndex(const QModelIndex & index) const {
57     return QListView::rectForIndex(index);
58   }
59 
getViewOptions()60   inline QStyleOptionViewItem getViewOptions() {
61     return viewOptions();
62   }
63 
cursorOnSelectionCorner()64   inline bool cursorOnSelectionCorner() const {
65       return cursorOnSelectionCorner_;
66   }
67 
68 Q_SIGNALS:
69   void activatedFiltered(const QModelIndex &index);
70 
71 protected:
72   QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override;
73   void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
74 
75 public Q_SLOTS:
76   void selectAll() override;
77 
78 private Q_SLOTS:
79   void activation(const QModelIndex &index);
80 
81 private:
82   bool activationAllowed_;
83   mutable bool cursorOnSelectionCorner_;
84   bool mouseLeftPressed_;
85   QPoint globalItemPressPoint_; // to prevent dragging when only the view is scrolled
86 };
87 
88 class FolderViewTreeView : public QTreeView {
89   Q_OBJECT
90 public:
91   friend class FolderView;
92   FolderViewTreeView(QWidget* parent = nullptr);
93   ~FolderViewTreeView() override;
94   void setModel(QAbstractItemModel* model) override;
95   void mousePressEvent(QMouseEvent* event) override;
96   void mouseMoveEvent(QMouseEvent* event) override;
97   void mouseReleaseEvent(QMouseEvent* event) override;
98   void mouseDoubleClickEvent(QMouseEvent* event) override;
99   void dragEnterEvent(QDragEnterEvent* event) override;
100   void dragMoveEvent(QDragMoveEvent* e) override;
101   void dragLeaveEvent(QDragLeaveEvent* e) override;
102   void dropEvent(QDropEvent* e) override;
103 
104   // for rubberband
105   void paintEvent(QPaintEvent * event) override;
106   void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override;
107 
108   void rowsInserted(const QModelIndex& parent,int start, int end) override;
109   void rowsAboutToBeRemoved(const QModelIndex& parent,int start, int end) override;
110   void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles = QVector<int>{}) override;
111   void reset() override;
112 
113   void resizeEvent(QResizeEvent* event) override;
114   void queueLayoutColumns();
115 
keyboardSearch(const QString & search)116   void keyboardSearch(const QString &search) override {
117     QAbstractItemView::keyboardSearch(search); // let items be selected by typing
118   }
119 
120   void setCustomColumnWidths(const QList<int> &widths);
121 
122   void setHiddenColumns(const QSet<int> &columns);
123 
124 Q_SIGNALS:
125   void activatedFiltered(const QModelIndex &index);
126   void columnResizedByUser(int visualIndex, int newWidth);
127   void autoResizeEnabled();
128   void columnHiddenByUser(int visibleIndex, bool hidden);
129 
130 private Q_SLOTS:
131   void layoutColumns();
132   void activation(const QModelIndex &index);
133   void onSortFilterChanged();
134   void headerContextMenu(const QPoint &p);
135 
136 private:
137   bool doingLayout_;
138   QTimer* layoutTimer_;
139   bool activationAllowed_;
140   QList<int> customColumnWidths_;
141   QSet<int> hiddenColumns_;
142   QPoint globalItemPressPoint_; // to prevent dragging when only the view is scrolled
143 
144   // for rubberband
145   QPoint mousePressPoint_;
146   QRect rubberBandRect_;
147   QItemSelectionModel::SelectionFlag ctrlDragSelectionFlag_;
148 };
149 
150 
151 } // namespace Fm
152 
153 #endif // FM_FOLDERVIEW_P_H
154