kate Library API Documentation

katebookmarks.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002, 2003 Anders Lund <anders.lund@lund.tdcadsl.dk>
00003    Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "katebookmarks.h"
00021 #include "katebookmarks.moc"
00022 
00023 #include "katedocument.h"
00024 #include "kateview.h"
00025 
00026 #include <klocale.h>
00027 #include <kaction.h>
00028 #include <kpopupmenu.h>
00029 #include <kstringhandler.h>
00030 #include <kdebug.h>
00031 
00032 #include <qregexp.h>
00033 #include <qmemarray.h>
00034 
00042 static void ssort( QMemArray<uint> &a, int max )
00043 {
00044   uint tmp, j, maxpos;
00045   for ( uint h = max; h >= 1; h-- )
00046   {
00047     maxpos = 0;
00048     for ( j = 0; j <= h; j++ )
00049       maxpos = a[j] > a[maxpos] ? j : maxpos;
00050     tmp = a[maxpos];
00051     a[maxpos] = a[h];
00052     a[h] = tmp;
00053   }
00054 }
00055 
00056 // TODO add a insort() or bubble_sort - more efficient for aboutToShow() ?
00057 
00058 KateBookmarks::KateBookmarks( KateView* view, Sorting sort )
00059   : QObject( view, "kate bookmarks" )
00060   , m_view( view )
00061   , m_sorting( sort )
00062 {
00063   connect (view->getDoc(), SIGNAL(marksChanged()), this, SLOT(marksChanged()));
00064 }
00065 
00066 KateBookmarks::~KateBookmarks()
00067 {
00068 }
00069 
00070 void KateBookmarks::createActions( KActionCollection* ac )
00071 {
00072   m_bookmarkMenu = new KActionMenu(
00073     i18n("&Bookmarks"), ac, "bookmarks" );
00074   m_bookmarkMenu->setWhatsThis(i18n("Bookmark manipulation"));
00075 
00076   // setup bookmark menu
00077   m_bookmarkToggle = new KAction(
00078     i18n("Toggle &Bookmark"), "bookmark", CTRL+Key_B,
00079     this, SLOT(toggleBookmark()),
00080     ac, "bookmarks_toggle" );
00081   m_bookmarkToggle->setWhatsThis(i18n("If a line has no bookmark then add one, otherwise remove it."));
00082 
00083   m_bookmarkClear = new KAction(
00084     i18n("Clear Bookmarks"), 0,
00085     this, SLOT(clearBookmarks()),
00086     ac, "bookmarks_clear");
00087   m_bookmarkClear->setWhatsThis(i18n("Remove all bookmarks of the current document."));
00088 
00089   m_goNext = new KAction(
00090     i18n("Next Bookmark"), "next", ALT + Key_PageDown,
00091     this, SLOT(goNext()),
00092     ac, "bookmarks_next");
00093   m_goNext->setWhatsThis(i18n("Go to the next bookmark."));
00094 
00095   m_goPrevious = new KAction(
00096     i18n("Previous Bookmark"), "previous", ALT + Key_PageUp,
00097     this, SLOT(goPrevious()),
00098     ac, "bookmarks_previous");
00099   m_goPrevious->setWhatsThis(i18n("Go to the previous bookmark."));
00100 
00101   QPopupMenu *m = (QPopupMenu*)m_bookmarkMenu->popupMenu ();
00102 
00103   // connect bookmarks menu aboutToshow
00104   connect( m, SIGNAL(aboutToShow()),
00105            this, SLOT(bookmarkMenuAboutToShow()));
00106 
00107   // anders: I ensure the next/prev actions are available
00108   // and reset their texts (for edit shortcuts dialog, call me picky!).
00109   // TODO - come up with a better solution, please anyone?
00110   connect( m, SIGNAL(aboutToHide()),
00111            this, SLOT(bookmarkMenuAboutToHide()) );
00112 
00113   bookmarkMenuAboutToHide();
00114   marksChanged ();
00115 }
00116 
00117 void KateBookmarks::toggleBookmark ()
00118 {
00119   uint mark = m_view->getDoc()->mark( m_view->cursorLine() );
00120   if( mark & KTextEditor::MarkInterface::markType01 )
00121     m_view->getDoc()->removeMark( m_view->cursorLine(),
00122         KTextEditor::MarkInterface::markType01 );
00123   else
00124     m_view->getDoc()->addMark( m_view->cursorLine(),
00125         KTextEditor::MarkInterface::markType01 );
00126 }
00127 
00128 void KateBookmarks::clearBookmarks ()
00129 {
00130   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00131   for (uint i=0; i < m.count(); i++)
00132     m_view->getDoc()->removeMark( m.at(i)->line, KTextEditor::MarkInterface::markType01 );
00133 
00134   // just to be sure ;)
00135   marksChanged ();
00136 }
00137 
00138 void KateBookmarks::bookmarkMenuAboutToShow()
00139 {
00140   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00141 
00142   m_bookmarkMenu->popupMenu()->clear();
00143   m_bookmarkMenu->insert ( m_bookmarkToggle );
00144   m_bookmarkMenu->insert ( m_bookmarkClear );
00145 
00146   KTextEditor::Mark *next = 0;
00147   KTextEditor::Mark *prev = 0;
00148   uint line = m_view->cursorLine();
00149 
00150   const QRegExp re("&(?!&)");
00151 
00152   int idx( -1 );
00153   QMemArray<uint> sortArray( m.count() );
00154   QPtrListIterator<KTextEditor::Mark> it( m );
00155   kdDebug()<<"Redoing bookmarks menu, "<<it.count()<<" bookmarks in file"<<endl;
00156   if ( it.count() > 0 )
00157         m_bookmarkMenu->popupMenu()->insertSeparator();
00158   for( int i = 0; *it; ++it, ++i ) {
00159     if( (*it)->type & KTextEditor::MarkInterface::markType01 ) {
00160       QString bText = KStringHandler::rEmSqueeze
00161                       ( m_view->getDoc()->textLine( (*it)->line ),
00162                         m_bookmarkMenu->popupMenu()->fontMetrics(), 32 );
00163       bText.replace(re, "&&"); // kill undesired accellerators!
00164       if ( m_sorting == Position )
00165       {
00166         sortArray[i] = (*it)->line;
00167         ssort( sortArray, i );
00168         idx = sortArray.find( (*it)->line ) + 3;
00169       }
00170         m_bookmarkMenu->popupMenu()->insertItem(
00171         QString("%1 - \"%2\"").arg( (*it)->line+1 ).arg( bText ),
00172         m_view, SLOT (gotoLineNumber(int)), 0, (*it)->line, idx );
00173       if ( (*it)->line < line )
00174       {
00175         if ( ! prev || prev->line < (*it)->line )
00176           prev = (*it);
00177       }
00178       else if ( (*it)->line > line )
00179       {
00180         if ( ! next || next->line > (*it)->line )
00181           next = (*it);
00182       }
00183     }
00184   }
00185 
00186   idx = 3;
00187   if ( next )
00188   {
00189     m_goNext->setText( i18n("&Next: %1 - \"%2\"").arg( next->line + 1 )
00190         .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( next->line ), 24 ) ) );
00191     m_bookmarkMenu->insert ( m_goNext, idx );
00192     idx++;
00193   }
00194   if ( prev )
00195   {
00196     m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"").arg(prev->line + 1 )
00197         .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( prev->line ), 24 ) ) );
00198     m_bookmarkMenu->insert ( m_goPrevious, idx );
00199     idx++;
00200   }
00201   if ( next || prev )
00202     m_bookmarkMenu->popupMenu()->insertSeparator( idx );
00203 }
00204 
00205 /*
00206    Make sure next/prev actions are plugged, and have a clean text
00207 */
00208 void KateBookmarks::bookmarkMenuAboutToHide()
00209 {
00210   //kdDebug()<<"KateBookmarks::bookmarkMenuAboutToHide()"<<endl;
00211   m_bookmarkMenu->insert ( m_bookmarkToggle );
00212   m_bookmarkMenu->insert ( m_bookmarkClear );
00213   m_goNext->setText( i18n("Next Bookmark") );
00214   m_bookmarkMenu->insert ( m_goNext );
00215   m_goPrevious->setText( i18n("Previous Bookmark") );
00216   m_bookmarkMenu->insert (m_goPrevious );
00217 }
00218 
00219 void KateBookmarks::goNext()
00220 {
00221   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00222   if (m.isEmpty())
00223     return;
00224 
00225   uint line = m_view->cursorLine();
00226   int found = -1;
00227 
00228   for (uint z=0; z < m.count(); z++)
00229   {
00230     if ((m.at(z)->line > line) && ((found == -1) || (uint(found) > m.at(z)->line)))
00231       found = m.at(z)->line;
00232   }
00233 
00234   if (found != -1)
00235     m_view->gotoLineNumber ( found );
00236 }
00237 
00238 void KateBookmarks::goPrevious()
00239 {
00240   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00241   if (m.isEmpty())
00242     return;
00243 
00244   uint line = m_view->cursorLine();
00245   int found = -1;
00246 
00247   for (uint z=0; z < m.count(); z++)
00248   {
00249     if ((m.at(z)->line < line) && ((found == -1) || (uint(found) < m.at(z)->line)))
00250       found = m.at(z)->line;
00251   }
00252 
00253   if (found != -1)
00254     m_view->gotoLineNumber ( found );
00255 }
00256 
00257 void KateBookmarks::marksChanged ()
00258 {
00259   m_bookmarkClear->setEnabled( !m_view->getDoc()->marks().isEmpty() );
00260 }
00261 
00262 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:57 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003