| 1 | #pragma once |
| 2 | |
| 3 | #include "pch.h" |
| 4 | |
| 5 | struct Item { |
| 6 | Item() {} |
| 7 | |
| 8 | ~Item() { |
| 9 | for (auto child : childs) { |
| 10 | if (child->isLoading() || state == LoadingIcon) { |
| 11 | child->isDeleted = true; |
| 12 | } else { |
| 13 | delete child; |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | bool isLoading() const { return state == Loading1 || state == Loading2; } |
| 19 | |
| 20 | int num() const { |
| 21 | Q_ASSERT(parent); |
| 22 | return parent->childs.indexOf(const_cast<Item *>(this)); |
| 23 | } |
| 24 | |
| 25 | Item *parent = nullptr; |
| 26 | |
| 27 | enum State { Unknown, Loading1, Loading2, Ready, Special, LoadingIcon }; |
| 28 | |
| 29 | State state = Unknown; |
| 30 | bool isFolder = false; |
| 31 | bool isDeleted = false; |
| 32 | QString name; |
| 33 | QDir path; |
| 34 | QString modified; |
| 35 | quint64 size = 0; |
| 36 | |
| 37 | QVector<Item *> childs; |
| 38 | }; |
| 39 | |
| 40 | class IconCache; |
| 41 | class ItemSorter; |
| 42 | |
| 43 | class ItemModel : public QAbstractItemModel { |
| 44 | Q_OBJECT |
| 45 | public: |
| 46 | ItemModel(IconCache *icons, const QString &remote, QObject *parent); |
| 47 | ~ItemModel(); |
| 48 | |
| 49 | const QDir &path(const QModelIndex &index) const; |
| 50 | bool isLoading(const QModelIndex &index) const; |
| 51 | void refresh(const QModelIndex &index); |
| 52 | void rename(const QModelIndex &index, const QString &name); |
| 53 | bool isTopLevel(const QModelIndex &index) const; |
| 54 | bool isFolder(const QModelIndex &index) const; |
| 55 | |
| 56 | QModelIndex addRoot(const QString &name, const QString &path); |
| 57 | |
| 58 | QModelIndex index(int row, int column, |
| 59 | const QModelIndex &parent) const override; |
| 60 | QModelIndex parent(const QModelIndex &index) const override; |
| 61 | bool hasChildren(const QModelIndex &parent) const override; |
| 62 | int rowCount(const QModelIndex &parent) const override; |
| 63 | int columnCount(const QModelIndex &parent) const override; |
| 64 | void sort(int column, Qt::SortOrder order) override; |
| 65 | QVariant data(const QModelIndex &index, int role) const override; |
| 66 | QVariant headerData(int section, Qt::Orientation orientation, |
| 67 | int role) const override; |
| 68 | |
| 69 | bool removeRows(int row, int count, const QModelIndex &parent) override; |
| 70 | |
| 71 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 72 | |
| 73 | bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, |
| 74 | int column, const QModelIndex &parent) const override; |
| 75 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, |
| 76 | int column, const QModelIndex &parent) override; |
| 77 | |
| 78 | signals: |
| 79 | void getIcon(Item *item, const QPersistentModelIndex &index); |
| 80 | void drop(const QDir &path, const QModelIndex &parent); |
| 81 | |
| 82 | private: |
| 83 | Item *mRoot; |
| 84 | |
| 85 | QString mRemote; |
| 86 | |
| 87 | QHash<QString, QIcon> mLoadedIcons; |
| 88 | |
| 89 | bool mFolderIcons; |
| 90 | bool mFileIcons; |
| 91 | |
| 92 | QIcon mDriveIcon; |
| 93 | QIcon mFolderIcon; |
| 94 | QIcon mFileIcon; |
| 95 | |
| 96 | QFont mFixedFont; |
| 97 | |
| 98 | int mSortColumn; |
| 99 | Qt::SortOrder mSortOrder; |
| 100 | |
| 101 | QRegExp mRegExpFolder; |
| 102 | QRegExp mRegExpFile; |
| 103 | |
| 104 | Item *get(const QModelIndex &index) const; |
| 105 | void load(const QPersistentModelIndex &parentIndex, Item *parent); |
| 106 | |
| 107 | void sortRecursive(Item *item, const ItemSorter &sorter); |
| 108 | void sort(const QModelIndex &parent, Item *item); |
| 109 | }; |