Sandboxie/SandboxiePlus/MiscHelpers/Common/TreeItemModel.h

147 lines
4.5 KiB
C
Raw Normal View History

2021-10-16 16:19:51 +01:00
#pragma once
#include "TreeViewEx.h"
2020-06-01 17:11:56 +01:00
#include "../mischelpers_global.h"
2022-09-29 17:28:48 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool MISCHELPERS_EXPORT operator < (const QVariant& l, const QVariant& r);
#endif
2021-10-16 16:19:51 +01:00
class MISCHELPERS_EXPORT CTreeItemModel : public QAbstractItemModelEx
{
Q_OBJECT
public:
CTreeItemModel(QObject *parent = 0);
virtual ~CTreeItemModel();
void SetUseIcons(bool bUseIcons) { m_bUseIcons = bUseIcons; }
static void SetDarkMode(bool bDark) { m_DarkMode = bDark;}
2023-01-07 15:57:55 +00:00
static bool GetDarkMode() { return m_DarkMode;}
2021-10-16 16:19:51 +01:00
//void CountItems();
QModelIndex FindIndex(const QVariant& ID);
void RemoveIndex(const QModelIndex &index);
int Count() const { return m_Map.count(); }
QVariant GetItemID(const QModelIndex& index) const;
QVariant Data(const QModelIndex &index, int role, int section) const;
// derived functions
virtual QVariant data(const QModelIndex &index, int role) const;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
virtual QModelIndex parent(const QModelIndex &index) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const = 0;
public slots:
virtual void Clear();
signals:
void CheckChanged(const QVariant& ID, bool State);
void ToolTipCallback(const QVariant& ID, QString& ToolTip) const;
void Updated();
protected:
struct STreeNode
{
2023-05-29 19:50:51 +01:00
STreeNode(CTreeItemModel* pModel, const QVariant& Id) {
2021-10-16 16:19:51 +01:00
ID = Id;
Parent = NULL;
Row = 0;
//AllChildren = 0;
Virtual = false;
IsBold = false;
IsGray = false;
2023-05-29 19:50:51 +01:00
Model = pModel;
Model->m_Nodes.insert(this);
}
virtual ~STreeNode(){
Model->m_Nodes.remove(this);
2021-10-16 16:19:51 +01:00
}
QVariant ID;
STreeNode* Parent;
int Row;
QList<QVariant> Path;
QList<STreeNode*> Children;
//int AllChildren;
2023-01-07 15:57:55 +00:00
//QMap<QVariant, int> Aux;
2021-10-16 16:19:51 +01:00
bool Virtual;
QVariant Icon;
bool IsBold;
bool IsGray;
QColor Color;
struct SValue
{
QVariant Raw;
QVariant SortKey;
2022-05-16 20:30:40 +01:00
QVariant Formatted;
2021-10-16 16:19:51 +01:00
};
QVector<SValue> Values;
2023-05-29 19:50:51 +01:00
CTreeItemModel* Model;
2021-10-16 16:19:51 +01:00
};
virtual QVariant NodeData(STreeNode* pNode, int role, int section) const;
2023-01-07 15:57:55 +00:00
virtual STreeNode* MkNode(const QVariant& Id) = 0;
virtual void FreeNode(STreeNode* pNode) {
foreach(STreeNode* pSubNode, pNode->Children)
FreeNode(pSubNode);
delete pNode;
}
2021-10-16 16:19:51 +01:00
virtual STreeNode* MkVirtualNode(const QVariant& Id, STreeNode* pParent);
2023-01-07 15:57:55 +00:00
void Sync(QMap<QList<QVariant>, QList<STreeNode*> >& New, QHash<QVariant, STreeNode*>& Old, QList<QModelIndex>* pNewBranches = NULL);
2021-10-16 16:19:51 +01:00
void Purge(STreeNode* pParent, const QModelIndex &parent, QHash<QVariant, STreeNode*>& Old);
2023-01-07 15:57:55 +00:00
void Fill(STreeNode* pParent, /*const QModelIndex &parent,*/ const QList<QVariant>& Paths, int PathsIndex, const QList<STreeNode*>& New, QList<QModelIndex>* pNewBranches);
2021-10-16 16:19:51 +01:00
QModelIndex Find(STreeNode* pParent, STreeNode* pNode);
//int CountItems(STreeNode* pRoot);
virtual QVariant GetDefaultIcon() const { return QVariant(); }
STreeNode* m_Root;
QHash<QVariant, STreeNode*> m_Map;
2023-05-29 19:50:51 +01:00
QSet<STreeNode*> m_Nodes;
2021-10-16 16:19:51 +01:00
bool m_bUseIcons;
static bool m_DarkMode;
};
class MISCHELPERS_EXPORT CSimpleTreeModel : public CTreeItemModel
{
Q_OBJECT
public:
CSimpleTreeModel(QObject *parent = 0);
2023-01-07 15:57:55 +00:00
virtual ~CSimpleTreeModel();
2021-10-16 16:19:51 +01:00
2022-09-29 17:28:48 +01:00
void SetTree(bool bTree) { m_bTree = bTree; }
bool IsTree() const { return m_bTree; }
2021-10-16 16:19:51 +01:00
void Sync(const QMap<QVariant, QVariantMap>& List);
void AddColumn(const QString& Name, const QString& Key) { m_ColumnKeys.append(qMakePair(Name, Key)); }
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
protected:
2023-05-29 19:50:51 +01:00
virtual STreeNode* MkNode(const QVariant& Id) { return new STreeNode(this, Id); }
2023-01-07 15:57:55 +00:00
virtual void FreeNode(STreeNode* pNode) { delete pNode; }
2021-10-16 16:19:51 +01:00
QList<QVariant> MakePath(const QVariantMap& Cur, const QMap<QVariant, QVariantMap>& List);
bool TestPath(const QList<QVariant>& Path, const QVariantMap& Cur, const QMap<QVariant, QVariantMap>& List, int Index = 0);
2022-09-29 17:28:48 +01:00
bool m_bTree;
2021-10-16 16:19:51 +01:00
QList<QPair<QString, QString>> m_ColumnKeys;
2020-06-01 17:11:56 +01:00
};