OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / bookmarks / bookmarksplugin.cpp
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 #include "bookmarksplugin.h"
35 #include "bookmarkmanager.h"
36 #include "bookmarks_global.h"
37
38 #include <coreplugin/icore.h>
39 #include <coreplugin/editormanager/ieditor.h>
40 #include <coreplugin/editormanager/editormanager.h>
41 #include <coreplugin/coreconstants.h>
42 #include <coreplugin/actionmanager/actionmanager.h>
43 #include <coreplugin/actionmanager/actioncontainer.h>
44 #include <coreplugin/actionmanager/command.h>
45 #include <coreplugin/uniqueidmanager.h>
46 #include <extensionsystem/pluginmanager.h>
47 #include <texteditor/itexteditor.h>
48 #include <texteditor/texteditorconstants.h>
49
50 #include <QtCore/QtPlugin>
51 #include <QtCore/QDebug>
52
53 #include <QtGui/QMenu>
54
55 using namespace Bookmarks::Constants;
56 using namespace Bookmarks::Internal;
57 using namespace TextEditor;
58
59 BookmarksPlugin *BookmarksPlugin::m_instance = 0;
60
61 BookmarksPlugin::BookmarksPlugin()
62     : m_bookmarkManager(0),
63       m_bookmarkMarginActionLineNumber(0)
64 {
65     m_instance = this;
66 }
67
68 void BookmarksPlugin::extensionsInitialized()
69 {
70 }
71
72 bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
73 {
74     Core::ICore *core = Core::ICore::instance();
75     Core::ActionManager *am = core->actionManager();
76     Core::Context textcontext(TextEditor::Constants::C_TEXTEDITOR);
77     Core::Context globalcontext(Core::Constants::C_GLOBAL);
78
79     Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
80     Core::ActionContainer *mbm = am->createMenu(Core::Id(BOOKMARKS_MENU));
81     mbm->menu()->setTitle(tr("&Bookmarks"));
82     mtools->addMenu(mbm);
83
84     //Toggle
85     m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
86     Core::Command *cmd =
87         am->registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
88 #ifndef Q_WS_MAC
89     cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M")));
90 #else
91     cmd->setDefaultKeySequence(QKeySequence(tr("Meta+M")));
92 #endif
93     mbm->addAction(cmd);
94
95     QAction *sep = new QAction(this);
96     sep->setSeparator(true);
97     cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.Toggle"), textcontext);
98     mbm->addAction(cmd);
99
100     //Previous
101     m_prevAction = new QAction(tr("Previous Bookmark"), this);
102     cmd = am->registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
103 #ifndef Q_WS_MAC
104     cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+,")));
105 #else
106     cmd->setDefaultKeySequence(QKeySequence(tr("Meta+,")));
107 #endif
108     mbm->addAction(cmd);
109
110     //Next
111     m_nextAction = new QAction(tr("Next Bookmark"), this);
112     cmd = am->registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
113 #ifndef Q_WS_MAC
114     cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+.")));
115 #else
116     cmd->setDefaultKeySequence(QKeySequence(tr("Meta+.")));
117 #endif
118     mbm->addAction(cmd);
119
120     sep = new QAction(this);
121     sep->setSeparator(true);
122     cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.DirNavigation"), globalcontext);
123     mbm->addAction(cmd);
124
125     //Previous Doc
126     m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
127     cmd = am->registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
128     mbm->addAction(cmd);
129
130     //Next Doc
131     m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
132     cmd = am->registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
133     mbm->addAction(cmd);
134
135     m_bookmarkManager = new BookmarkManager;
136
137     connect(m_toggleAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(toggleBookmark()));
138     connect(m_prevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prev()));
139     connect(m_nextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(next()));
140     connect(m_docPrevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prevInDocument()));
141     connect(m_docNextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(nextInDocument()));
142     connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int)));
143     updateActions(m_bookmarkManager->state());
144     addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
145
146     m_bookmarkMarginAction = new QAction(this);
147     m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
148     connect(m_bookmarkMarginAction, SIGNAL(triggered()),
149         this, SLOT(bookmarkMarginActionTriggered()));
150
151     // EditorManager
152     QObject *editorManager = core->editorManager();
153     connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)),
154         this, SLOT(editorAboutToClose(Core::IEditor*)));
155     connect(editorManager, SIGNAL(editorOpened(Core::IEditor*)),
156         this, SLOT(editorOpened(Core::IEditor*)));
157
158     return true;
159 }
160
161 BookmarksPlugin::~BookmarksPlugin()
162 {
163     delete m_bookmarkManager;
164 }
165
166 void BookmarksPlugin::updateActions(int state)
167 {
168     const bool hasbm    = state >= BookmarkManager::HasBookMarks;
169     const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
170
171     m_toggleAction->setEnabled(true);
172     m_prevAction->setEnabled(hasbm);
173     m_nextAction->setEnabled(hasbm);
174     m_docPrevAction->setEnabled(hasdocbm);
175     m_docNextAction->setEnabled(hasdocbm);
176 }
177
178 void BookmarksPlugin::editorOpened(Core::IEditor *editor)
179 {
180     if (qobject_cast<ITextEditor *>(editor)) {
181         connect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
182                 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
183     }
184 }
185
186 void BookmarksPlugin::editorAboutToClose(Core::IEditor *editor)
187 {
188     if (qobject_cast<ITextEditor *>(editor)) {
189         disconnect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
190                 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
191     }
192 }
193
194 void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
195     int lineNumber, QMenu *menu)
196 {
197     m_bookmarkMarginActionLineNumber = lineNumber;
198     m_bookmarkMarginActionFileName = editor->file()->fileName();
199     menu->addAction(m_bookmarkMarginAction);
200 }
201
202 void BookmarksPlugin::bookmarkMarginActionTriggered()
203 {
204     m_bookmarkManager->toggleBookmark(
205         m_bookmarkMarginActionFileName,
206         m_bookmarkMarginActionLineNumber
207     );
208 }
209
210 Q_EXPORT_PLUGIN(BookmarksPlugin)