OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / bookmarks / bookmarkmanager.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef BOOKMARKMANAGER_H
34 #define BOOKMARKMANAGER_H
35
36 #include <coreplugin/icontext.h>
37 #include <coreplugin/inavigationwidgetfactory.h>
38
39 #include <QtCore/QAbstractItemModel>
40 #include <QtCore/QMultiMap>
41 #include <QtCore/QList>
42 #include <QtGui/QListView>
43 #include <QtGui/QPixmap>
44 #include <QtGui/QStyledItemDelegate>
45
46 namespace ProjectExplorer {
47 class SessionManager;
48 }
49
50 namespace Core {
51 class IEditor;
52 }
53
54 namespace TextEditor {
55 class ITextEditor;
56 }
57
58 namespace Bookmarks {
59 namespace Internal {
60
61 class Bookmark;
62 class BookmarksPlugin;
63 class BookmarkContext;
64
65 class BookmarkManager : public QAbstractItemModel
66 {
67     Q_OBJECT
68
69 public:
70     BookmarkManager();
71     ~BookmarkManager();
72
73     QIcon bookmarkIcon() const { return m_bookmarkIcon; }
74
75     void updateBookmark(Bookmark *bookmark);
76     void removeBookmark(Bookmark *bookmark); // Does not remove the mark
77     void removeAllBookmarks();
78     Bookmark *bookmarkForIndex(QModelIndex index);
79
80     enum State { NoBookMarks, HasBookMarks, HasBookmarksInDocument };
81     State state() const;
82
83     // Model stuff
84     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
85     QModelIndex parent(const QModelIndex &child) const;
86     int rowCount(const QModelIndex &parent = QModelIndex()) const;
87     int columnCount(const QModelIndex &parent = QModelIndex()) const;
88     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
89
90     // this QItemSelectionModel is shared by all views
91     QItemSelectionModel *selectionModel() const;
92
93     enum Roles {
94         Filename = Qt::UserRole,
95         LineNumber = Qt::UserRole + 1,
96         Directory = Qt::UserRole + 2,
97         LineText = Qt::UserRole + 3
98     };
99
100 public slots:
101     void toggleBookmark();
102     void toggleBookmark(const QString &fileName, int lineNumber);
103     void nextInDocument();
104     void prevInDocument();
105     void next();
106     void prev();
107     void moveUp();
108     void moveDown();
109     bool gotoBookmark(Bookmark *bookmark);
110
111 signals:
112     void updateActions(int state);
113     void currentIndexChanged(const QModelIndex &);
114
115 private slots:
116     void updateActionStatus();
117     void loadBookmarks();
118
119 private:
120     TextEditor::ITextEditor *currentTextEditor() const;
121     ProjectExplorer::SessionManager* sessionManager() const;
122
123     void documentPrevNext(bool next);
124
125     Bookmark* findBookmark(const QString &path, const QString &fileName, int lineNumber);
126     void addBookmark(Bookmark *bookmark, bool userset = true);
127     void addBookmark(const QString &s);
128     static QString bookmarkToString(const Bookmark *b);
129     void saveBookmarks();
130
131     typedef QMultiMap<QString, Bookmark *> FileNameBookmarksMap;
132     typedef QMap<QString, FileNameBookmarksMap *> DirectoryFileBookmarksMap;
133
134     DirectoryFileBookmarksMap m_bookmarksMap;
135
136     const QIcon m_bookmarkIcon;
137
138     QList<Bookmark *> m_bookmarksList;
139     QItemSelectionModel *m_selectionModel;
140 };
141
142 class BookmarkView : public QListView
143 {
144     Q_OBJECT
145 public:
146     BookmarkView(QWidget *parent = 0);
147     ~BookmarkView();
148     void setModel(QAbstractItemModel *model);
149 public slots:
150     void gotoBookmark(const QModelIndex &index);
151 protected slots:
152     void removeFromContextMenu();
153     void removeAll();
154 protected:
155     void contextMenuEvent(QContextMenuEvent *event);
156     void removeBookmark(const QModelIndex &index);
157 private:
158     BookmarkContext *m_bookmarkContext;
159     QModelIndex m_contextMenuIndex;
160     BookmarkManager *m_manager;
161 };
162
163 class BookmarkContext : public Core::IContext
164 {
165 public:
166     BookmarkContext(BookmarkView *widget);
167     virtual Core::Context context() const;
168     virtual QWidget *widget();
169 private:
170     BookmarkView *m_bookmarkView;
171     const Core::Context m_context;
172 };
173
174 class BookmarkViewFactory : public Core::INavigationWidgetFactory
175 {
176 public:
177     BookmarkViewFactory(BookmarkManager *bm);
178     QString displayName() const;
179     int priority() const;
180     QString id() const;
181     QKeySequence activationSequence() const;
182     Core::NavigationView createWidget();
183 private:
184     BookmarkManager *m_manager;
185 };
186
187 class BookmarkDelegate : public QStyledItemDelegate
188 {
189     Q_OBJECT
190 public:
191     BookmarkDelegate(QObject * parent = 0);
192     ~BookmarkDelegate();
193     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
194     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
195
196 private:
197     void generateGradientPixmap(int width, int height, QColor color, bool selected) const;
198     mutable QPixmap *m_normalPixmap;
199     mutable QPixmap *m_selectedPixmap;
200 };
201
202 } // namespace Internal
203 } // namespace Bookmarks
204
205 #endif // BOOKMARKMANAGER_H