Bookmarks: Support manual editing of line number

Task-number: QTCREATORBUG-9721
Change-Id: I914568d58857bf897a3894a1d2a6082126e47b62
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
Orgad Shaneh
2014-07-09 17:18:03 +03:00
committed by Orgad Shaneh
parent 6956eea8bf
commit 622c55cbf8
9 changed files with 72 additions and 27 deletions

View File

@@ -57,6 +57,14 @@ void Bookmark::updateLineNumber(int line)
} }
} }
void Bookmark::move(int line)
{
if (line != lineNumber()) {
BaseTextMark::move(line);
m_manager->updateBookmark(this);
}
}
void Bookmark::updateBlock(const QTextBlock &block) void Bookmark::updateBlock(const QTextBlock &block)
{ {
if (m_lineText != block.text()) { if (m_lineText != block.text()) {

View File

@@ -44,6 +44,7 @@ public:
Bookmark(const QString &fileName, int lineNumber, BookmarkManager *manager); Bookmark(const QString &fileName, int lineNumber, BookmarkManager *manager);
void updateLineNumber(int lineNumber); void updateLineNumber(int lineNumber);
void move(int line);
void updateBlock(const QTextBlock &block); void updateBlock(const QTextBlock &block);
void updateFileName(const QString &fileName); void updateFileName(const QString &fileName);
void setNote(const QString &note); void setNote(const QString &note);

View File

@@ -45,15 +45,17 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/checkablemessagebox.h> #include <utils/checkablemessagebox.h>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QAction> #include <QAction>
#include <QContextMenuEvent> #include <QContextMenuEvent>
#include <QDebug>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDir>
#include <QFileInfo>
#include <QFormLayout>
#include <QLineEdit>
#include <QMenu> #include <QMenu>
#include <QPainter> #include <QPainter>
#include <QInputDialog>
Q_DECLARE_METATYPE(Bookmarks::Internal::Bookmark*) Q_DECLARE_METATYPE(Bookmarks::Internal::Bookmark*)
@@ -243,7 +245,7 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
QMenu menu; QMenu menu;
QAction *moveUp = menu.addAction(tr("Move Up")); QAction *moveUp = menu.addAction(tr("Move Up"));
QAction *moveDown = menu.addAction(tr("Move Down")); QAction *moveDown = menu.addAction(tr("Move Down"));
QAction *editNote = menu.addAction(tr("Edit Note")); QAction *edit = menu.addAction(tr("&Edit"));
menu.addSeparator(); menu.addSeparator();
QAction *remove = menu.addAction(tr("&Remove")); QAction *remove = menu.addAction(tr("&Remove"));
menu.addSeparator(); menu.addSeparator();
@@ -254,7 +256,7 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
moveUp->setEnabled(false); moveUp->setEnabled(false);
moveDown->setEnabled(false); moveDown->setEnabled(false);
remove->setEnabled(false); remove->setEnabled(false);
editNote->setEnabled(false); edit->setEnabled(false);
} }
if (model()->rowCount() == 0) if (model()->rowCount() == 0)
@@ -268,8 +270,8 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
this, SLOT(removeFromContextMenu())); this, SLOT(removeFromContextMenu()));
connect(removeAll, SIGNAL(triggered()), connect(removeAll, SIGNAL(triggered()),
this, SLOT(removeAll())); this, SLOT(removeAll()));
connect(editNote, SIGNAL(triggered()), connect(edit, SIGNAL(triggered()),
m_manager, SLOT(editNote())); m_manager, SLOT(edit()));
menu.exec(mapToGlobal(event->pos())); menu.exec(mapToGlobal(event->pos()));
} }
@@ -673,27 +675,36 @@ void BookmarkManager::moveDown()
saveBookmarks(); saveBookmarks();
} }
void BookmarkManager::editNote(const QString &fileName, int lineNumber) void BookmarkManager::edit(const QString &fileName, int lineNumber)
{ {
Bookmark *b = findBookmark(fileName, lineNumber); Bookmark *b = findBookmark(fileName, lineNumber);
QModelIndex current = selectionModel()->currentIndex(); QModelIndex current = selectionModel()->currentIndex();
selectionModel()->setCurrentIndex(current.sibling(m_bookmarksList.indexOf(b), 0), selectionModel()->setCurrentIndex(current.sibling(m_bookmarksList.indexOf(b), 0),
QItemSelectionModel::Select | QItemSelectionModel::Clear); QItemSelectionModel::Select | QItemSelectionModel::Clear);
editNote(); edit();
} }
void BookmarkManager::editNote() void BookmarkManager::edit()
{ {
QModelIndex current = selectionModel()->currentIndex(); QModelIndex current = selectionModel()->currentIndex();
Bookmark *b = m_bookmarksList.at(current.row()); Bookmark *b = m_bookmarksList.at(current.row());
bool inputOk = false; QDialog dlg;
QString noteText = QInputDialog::getText(0, tr("Edit Note"), dlg.setWindowTitle(tr("Edit Bookmark"));
tr("Note text:"), QLineEdit::Normal, auto layout = new QFormLayout(&dlg);
b->note(), &inputOk); auto noteEdit = new QLineEdit(b->note());
if (inputOk) { auto lineNumberEdit = new QLineEdit(QString::number(b->lineNumber()));
b->updateNote(noteText.replace(QLatin1Char('\t'), QLatin1Char(' '))); auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), &dlg, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), &dlg, SLOT(reject()));
lineNumberEdit->setValidator(new QIntValidator(0, INT_MAX));
layout->addRow(tr("Note text:"), noteEdit);
layout->addRow(tr("Line number:"), lineNumberEdit);
layout->addWidget(buttonBox);
if (dlg.exec() == QDialog::Accepted) {
b->move(lineNumberEdit->text().toInt());
b->updateNote(noteEdit->text().replace(QLatin1Char('\t'), QLatin1Char(' ')));
emit dataChanged(current, current); emit dataChanged(current, current);
saveBookmarks(); saveBookmarks();
} }

