1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "ui_bookmarkdialog.h"
29 
30 #include <utils/navigationtreeview.h>
31 
32 #include <QUrl>
33 #include <QObject>
34 #include <QString>
35 #include <QByteArray>
36 #include <QDataStream>
37 
38 #include <QIcon>
39 #include <QDialog>
40 #include <QWidget>
41 #include <QTreeView>
42 #include <QStandardItemModel>
43 
44 QT_BEGIN_NAMESPACE
45 class QEvent;
46 class QLineEdit;
47 class QTreeView;
48 class QToolButton;
49 class QStandardItem;
50 class QAbstractItemModel;
51 class QSortFilterProxyModel;
52 QT_END_NAMESPACE
53 
54 namespace Utils { class FancyLineEdit; }
55 
56 class BookmarkManager;
57 
58 class BookmarkDialog : public QDialog
59 {
60     Q_OBJECT
61 
62 public:
63     BookmarkDialog(BookmarkManager *manager, const QString &title,
64         const QString &url, QWidget *parent = 0);
65     ~BookmarkDialog();
66 
67 private:
68     void addAccepted();
69     void addNewFolder();
70     void toolButtonClicked();
71     void itemChanged(QStandardItem *item);
72     void textChanged(const QString& string);
73     void selectBookmarkFolder(int index);
74     void showContextMenu(const QPoint &point);
75     void currentChanged(const QModelIndex& current);
76     bool eventFilter(QObject *object, QEvent *e) override;
77 
78     QString m_url;
79     QString m_title;
80 
81     QString oldText;
82     QStandardItem *renameItem;
83 
84     Ui::BookmarkDialog ui;
85     BookmarkManager *bookmarkManager;
86     QSortFilterProxyModel *proxyModel;
87 };
88 
89 class TreeView : public Utils::NavigationTreeView
90 {
91     Q_OBJECT
92 
93 public:
NavigationTreeView(parent)94     TreeView(QWidget* parent = 0) : Utils::NavigationTreeView(parent) {}
subclassKeyPressEvent(QKeyEvent * event)95     void subclassKeyPressEvent(QKeyEvent* event)
96     {
97         Utils::NavigationTreeView::keyPressEvent(event);
98     }
99 };
100 
101 class BookmarkWidget : public QWidget
102 {
103     Q_OBJECT
104 
105 public:
106     explicit BookmarkWidget(BookmarkManager *manager, QWidget *parent = 0);
107     ~BookmarkWidget();
108 
109     void setOpenInNewPageActionVisible(bool visible);
110 
111 signals:
112     void addBookmark();
113     void linkActivated(const QUrl &url);
114     void createPage(const QUrl &url, bool fromSearch);
115 
116 private:
117     void filterChanged();
118     void expand(const QModelIndex& index);
119     void activated(const QModelIndex &index);
120     void showContextMenu(const QPoint &point);
121     void setup();
122     void expandItems();
123     bool eventFilter(QObject *object, QEvent *event) override;
124 
125     QRegularExpression regExp;
126     TreeView *treeView;
127     Utils::FancyLineEdit *searchField;
128     BookmarkManager *bookmarkManager;
129     QSortFilterProxyModel* filterBookmarkModel;
130     bool m_isOpenInNewPageActionVisible;
131 };
132 
133 class BookmarkModel : public QStandardItemModel
134 {
135     Q_OBJECT
136 
137 public:
138     BookmarkModel(int rows, int columns, QObject *parent = 0);
139     ~BookmarkModel();
140 
141     Qt::DropActions supportedDropActions() const override;
142     Qt::ItemFlags flags(const QModelIndex &index) const override;
143 };
144 
145 class BookmarkManager : public QObject
146 {
147     Q_OBJECT
148 
149 public:
150     BookmarkManager();
151     ~BookmarkManager();
152 
153     BookmarkModel *treeBookmarkModel() const;
154     BookmarkModel *listBookmarkModel() const;
155 
156     void saveBookmarks();
157     QStringList bookmarkFolders() const;
158     QModelIndex addNewFolder(const QModelIndex &index);
159     void removeBookmarkItem(QTreeView *treeView, const QModelIndex& index);
160     void showBookmarkDialog(QWidget *parent, const QString &name, const QString &url);
161     void addNewBookmark(const QModelIndex &index, const QString &name, const QString &url);
162     void setupBookmarkModels();
163 
164 private:
165     void itemChanged(QStandardItem *item);
166     QString uniqueFolderName() const;
167     void removeBookmarkFolderItems(QStandardItem *item);
168     void readBookmarksRecursive(const QStandardItem *item, QDataStream &stream,
169         const qint32 depth) const;
170 
171     const QIcon m_folderIcon;
172     const QIcon m_bookmarkIcon;
173 
174     QString oldText;
175 
176     BookmarkModel *treeModel;
177     BookmarkModel *listModel;
178     QStandardItem *renameItem;
179     bool m_isModelSetup = false;
180 };
181