digiKam
bookmarksmngr.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2017-05-15
7  * Description : low level manager for bookmarks
8  *
9  * Copyright (C) 2017-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #ifndef DIGIKAM_BOOKMARKS_MNGR_H
25 #define DIGIKAM_BOOKMARKS_MNGR_H
26 
27 // Qt includes
28 
29 #include <QObject>
30 #include <QAbstractItemModel>
31 #include <QUndoCommand>
32 #include <QSortFilterProxyModel>
33 
34 // Local includes
35 
36 #include "searchtextbar.h"
37 
38 namespace Digikam
39 {
40 
41 class BookmarksManager;
42 class BookmarkNode;
43 
44 class RemoveBookmarksCommand : public QUndoCommand
45 {
46 public:
47 
48  explicit RemoveBookmarksCommand(BookmarksManager* const mngr,
49  BookmarkNode* const parent,
50  int row);
51  ~RemoveBookmarksCommand() override;
52 
53  void undo() override;
54  void redo() override;
55 
56 protected:
57 
58  int m_row;
62  bool m_done;
63 };
64 
65 //---------------------------------------------------------------------------------
66 
68 {
69 public:
70 
71  explicit InsertBookmarksCommand(BookmarksManager* const mngr,
72  BookmarkNode* const parent,
73  BookmarkNode* const node,
74  int row);
75 
76  void undo() override;
77  void redo() override;
78 };
79 
80 //---------------------------------------------------------------------------------
81 
82 class ChangeBookmarkCommand : public QUndoCommand
83 {
84 public:
85 
87  {
88  Url = 0,
90  Desc
91  };
92 
93 public:
94 
95  explicit ChangeBookmarkCommand(BookmarksManager* const mngr,
96  BookmarkNode* const node,
97  const QString& newValue,
98  BookmarkData type);
99  ~ChangeBookmarkCommand() override;
100 
101  void undo() override;
102  void redo() override;
103 
104 private:
105 
106  class Private;
107  Private* const d;
108 };
109 
113 class BookmarksModel : public QAbstractItemModel
114 {
115  Q_OBJECT
116 
117 public:
118 
119  enum Roles
120  {
121  TypeRole = Qt::UserRole + 1,
122  UrlRole = Qt::UserRole + 2,
123  UrlStringRole = Qt::UserRole + 3,
124  SeparatorRole = Qt::UserRole + 4,
125  DateAddedRole = Qt::UserRole + 5
126  };
127 
128 public:
129 
130  explicit BookmarksModel(BookmarksManager* const mngr, QObject* const parent = nullptr);
131  ~BookmarksModel() override;
132 
134  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
135  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
136  int columnCount(const QModelIndex& parent = QModelIndex()) const override;
137  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
138  QModelIndex index(int, int, const QModelIndex& = QModelIndex()) const override;
139  QModelIndex parent(const QModelIndex& index= QModelIndex()) const override;
140  Qt::ItemFlags flags(const QModelIndex& index) const override;
141  Qt::DropActions supportedDropActions () const override;
142  QMimeData* mimeData(const QModelIndexList& indexes) const override;
143  QStringList mimeTypes() const override;
144  bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
145  BookmarkNode* node(const QModelIndex& index) const;
146  QModelIndex index(BookmarkNode* node) const;
147 
148  bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row,
149  int column, const QModelIndex& parent) override;
150 
151  bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
152  bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
153 
154 public Q_SLOTS:
155 
156  void entryAdded(BookmarkNode* item);
157  void entryRemoved(BookmarkNode* parent, int row, BookmarkNode* item);
158  void entryChanged(BookmarkNode* item);
159 
160 private:
161 
162  class Private;
163  Private* const d;
164 };
165 
170 class AddBookmarkProxyModel : public QSortFilterProxyModel
171 {
172  Q_OBJECT
173 
174 public:
175 
176  explicit AddBookmarkProxyModel(QObject* const parent = nullptr);
177 
178  int columnCount(const QModelIndex& parent = QModelIndex()) const override;
179 
180 protected:
181 
182  bool filterAcceptsRow(int srow, const QModelIndex& sparent) const override;
183 };
184 
185 //---------------------------------------------------------------------------------
186 
187 class TreeProxyModel : public QSortFilterProxyModel
188 {
189  Q_OBJECT
190 
191 public:
192 
193  explicit TreeProxyModel(QObject* const parent = nullptr);
194 
195  int columnCount(const QModelIndex&) const override;
196 
197 Q_SIGNALS:
198 
200 
201 protected:
202 
203  bool filterAcceptsRow(int srow, const QModelIndex& sparent) const override;
204 
205 private:
206 
207  void emitResult(bool v);
208 };
209 
213 class BookmarksManager : public QObject
214 {
215  Q_OBJECT
216 
217 public:
218 
219  explicit BookmarksManager(const QString& bookmarksFile, QObject* const parent = nullptr);
220  ~BookmarksManager() override;
221 
222  void addBookmark(BookmarkNode* const parent, BookmarkNode* const node, int row = -1);
223  void removeBookmark(BookmarkNode* const node);
224  void setTitle(BookmarkNode* const node, const QString& newTitle);
225  void setUrl(BookmarkNode* const node, const QString& newUrl);
226  void setComment(BookmarkNode* const node, const QString& newDesc);
227  void changeExpanded();
228 
231  QUndoStack* undoRedoStack() const;
232 
233  void save();
234  void load();
235 
236 Q_SIGNALS:
237 
239  void entryRemoved(BookmarkNode* parent, int row, BookmarkNode* item);
241 
242 public Q_SLOTS:
243 
244  void importBookmarks();
245  void exportBookmarks();
246 
247 private:
248 
249  class Private;
250  Private* const d;
251 
253  friend class ChangeBookmarkCommand;
254 };
255 
256 } // namespace Digikam
257 
258 #endif // DIGIKAM_BOOKMARKS_MNGR_H
Definition: bookmarksmngr.h:171
bool filterAcceptsRow(int srow, const QModelIndex &sparent) const override
Definition: bookmarksmngr.cpp:712
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: bookmarksmngr.cpp:707
AddBookmarkProxyModel(QObject *const parent=nullptr)
Definition: bookmarksmngr.cpp:702
Definition: bookmarknode.h:41
Definition: bookmarksmngr.h:214
void setTitle(BookmarkNode *const node, const QString &newTitle)
Definition: bookmarksmngr.cpp:871
void entryChanged(BookmarkNode *item)
void setUrl(BookmarkNode *const node, const QString &newUrl)
Definition: bookmarksmngr.cpp:885
~BookmarksManager() override
Definition: bookmarksmngr.cpp:793
void save()
Definition: bookmarksmngr.cpp:826
void setComment(BookmarkNode *const node, const QString &newDesc)
Definition: bookmarksmngr.cpp:899
void importBookmarks()
Definition: bookmarksmngr.cpp:938
BookmarkNode * bookmarks()
Definition: bookmarksmngr.cpp:913
void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item)
void addBookmark(BookmarkNode *const parent, BookmarkNode *const node, int row=-1)
Definition: bookmarksmngr.cpp:843
void removeBookmark(BookmarkNode *const node)
Definition: bookmarksmngr.cpp:856
void entryAdded(BookmarkNode *item)
void exportBookmarks()
Definition: bookmarksmngr.cpp:965
BookmarksManager(const QString &bookmarksFile, QObject *const parent=nullptr)
Definition: bookmarksmngr.cpp:785
void load()
Definition: bookmarksmngr.cpp:803
BookmarksModel * bookmarksModel()
Definition: bookmarksmngr.cpp:923
QUndoStack * undoRedoStack() const
Definition: bookmarksmngr.cpp:933
void changeExpanded()
Definition: bookmarksmngr.cpp:799
Definition: bookmarksmngr.h:114
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: bookmarksmngr.cpp:442
QStringList mimeTypes() const override
Definition: bookmarksmngr.cpp:550
~BookmarksModel() override
Definition: bookmarksmngr.cpp:254
BookmarksManager * bookmarksManager() const
Definition: bookmarksmngr.cpp:259
BookmarkNode * node(const QModelIndex &index) const
Definition: bookmarksmngr.cpp:688
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Definition: bookmarksmngr.cpp:503
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: bookmarksmngr.cpp:585
void entryChanged(BookmarkNode *item)
Definition: bookmarksmngr.cpp:301
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition: bookmarksmngr.cpp:307
Roles
Definition: bookmarksmngr.h:120
@ TypeRole
Definition: bookmarksmngr.h:121
@ UrlStringRole
Definition: bookmarksmngr.h:123
@ SeparatorRole
Definition: bookmarksmngr.h:124
@ DateAddedRole
Definition: bookmarksmngr.h:125
@ UrlRole
Definition: bookmarksmngr.h:122
QModelIndex parent(const QModelIndex &index=QModelIndex()) const override
Definition: bookmarksmngr.cpp:478
Qt::DropActions supportedDropActions() const override
Definition: bookmarksmngr.cpp:545
void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item)
Definition: bookmarksmngr.cpp:291
BookmarksModel(BookmarksManager *const mngr, QObject *const parent=nullptr)
Definition: bookmarksmngr.cpp:238
QMimeData * mimeData(const QModelIndexList &indexes) const override
Definition: bookmarksmngr.cpp:558
void entryAdded(BookmarkNode *item)
Definition: bookmarksmngr.cpp:276
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: bookmarksmngr.cpp:352
QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const override
Definition: bookmarksmngr.cpp:464
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: bookmarksmngr.cpp:447
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: bookmarksmngr.cpp:638
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: bookmarksmngr.cpp:516
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: bookmarksmngr.cpp:331
Definition: bookmarksmngr.h:83
void undo() override
Definition: bookmarksmngr.cpp:170
BookmarkData
Definition: bookmarksmngr.h:87
@ Desc
Definition: bookmarksmngr.h:90
@ Title
Definition: bookmarksmngr.h:89
@ Url
Definition: bookmarksmngr.h:88
~ChangeBookmarkCommand() override
Definition: bookmarksmngr.cpp:165
void redo() override
Definition: bookmarksmngr.cpp:196
ChangeBookmarkCommand(BookmarksManager *const mngr, BookmarkNode *const node, const QString &newValue, BookmarkData type)
Definition: bookmarksmngr.cpp:128
Definition: bookmarksmngr.h:68
InsertBookmarksCommand(BookmarksManager *const mngr, BookmarkNode *const parent, BookmarkNode *const node, int row)
Definition: bookmarksmngr.cpp:88
void redo() override
Definition: bookmarksmngr.cpp:103
void undo() override
Definition: bookmarksmngr.cpp:98
Definition: bookmarksmngr.h:45
void redo() override
Definition: bookmarksmngr.cpp:79
RemoveBookmarksCommand(BookmarksManager *const mngr, BookmarkNode *const parent, int row)
Definition: bookmarksmngr.cpp:52
int m_row
Definition: bookmarksmngr.h:58
~RemoveBookmarksCommand() override
Definition: bookmarksmngr.cpp:64
BookmarkNode * m_node
Definition: bookmarksmngr.h:60
void undo() override
Definition: bookmarksmngr.cpp:72
bool m_done
Definition: bookmarksmngr.h:62
BookmarksManager * m_bookmarkManager
Definition: bookmarksmngr.h:59
BookmarkNode * m_parent
Definition: bookmarksmngr.h:61
Definition: bookmarksmngr.h:188
bool filterAcceptsRow(int srow, const QModelIndex &sparent) const override
Definition: bookmarksmngr.cpp:735
void signalFilterAccepts(bool)
int columnCount(const QModelIndex &) const override
Definition: bookmarksmngr.cpp:727
TreeProxyModel(QObject *const parent=nullptr)
Definition: bookmarksmngr.cpp:721
qulonglong value
Definition: itemviewutilities.cpp:592
@ Orientation
Definition: coredbfields.h:86
Definition: datefolderview.cpp:43
DropAction
Definition: itemdragdrop.cpp:60