Rabbit Remote Control 0.1.0-bate8
Loading...
Searching...
No Matches
FavoriteModel.h
1// Author: Kang Lin <kl222@126.com>
2
3#pragma once
4
5#include <QVector>
6#include <QAbstractItemModel>
7#include "FavoriteDatabase.h"
8
9class CFavoriteModel : public QAbstractItemModel
10{
11 Q_OBJECT
12
13public:
14 explicit CFavoriteModel(CFavoriteDatabase* pDatabase, QObject *parent = nullptr);
16
17 bool AddNode(const QString& szName, int parentId);
18 bool AddFavorite(const QString &szFile,
19 const QString& szName,
20 const QIcon &icon,
21 const QString szDescription,
22 int parentId = 0);
23 bool UpdateFavorite(
24 const QString &szFile, const QString &szName = QString(),
25 const QString &szDescription = QString(), const QIcon &icon = QIcon());
26 CFavoriteDatabase::Item GetFavorite(const QString& szFile);
27 void Refresh();
28
29 bool Move(QModelIndex index, QModelIndex parentIndex);
30 bool Copy(QModelIndex index, QModelIndex parentIndex);
31 enum RoleType {
32 RoleFile = Qt::UserRole,
33 RoleNodeType,
34 RoleItem
35 };
36
37 // Basic functionality:
38 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
39 QModelIndex parent(const QModelIndex &index) const override;
40
41 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
42 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
43
44 // Fetch data dynamically:
45 bool canFetchMore(const QModelIndex &parent) const override;
46 void fetchMore(const QModelIndex &parent) override;
47
48 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
49
50 // Editable:
51 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
52
53 Qt::ItemFlags flags(const QModelIndex &index) const override;
54
55 // Remove data:
56 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
57
58private:
59 CFavoriteDatabase* m_pDatabase;
60
61 struct tree {
63 tree* parent = nullptr;
64 QVector<tree*> children;
65
66 tree();
68 ~tree();
69
70 int GetRow() const;
71 void SetRow(int r);
72 bool IsFolder() const;
73 bool IsFavorite() const;
74 int ChildCount() const;
75 tree* ChildAt(int index) const;
76 int GetInserIndex(tree* child);
77 bool AddChild(tree* child);
78 bool InsertChild(int index, tree* child);
79 bool RemoveChild(tree* child);
80 QList<tree*> FindChild(const CFavoriteDatabase::Item &item) const;
81 QList<tree*> FindRecursive(const CFavoriteDatabase::Item &item) const;
82
83 private:
84 int m_row = 0; // the row of item in the it's parent
85 };
86
87 tree* m_pRoot;
88
89 QMap<int, tree*> m_Folders;
90 tree* GetTree(int id) const;
91
92 tree* GetTree(QModelIndex index) const;
93 QModelIndex CreateIndex(tree* t) const;
94
95 void ClearTree(tree* node);
96 bool AddTree(const CFavoriteDatabase::Item& item, int parentId);
97 bool UpdateTree(const QString &szFile);
98 bool MoveTree(const CFavoriteDatabase::Item& item, int newParentId);
99};
The CFavoriteDatabase class.