1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "bookmarksplugin.h"
35 #include "bookmarkmanager.h"
36 #include "bookmarks_global.h"
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>
50 #include <QtCore/QtPlugin>
51 #include <QtCore/QDebug>
53 #include <QtGui/QMenu>
55 using namespace Bookmarks::Constants;
56 using namespace Bookmarks::Internal;
57 using namespace TextEditor;
59 BookmarksPlugin *BookmarksPlugin::m_instance = 0;
61 BookmarksPlugin::BookmarksPlugin()
62 : m_bookmarkManager(0),
63 m_bookmarkMarginActionLineNumber(0)
68 void BookmarksPlugin::extensionsInitialized()
72 bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
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);
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"));
85 m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
87 am->registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
89 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M")));
91 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+M")));
95 QAction *sep = new QAction(this);
96 sep->setSeparator(true);
97 cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.Toggle"), textcontext);
101 m_prevAction = new QAction(tr("Previous Bookmark"), this);
102 cmd = am->registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
104 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+,")));
106 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+,")));
111 m_nextAction = new QAction(tr("Next Bookmark"), this);
112 cmd = am->registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
114 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+.")));
116 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+.")));
120 sep = new QAction(this);
121 sep->setSeparator(true);
122 cmd = am->registerAction(sep, Core::Id("Bookmarks.Sep.DirNavigation"), globalcontext);
126 m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
127 cmd = am->registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
131 m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
132 cmd = am->registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
135 m_bookmarkManager = new BookmarkManager;
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));
146 m_bookmarkMarginAction = new QAction(this);
147 m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
148 connect(m_bookmarkMarginAction, SIGNAL(triggered()),
149 this, SLOT(bookmarkMarginActionTriggered()));
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*)));
161 BookmarksPlugin::~BookmarksPlugin()
163 delete m_bookmarkManager;
166 void BookmarksPlugin::updateActions(int state)
168 const bool hasbm = state >= BookmarkManager::HasBookMarks;
169 const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
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);
178 void BookmarksPlugin::editorOpened(Core::IEditor *editor)
180 if (qobject_cast<ITextEditor *>(editor)) {
181 connect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
182 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
186 void BookmarksPlugin::editorAboutToClose(Core::IEditor *editor)
188 if (qobject_cast<ITextEditor *>(editor)) {
189 disconnect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
190 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
194 void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
195 int lineNumber, QMenu *menu)
197 m_bookmarkMarginActionLineNumber = lineNumber;
198 m_bookmarkMarginActionFileName = editor->file()->fileName();
199 menu->addAction(m_bookmarkMarginAction);
202 void BookmarksPlugin::bookmarkMarginActionTriggered()
204 m_bookmarkManager->toggleBookmark(
205 m_bookmarkMarginActionFileName,
206 m_bookmarkMarginActionLineNumber
210 Q_EXPORT_PLUGIN(BookmarksPlugin)