#pragma once #include "TreeViewEx.h" #include "../mischelpers_global.h" class MISCHELPERS_EXPORT CTreeItemModel : public QAbstractItemModelEx { Q_OBJECT public: CTreeItemModel(QObject *parent = 0); virtual ~CTreeItemModel(); void SetTree(bool bTree) { m_bTree = bTree; } bool IsTree() const { return m_bTree; } void SetUseIcons(bool bUseIcons) { m_bUseIcons = bUseIcons; } static void SetDarkMode(bool bDark) { m_DarkMode = bDark;} //void CountItems(); QModelIndex FindIndex(const QVariant& ID); void RemoveIndex(const QModelIndex &index); 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: void Clear(); signals: void CheckChanged(const QVariant& ID, bool State); void ToolTipCallback(const QVariant& ID, QString& ToolTip) const; void Updated(); protected: struct STreeNode { STreeNode(const QVariant& Id){ ID = Id; Parent = NULL; Row = 0; //AllChildren = 0; IsBold = false; IsGray = false; } virtual ~STreeNode(){ foreach(STreeNode* pNode, Children) delete pNode; } QVariant ID; STreeNode* Parent; int Row; QList Path; QList Children; //int AllChildren; QMap Aux; QVariant Icon; bool IsBold; bool IsGray; QColor Color; struct SValue { QVariant Raw; QVariant SortKey; QVariant Formated; }; QVector Values; }; virtual QVariant NodeData(STreeNode* pNode, int role, int section) const; virtual STreeNode* MkNode(const QVariant& Id) = 0; // { return new STreeNode(Id); } void Sync(QMap, QList >& New, QHash& Old); void Purge(STreeNode* pParent, const QModelIndex &parent, QHash& Old); void Fill(STreeNode* pParent, const QModelIndex &parent, const QList& Paths, int PathsIndex, const QList& New, const QList& Path); QModelIndex Find(STreeNode* pParent, STreeNode* pNode); //int CountItems(STreeNode* pRoot); virtual QVariant GetDefaultIcon() const { return QVariant(); } STreeNode* m_Root; QHash m_Map; bool m_bTree; bool m_bUseIcons; static bool m_DarkMode; }; class MISCHELPERS_EXPORT CSimpleTreeModel : public CTreeItemModel { Q_OBJECT public: CSimpleTreeModel(QObject *parent = 0); void Sync(const QMap& List); QVariant GetItemID(const QModelIndex &index) const; void setHeaderLabels(const QStringList& Columns) { m_Headers = Columns; } virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; protected: virtual STreeNode* MkNode(const QVariant& Id) { return new STreeNode(Id); } QList MakePath(const QVariantMap& Cur, const QMap& List); bool TestPath(const QList& Path, const QVariantMap& Cur, const QMap& List, int Index = 0); QStringList m_Headers; };