QtPdWidgets  2.3.1
ParameterFileSystemModel.h
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * Copyright (C) 2023 Daniel Ramirez <dr@igh.de>
4  * 2023 Florian Pose <fp@igh.de>
5  *
6  * This file is part of the QtPdWidgets library.
7  *
8  * The QtPdWidgets library is free software: you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License as
10  * published by the Free Software Foundation, either version 3 of the License,
11  * or (at your option) any later version.
12  *
13  * The QtPdWidgets library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the QtPdWidgets Library. If not, see
20  * <http://www.gnu.org/licenses/>.
21  *
22  ****************************************************************************/
23 
24 #ifndef PD_PARAMETERFILESYSTEMMODEL_H
25 #define PD_PARAMETERFILESYSTEMMODEL_H
26 
27 #include <chrono>
28 #include <QAbstractListModel>
29 #include <QFileInfo>
30 
31 namespace Pd
32 {
34  public QAbstractListModel
35  {
36  Q_OBJECT
37 
38  public:
39  static constexpr auto DELETE_DELAY = std::chrono::seconds{1};
40 
41  explicit ParameterFileSystemModel(QObject *parent = nullptr);
43 
44  // change directory path and reset model
45  void setPath(const QString path);
46 
47  int rowCount(const QModelIndex &) const override;
48  QVariant data(const QModelIndex &, int) const override;
49  Qt::ItemFlags flags(const QModelIndex &) const override;
50 
51  const QFileInfo *getFileInfo(const QModelIndex &idx) const
52  {
53  if (idx.isValid() && idx.row() >= 0 && idx.row() < files.size())
54  {
55  return &files.at(idx.row());
56  }
57  return nullptr;
58  }
59  const QFileInfo *getFileInfo(int row) const
60  {
61  if (row >= 0 && row < files.size())
62  {
63  return &files.at(row);
64  }
65  return nullptr;
66  }
67 
68  public slots:
69  void refresh();
71 
72 
73  private:
74  class FileInfoWithTimer : public QFileInfo {
75  public:
76  FileInfoWithTimer(QFileInfo const& fi) : QFileInfo(fi) {}
77 
79  bool is_really_deleted_ = false;
80  };
81 
82  class FileInfoList : public QList<FileInfoWithTimer>
83  {
84  public:
85  FileInfoList() = default;
86  FileInfoList(const QFileInfoList& list)
87  {
88  for (const auto& l : list) {
89  append(l);
90  }
91  }
92  };
93 
94  QString path_;
95  FileInfoList files; // elements are QFileInfo
96 
97  QFileInfoList getInfo() const;
98  void addItems(const QFileInfoList& items);
99  void startDeleteDelayTimer(const QPersistentModelIndex& idx);
100  };
101 
102  /************************************************************************/
103 
104 } // namespace Pd
105 
106 #endif
Definition: Bar.h:36
int rowCount(const QModelIndex &) const override
Definition: ParameterFileSystemModel.cpp:87
void removeReallyDeletedFiles()
Definition: ParameterFileSystemModel.cpp:150
ParameterFileSystemModel(QObject *parent=nullptr)
Definition: ParameterFileSystemModel.cpp:40
~ParameterFileSystemModel()
Definition: ParameterFileSystemModel.cpp:50
void startDeleteDelayTimer(const QPersistentModelIndex &idx)
Definition: ParameterFileSystemModel.cpp:221
Definition: ParameterFileSystemModel.h:74
Definition: ParameterFileSystemModel.h:82
FileInfoList(const QFileInfoList &list)
Definition: ParameterFileSystemModel.h:86
Definition: ParameterFileSystemModel.h:33
const QFileInfo * getFileInfo(int row) const
Definition: ParameterFileSystemModel.h:59
QFileInfoList getInfo() const
Definition: ParameterFileSystemModel.cpp:54
bool delete_delay_timer_running_
Definition: ParameterFileSystemModel.h:78
QString path_
Definition: ParameterFileSystemModel.h:94
const QFileInfo * getFileInfo(const QModelIndex &idx) const
Definition: ParameterFileSystemModel.h:51
bool is_really_deleted_
Definition: ParameterFileSystemModel.h:79
void setPath(const QString path)
Definition: ParameterFileSystemModel.cpp:77
Qt::ItemFlags flags(const QModelIndex &) const override
Definition: ParameterFileSystemModel.cpp:137
void addItems(const QFileInfoList &items)
Definition: ParameterFileSystemModel.cpp:60
static constexpr auto DELETE_DELAY
Definition: ParameterFileSystemModel.h:39
FileInfoWithTimer(QFileInfo const &fi)
Definition: ParameterFileSystemModel.h:76
QVariant data(const QModelIndex &, int) const override
Definition: ParameterFileSystemModel.cpp:106
FileInfoList files
Definition: ParameterFileSystemModel.h:95
void refresh()
Definition: ParameterFileSystemModel.cpp:190