Commit 80323dfb authored by hjk's avatar hjk

BookMarks: Some code modernization

Change-Id: Ifb39d67d69bb97f1516bcdc70da6ca118c797126
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
parent 79561071
......@@ -233,10 +233,8 @@ BookmarkView::BookmarkView(BookmarkManager *manager) :
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
connect(this, SIGNAL(clicked(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
connect(this, SIGNAL(activated(QModelIndex)),
this, SLOT(gotoBookmark(QModelIndex)));
connect(this, &QAbstractItemView::clicked, this, &BookmarkView::gotoBookmark);
connect(this, &QAbstractItemView::activated, this, &BookmarkView::gotoBookmark);
}
BookmarkView::~BookmarkView()
......@@ -266,16 +264,11 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
if (model()->rowCount() == 0)
removeAll->setEnabled(false);
connect(moveUp, SIGNAL(triggered()),
m_manager, SLOT(moveUp()));
connect(moveDown, SIGNAL(triggered()),
m_manager, SLOT(moveDown()));
connect(remove, SIGNAL(triggered()),
this, SLOT(removeFromContextMenu()));
connect(removeAll, SIGNAL(triggered()),
this, SLOT(removeAll()));
connect(edit, SIGNAL(triggered()),
m_manager, SLOT(edit()));
connect(moveUp, &QAction::triggered, m_manager, &BookmarkManager::moveUp);
connect(moveDown, &QAction::triggered, m_manager, &BookmarkManager::moveDown);
connect(remove, &QAction::triggered, this, &BookmarkView::removeFromContextMenu);
connect(removeAll, &QAction::triggered, this, &BookmarkView::removeAll);
connect(edit, &QAction::triggered, m_manager, &BookmarkManager::edit);
menu.exec(mapToGlobal(event->pos()));
}
......@@ -332,11 +325,11 @@ BookmarkManager::BookmarkManager() :
m_bookmarkIcon(QLatin1String(":/bookmarks/images/bookmark.png")),
m_selectionModel(new QItemSelectionModel(this, this))
{
connect(Core::ICore::instance(), SIGNAL(contextChanged(QList<Core::IContext*>,Core::Context)),
this, SLOT(updateActionStatus()));
connect(Core::ICore::instance(), &ICore::contextChanged,
this, &BookmarkManager::updateActionStatus);
connect(SessionManager::instance(), SIGNAL(sessionLoaded(QString)),
this, SLOT(loadBookmarks()));
connect(SessionManager::instance(), &SessionManager::sessionLoaded,
this, &BookmarkManager::loadBookmarks);
updateActionStatus();
}
......@@ -440,7 +433,7 @@ QMimeData *BookmarkManager::mimeData(const QModelIndexList &indexes) const
return data;
}
void BookmarkManager::toggleBookmark()
void BookmarkManager::toggleBookmark(bool)
{
BaseTextEditor *editor = BaseTextEditor::currentTextEditor();
if (!editor)
......@@ -706,7 +699,7 @@ void BookmarkManager::moveDown()
saveBookmarks();
}
void BookmarkManager::edit(const QString &fileName, int lineNumber)
void BookmarkManager::editByFileAndLine(const QString &fileName, int lineNumber)
{
Bookmark *b = findBookmark(fileName, lineNumber);
QModelIndex current = selectionModel()->currentIndex();
......@@ -731,8 +724,8 @@ void BookmarkManager::edit()
lineNumberSpinbox->setValue(b->lineNumber());
lineNumberSpinbox->setMaximumWidth(100);
auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), &dlg, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), &dlg, SLOT(reject()));
connect(buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
layout->addRow(tr("Note text:"), noteEdit);
layout->addRow(tr("Line number:"), lineNumberSpinbox);
layout->addWidget(buttonBox);
......
......@@ -100,8 +100,7 @@ public:
void handleBookmarkTooltipRequest(TextEditor::TextEditorWidget *widget,
const QPoint &pos, int line);
public slots:
void toggleBookmark();
void toggleBookmark(bool);
void toggleBookmark(const QString &fileName, int lineNumber);
void nextInDocument();
void prevInDocument();
......@@ -110,18 +109,17 @@ public slots:
void moveUp();
void moveDown();
void edit();
void edit(const QString &fileName, int lineNumber);
void editByFileAndLine(const QString &fileName, int lineNumber);
bool gotoBookmark(Bookmark *bookmark);
signals:
void updateActions(int state);
void currentIndexChanged(const QModelIndex &);
private slots:
private:
void updateActionStatus();
void loadBookmarks();
private:
void documentPrevNext(bool next);
Bookmark *findBookmark(const QString &filePath, int lineNumber);
......
......@@ -50,86 +50,94 @@
#include <QMenu>
using namespace Bookmarks::Constants;
using namespace Bookmarks::Internal;
using namespace Core;
using namespace TextEditor;
namespace Bookmarks {
namespace Internal {
BookmarksPlugin::BookmarksPlugin()
: m_bookmarkManager(0),
m_bookmarkMarginActionLineNumber(0)
{
}
void BookmarksPlugin::extensionsInitialized()
{
}
bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
{
Core::Context textcontext(TextEditor::Constants::C_TEXTEDITOR);
Core::Context globalcontext(Core::Constants::C_GLOBAL);
Context textcontext(TextEditor::Constants::C_TEXTEDITOR);
Context globalcontext(Core::Constants::C_GLOBAL);
Core::ActionContainer *mtools = Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
Core::ActionContainer *mbm = Core::ActionManager::createMenu(Core::Id(BOOKMARKS_MENU));
ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
ActionContainer *mbm = ActionManager::createMenu(Id(BOOKMARKS_MENU));
mbm->menu()->setTitle(tr("&Bookmarks"));
mtools->addMenu(mbm);
//Toggle
m_toggleAction = new QAction(tr("Toggle Bookmark"), this);
Core::Command *cmd =
Core::ActionManager::registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+M") : tr("Ctrl+M")));
Command *cmd = ActionManager::registerAction(m_toggleAction, BOOKMARKS_TOGGLE_ACTION, textcontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+M") : tr("Ctrl+M")));
mbm->addAction(cmd);
mbm->addSeparator(textcontext);
//Previous
m_prevAction = new QAction(tr("Previous Bookmark"), this);
cmd = Core::ActionManager::registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+,") : tr("Ctrl+,")));
cmd = ActionManager::registerAction(m_prevAction, BOOKMARKS_PREV_ACTION, globalcontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+,") : tr("Ctrl+,")));
mbm->addAction(cmd);
//Next
m_nextAction = new QAction(tr("Next Bookmark"), this);
cmd = Core::ActionManager::registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+.") : tr("Ctrl+.")));
cmd = ActionManager::registerAction(m_nextAction, BOOKMARKS_NEXT_ACTION, globalcontext);
cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+.") : tr("Ctrl+.")));
mbm->addAction(cmd);
mbm->addSeparator(globalcontext);
//Previous Doc
m_docPrevAction = new QAction(tr("Previous Bookmark in Document"), this);
cmd = Core::ActionManager::registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
cmd = ActionManager::registerAction(m_docPrevAction, BOOKMARKS_PREVDOC_ACTION, globalcontext);
mbm->addAction(cmd);
//Next Doc
m_docNextAction = new QAction(tr("Next Bookmark in Document"), this);
cmd = Core::ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
cmd = ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
mbm->addAction(cmd);
m_editBookmarkAction = new QAction(tr("Edit Bookmark"), this);
m_bookmarkManager = new BookmarkManager;
connect(m_toggleAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(toggleBookmark()));
connect(m_prevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prev()));
connect(m_nextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(next()));
connect(m_docPrevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prevInDocument()));
connect(m_docNextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(nextInDocument()));
connect(m_editBookmarkAction, SIGNAL(triggered()), this, SLOT(editBookmarkActionTriggered()));
connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int)));
connect(m_toggleAction, &QAction::triggered, [this]() {
if (BaseTextEditor *editor = BaseTextEditor::currentTextEditor())
m_bookmarkManager->toggleBookmark(editor->document()->filePath(), editor->currentLine());
});
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);
updateActions(m_bookmarkManager->state());
addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
m_bookmarkMarginAction = new QAction(this);
m_bookmarkMarginAction->setText(tr("Toggle Bookmark"));
connect(m_bookmarkMarginAction, SIGNAL(triggered()),
this, SLOT(bookmarkMarginActionTriggered()));
connect(m_bookmarkMarginAction, &QAction::triggered, [this]() {
m_bookmarkManager->toggleBookmark(m_bookmarkMarginActionFileName,
m_bookmarkMarginActionLineNumber);
});
// EditorManager
connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
this, SLOT(editorAboutToClose(Core::IEditor*)));
connect(Core::EditorManager::instance(), SIGNAL(editorOpened(Core::IEditor*)),
this, SLOT(editorOpened(Core::IEditor*)));
connect(EditorManager::instance(), &EditorManager::editorAboutToClose,
this, &BookmarksPlugin::editorAboutToClose);
connect(EditorManager::instance(), &EditorManager::editorOpened,
this, &BookmarksPlugin::editorOpened);
return true;
}
......@@ -151,7 +159,7 @@ void BookmarksPlugin::updateActions(int state)
m_docNextAction->setEnabled(hasdocbm);
}
void BookmarksPlugin::editorOpened(Core::IEditor *editor)
void BookmarksPlugin::editorOpened(IEditor *editor)
{
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
connect(widget, &TextEditorWidget::markRequested,
......@@ -165,7 +173,7 @@ void BookmarksPlugin::editorOpened(Core::IEditor *editor)
}
}
void BookmarksPlugin::editorAboutToClose(Core::IEditor *editor)
void BookmarksPlugin::editorAboutToClose(IEditor *editor)
{
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
connect(widget, &TextEditorWidget::markContextMenuRequested,
......@@ -188,13 +196,5 @@ void BookmarksPlugin::requestContextMenu(TextEditorWidget *widget,
menu->addAction(m_editBookmarkAction);
}
void BookmarksPlugin::bookmarkMarginActionTriggered()
{
m_bookmarkManager->toggleBookmark(m_bookmarkMarginActionFileName,
m_bookmarkMarginActionLineNumber);
}
void BookmarksPlugin::editBookmarkActionTriggered()
{
m_bookmarkManager->edit(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber);
}
} // namespace Internal
} // namespace Bookmarks
......@@ -60,18 +60,13 @@ public:
~BookmarksPlugin();
bool initialize(const QStringList &arguments, QString *errorMessage);
void extensionsInitialized();
void extensionsInitialized() {}
public slots:
private:
void updateActions(int stateMask);
private slots:
void editorOpened(Core::IEditor *editor);
void editorAboutToClose(Core::IEditor *editor);
void bookmarkMarginActionTriggered();
void editBookmarkActionTriggered();
private:
void requestContextMenu(TextEditor::TextEditorWidget *widget,
int lineNumber, QMenu *menu);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment