• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • sources
  • kde-4.12
  • applications
  • kate
  • part
  • utils
katebookmarks.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2002, 2003, 2004 Anders Lund <anders.lund@lund.tdcadsl.dk>
3  Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "katebookmarks.h"
21 #include "katebookmarks.moc"
22 
23 #include "katedocument.h"
24 #include "kateview.h"
25 
26 #include <klocale.h>
27 #include <kaction.h>
28 #include <kactioncollection.h>
29 #include <kguiitem.h>
30 #include <kicon.h>
31 #include <kmenu.h>
32 #include <kstringhandler.h>
33 #include <kxmlguiclient.h>
34 #include <kxmlguifactory.h>
35 #include <ktoggleaction.h>
36 #include <kactionmenu.h>
37 
38 #include <QtCore/QRegExp>
39 #include <QtCore/QEvent>
40 #include <QtCore/QVector>
41 
42 namespace KTextEditor{ class Document; }
43 
51 static void ssort( QVector<uint> &a, int max )
52 {
53  uint tmp, j, maxpos;
54  for ( uint h = max; h >= 1; h-- )
55  {
56  maxpos = 0;
57  for ( j = 0; j <= h; j++ )
58  maxpos = a[j] > a[maxpos] ? j : maxpos;
59  tmp = a[maxpos];
60  a[maxpos] = a[h];
61  a[h] = tmp;
62  }
63 }
64 
65 // TODO add a insort() or bubble_sort - more efficient for aboutToShow() ?
66 
67 KateBookmarks::KateBookmarks( KateView* view, Sorting sort )
68  : QObject( view )
69  , m_view( view )
70  , m_bookmarkClear (0)
71  , m_sorting( sort )
72 {
73  setObjectName( "kate bookmarks" );
74  connect (view->doc(), SIGNAL(marksChanged(KTextEditor::Document*)), this, SLOT(marksChanged()));
75  _tries=0;
76  m_bookmarksMenu = 0L;
77 }
78 
79 KateBookmarks::~KateBookmarks()
80 {
81 }
82 
83 void KateBookmarks::createActions( KActionCollection* ac )
84 {
85  m_bookmarkToggle = new KToggleAction( i18n("Set &Bookmark"), this );
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()) );
91 
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()) );
96 
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()) );
103 
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()) );
110 
111  KActionMenu *actionMenu = new KActionMenu(i18n("&Bookmarks"), this);
112  ac->addAction("bookmarks", actionMenu);
113  m_bookmarksMenu = actionMenu->menu();
114 
115  connect( m_bookmarksMenu, SIGNAL(aboutToShow()), this, SLOT(bookmarkMenuAboutToShow()));
116 
117  marksChanged ();
118 
119  // Always want the actions with shortcuts plugged into something so their shortcuts can work
120  m_view->addAction(m_bookmarkToggle);
121  m_view->addAction(m_bookmarkClear);
122  m_view->addAction(m_goNext);
123  m_view->addAction(m_goPrevious);
124 }
125 
126 void KateBookmarks::toggleBookmark ()
127 {
128  uint mark = m_view->doc()->mark( m_view->cursorPosition().line() );
129  if( mark & KTextEditor::MarkInterface::markType01 )
130  m_view->doc()->removeMark( m_view->cursorPosition().line(),
131  KTextEditor::MarkInterface::markType01 );
132  else
133  m_view->doc()->addMark( m_view->cursorPosition().line(),
134  KTextEditor::MarkInterface::markType01 );
135 }
136 
137 void KateBookmarks::clearBookmarks ()
138 {
139  QHash<int, KTextEditor::Mark*> m = m_view->doc()->marks();
140  for (QHash<int, KTextEditor::Mark*>::const_iterator i = m.constBegin(); i != m.constEnd(); ++i)
141  m_view->doc()->removeMark( i.value()->line, KTextEditor::MarkInterface::markType01 );
142 
143  // just to be sure ;)
144  // dominik: the following line can be deleted afaics, as Document::removeMark emits this signal.
145  marksChanged ();
146 }
147 
148 void KateBookmarks::insertBookmarks( QMenu& menu )
149 {
150  int line = m_view->cursorPosition().line();
151  const QRegExp re("&(?!&)");
152  int idx( -1 );
153  KTextEditor::Mark *next = 0;
154  KTextEditor::Mark *prev = 0;
155 
156  const QHash<int, KTextEditor::Mark*> &m = m_view->doc()->marks();
157  QVector<uint> sortArray( m.size() );
158 
159  if ( m.isEmpty() )
160  return;
161 
162  int i = 0;
163  QAction* firstNewAction = menu.addSeparator();
164  for (QHash<int, KTextEditor::Mark*>::const_iterator it = m.constBegin(); it != m.constEnd(); ++it, ++i)
165  {
166  if( it.value()->type & KTextEditor::MarkInterface::markType01 )
167  {
168  QString bText = menu.fontMetrics().elidedText
169  ( m_view->doc()->line( it.value()->line ),
170  Qt::ElideRight,
171  menu.fontMetrics().maxWidth() * 32 );
172  bText.replace(re, "&&"); // kill undesired accellerators!
173  bText.replace('\t', ' '); // kill tabs, as they are interpreted as shortcuts
174 
175  QAction *before=0;
176  if ( m_sorting == Position )
177  {
178  sortArray[i] = it.value()->line;
179  ssort( sortArray, i );
180 
181  for (int i=0; i < sortArray.size(); ++i)
182  {
183  if ((int)sortArray[i] == it.value()->line)
184  {
185  idx = i + 3;
186  if (idx>=menu.actions().size()) before=0;
187  else before=menu.actions()[idx];
188  break;
189  }
190  }
191  }
192 
193  if (before) {
194  QAction *a=new QAction(QString("%1 %3 - \"%2\"")
195  .arg( it.value()->line+1 ).arg( bText )
196  .arg(m_view->getViInputModeManager()->getMarksOnTheLine(it.value()->line)),&menu);
197  menu.insertAction(before,a);
198  connect(a,SIGNAL(activated()),this,SLOT(gotoLine()));
199  a->setData(it.value()->line);
200  if (!firstNewAction) firstNewAction = a;
201 
202  } else {
203  QAction* a = menu.addAction(QString("%1 %3 - \"%2\"")
204  .arg( it.value()->line+1 ).arg( bText )
205  .arg(m_view->getViInputModeManager()->getMarksOnTheLine(it.value()->line)),
206  this, SLOT(gotoLine()));
207  a->setData(it.value()->line);
208  }
209 
210  if ( it.value()->line < line )
211  {
212  if ( ! prev || prev->line < it.value()->line )
213  prev = (*it);
214  }
215 
216  else if ( it.value()->line > line )
217  {
218  if ( ! next || next->line > it.value()->line )
219  next = it.value();
220  }
221  }
222  }
223 
224  if ( next )
225  {
226  m_goNext->setText( i18n("&Next: %1 - \"%2\"", next->line + 1 ,
227  KStringHandler::rsqueeze( m_view->doc()->line( next->line ), 24 ) ) );
228  menu.insertAction(firstNewAction, m_goNext);
229  firstNewAction = m_goNext;
230  }
231  if ( prev )
232  {
233  m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"", prev->line + 1 ,
234  KStringHandler::rsqueeze( m_view->doc()->line( prev->line ), 24 ) ) );
235  menu.insertAction(firstNewAction, m_goPrevious);
236  firstNewAction = m_goPrevious;
237  }
238 
239  if ( next || prev )
240  menu.insertSeparator(firstNewAction);
241 }
242 
243 void KateBookmarks::gotoLine()
244 {
245  if (!sender()) return;
246  gotoLine(((QAction*)(sender()))->data().toInt());
247 }
248 
249 void KateBookmarks::gotoLine (int line)
250 {
251  m_view->setCursorPosition(KTextEditor::Cursor(line, 0));
252 }
253 
254 void KateBookmarks::bookmarkMenuAboutToShow()
255 {
256  m_bookmarksMenu->clear();
257  m_bookmarkToggle->setChecked( m_view->doc()->mark( m_view->cursorPosition().line() )
258  & KTextEditor::MarkInterface::markType01 );
259  m_bookmarksMenu->addAction(m_bookmarkToggle);
260  m_bookmarksMenu->addAction(m_bookmarkClear);
261 
262  m_goNext->setText( i18n("Next Bookmark") );
263  m_goPrevious->setText( i18n("Previous Bookmark") );
264 
265  insertBookmarks(*m_bookmarksMenu);
266 }
267 
268 void KateBookmarks::goNext()
269 {
270  const QHash<int, KTextEditor::Mark*> &m = m_view->doc()->marks();
271  if (m.isEmpty())
272  return;
273 
274  int line = m_view->cursorPosition().line();
275  int found = -1;
276 
277  for (QHash<int, KTextEditor::Mark*>::const_iterator it = m.constBegin(); it != m.constEnd(); ++it)
278  {
279  if ( (it.value()->line > line) && ((found == -1) || (found > it.value()->line)) )
280  found = it.value()->line;
281  }
282 
283  if (found != -1)
284  gotoLine ( found );
285 }
286 
287 void KateBookmarks::goPrevious()
288 {
289  const QHash<int, KTextEditor::Mark*> &m = m_view->doc()->marks();
290  if (m.isEmpty())
291  return;
292 
293  int line = m_view->cursorPosition().line();
294  int found = -1;
295 
296  for (QHash<int, KTextEditor::Mark*>::const_iterator it = m.constBegin(); it != m.constEnd(); ++it)
297  {
298  if ((it.value()->line < line) && ((found == -1) || (found < it.value()->line)))
299  found = it.value()->line;
300  }
301 
302  if (found != -1)
303  gotoLine ( found );
304 }
305 
306 void KateBookmarks::marksChanged ()
307 {
308  if (m_bookmarkClear)
309  m_bookmarkClear->setEnabled( !m_view->doc()->marks().isEmpty() );
310 }
311 
312 // kate: space-indent on; indent-width 2; replace-tabs on;
KateDocument::line
virtual QString line(int line) const
Definition: katedocument.cpp:447
KateBookmarks::Position
Definition: katebookmarks.h:39
KActionCollection
KTextEditor::MarkInterface::markType01
kateview.h
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
ssort
static void ssort(QVector< uint > &a, int max)
Utility: selection sort sort a QMemArray in ascending order.
Definition: katebookmarks.cpp:51
KateDocument::addMark
virtual void addMark(int line, uint markType)
Definition: katedocument.cpp:1677
KStringHandler::rsqueeze
QString rsqueeze(const QString &str, int maxlen=40)
kactioncollection.h
katedocument.h
kxmlguifactory.h
KActionCollection::addAction
QAction * addAction(const QString &name, QAction *action)
QString
QHash< int, KTextEditor::Mark * >
KTextEditor::Mark::line
int line
KateView::setCursorPosition
bool setCursorPosition(KTextEditor::Cursor position)
Definition: kateview.cpp:2393
QObject
KTextEditor::Cursor
klocale.h
KateView::getViInputModeManager
KateViInputModeManager * getViInputModeManager()
Definition: kateview.cpp:1567
katebookmarks.h
KateDocument::mark
virtual uint mark(int line)
Definition: katedocument.cpp:1646
KateBookmarks::insertBookmarks
void insertBookmarks(QMenu &menu)
Definition: katebookmarks.cpp:148
KateBookmarks::createActions
void createActions(KActionCollection *)
Definition: katebookmarks.cpp:83
kmenu.h
KTextEditor::Document
KTextEditor::Mark
KIcon
kactionmenu.h
KateBookmarks::~KateBookmarks
virtual ~KateBookmarks()
Definition: katebookmarks.cpp:79
KActionMenu::menu
KMenu * menu()
next
KAction * next(const QObject *recvr, const char *slot, QObject *parent)
KateView
Definition: kateview.h:78
KAction::setShortcut
void setShortcut(const KShortcut &shortcut, ShortcutTypes type=ShortcutTypes(ActiveShortcut|DefaultShortcut))
kaction.h
KateBookmarks::Sorting
Sorting
Definition: katebookmarks.h:39
kstringhandler.h
QMenu
KateView::cursorPosition
KTextEditor::Cursor cursorPosition() const
Definition: kateview.cpp:2398
KTextEditor::Cursor::line
virtual int line() const
kxmlguiclient.h
KActionMenu
KateBookmarks::KateBookmarks
KateBookmarks(KateView *parent, Sorting sort=Position)
Definition: katebookmarks.cpp:67
KateDocument::removeMark
virtual void removeMark(int line, uint markType)
Definition: katedocument.cpp:1714
KAction
KateDocument::marks
virtual const QHash< int, KTextEditor::Mark * > & marks()
Definition: katedocument.cpp:1747
ktoggleaction.h
KToggleAction
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:552
kicon.h
kguiitem.h
QAction
KateViInputModeManager::getMarksOnTheLine
QString getMarksOnTheLine(int line)
Definition: kateviinputmodemanager.cpp:852
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Applications
  •   Libraries
  •     libkonq
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal