OSDN Git Service

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