BibleTime
btbookmarksmodel.h
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #pragma once
14 
15 #include <QAbstractItemModel>
16 
17 
19 class CSwordModuleInfo;
20 
21 /**
22  Model to load and display bookmarks. It is saved periodically if it was loaded
23  from default bookmarks file. No more one such model allowed at time.
24 */
26 
27  Q_OBJECT
28 
29 
30 public: // methods:
31 
33  TypeRole = Qt::UserRole + 1
34  };
35 
36  BtBookmarksModel(QObject * parent = nullptr);
37 
38  /**
39  \brief Constructor/destructor for new bookmarks model, data is loaded on first
40  constructor call and unloaded on last destructor call.
41 
42  \param[in] fileName loads a list of items (with subitem trees) from a named file
43  or from the default bookmarks file if empty.
44  */
45  BtBookmarksModel(QString const & fileName = QString(),
46  QObject * parent = nullptr);
47  ~BtBookmarksModel() override;
48 
49  /** Reimplemented from QAbstractItemModel */
50  int rowCount(const QModelIndex & parent = QModelIndex()) const override;
51  int columnCount(const QModelIndex & parent = QModelIndex()) const override;
52  bool hasChildren(const QModelIndex & parent = QModelIndex()) const override;
53  QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
54  QModelIndex parent(const QModelIndex & index) const override;
55  QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
56  Qt::ItemFlags flags(const QModelIndex & index) const override;
57  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
58  bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
59  bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) override;
60  bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
61 
62  /**
63  \brief add new item with given parameters
64  */
65  QModelIndex addBookmark(int const row,
66  QModelIndex const & parent,
67  CSwordModuleInfo const & module,
68  QString const & key,
69  QString const & description = QString(),
70  QString const & title = QString());
71 
72  /**
73  \brief add new folder.
74  */
75  QModelIndex addFolder(int row, const QModelIndex & parent, const QString & name = QString());
76 
77  /**
78  \brief Copies item to target position.
79 
80  \param[in] row new item will occupy given row.
81  \param[in] parent if invalid new item will be placed on top level.
82  \param[in] toCopy item to copy.
83  */
84  void copyItems(int row, const QModelIndex & parent, const QModelIndexList & toCopy);
85 
86  /**
87  \returns whether item of index is a folder.
88  */
89  bool isFolder(const QModelIndex & index) const;
90 
91  /**
92  \returns whether item of index is a bookmark.
93  */
94  bool isBookmark(const QModelIndex & index) const;
95 
96  /**
97  \returns true if the testIndex is baseIndex or a direct or indirect subitem of baseIndex.
98  */
99  bool hasDescendant(const QModelIndex & baseIndex, const QModelIndex & testIndex) const;
100 
101  /**
102  \returns description for specified index if have.
103  */
104  QString description(const QModelIndex & index) const;
105 
106  /**
107  \brief set descritpion for index.
108  */
109  void setDescription(const QModelIndex & index, const QString & description);
110 
111  /**
112  \returns sword module for specified index if have.
113  */
114  CSwordModuleInfo * module(const QModelIndex & index) const;
115 
116  /**
117  \returns key for specified index if have.
118  */
119  QString key(const QModelIndex & index) const;
120 
121  /**
122  \param[in] parent sort items under specified index, if invalid sort all
123  items.
124  */
125  void sortItems(QModelIndex const & parent = QModelIndex(),
126  Qt::SortOrder const order = Qt::AscendingOrder);
127 
128  /**
129  \returns roleNames needed for QML use
130  */
131  QHash<int, QByteArray> roleNames() const override;
132 
133 public Q_SLOTS:
134 
135  /**
136  \brief Save bookmarks or specified branch to file.
137 
138  \param[in] fileName use file or save to the default bookmarks file if it is empty,
139  file will be overwriten if it exists.
140  \param[in] rootItem is used to save specified branch of bookmark items or save all
141  bookmarks if it is empty.
142  \returns true if success.
143  */
144  bool save(QString fileName = QString(), const QModelIndex & rootItem = QModelIndex());
145 
146  /**
147  \brief Import bookmarks from file.
148 
149  \param[in] fileName file to load bookmarks.
150  \param[in] rootItem bookmarks will be loaded under specified item, if empty, items
151  will be loaded on top level. Items will be placed in append mode.
152  \returns true if success.
153  */
154  bool load(QString fileName = QString(), const QModelIndex & rootItem = QModelIndex());
155 
156 private:
157 
158  bool slotSave() { return save(); }
159 
160 private: // fields:
161  Q_DECLARE_PRIVATE(BtBookmarksModel)
163 
164 };
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void copyItems(int row, const QModelIndex &parent, const QModelIndexList &toCopy)
Copies item to target position.
bool isBookmark(const QModelIndex &index) const
bool hasDescendant(const QModelIndex &baseIndex, const QModelIndex &testIndex) const
bool isFolder(const QModelIndex &index) const
void sortItems(QModelIndex const &parent=QModelIndex(), Qt::SortOrder const order=Qt::AscendingOrder)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QString description(const QModelIndex &index) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames() const override
~BtBookmarksModel() override
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void setDescription(const QModelIndex &index, const QString &description)
set descritpion for index.
QModelIndex parent(const QModelIndex &index) const override
bool load(QString fileName=QString(), const QModelIndex &rootItem=QModelIndex())
Import bookmarks from file.
BtBookmarksModel(QObject *parent=nullptr)
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
CSwordModuleInfo * module(const QModelIndex &index) const
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QModelIndex addFolder(int row, const QModelIndex &parent, const QString &name=QString())
add new folder.
bool save(QString fileName=QString(), const QModelIndex &rootItem=QModelIndex())
Save bookmarks or specified branch to file.
BtBookmarksModelPrivate *const d_ptr
QModelIndex addBookmark(int const row, QModelIndex const &parent, CSwordModuleInfo const &module, QString const &key, QString const &description=QString(), QString const &title=QString())
add new item with given parameters
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QString key(const QModelIndex &index) const