View File

@@ -97,8 +97,8 @@ public slots:
void prev(); void prev();
void moveUp(); void moveUp();
void moveDown(); void moveDown();
void editNote(); void edit();
void editNote(const QString &fileName, int lineNumber); void edit(const QString &fileName, int lineNumber);
bool gotoBookmark(Bookmark *bookmark); bool gotoBookmark(Bookmark *bookmark);
signals: signals:

View File

@@ -105,7 +105,7 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
cmd = Core::ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext); cmd = Core::ActionManager::registerAction(m_docNextAction, BOOKMARKS_NEXTDOC_ACTION, globalcontext);
mbm->addAction(cmd); mbm->addAction(cmd);
m_editNoteAction = new QAction(tr("Edit Bookmark Note"), this); m_editBookmarkAction = new QAction(tr("Edit Bookmark"), this);
m_bookmarkManager = new BookmarkManager; m_bookmarkManager = new BookmarkManager;
@@ -114,7 +114,7 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
connect(m_nextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(next())); connect(m_nextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(next()));
connect(m_docPrevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prevInDocument())); connect(m_docPrevAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(prevInDocument()));
connect(m_docNextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(nextInDocument())); connect(m_docNextAction, SIGNAL(triggered()), m_bookmarkManager, SLOT(nextInDocument()));
connect(m_editNoteAction, SIGNAL(triggered()), this, SLOT(bookmarkEditNoteActionTriggered())); connect(m_editBookmarkAction, SIGNAL(triggered()), this, SLOT(editBookmarkActionTriggered()));
connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int))); connect(m_bookmarkManager, SIGNAL(updateActions(int)), this, SLOT(updateActions(int)));
updateActions(m_bookmarkManager->state()); updateActions(m_bookmarkManager->state());
addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager)); addAutoReleasedObject(new BookmarkViewFactory(m_bookmarkManager));
@@ -189,7 +189,7 @@ void BookmarksPlugin::requestContextMenu(TextEditor::ITextEditor *editor,
menu->addAction(m_bookmarkMarginAction); menu->addAction(m_bookmarkMarginAction);
if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber)) if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber))
menu->addAction(m_editNoteAction); menu->addAction(m_editBookmarkAction);
} }
void BookmarksPlugin::bookmarkMarginActionTriggered() void BookmarksPlugin::bookmarkMarginActionTriggered()
@@ -198,9 +198,9 @@ void BookmarksPlugin::bookmarkMarginActionTriggered()
m_bookmarkMarginActionLineNumber); m_bookmarkMarginActionLineNumber);
} }
void BookmarksPlugin::bookmarkEditNoteActionTriggered() void BookmarksPlugin::editBookmarkActionTriggered()
{ {
m_bookmarkManager->editNote(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber); m_bookmarkManager->edit(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber);
} }
Q_EXPORT_PLUGIN(BookmarksPlugin) Q_EXPORT_PLUGIN(BookmarksPlugin)

