Commit d47b058c authored by Vasiliy Sorokin's avatar Vasiliy Sorokin Committed by Daniel Teske

Added feature for change note for bookmarks.

Task-number: QTCREATORBUG-5572

Change-Id: I12e84928e1c9d2b212991fafa7de99e0ec618d70
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent f3194ee1
......@@ -78,11 +78,27 @@ void Bookmark::updateFileName(const QString &fileName)
BaseTextMark::updateFileName(fileName);
}
void Bookmark::setNote(const QString &note)
{
m_note = note;
}
void Bookmark::updateNote(const QString &note)
{
setNote(note);
m_manager->updateBookmark(this);
}
QString Bookmark::lineText() const
{
return m_lineText;
}
QString Bookmark::note() const
{
return m_note;
}
QString Bookmark::filePath() const
{
return m_fileName;
......
......@@ -53,12 +53,15 @@ public:
void updateLineNumber(int lineNumber);
void updateBlock(const QTextBlock &block);
void updateFileName(const QString &fileName);
void setNote(const QString &note);
void updateNote(const QString &note);
void removedFromEditor();
QString filePath() const;
QString fileName() const;
QString path() const;
QString lineText() const;
QString note() const;
private:
BookmarkManager *m_manager;
......@@ -66,6 +69,7 @@ private:
QString m_onlyFile;
QString m_path;
QString m_lineText;
QString m_note;
};
} // namespace Internal
......
......@@ -34,6 +34,7 @@
#include "bookmarksplugin.h"
#include "bookmarks_global.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -41,6 +42,8 @@
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tooltip/tooltip.h>
#include <texteditor/tooltip/tipcontents.h>
#include <utils/qtcassert.h>
#include <QDebug>
......@@ -51,6 +54,7 @@
#include <QContextMenuEvent>
#include <QMenu>
#include <QPainter>
#include <QInputDialog>
Q_DECLARE_METATYPE(Bookmarks::Internal::Bookmark*)
......@@ -195,8 +199,10 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
//
// painter->drawText(3, opt.rect.top() + fm.ascent() + fm.height() + 6, directory);
QString note = index.data(BookmarkManager::Note).toString().trimmed();
QString lineText = index.data(BookmarkManager::LineText).toString().trimmed();
painter->drawText(6, opt.rect.top() + fm.ascent() + fm.height() + 6, lineText);
painter->drawText(6, opt.rect.top() + fm.ascent() + fm.height() + 6, note.isEmpty() ? lineText : note);
// Separator lines
painter->setPen(QColor::fromRgb(150,150,150));
......@@ -234,6 +240,8 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
QAction *moveDown = menu.addAction(tr("Move Down"));
QAction *remove = menu.addAction(tr("&Remove"));
QAction *removeAll = menu.addAction(tr("Remove All"));
QAction *editNote = menu.addAction(tr("Edit note"));
m_contextMenuIndex = indexAt(event->pos());
if (!m_contextMenuIndex.isValid()) {
moveUp->setEnabled(false);
......@@ -252,6 +260,8 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
this, SLOT(removeFromContextMenu()));
connect(removeAll, SIGNAL(triggered()),
this, SLOT(removeAll()));
connect(editNote, SIGNAL(triggered()),
m_manager, SLOT(editNote()));
menu.exec(mapToGlobal(event->pos()));
}
......@@ -339,6 +349,12 @@ QItemSelectionModel *BookmarkManager::selectionModel() const
return m_selectionModel;
}
bool BookmarkManager::hasBookmarkInPosition(const QString &fileName, int lineNumber)
{
QFileInfo fi(fileName);
return findBookmark(fi.path(), fi.fileName(), lineNumber);
}
QModelIndex BookmarkManager::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid())
......@@ -380,6 +396,8 @@ QVariant BookmarkManager::data(const QModelIndex &index, int role) const
return m_bookmarksList.at(index.row())->path();
else if (role == BookmarkManager::LineText)
return m_bookmarksList.at(index.row())->lineText();
else if (role == BookmarkManager::Note)
return m_bookmarksList.at(index.row())->note();
else if (role == Qt::ToolTipRole)
return QDir::toNativeSeparators(m_bookmarksList.at(index.row())->filePath());
......@@ -665,6 +683,32 @@ void BookmarkManager::moveDown()
selectionModel()->setCurrentIndex(current.sibling(row, 0), QItemSelectionModel::Select | QItemSelectionModel::Clear);
}
void BookmarkManager::editNote(const QString &fileName, int lineNumber)
{
QFileInfo fi(fileName);
Bookmark *b = findBookmark(fi.path(), fi.fileName(), lineNumber);
QModelIndex current = selectionModel()->currentIndex();
selectionModel()->setCurrentIndex(current.sibling(m_bookmarksList.indexOf(b), 0),
QItemSelectionModel::Select | QItemSelectionModel::Clear);
editNote();
}
void BookmarkManager::editNote()
{
QModelIndex current = selectionModel()->currentIndex();
Bookmark *b = m_bookmarksList.at(current.row());
bool inputOk = false;
QString noteText = QInputDialog::getText(0, tr("Edit note"),
tr("Note text:"), QLineEdit::Normal,
b->note(), &inputOk);
if (inputOk) {
b->updateNote(noteText.replace('\t', ' '));
emit dataChanged(current, current);
}
}
/* Returns the bookmark at the given file and line number, or 0 if no such bookmark exists. */
Bookmark *BookmarkManager::findBookmark(const QString &path, const QString &fileName, int lineNumber)
{
......@@ -704,15 +748,22 @@ void BookmarkManager::addBookmark(Bookmark *bookmark, bool userset)
/* Adds a new bookmark based on information parsed from the string. */
void BookmarkManager::addBookmark(const QString &s)
{
int index2 = s.lastIndexOf(':');
// index3 is a frontier beetween note text and other bookmarks data
int index3 = s.lastIndexOf('\t');
if (index3 < 0)
index3 = s.size();
int index2 = s.lastIndexOf(':', index3 - 1);
int index1 = s.indexOf(':');
if (index2 != -1 || index1 != -1) {
if (index3 != -1 || index2 != -1 || index1 != -1) {
const QString &filePath = s.mid(index1+1, index2-index1-1);
const int lineNumber = s.mid(index2 + 1).toInt();
const QString &note = s.mid(index3 + 1);
const int lineNumber = s.mid(index2 + 1, index3 - index2 - 1).toInt();
const QFileInfo fi(filePath);
if (!filePath.isEmpty() && !findBookmark(fi.path(), fi.fileName(), lineNumber)) {
Bookmark *b = new Bookmark(filePath, lineNumber, this);
b->setNote(note);
b->init();
addBookmark(b, false);
}
......@@ -725,8 +776,12 @@ void BookmarkManager::addBookmark(const QString &s)
QString BookmarkManager::bookmarkToString(const Bookmark *b)
{
const QLatin1Char colon(':');
// Using \t as delimiter because any another symbol can be a part of note.
const QLatin1Char noteDelimiter('\t');
// Empty string was the name of the bookmark, which now is always ""
return QLatin1String("") + colon + b->filePath() + colon + QString::number(b->lineNumber());
return QLatin1String("") + colon + b->filePath() +
colon + QString::number(b->lineNumber()) +
noteDelimiter + b->note();
}
/* Saves the bookmarks to the session settings. */
......@@ -740,6 +795,18 @@ void BookmarkManager::saveBookmarks()
sessionManager()->setValue("Bookmarks", list);
}
void BookmarkManager::operateTooltip(TextEditor::ITextEditor *textEditor, const QPoint &pos, Bookmark *mark)
{
if (!mark)
return;
if (mark->note().isEmpty()) {
TextEditor::ToolTip::instance()->hide();
} else {
TextEditor::ToolTip::instance()->show(pos, TextEditor::TextContent(mark->note()), textEditor->widget());
}
}
/* Loads the bookmarks from the session settings. */
void BookmarkManager::loadBookmarks()
{
......@@ -759,6 +826,16 @@ void BookmarkManager::handleBookmarkRequest(TextEditor::ITextEditor *textEditor,
toggleBookmark(textEditor->document()->fileName(), line);
}
void BookmarkManager::handleBookmarkTooltipRequest(TextEditor::ITextEditor *textEditor, const QPoint &pos,
int line)
{
if (textEditor->document()) {
const QFileInfo fi(textEditor->document()->fileName());
Bookmark *mark = findBookmark(fi.path(), fi.fileName(), line);
operateTooltip(textEditor, pos, mark);
}
}
// BookmarkViewFactory
BookmarkViewFactory::BookmarkViewFactory(BookmarkManager *bm)
......
......@@ -86,11 +86,14 @@ public:
// this QItemSelectionModel is shared by all views
QItemSelectionModel *selectionModel() const;
bool hasBookmarkInPosition(const QString &fileName, int lineNumber);
enum Roles {
Filename = Qt::UserRole,
LineNumber = Qt::UserRole + 1,
Directory = Qt::UserRole + 2,
LineText = Qt::UserRole + 3
LineText = Qt::UserRole + 3,
Note = Qt::UserRole + 4
};
public slots:
......@@ -102,6 +105,8 @@ public slots:
void prev();
void moveUp();
void moveDown();
void editNote();
void editNote(const QString &fileName, int lineNumber);
bool gotoBookmark(Bookmark *bookmark);
signals:
......@@ -114,6 +119,9 @@ private slots:
void handleBookmarkRequest(TextEditor::ITextEditor * textEditor,
int line,
TextEditor::ITextEditor::MarkRequestKind kind);
void handleBookmarkTooltipRequest(TextEditor::ITextEditor *textEditor,
const QPoint &pos,
int line);
private:
TextEditor::ITextEditor *currentTextEditor() const;
......@@ -126,6 +134,7 @@ private:
void addBookmark(const QString &s);
static QString bookmarkToString(const Bookmark *b);
void saveBookmarks();
void operateTooltip(TextEditor::ITextEditor *textEditor, const QPoint &pos, Bookmark *mark);
typedef QMultiMap<QString, Bookmark *> FileNameBookmarksMap;
typedef QMap<QString, FileNameBookmarksMap *> DirectoryFileBookmarksMap;
......
......@@ -37,6 +37,7 @@ namespace Constants {
const char BOOKMARKS_TOGGLE_ACTION[] = "Bookmarks.Toggle";
const char BOOKMARKS_MOVEUP_ACTION[] = "Bookmarks.MoveUp";
const char BOOKMARKS_MOVEDOWN_ACTION[] = "Bookmarks.MoveDown";
const char BOOKMARKS_EDITNOTE_ACTION[] = "Bookmarks.EditNote";
const char BOOKMARKS_PREV_ACTION[] = "Bookmarks.Previous";
const char BOOKMARKS_NEXT_ACTION[] = "Bookmarks.Next";
const char BOOKMARKS_PREVDIR_ACTION[] = "Bookmarks.PreviousDirectory";
......
......@@ -109,6 +109,8 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
cmd = Core::ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
mbm->addAction(cmd);
m_editNoteAction = new QAction(tr("Edit Bookmark Note"), this);
m_bookmarkManager = new BookmarkManager;
connect(m_toggleAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(toggleBookmark()));
......@@ -116,6 +118,7 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
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_editNoteAction, SIGNAL(triggered()), this, SLOT(bookmarkEditNoteActionTriggered()));
connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int)));
updateActions(m_bookmarkManager->state());
addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
......@@ -164,6 +167,10 @@ void BookmarksPlugin::editorOpened(Core::IEditor *editor)
m_bookmarkManager,
SLOT(handleBookmarkRequest(TextEditor::ITextEditor*,int,
TextEditor::ITextEditor::MarkRequestKind)));
connect(editor,
SIGNAL(markTooltipRequested(TextEditor::ITextEditor*,QPoint,int)),
m_bookmarkManager,
SLOT(handleBookmarkTooltipRequest(TextEditor::ITextEditor*,QPoint,int)));
}
}
......@@ -180,15 +187,21 @@ void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
{
m_bookmarkMarginActionLineNumber = lineNumber;
m_bookmarkMarginActionFileName = editor->document()->fileName();
menu->addAction(m_bookmarkMarginAction);
if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber))
menu->addAction(m_editNoteAction);
}
void BookmarksPlugin::bookmarkMarginActionTriggered()
{
m_bookmarkManager->toggleBookmark(
m_bookmarkMarginActionFileName,
m_bookmarkMarginActionLineNumber
);
m_bookmarkManager->toggleBookmark(m_bookmarkMarginActionFileName,
m_bookmarkMarginActionLineNumber);
}
void BookmarksPlugin::bookmarkEditNoteActionTriggered()
{
m_bookmarkManager->editNote(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber);
}
Q_EXPORT_PLUGIN(BookmarksPlugin)
......@@ -77,6 +77,7 @@ private slots:
void requestContextMenu(TextEditor::ITextEditor *editor,
int lineNumber, QMenu *menu);
void bookmarkMarginActionTriggered();
void bookmarkEditNoteActionTriggered();
private:
static BookmarksPlugin *m_instance;
......@@ -87,6 +88,7 @@ private:
QAction *m_nextAction;
QAction *m_docPrevAction;
QAction *m_docNextAction;
QAction *m_editNoteAction;
QAction *m_bookmarkMarginAction;
int m_bookmarkMarginActionLineNumber;
......
......@@ -4385,6 +4385,12 @@ void BaseTextEditorWidget::extraAreaMouseEvent(QMouseEvent *e)
// Set whether the mouse cursor is a hand or normal arrow
if (e->type() == QEvent::MouseMove) {
bool hand = (e->pos().x() <= markWidth);
if (hand) {
//Find line by cursor position
int line = cursor.blockNumber() + 1;
emit editor()->markTooltipRequested(editor(), mapToGlobal(e->pos()), line);
}
if (hand != (d->m_extraArea->cursor().shape() == Qt::PointingHandCursor))
d->m_extraArea->setCursor(hand ? Qt::PointingHandCursor : Qt::ArrowCursor);
}
......
......@@ -117,6 +117,7 @@ signals:
void markContextMenuRequested(TextEditor::ITextEditor *editor, int line, QMenu *menu);
void tooltipOverrideRequested(TextEditor::ITextEditor *editor, const QPoint &globalPos, int position, bool *handled);
void tooltipRequested(TextEditor::ITextEditor *editor, const QPoint &globalPos, int position);
void markTooltipRequested(TextEditor::ITextEditor *editor, const QPoint &globalPos, int line);
void contextHelpIdRequested(TextEditor::ITextEditor *editor, int position);
};
......
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