summaryrefslogtreecommitdiff
path: root/src/modlistbypriorityproxy.h
blob: e5a8adff8f3c4919cd2e163f5c603cf89a4108cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef MODLISBYPRIORITYPROXY_H
#define MODLISBYPRIORITYPROXY_H

#include <optional>
#include <set>
#include <vector>

#include <QAbstractProxyModel>
#include <QModelIndex>
#include <QMultiHash>
#include <QStringList>
#include <QIcon>
#include <QSet>

#include "modinfo.h"
#include "modlistview.h"

class ModList;
class Profile;

class ModListByPriorityProxy : public QAbstractProxyModel
{
  Q_OBJECT

public:
  explicit ModListByPriorityProxy(Profile* profile, OrganizerCore& core, QObject* parent = nullptr);
  ~ModListByPriorityProxy();

  void setProfile(Profile* profile);
  void refresh();

  void setSourceModel(QAbstractItemModel* sourceModel) override;

  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  QModelIndex parent(const QModelIndex& child) const override;
  QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
  int columnCount(const QModelIndex& index) const override;
  bool hasChildren(const QModelIndex& parent) const override;

  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
  bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const override;
  bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;

  QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
  QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;

  // check the internal state for expanded/collapse items and emit a expandItem
  // signal for each of the expanded item, useful to refresh the tree state after
  // layout modification
  void refreshExpandedItems() const;

signals:
  void expandItem(const QModelIndex& index) const;

public slots:

  void onDropEnter(const QMimeData* data, ModListView::DropPosition dropPosition);
  void expanded(const QModelIndex& index);
  void collapsed(const QModelIndex& index);

protected:

  void modelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles = QVector<int>());

private:

  void buildTree();
  void expandItems(const QModelIndex& index) const;

  struct TreeItem {
    ModInfo::Ptr mod;
    unsigned int index;
    std::vector<std::unique_ptr<TreeItem>> children;
    TreeItem* parent;

    std::size_t childIndex(TreeItem* child) const {
      for (std::size_t i = 0; i < children.size(); ++i) {
        if (children[i].get() == child) {
          return i;
        }
      }
      return -1;
    }

    TreeItem() : TreeItem(nullptr, -1) { }
    TreeItem(ModInfo::Ptr mod, unsigned int index, TreeItem* parent = nullptr) :
      mod(mod), index(index), parent(parent) { }
  };

  TreeItem m_Root;
  std::map<unsigned int, TreeItem*> m_IndexToItem;
  std::set<QString> m_CollapsedItems;

private:
  OrganizerCore& m_core;
  Profile* m_profile;
  ModListView::DropPosition m_dropPosition = ModListView::DropPosition::OnItem;
};

#endif //GROUPINGPROXY_H