Remove #include <extensionsystem/pluginmanager.h> from icore.h, a
[qt-creator-color-themes.git] / src / plugins / bookmarks / bookmarksplugin.cpp
blob961b11bd71437ee64125d6f1054ff3ef162575ae
1 /***************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Qt Software Information (qt-info@nokia.com)
8 **
9 **
10 ** Non-Open Source Usage
12 ** Licensees may use this file in accordance with the Qt Beta Version
13 ** License Agreement, Agreement version 2.2 provided with the Software or,
14 ** alternatively, in accordance with the terms contained in a written
15 ** agreement between you and Nokia.
17 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU General
20 ** Public License versions 2.0 or 3.0 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.GPL included in the packaging
22 ** of this file. Please review the following information to ensure GNU
23 ** General Public Licensing requirements will be met:
25 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
26 ** http://www.gnu.org/copyleft/gpl.html.
28 ** In addition, as a special exception, Nokia gives you certain additional
29 ** rights. These rights are described in the Nokia Qt GPL Exception
30 ** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
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/uniqueidmanager.h>
43 #include <coreplugin/actionmanager/actionmanager.h>
44 #include <extensionsystem/pluginmanager.h>
45 #include <texteditor/itexteditor.h>
46 #include <texteditor/texteditorconstants.h>
48 #include <QtCore/QtPlugin>
49 #include <QtCore/QDebug>
51 #include <QtGui/QMenu>
53 using namespace Bookmarks::Constants;
54 using namespace Bookmarks::Internal;
55 using namespace TextEditor;
57 BookmarksPlugin *BookmarksPlugin::m_instance = 0;
59 BookmarksPlugin::BookmarksPlugin()
60 : m_bookmarkManager(0), m_core(0)
62 m_instance = this;
65 void BookmarksPlugin::extensionsInitialized()
69 bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
71 m_core = ExtensionSystem::PluginManager::instance()->getObject<Core::ICore>();
72 Core::ActionManager *am = m_core->actionManager();
74 QList<int> context = QList<int>() << m_core->uniqueIDManager()->
75 uniqueIdentifier(Constants::BOOKMARKS_CONTEXT);
76 QList<int> textcontext, globalcontext;
77 textcontext << m_core->uniqueIDManager()->
78 uniqueIdentifier(TextEditor::Constants::C_TEXTEDITOR);
79 globalcontext << Core::Constants::C_GLOBAL_ID;
81 Core::ActionContainer *mtools =
82 am->actionContainer(Core::Constants::M_TOOLS);
84 Core::ActionContainer *mbm =
85 am->createMenu(QLatin1String(BOOKMARKS_MENU));
86 mbm->menu()->setTitle(tr("&Bookmarks"));
87 mtools->addMenu(mbm);
89 //Toggle
90 m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
91 Core::Command *cmd =
92 am->registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
93 #ifndef Q_OS_MAC
94 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+M")));
95 #else
96 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+M")));
97 #endif
98 mbm->addAction(cmd);
100 QAction *sep = new QAction(this);
101 sep->setSeparator(true);
102 cmd = am->registerAction(sep, QLatin1String("Bookmarks.Sep.Toggle"), textcontext);
103 mbm->addAction(cmd);
105 // Move Up
106 m_moveUpAction = new QAction(tr("Move Up"), this);
107 cmd = am->registerAction(m_moveUpAction, BOOKMARKS_MOVEUP_ACTION, context);
108 mbm->addAction(cmd);
110 // Move Down
111 m_moveDownAction = new QAction(tr("Move Down"), this);
112 cmd = am->registerAction(m_moveDownAction, BOOKMARKS_MOVEDOWN_ACTION, context);
113 mbm->addAction(cmd);
115 sep = new QAction(this);
116 sep->setSeparator(true);
117 cmd = am->registerAction(sep, QLatin1String("Bookmarks.Sep.Navigation"), context);
118 mbm->addAction(cmd);
120 //Previous
121 m_prevAction = new QAction(tr("Previous Bookmark"), this);
122 cmd = am->registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
123 #ifndef Q_OS_MAC
124 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+,")));
125 #else
126 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+,")));
127 #endif
128 mbm->addAction(cmd);
130 //Next
131 m_nextAction = new QAction(tr("Next Bookmark"), this);
132 cmd = am->registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
133 #ifndef Q_OS_MAC
134 cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+.")));
135 #else
136 cmd->setDefaultKeySequence(QKeySequence(tr("Meta+.")));
137 #endif
138 mbm->addAction(cmd);
140 sep = new QAction(this);
141 sep->setSeparator(true);
142 cmd = am->registerAction(sep, QLatin1String("Bookmarks.Sep.DirNavigation"), globalcontext);
143 mbm->addAction(cmd);
145 //Previous Doc
146 m_docPrevAction = new QAction(tr("Previous Bookmark In Document"), this);
147 cmd = am->registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
148 mbm->addAction(cmd);
150 //Next Doc
151 m_docNextAction = new QAction(tr("Next Bookmark In Document"), this);
152 cmd = am->registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
153 mbm->addAction(cmd);
155 m_bookmarkManager = new BookmarkManager;
157 connect(m_toggleAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(toggleBookmark()));
158 connect(m_prevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prev()));
159 connect(m_nextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(next()));
160 connect(m_docPrevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prevInDocument()));
161 connect(m_docNextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(nextInDocument()));
162 connect(m_moveUpAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(moveUp()));
163 connect(m_moveDownAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(moveDown()));
164 connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int)));
165 updateActions(m_bookmarkManager->state());
166 addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
168 m_bookmarkMarginAction = new QAction(this);
169 m_bookmarkMarginAction->setText("Toggle Bookmark");
170 //m_bookmarkAction->setIcon(QIcon(":/gdbdebugger/images/breakpoint.svg"));
171 connect(m_bookmarkMarginAction, SIGNAL(triggered()),
172 this, SLOT(bookmarkMarginActionTriggered()));
174 // EditorManager
175 QObject *editorManager = m_core->editorManager();
176 connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)),
177 this, SLOT(editorAboutToClose(Core::IEditor*)));
178 connect(editorManager, SIGNAL(editorOpened(Core::IEditor*)),
179 this, SLOT(editorOpened(Core::IEditor*)));
181 return true;
184 BookmarksPlugin::~BookmarksPlugin()
186 delete m_bookmarkManager;
189 void BookmarksPlugin::updateActions(int state)
191 const bool hasbm = state >= BookmarkManager::HasBookMarks;
192 const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
194 m_toggleAction->setEnabled(true);
195 m_prevAction->setEnabled(hasbm);
196 m_nextAction->setEnabled(hasbm);
197 m_docPrevAction->setEnabled(hasdocbm);
198 m_docNextAction->setEnabled(hasdocbm);
199 m_moveUpAction->setEnabled(hasbm);
200 m_moveDownAction->setEnabled(hasbm);
203 void BookmarksPlugin::editorOpened(Core::IEditor *editor)
205 if (qobject_cast<ITextEditor *>(editor)) {
206 connect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
207 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
211 void BookmarksPlugin::editorAboutToClose(Core::IEditor *editor)
213 if (qobject_cast<ITextEditor *>(editor)) {
214 disconnect(editor, SIGNAL(markContextMenuRequested(TextEditor::ITextEditor*,int,QMenu*)),
215 this, SLOT(requestContextMenu(TextEditor::ITextEditor*,int,QMenu*)));
219 void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
220 int lineNumber, QMenu *menu)
222 m_bookmarkMarginActionLineNumber = lineNumber;
223 m_bookmarkMarginActionFileName = editor->file()->fileName();
224 menu->addAction(m_bookmarkMarginAction);
227 void BookmarksPlugin::bookmarkMarginActionTriggered()
229 m_bookmarkManager->toggleBookmark(
230 m_bookmarkMarginActionFileName,
231 m_bookmarkMarginActionLineNumber
235 Q_EXPORT_PLUGIN(BookmarksPlugin)