1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://www.qt.io/licensing/ 5 ** 6 ** This file is part of the demonstration applications of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:LGPL21$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see http://www.qt.io/terms-conditions. For further 15 ** information use the contact form at http://www.qt.io/contact-us. 16 ** 17 ** GNU Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 2.1 or version 3 as published by the Free 20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 22 ** following information to ensure the GNU Lesser General Public License 23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 25 ** 26 ** As a special exception, The Qt Company gives you certain additional 27 ** rights. These rights are described in The Qt Company LGPL Exception 28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 29 ** 30 ** $QT_END_LICENSE$ 31 ** 32 ****************************************************************************/ 33 34 #ifndef BOOKMARKS_H 35 #define BOOKMARKS_H 36 37 #include <QtCore/QObject> 38 #include <QtCore/QAbstractItemModel> 39 40 #include <QtWidgets/QUndoCommand> 41 42 /*! 43 Bookmark manager, owner of the bookmarks, loads, saves and basic tasks 44 */ 45 class AutoSaver; 46 class BookmarkNode; 47 class BookmarksModel; 48 class BookmarksManager : public QObject 49 { 50 Q_OBJECT 51 52 signals: 53 void entryAdded(BookmarkNode *item); 54 void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item); 55 void entryChanged(BookmarkNode *item); 56 57 public: 58 BookmarksManager(QObject *parent = 0); 59 ~BookmarksManager(); 60 61 void addBookmark(BookmarkNode *parent, BookmarkNode *node, int row = -1); 62 void removeBookmark(BookmarkNode *node); 63 void setTitle(BookmarkNode *node, const QString &newTitle); 64 void setUrl(BookmarkNode *node, const QString &newUrl); 65 void changeExpanded(); 66 67 BookmarkNode *bookmarks(); 68 BookmarkNode *menu(); 69 BookmarkNode *toolbar(); 70 71 BookmarksModel *bookmarksModel(); undoRedoStack()72 QUndoStack *undoRedoStack() { return &m_commands; }; 73 74 public slots: 75 void importBookmarks(); 76 void exportBookmarks(); 77 78 private slots: 79 void save() const; 80 81 private: 82 void load(); 83 84 bool m_loaded; 85 AutoSaver *m_saveTimer; 86 BookmarkNode *m_bookmarkRootNode; 87 BookmarksModel *m_bookmarkModel; 88 QUndoStack m_commands; 89 90 friend class RemoveBookmarksCommand; 91 friend class ChangeBookmarkCommand; 92 }; 93 94 class RemoveBookmarksCommand : public QUndoCommand 95 { 96 97 public: 98 RemoveBookmarksCommand(BookmarksManager *m_bookmarkManagaer, BookmarkNode *parent, int row); 99 ~RemoveBookmarksCommand(); 100 void undo(); 101 void redo(); 102 103 protected: 104 int m_row; 105 BookmarksManager *m_bookmarkManagaer; 106 BookmarkNode *m_node; 107 BookmarkNode *m_parent; 108 bool m_done; 109 }; 110 111 class InsertBookmarksCommand : public RemoveBookmarksCommand 112 { 113 114 public: 115 InsertBookmarksCommand(BookmarksManager *m_bookmarkManagaer, 116 BookmarkNode *parent, BookmarkNode *node, int row); undo()117 void undo() { RemoveBookmarksCommand::redo(); } redo()118 void redo() { RemoveBookmarksCommand::undo(); } 119 120 }; 121 122 class ChangeBookmarkCommand : public QUndoCommand 123 { 124 125 public: 126 ChangeBookmarkCommand(BookmarksManager *m_bookmarkManagaer, 127 BookmarkNode *node, const QString &newValue, bool title); 128 void undo(); 129 void redo(); 130 131 private: 132 BookmarksManager *m_bookmarkManagaer; 133 bool m_title; 134 QString m_oldValue; 135 QString m_newValue; 136 BookmarkNode *m_node; 137 }; 138 139 /*! 140 BookmarksModel is a QAbstractItemModel wrapper around the BookmarkManager 141 */ 142 class BookmarksModel : public QAbstractItemModel 143 { 144 Q_OBJECT 145 146 public slots: 147 void entryAdded(BookmarkNode *item); 148 void entryRemoved(BookmarkNode *parent, int row, BookmarkNode *item); 149 void entryChanged(BookmarkNode *item); 150 151 public: 152 enum Roles { 153 TypeRole = Qt::UserRole + 1, 154 UrlRole = Qt::UserRole + 2, 155 UrlStringRole = Qt::UserRole + 3, 156 SeparatorRole = Qt::UserRole + 4 157 }; 158 159 BookmarksModel(BookmarksManager *bookmarkManager, QObject *parent = 0); bookmarksManager()160 inline BookmarksManager *bookmarksManager() const { return m_bookmarksManager; } 161 162 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; 163 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 164 int columnCount(const QModelIndex &parent = QModelIndex()) const; 165 int rowCount(const QModelIndex &parent = QModelIndex()) const; 166 QModelIndex index(int, int, const QModelIndex& = QModelIndex()) const; 167 QModelIndex parent(const QModelIndex& index= QModelIndex()) const; 168 Qt::ItemFlags flags(const QModelIndex &index) const; 169 Qt::DropActions supportedDropActions () const; 170 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); 171 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); 172 QMimeData *mimeData(const QModelIndexList &indexes) const; 173 QStringList mimeTypes() const; 174 bool dropMimeData(const QMimeData *data, 175 Qt::DropAction action, int row, int column, const QModelIndex &parent); 176 bool hasChildren(const QModelIndex &parent = QModelIndex()) const; 177 178 BookmarkNode *node(const QModelIndex &index) const; 179 QModelIndex index(BookmarkNode *node) const; 180 181 private: 182 183 bool m_endMacro; 184 BookmarksManager *m_bookmarksManager; 185 }; 186 187 // Menu that is dynamically populated from the bookmarks 188 #include "modelmenu.h" 189 class BookmarksMenu : public ModelMenu 190 { 191 Q_OBJECT 192 193 signals: 194 void openUrl(const QUrl &url); 195 196 public: 197 BookmarksMenu(QWidget *parent = 0); 198 void setInitialActions(QList<QAction*> actions); 199 200 protected: 201 bool prePopulated(); 202 203 private slots: 204 void activated(const QModelIndex &index); 205 206 private: 207 BookmarksManager *m_bookmarksManager; 208 QList<QAction*> m_initialActions; 209 }; 210 211 /* 212 Proxy model that filters out the bookmarks so only the folders 213 are left behind. Used in the add bookmark dialog combobox. 214 */ 215 #include <QtCore/QSortFilterProxyModel> 216 class AddBookmarkProxyModel : public QSortFilterProxyModel 217 { 218 Q_OBJECT 219 public: 220 AddBookmarkProxyModel(QObject * parent = 0); 221 int columnCount(const QModelIndex & parent = QModelIndex()) const; 222 223 protected: 224 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; 225 }; 226 227 /*! 228 Add bookmark dialog 229 */ 230 #include "ui_addbookmarkdialog.h" 231 class AddBookmarkDialog : public QDialog, public Ui_AddBookmarkDialog 232 { 233 Q_OBJECT 234 235 public: 236 AddBookmarkDialog(const QString &url, const QString &title, QWidget *parent = 0, BookmarksManager *bookmarkManager = 0); 237 238 private slots: 239 void accept(); 240 241 private: 242 QString m_url; 243 BookmarksManager *m_bookmarksManager; 244 AddBookmarkProxyModel *m_proxyModel; 245 }; 246 247 #include "ui_bookmarks.h" 248 class TreeProxyModel; 249 class BookmarksDialog : public QDialog, public Ui_BookmarksDialog 250 { 251 Q_OBJECT 252 253 signals: 254 void openUrl(const QUrl &url); 255 256 public: 257 BookmarksDialog(QWidget *parent = 0, BookmarksManager *manager = 0); 258 ~BookmarksDialog(); 259 260 private slots: 261 void customContextMenuRequested(const QPoint &pos); 262 void open(); 263 void newFolder(); 264 265 private: 266 void expandNodes(BookmarkNode *node); 267 bool saveExpandedNodes(const QModelIndex &parent); 268 269 BookmarksManager *m_bookmarksManager; 270 BookmarksModel *m_bookmarksModel; 271 TreeProxyModel *m_proxyModel; 272 }; 273 274 #include <QtWidgets/QToolBar> 275 class BookmarksToolBar : public QToolBar 276 { 277 Q_OBJECT 278 279 signals: 280 void openUrl(const QUrl &url); 281 282 public: 283 BookmarksToolBar(BookmarksModel *model, QWidget *parent = 0); 284 void setRootIndex(const QModelIndex &index); 285 QModelIndex rootIndex() const; 286 287 protected: 288 void dragEnterEvent(QDragEnterEvent *event); 289 void dropEvent(QDropEvent *event); 290 291 private slots: 292 void triggered(QAction *action); 293 void activated(const QModelIndex &index); 294 void build(); 295 296 private: 297 BookmarksModel *m_bookmarksModel; 298 QPersistentModelIndex m_root; 299 }; 300 301 #endif // BOOKMARKS_H 302