bookmarkmanager.h 6.33 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
#ifndef BOOKMARKMANAGER_H
#define BOOKMARKMANAGER_H

33
#include <utils/itemviews.h>
hjk's avatar
hjk committed
34
35
36
#include <coreplugin/icontext.h>
#include <coreplugin/inavigationwidgetfactory.h>

37
38
#include <texteditor/itexteditor.h>

39
40
41
42
43
44
#include <QAbstractItemModel>
#include <QMultiMap>
#include <QList>
#include <QListView>
#include <QPixmap>
#include <QStyledItemDelegate>
con's avatar
con committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

namespace Bookmarks {
namespace Internal {

class Bookmark;
class BookmarksPlugin;
class BookmarkContext;

class BookmarkManager : public QAbstractItemModel
{
    Q_OBJECT

public:
    BookmarkManager();
    ~BookmarkManager();
60
61
62

    QIcon bookmarkIcon() const { return m_bookmarkIcon; }

con's avatar
con committed
63
64
    void updateBookmark(Bookmark *bookmark);
    void removeBookmark(Bookmark *bookmark); // Does not remove the mark
65
    void removeAllBookmarks();
hjk's avatar
hjk committed
66
    Bookmark *bookmarkForIndex(const QModelIndex &index);
con's avatar
con committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    enum State { NoBookMarks, HasBookMarks, HasBookmarksInDocument };
    State state() const;

    // Model stuff
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &child) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

    // this QItemSelectionModel is shared by all views
    QItemSelectionModel *selectionModel() const;

81
82
    bool hasBookmarkInPosition(const QString &fileName, int lineNumber);

hjk's avatar
hjk committed
83
84
85
86
    enum Roles {
        Filename = Qt::UserRole,
        LineNumber = Qt::UserRole + 1,
        Directory = Qt::UserRole + 2,
87
88
        LineText = Qt::UserRole + 3,
        Note = Qt::UserRole + 4
hjk's avatar
hjk committed
89
    };
con's avatar
con committed
90
91
92

public slots:
    void toggleBookmark();
hjk's avatar
hjk committed
93
    void toggleBookmark(const QString &fileName, int lineNumber);
con's avatar
con committed
94
95
96
97
98
99
    void nextInDocument();
    void prevInDocument();
    void next();
    void prev();
    void moveUp();
    void moveDown();
100
101
    void editNote();
    void editNote(const QString &fileName, int lineNumber);
102
    bool gotoBookmark(Bookmark *bookmark);
con's avatar
con committed
103
104
105
106
107
108
109
110

signals:
    void updateActions(int state);
    void currentIndexChanged(const QModelIndex &);

private slots:
    void updateActionStatus();
    void loadBookmarks();
111
112
113
    void handleBookmarkRequest(TextEditor::ITextEditor * textEditor,
                               int line,
                               TextEditor::ITextEditor::MarkRequestKind kind);
114
115
116
    void handleBookmarkTooltipRequest(TextEditor::ITextEditor *textEditor,
                                      const QPoint &pos,
                                      int line);
hjk's avatar
hjk committed
117

con's avatar
con committed
118
119
120
121
122
123
124
125
126
127
private:
    TextEditor::ITextEditor *currentTextEditor() const;

    void documentPrevNext(bool next);

    Bookmark* findBookmark(const QString &path, const QString &fileName, int lineNumber);
    void addBookmark(Bookmark *bookmark, bool userset = true);
    void addBookmark(const QString &s);
    static QString bookmarkToString(const Bookmark *b);
    void saveBookmarks();
128
    void operateTooltip(TextEditor::ITextEditor *textEditor, const QPoint &pos, Bookmark *mark);
con's avatar
con committed
129

hjk's avatar
hjk committed
130
131
    typedef QMultiMap<QString, Bookmark *> FileNameBookmarksMap;
    typedef QMap<QString, FileNameBookmarksMap *> DirectoryFileBookmarksMap;
con's avatar
con committed
132
133
134

    DirectoryFileBookmarksMap m_bookmarksMap;

135
    const QIcon m_bookmarkIcon;
con's avatar
con committed
136
137
138
139
140

    QList<Bookmark *> m_bookmarksList;
    QItemSelectionModel *m_selectionModel;
};

141
class BookmarkView : public Utils::ListView
hjk's avatar
hjk committed
142
{
con's avatar
con committed
143
144
145
146
    Q_OBJECT
public:
    BookmarkView(QWidget *parent = 0);
    ~BookmarkView();
hjk's avatar
hjk committed
147
    void setModel(QAbstractItemModel *model);
con's avatar
con committed
148
149
150
151
152
153
154
public slots:
    void gotoBookmark(const QModelIndex &index);
protected slots:
    void removeFromContextMenu();
    void removeAll();
protected:
    void contextMenuEvent(QContextMenuEvent *event);
hjk's avatar
hjk committed
155
    void removeBookmark(const QModelIndex &index);
156
    void keyPressEvent(QKeyEvent *event);
con's avatar
con committed
157
158
159
private:
    BookmarkContext *m_bookmarkContext;
    QModelIndex m_contextMenuIndex;
160
    BookmarkManager *m_manager;
con's avatar
con committed
161
162
163
164
165
};

class BookmarkContext : public Core::IContext
{
public:
166
    BookmarkContext(QWidget *widget);
con's avatar
con committed
167
168
169
170
};

class BookmarkViewFactory : public Core::INavigationWidgetFactory
{
Friedemann Kleint's avatar
Friedemann Kleint committed
171
    Q_OBJECT
con's avatar
con committed
172
173
public:
    BookmarkViewFactory(BookmarkManager *bm);
174
    QString displayName() const;
175
    int priority() const;
176
    Core::Id id() const;
177
178
    QKeySequence activationSequence() const;
    Core::NavigationView createWidget();
con's avatar
con committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
private:
    BookmarkManager *m_manager;
};

class BookmarkDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    BookmarkDelegate(QObject * parent = 0);
    ~BookmarkDelegate();
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

private:
193
    void generateGradientPixmap(int width, int height, const QColor &color, bool selected) const;
con's avatar
con committed
194
195
196
197
198
199
200
201
    mutable QPixmap *m_normalPixmap;
    mutable QPixmap *m_selectedPixmap;
};

} // namespace Internal
} // namespace Bookmarks

#endif // BOOKMARKMANAGER_H