File indexing completed on 2024-12-22 04:48:19

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include "kleverconfig.h"
0007 #include <QAbstractItemModel>
0008 #include <QFileInfo>
0009 #include <memory>
0010 
0011 class NoteTreeModel;
0012 class TreeItem
0013 {
0014 public:
0015     explicit TreeItem(const QString &path, const int depth_level, NoteTreeModel *model, TreeItem *parentItem = nullptr);
0016 
0017     void appendChild(std::unique_ptr<TreeItem> &&child);
0018 
0019     TreeItem *child(int row) const;
0020     std::unique_ptr<TreeItem> uniqueChildAt(int row);
0021     int childCount() const;
0022     QVariant data(int role) const;
0023     int row() const;
0024     TreeItem *parentItem() const;
0025     void remove();
0026     void changeDisplayName(const QString &name);
0027     void changePath(const QString &newPart, const QModelIndex &parentModelIndex, int newPartIdx = -1);
0028     void askForFocus(const QModelIndex &itemIndex);
0029     void askForExpand(const QModelIndex &itemIndex);
0030 
0031 private:
0032     // Position in tree
0033     std::vector<std::unique_ptr<TreeItem>> m_childItems;
0034     TreeItem *m_parentItem;
0035 
0036     NoteTreeModel *m_model;
0037 
0038     // Content
0039     QString m_path;
0040     int m_depth_level;
0041     QString m_displayName;
0042     bool m_wantFocus = false;
0043     bool m_wantExpand = false;
0044 };
0045 
0046 class NoteTreeModel : public QAbstractItemModel
0047 {
0048     Q_OBJECT
0049     Q_PROPERTY(bool noteMapEnabled WRITE setNoteMapEnabled) // QML will handle the signal and change it for us
0050 public:
0051     explicit NoteTreeModel(QObject *parent = nullptr);
0052 
0053     enum ExtraRoles {
0054         PathRole = Qt::UserRole + 1, // To get a string with the fullPath of the Category/Group/Note
0055         DisplayNameRole, // To get a string with the name of the Category/Group/Note to be displayed instead of the hidden name
0056         IconNameRole, // To get a string with the icon name associated with the Category/Group/Note
0057         UseCaseRole, // To get a string to know if the item is a Category/Group/Note
0058         NoteNameRole, // For only filtering between notes in the searchBar
0059         BranchNameRole, // To get the name of the category + group of a note
0060         FullNameRole, // To get the "full" name of the category + group + note of a note
0061         WantFocusRole, // For send a signal to the qml ItemDelegate using dataChanged, asking for focus
0062         WantExpandRole, // For send a signal to the qml ItemDelegate using dataChanged, asking to expands
0063     };
0064 
0065     QVariant data(const QModelIndex &index, int role) const override;
0066     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0067     QModelIndex parent(const QModelIndex &index) const override;
0068     int rowCount(const QModelIndex &parent = {}) const override;
0069     int columnCount(const QModelIndex &parent = {}) const override;
0070     QHash<int, QByteArray> roleNames() const override;
0071     Q_INVOKABLE void addRow(const QString &rowName, const QString &path, const int rowLevel, const QModelIndex &parentModelIndex = QModelIndex());
0072     Q_INVOKABLE void removeFromTree(const QModelIndex &index);
0073     Q_INVOKABLE void rename(const QModelIndex &rowModelIndex, const QString &newName);
0074     Q_INVOKABLE void askForFocus(const QModelIndex &rowModelIndex);
0075     Q_INVOKABLE void askForExpand(const QModelIndex &rowModelIndex);
0076     Q_INVOKABLE void initModel();
0077     Q_INVOKABLE QModelIndex getNoteModelIndex(const QString &notePath);
0078 
0079     // NoteMapper
0080     void setNoteMapEnabled(const bool noteMapEnabled);
0081     bool noteMapEnabled();
0082     bool isInit();
0083     void addInitialGlobalPath(const QString &path);
0084 
0085 Q_SIGNALS:
0086     void errorOccurred(const QString &errorMessage);
0087     // NoteMapper
0088     void newGlobalPathFound(const QString &path);
0089     void globalPathUpdated(const QString &oldPath, const QString &newPath);
0090     void globalPathRemoved(const QString &path);
0091     void initialGlobalPathsSent(const QStringList &initialGlobalPaths);
0092 
0093 private:
0094     // NoteMapper
0095     bool m_noteMapEnabled = KleverConfig::noteMapEnabled();
0096     bool m_isInit = false;
0097     QStringList m_initialGlobalPaths;
0098 
0099     QString m_path;
0100     std::unique_ptr<TreeItem> m_rootItem;
0101     QFileInfo m_fileInfo;
0102 
0103     // Storage Handler
0104     bool makeStorage(const QString &storagePath);
0105     bool makeCategory(const QString &storagePath, const QString &categoryName);
0106     bool makeGroup(const QString &categoryPath, const QString &groupName);
0107     bool makeNote(const QString &groupPath, const QString &noteName);
0108 };