21 #include "katebookmarks.moc"
38 #include <QtCore/QRegExp>
39 #include <QtCore/QEvent>
40 #include <QtCore/QVector>
42 namespace KTextEditor{
class Document; }
51 static void ssort( QVector<uint> &a,
int max )
54 for ( uint h = max; h >= 1; h-- )
57 for ( j = 0; j <= h; j++ )
58 maxpos = a[j] > a[maxpos] ? j : maxpos;
73 setObjectName(
"kate bookmarks" );
86 ac->
addAction(
"bookmarks_toggle", m_bookmarkToggle );
87 m_bookmarkToggle->setIcon(
KIcon(
"bookmark-new" ) );
88 m_bookmarkToggle->
setShortcut( Qt::CTRL+Qt::Key_B );
89 m_bookmarkToggle->setWhatsThis(
i18n(
"If a line has no bookmark then add one, otherwise remove it."));
90 connect( m_bookmarkToggle, SIGNAL(triggered()),
this, SLOT(toggleBookmark()) );
92 m_bookmarkClear =
new KAction(
i18n(
"Clear &All Bookmarks"),
this );
93 ac->
addAction(
"bookmarks_clear", m_bookmarkClear);
94 m_bookmarkClear->setWhatsThis(
i18n(
"Remove all bookmarks of the current document."));
95 connect( m_bookmarkClear, SIGNAL(triggered()),
this, SLOT(clearBookmarks()) );
97 m_goNext =
new KAction(
i18n(
"Next Bookmark"),
this);
98 ac->
addAction(
"bookmarks_next", m_goNext);
99 m_goNext->setIcon(
KIcon(
"go-down-search" ) );
100 m_goNext->
setShortcut( Qt::ALT + Qt::Key_PageDown );
101 m_goNext->setWhatsThis(
i18n(
"Go to the next bookmark."));
102 connect( m_goNext, SIGNAL(triggered()),
this, SLOT(goNext()) );
104 m_goPrevious =
new KAction(
i18n(
"Previous Bookmark"),
this);
105 ac->
addAction(
"bookmarks_previous", m_goPrevious);
106 m_goPrevious->setIcon(
KIcon(
"go-up-search" ) );
107 m_goPrevious->
setShortcut( Qt::ALT + Qt::Key_PageUp );
108 m_goPrevious->setWhatsThis(
i18n(
"Go to the previous bookmark."));
109 connect( m_goPrevious, SIGNAL(triggered()),
this, SLOT(goPrevious()) );
113 m_bookmarksMenu = actionMenu->
menu();
115 connect( m_bookmarksMenu, SIGNAL(aboutToShow()),
this, SLOT(bookmarkMenuAboutToShow()));
120 m_view->addAction(m_bookmarkToggle);
121 m_view->addAction(m_bookmarkClear);
122 m_view->addAction(m_goNext);
123 m_view->addAction(m_goPrevious);
126 void KateBookmarks::toggleBookmark ()
137 void KateBookmarks::clearBookmarks ()
151 const QRegExp re(
"&(?!&)");
157 QVector<uint> sortArray( m.size() );
163 QAction* firstNewAction = menu.addSeparator();
168 QString bText = menu.fontMetrics().elidedText
169 ( m_view->
doc()->
line( it.value()->line ),
171 menu.fontMetrics().maxWidth() * 32 );
172 bText.replace(re,
"&&");
173 bText.replace(
'\t',
' ');
178 sortArray[i] = it.value()->line;
179 ssort( sortArray, i );
181 for (
int i=0; i < sortArray.size(); ++i)
183 if ((
int)sortArray[i] == it.value()->line)
186 if (idx>=menu.actions().size()) before=0;
187 else before=menu.actions()[idx];
195 .arg( it.value()->line+1 ).arg( bText )
197 menu.insertAction(before,a);
198 connect(a,SIGNAL(activated()),
this,SLOT(gotoLine()));
199 a->setData(it.value()->line);
200 if (!firstNewAction) firstNewAction = a;
204 .arg( it.value()->line+1 ).arg( bText )
206 this, SLOT(gotoLine()));
207 a->setData(it.value()->line);
210 if ( it.value()->line < line )
212 if ( ! prev || prev->
line < it.value()->line )
216 else if ( it.value()->line > line )
218 if ( ! next || next->
line > it.value()->line )
226 m_goNext->setText(
i18n(
"&Next: %1 - \"%2\"", next->
line + 1 ,
228 menu.insertAction(firstNewAction, m_goNext);
229 firstNewAction = m_goNext;
233 m_goPrevious->setText(
i18n(
"&Previous: %1 - \"%2\"", prev->
line + 1 ,
235 menu.insertAction(firstNewAction, m_goPrevious);
236 firstNewAction = m_goPrevious;
240 menu.insertSeparator(firstNewAction);
243 void KateBookmarks::gotoLine()
245 if (!sender())
return;
246 gotoLine(((
QAction*)(sender()))->data().toInt());
249 void KateBookmarks::gotoLine (
int line)
254 void KateBookmarks::bookmarkMenuAboutToShow()
256 m_bookmarksMenu->clear();
258 & KTextEditor::MarkInterface::markType01 );
259 m_bookmarksMenu->addAction(m_bookmarkToggle);
260 m_bookmarksMenu->addAction(m_bookmarkClear);
262 m_goNext->setText(
i18n(
"Next Bookmark") );
263 m_goPrevious->setText(
i18n(
"Previous Bookmark") );
268 void KateBookmarks::goNext()
279 if ( (it.value()->line > line) && ((found == -1) || (found > it.value()->line)) )
280 found = it.value()->line;
287 void KateBookmarks::goPrevious()
298 if ((it.value()->line < line) && ((found == -1) || (found < it.value()->line)))
299 found = it.value()->line;
306 void KateBookmarks::marksChanged ()
309 m_bookmarkClear->setEnabled( !m_view->
doc()->
marks().isEmpty() );
virtual QString line(int line) const
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
static void ssort(QVector< uint > &a, int max)
Utility: selection sort sort a QMemArray in ascending order.
virtual void addMark(int line, uint markType)
QString rsqueeze(const QString &str, int maxlen=40)
QAction * addAction(const QString &name, QAction *action)
bool setCursorPosition(KTextEditor::Cursor position)
KateViInputModeManager * getViInputModeManager()
virtual uint mark(int line)
void insertBookmarks(QMenu &menu)
void createActions(KActionCollection *)
KAction * next(const QObject *recvr, const char *slot, QObject *parent)
void setShortcut(const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
KTextEditor::Cursor cursorPosition() const
KateBookmarks(KateView *parent, Sorting sort=Position)
virtual void removeMark(int line, uint markType)
virtual const QHash< int, KTextEditor::Mark * > & marks()
KateDocument * doc()
accessor to katedocument pointer