OSDN Git Service

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