玉兔远程控制 0.1.0-bate6
载入中...
搜索中...
未找到
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 enum RoleType {
30 RoleFile = Qt::UserRole,
31 RoleNodeType,
32 RoleItem
33 };
34
35 // Basic functionality:
36 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
37 QModelIndex parent(const QModelIndex &index) const override;
38
39 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
40 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
41
42 // Fetch data dynamically:
43 bool canFetchMore(const QModelIndex &parent) const override;
44 void fetchMore(const QModelIndex &parent) override;
45
46 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
47
48 // Editable:
49 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
50
51 Qt::ItemFlags flags(const QModelIndex &index) const override;
52
53 // Remove data:
54 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
55
56private:
57 CFavoriteDatabase* m_pDatabase;
58
59 struct tree {
61 tree* parent = nullptr;
62 QVector<tree*> children;
63
64 tree();
66 ~tree();
67
68 int GetRow() const;
69 void SetRow(int r);
70 bool IsFolder() const;
71 bool IsFavorite() const;
72 int ChildCount() const;
73 tree* ChildAt(int index) const;
74 int GetInserIndex(tree* child);
75 bool AddChild(tree* child);
76 bool InsertChild(int index, tree* child);
77 bool RemoveChild(tree* child);
78 tree* FindChild(int id) const;
79 tree* FindRecursive(int id) const;
80
81 private:
82 int m_row = 0; // the row of item in the it's parent
83 };
84
85 tree* m_pRoot;
86
87 QMap<int, tree*> m_Folders;
88 tree* GetTree(int id) const;
89
90 tree* GetTree(QModelIndex index) const;
91 QModelIndex CreateIndex(tree* t) const;
92
93 void ClearTree(tree* node);
94 bool AddTree(const CFavoriteDatabase::Item& item, int parentId);
95 bool UpdateTree(const QString &szFile);
96 bool MoveTree(int id, int newParentId);
97};