Filer
CustomItemDelegate.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2022-23 Simon Peter <probono@puredarwin.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
32 #ifndef CUSTOMITEMDELEGATE_H
33 #define CUSTOMITEMDELEGATE_H
34 
35 #include <QStyledItemDelegate>
36 #include <QFileSystemModel>
37 #include <QObject>
38 #include <QAction>
39 #include <QMenu>
40 #include <QMimeData>
41 #include "CustomFileSystemModel.h"
42 #include <QTimeLine>
43 #include <QItemSelectionModel>
44 #include <QStandardItemModel>
45 #include "CustomFileIconProvider.h"
46 #include <QAbstractProxyModel>
47 #include <QLineEdit>
48 
51  DelegatePositionRole = Qt::UserRole + 1
52 };
53 
62 class CustomItemDelegate : public QStyledItemDelegate
63 {
64 Q_OBJECT
65 
66 public:
72  explicit CustomItemDelegate(QObject* parent = nullptr, QAbstractProxyModel* fileSystemModel = nullptr);
73 
74  // Destructor
76 
83  void paint(QPainter *painter, const QStyleOptionViewItem &option,
84  const QModelIndex &index) const override;
85 
90  // void installEventFilterOnView(QAbstractItemView* view);
91 
98  // bool eventFilter(QObject* object, QEvent* event) override;
99 
104  void setSelectionModel(QItemSelectionModel* selectionModel);
105 
112  QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override {
113  // Get the current size
114  QSize size = QStyledItemDelegate::sizeHint(option, index);
115  return QSize(option.rect.width(), size.height());
116  }
117 
118 public slots:
123  void animationValueChanged(double value);
124 
128  void animationFinished();
129 
133  void stopAnimation();
134 
139  void startAnimation(const QModelIndex& index);
140 
145  bool isAnimationRunning() const;
146 
147  // The following are used to implement a custom editor for the delegate
148  // (when the user clicks on an item to rename it)
149 
157  QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
158 
165  void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
166 
167 signals:
173  void fileDropped(const QString& filePath, const QPoint& iconPosition);
174 
175 protected:
176  // Override the editorEvent() function to handle right-click events for the context menu
177  bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
178  const QModelIndex &index) override;
179 
180 private:
181  // Private member variable to hold a pointer to the QFileSystemModel object
182  QAbstractProxyModel *m_fileSystemModel;
183 
184  // We use this to flash the icon if the item was double-clicked
185  bool iconShown = false;
186  bool iconVisible = false;
187  int flashCount = 0;
188 
189  // Context menu
190  // It is generally not a good idea to create a context menu in an ItemDelegate object,
191  // because the ItemDelegate object is responsible for drawing the items in the view,
192  // and it is not responsible for handling user interactions with the items.
193  // We should probably fix this in a future version of the application.
194  QMenu menu;
195 
196  QTimeLine* animationTimeline;
197 
198  qreal currentAnimationValue;
199 
200  // Member variables to store the current index and option
201  mutable QModelIndex m_currentIndex;
202  QModelIndex m_animatedIndex; // The index of the item that should currently be animated
203  mutable QStyleOptionViewItem m_currentOption;
204 
205  QItemSelectionModel* m_selectionModel;
206 
207 private slots:
208  // Slot to handle drag enter events
209  // void onDragEnterEvent(QDragEnterEvent* event);
210 
211  // Slot to handle drop events
212  // void onDropEvent(QDropEvent* event);
213 };
214 
215 #endif // CUSTOMITEMDELEGATE_H
CustomItemDelegateRole
Add a custom role to store the delegate position.
Definition: CustomItemDelegate.h:50
A custom delegate for rendering items in views.
Definition: CustomItemDelegate.h:63
bool isAnimationRunning() const
Checks if an animation is currently running.
Definition: CustomItemDelegate.cpp:485
void animationFinished()
Slot called when the animation has finished.
Definition: CustomItemDelegate.cpp:432
void fileDropped(const QString &filePath, const QPoint &iconPosition)
Signal emitted when a file is dropped onto the delegate item.
void animationValueChanged(double value)
Animates the value change for the animation.
Definition: CustomItemDelegate.cpp:414
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Creates and returns a custom editor widget.
Definition: CustomItemDelegate.cpp:491
void setSelectionModel(QItemSelectionModel *selectionModel)
Installs event filters on the specified view to intercept events.
Definition: CustomItemDelegate.cpp:480
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Overrides the sizeHint method to set the size of the delegate items.
Definition: CustomItemDelegate.h:112
void stopAnimation()
Stops the currently running animation.
Definition: CustomItemDelegate.cpp:455
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Sets the model data based on the edited data in the editor.
Definition: CustomItemDelegate.cpp:520
void startAnimation(const QModelIndex &index)
Starts an animation for the specified index.
Definition: CustomItemDelegate.cpp:462
CustomItemDelegate(QObject *parent=nullptr, QAbstractProxyModel *fileSystemModel=nullptr)
Constructs a CustomItemDelegate object.
Definition: CustomItemDelegate.cpp:50
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Renders the item's appearance using a QPainter.
Definition: CustomItemDelegate.cpp:85