1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "bookmarksplugin.h"
34 #include "bookmarkmanager.h"
35 #include "bookmarks_global.h"
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>
49 #include <QtCore/QtPlugin>
50 #include <QtCore/QDebug>
52 #include <QtGui/QMenu>
54 using namespace Bookmarks::Constants;
55 using namespace Bookmarks::Internal;
56 using namespace TextEditor;
58 BookmarksPlugin *BookmarksPlugin::m_instance = 0;
60 BookmarksPlugin::BookmarksPlugin()
61 : m_bookmarkManager(0),
62 m_bookmarkMarginActionLineNumber(0)
67 void BookmarksPlugin::extensionsInitialized()
71 bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
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);
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"));
84 m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
86 am->registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
88 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M")));
90 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+M")));
94 QAction *sep = new QAction(this);
95 sep->setSeparator(true);
96 cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.Toggle"), textcontext);
100 m_prevAction = new QAction(tr("Previous Bookmark"), this);
101 cmd = am->registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
103 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+,")));
105 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+,")));
110 m_nextAction = new QAction(tr("Next Bookmark"), this);
111 cmd = am->registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
113 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+.")));
115 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+.")));
119 sep = new QAction(this);
120 sep->setSeparator(true);
121 cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.DirNavigation"), globalcontext);
125 m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
126 cmd = am->registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
130 m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
131 cmd = am->registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
134 m_bookmarkManager = new BookmarkManager;
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));
145 m_bookmarkMarginAction = new QAction(this);
146 m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
147 connect(m_bookmarkMarginAction, SIGNAL(triggered()),
148 this, SLOT(bookmarkMarginActionTriggered()));
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*)));
160 BookmarksPlugin::~BookmarksPlugin()
162 delete m_bookmarkManager;
165 void BookmarksPlugin::updateActions(int state)
167 const bool hasbm = state >= BookmarkManager::HasBookMarks;
168 const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
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);
177 void BookmarksPlugin::editorOpened(Core::IEditor *editor)
179 if (qobject_cast<ITextEditor *>(editor)) {
180 connect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
181 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
185 void BookmarksPlugin::editorAboutToClose(Core::IEditor *editor)
187 if (qobject_cast<ITextEditor *>(editor)) {
188 disconnect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
189 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
193 void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
194 int lineNumber, QMenu *menu)
196 m_bookmarkMarginActionLineNumber = lineNumber;
197 m_bookmarkMarginActionFileName = editor->file()->fileName();
198 menu->addAction(m_bookmarkMarginAction);
201 void BookmarksPlugin::bookmarkMarginActionTriggered()
203 m_bookmarkManager->toggleBookmark(
204 m_bookmarkMarginActionFileName,
205 m_bookmarkMarginActionLineNumber
209 Q_EXPORT_PLUGIN(BookmarksPlugin)