View File

@@ -70,7 +70,7 @@ private slots:
void requestContextMenu(TextEditor::ITextEditor *editor, void requestContextMenu(TextEditor::ITextEditor *editor,
int lineNumber, QMenu *menu); int lineNumber, QMenu *menu);
void bookmarkMarginActionTriggered(); void bookmarkMarginActionTriggered();
void bookmarkEditNoteActionTriggered(); void editBookmarkActionTriggered();
private: private:
static BookmarksPlugin *m_instance; static BookmarksPlugin *m_instance;
@@ -81,7 +81,7 @@ private:
QAction *m_nextAction; QAction *m_nextAction;
QAction *m_docPrevAction; QAction *m_docPrevAction;
QAction *m_docNextAction; QAction *m_docNextAction;
QAction *m_editNoteAction; QAction *m_editBookmarkAction;
QAction *m_bookmarkMarginAction; QAction *m_bookmarkMarginAction;
int m_bookmarkMarginActionLineNumber; int m_bookmarkMarginActionLineNumber;

View File

@@ -50,6 +50,7 @@ public:
TextMarks marksAt(int line) const; TextMarks marksAt(int line) const;
void removeMark(ITextMark *mark); void removeMark(ITextMark *mark);
void updateMark(ITextMark *mark); void updateMark(ITextMark *mark);
void moveMark(ITextMark *mark, int previousLine);
void removeMarkFromMarksCache(TextEditor::ITextMark *mark); void removeMarkFromMarksCache(TextEditor::ITextMark *mark);
private: private:
@@ -177,6 +178,18 @@ void DocumentMarker::updateMark(ITextMark *mark)
documentLayout->requestUpdate(); documentLayout->requestUpdate();
} }
void DocumentMarker::moveMark(ITextMark *mark, int previousLine)
{
QTextBlock block = document->findBlockByNumber(previousLine - 1);
if (TextBlockUserData *data = BaseTextDocumentLayout::testUserData(block)) {
if (!data->removeMark(mark))
qDebug() << "Could not find mark" << mark << "on line" << previousLine;
}
removeMarkFromMarksCache(mark);
mark->setMarkableInterface(0);
addMark(mark);
}
} // namespace Internal } // namespace Internal
} // namespace TextEditor } // namespace TextEditor

View File

@@ -53,6 +53,16 @@ void ITextMark::updateLineNumber(int lineNumber)
m_lineNumber = lineNumber; m_lineNumber = lineNumber;
} }
void ITextMark::move(int line)
{
if (line == m_lineNumber)
return;
const int previousLine = m_lineNumber;
m_lineNumber = line;
if (m_markableInterface)
m_markableInterface->moveMark(this, previousLine);
}
void ITextMark::updateBlock(const QTextBlock &) void ITextMark::updateBlock(const QTextBlock &)
{} {}

View File

@@ -72,6 +72,7 @@ public:
virtual void paint(QPainter *painter, const QRect &rect) const; virtual void paint(QPainter *painter, const QRect &rect) const;
virtual void updateLineNumber(int lineNumber); virtual void updateLineNumber(int lineNumber);
virtual void updateBlock(const QTextBlock &block); virtual void updateBlock(const QTextBlock &block);
virtual void move(int line);
virtual void removedFromEditor(); virtual void removedFromEditor();
virtual bool isClickable() const; virtual bool isClickable() const;
virtual void clicked(); virtual void clicked();
@@ -114,6 +115,7 @@ public:
virtual TextMarks marksAt(int line) const = 0; virtual TextMarks marksAt(int line) const = 0;
virtual void removeMark(ITextMark *mark) = 0; virtual void removeMark(ITextMark *mark) = 0;
virtual void updateMark(ITextMark *mark) = 0; virtual void updateMark(ITextMark *mark) = 0;
virtual void moveMark(ITextMark *mark, int previousLine) = 0;
}; };
} // namespace TextEditor } // namespace TextEditor