2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "bookmarksplugin.h"
|
|
|
|
|
#include "bookmarkmanager.h"
|
|
|
|
|
#include "bookmarks_global.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2008-12-05 16:39:33 +01:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
2009-01-13 13:39:31 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2011-09-05 16:10:37 +02:00
|
|
|
#include <coreplugin/id.h>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QtPlugin>
|
|
|
|
|
#include <QDebug>
|
2008-12-05 16:39:33 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMenu>
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace Bookmarks::Constants;
|
2014-10-15 12:25:29 +02:00
|
|
|
using namespace Core;
|
2008-12-05 16:39:33 +01:00
|
|
|
using namespace TextEditor;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
namespace Bookmarks {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
BookmarksPlugin::BookmarksPlugin()
|
2010-06-02 15:02:05 +02:00
|
|
|
: m_bookmarkManager(0),
|
|
|
|
|
m_bookmarkMarginActionLineNumber(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
|
|
|
|
|
{
|
2014-10-15 12:25:29 +02:00
|
|
|
Context textcontext(TextEditor::Constants::C_TEXTEDITOR);
|
|
|
|
|
Context globalcontext(Core::Constants::C_GLOBAL);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
|
|
|
|
|
ActionContainer *mbm = ActionManager::createMenu(Id(BOOKMARKS_MENU));
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->menu()->setTitle(tr("&Bookmarks"));
|
|
|
|
|
mtools->addMenu(mbm);
|
|
|
|
|
|
|
|
|
|
//Toggle
|
|
|
|
|
m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
|
2014-10-15 12:25:29 +02:00
|
|
|
Command *cmd = ActionManager::registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
|
|
|
|
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+M") : tr("Ctrl+M")));
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->addAction(cmd);
|
|
|
|
|
|
2012-06-05 14:22:20 +02:00
|
|
|
mbm->addSeparator(textcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
//Previous
|
|
|
|
|
m_prevAction = new QAction(tr("Previous Bookmark"), this);
|
2014-10-15 12:25:29 +02:00
|
|
|
cmd = ActionManager::registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
|
|
|
|
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+,") : tr("Ctrl+,")));
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->addAction(cmd);
|
|
|
|
|
|
|
|
|
|
//Next
|
|
|
|
|
m_nextAction = new QAction(tr("Next Bookmark"), this);
|
2014-10-15 12:25:29 +02:00
|
|
|
cmd = ActionManager::registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
|
|
|
|
|
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+.") : tr("Ctrl+.")));
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->addAction(cmd);
|
|
|
|
|
|
2012-06-05 14:22:20 +02:00
|
|
|
mbm->addSeparator(globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
//Previous Doc
|
2010-05-14 15:45:43 +02:00
|
|
|
m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
|
2014-10-15 12:25:29 +02:00
|
|
|
cmd = ActionManager::registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->addAction(cmd);
|
|
|
|
|
|
|
|
|
|
//Next Doc
|
2010-05-14 15:45:43 +02:00
|
|
|
m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
|
2014-10-15 12:25:29 +02:00
|
|
|
cmd = ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
|
2008-12-02 12:01:29 +01:00
|
|
|
mbm->addAction(cmd);
|
|
|
|
|
|
2014-07-09 17:18:03 +03:00
|
|
|
m_editBookmarkAction = new QAction(tr("Edit Bookmark"), this);
|
2012-09-20 11:27:22 +04:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
m_bookmarkManager = new BookmarkManager;
|
|
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
connect(m_toggleAction, &QAction::triggered, [this]() {
|
|
|
|
|
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor())
|
2014-12-21 21:54:30 +02:00
|
|
|
m_bookmarkManager->toggleBookmark(editor->document()->filePath().toString(), editor->currentLine());
|
2014-10-15 12:25:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(m_prevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prev);
|
|
|
|
|
connect(m_nextAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::next);
|
|
|
|
|
connect(m_docPrevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prevInDocument);
|
|
|
|
|
connect(m_docNextAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::nextInDocument);
|
|
|
|
|
|
|
|
|
|
connect(m_editBookmarkAction, &QAction::triggered, [this]() {
|
|
|
|
|
m_bookmarkManager->editByFileAndLine(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(m_bookmarkManager, &BookmarkManager::updateActions, this, &BookmarksPlugin::updateActions);
|
2008-12-02 12:01:29 +01:00
|
|
|
updateActions(m_bookmarkManager->state());
|
|
|
|
|
addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
|
|
|
|
|
|
2008-12-05 16:39:33 +01:00
|
|
|
m_bookmarkMarginAction = new QAction(this);
|
2009-04-17 21:11:52 +02:00
|
|
|
m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
|
2014-10-15 12:25:29 +02:00
|
|
|
connect(m_bookmarkMarginAction, &QAction::triggered, [this]() {
|
|
|
|
|
m_bookmarkManager->toggleBookmark(m_bookmarkMarginActionFileName,
|
|
|
|
|
m_bookmarkMarginActionLineNumber);
|
|
|
|
|
});
|
2008-12-05 16:39:33 +01:00
|
|
|
|
|
|
|
|
// EditorManager
|
2014-10-15 12:25:29 +02:00
|
|
|
connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
|
|
|
|
|
this, &BookmarksPlugin::editorAboutToClose);
|
|
|
|
|
connect(EditorManager::instance(), &EditorManager::editorOpened,
|
|
|
|
|
this, &BookmarksPlugin::editorOpened);
|
2008-12-05 16:39:33 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BookmarksPlugin::~BookmarksPlugin()
|
|
|
|
|
{
|
|
|
|
|
delete m_bookmarkManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BookmarksPlugin::updateActions(int state)
|
|
|
|
|
{
|
|
|
|
|
const bool hasbm = state >= BookmarkManager::HasBookMarks;
|
|
|
|
|
const bool hasdocbm = state == BookmarkManager::HasBookmarksInDocument;
|
|
|
|
|
|
|
|
|
|
m_toggleAction->setEnabled(true);
|
|
|
|
|
m_prevAction->setEnabled(hasbm);
|
|
|
|
|
m_nextAction->setEnabled(hasbm);
|
|
|
|
|
m_docPrevAction->setEnabled(hasdocbm);
|
|
|
|
|
m_docNextAction->setEnabled(hasdocbm);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
void BookmarksPlugin::editorOpened(IEditor *editor)
|
2008-12-05 16:39:33 +01:00
|
|
|
{
|
2014-09-30 16:54:26 +02:00
|
|
|
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
2014-10-17 10:21:57 +02:00
|
|
|
connect(widget, &TextEditorWidget::markRequested, m_bookmarkManager,
|
|
|
|
|
[this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
|
|
|
|
|
if (kind == BookmarkRequest && editor->document())
|
2014-12-21 21:54:30 +02:00
|
|
|
m_bookmarkManager->toggleBookmark(editor->document()->filePath().toString(), line);
|
2014-10-17 10:21:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connect(widget, &TextEditorWidget::markTooltipRequested, m_bookmarkManager,
|
|
|
|
|
[this, editor](TextEditorWidget *, const QPoint &pos, int line) {
|
|
|
|
|
if (editor->document())
|
|
|
|
|
m_bookmarkManager->handleBookmarkTooltipRequest(editor, pos, line);
|
|
|
|
|
});
|
2014-09-30 16:54:26 +02:00
|
|
|
|
|
|
|
|
connect(widget, &TextEditorWidget::markContextMenuRequested,
|
|
|
|
|
this, &BookmarksPlugin::requestContextMenu);
|
2008-12-10 13:58:02 +01:00
|
|
|
}
|
2008-12-05 16:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
void BookmarksPlugin::editorAboutToClose(IEditor *editor)
|
2008-12-05 16:39:33 +01:00
|
|
|
{
|
2014-09-30 16:54:26 +02:00
|
|
|
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
|
|
|
|
|
connect(widget, &TextEditorWidget::markContextMenuRequested,
|
|
|
|
|
this, &BookmarksPlugin::requestContextMenu);
|
2008-12-10 13:58:02 +01:00
|
|
|
}
|
2008-12-05 16:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-30 16:54:26 +02:00
|
|
|
void BookmarksPlugin::requestContextMenu(TextEditorWidget *widget,
|
2008-12-05 16:39:33 +01:00
|
|
|
int lineNumber, QMenu *menu)
|
|
|
|
|
{
|
2013-10-17 17:41:47 +02:00
|
|
|
// Don't set bookmarks in disassembler views.
|
2014-09-30 16:54:26 +02:00
|
|
|
if (widget->textDocument()->property("DisassemblerView").toBool())
|
2013-10-17 17:41:47 +02:00
|
|
|
return;
|
|
|
|
|
|
2008-12-05 16:39:33 +01:00
|
|
|
m_bookmarkMarginActionLineNumber = lineNumber;
|
2014-12-21 21:54:30 +02:00
|
|
|
m_bookmarkMarginActionFileName = widget->textDocument()->filePath().toString();
|
2012-09-20 11:27:22 +04:00
|
|
|
|
2008-12-05 16:39:33 +01:00
|
|
|
menu->addAction(m_bookmarkMarginAction);
|
2012-09-20 11:27:22 +04:00
|
|
|
if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber))
|
2014-07-09 17:18:03 +03:00
|
|
|
menu->addAction(m_editBookmarkAction);
|
2008-12-05 16:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-15 12:25:29 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Bookmarks
|