kate Library API Documentation

katebookmarks.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002, 2003, 2004 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 <kxmlguiclient.h>
00031 #include <kxmlguifactory.h>
00032 
00033 #include <qregexp.h>
00034 #include <qmemarray.h>
00035 #include <qevent.h>
00036 
00044 static void ssort( QMemArray<uint> &a, int max )
00045 {
00046   uint tmp, j, maxpos;
00047   for ( uint h = max; h >= 1; h-- )
00048   {
00049     maxpos = 0;
00050     for ( j = 0; j <= h; j++ )
00051       maxpos = a[j] > a[maxpos] ? j : maxpos;
00052     tmp = a[maxpos];
00053     a[maxpos] = a[h];
00054     a[h] = tmp;
00055   }
00056 }
00057 
00058 // TODO add a insort() or bubble_sort - more efficient for aboutToShow() ?
00059 
00060 KateBookmarks::KateBookmarks( KateView* view, Sorting sort )
00061   : QObject( view, "kate bookmarks" )
00062   , m_view( view )
00063   , m_sorting( sort )
00064 {
00065   connect (view->getDoc(), SIGNAL(marksChanged()), this, SLOT(marksChanged()));
00066   _tries=0;
00067   m_bookmarksMenu = 0L;
00068 }
00069 
00070 KateBookmarks::~KateBookmarks()
00071 {
00072 }
00073 
00074 void KateBookmarks::createActions( KActionCollection* ac )
00075 {
00076   m_bookmarkToggle = new KToggleAction(
00077     i18n("Set &Bookmark"), "bookmark", CTRL+Key_B,
00078     this, SLOT(toggleBookmark()),
00079     ac, "bookmarks_toggle" );
00080   m_bookmarkToggle->setWhatsThis(i18n("If a line has no bookmark then add one, otherwise remove it."));
00081   m_bookmarkToggle->setCheckedState( i18n("Clear &Bookmark") );
00082 
00083   m_bookmarkClear = new KAction(
00084     i18n("Clear &All 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   m_bookmarksMenu = (new KActionMenu(i18n("&Bookmarks"), ac, "bookmarks"))->popupMenu();
00102 
00103   //connect the aboutToShow() and aboutToHide() signals with
00104   //the bookmarkMenuAboutToShow() and bookmarkMenuAboutToHide() slots
00105   connect( m_bookmarksMenu, SIGNAL(aboutToShow()), this, SLOT(bookmarkMenuAboutToShow()));
00106   connect( m_bookmarksMenu, SIGNAL(aboutToHide()), this, SLOT(bookmarkMenuAboutToHide()) );
00107 
00108   marksChanged ();
00109   bookmarkMenuAboutToHide();
00110 
00111   connect( m_view, SIGNAL( gotFocus( Kate::View * ) ), this, SLOT( slotViewGotFocus( Kate::View * ) ) );
00112   connect( m_view, SIGNAL( lostFocus( Kate::View * ) ), this, SLOT( slotViewLostFocus( Kate::View * ) ) );
00113 }
00114 
00115 void KateBookmarks::toggleBookmark ()
00116 {
00117   uint mark = m_view->getDoc()->mark( m_view->cursorLine() );
00118   if( mark & KTextEditor::MarkInterface::markType01 )
00119     m_view->getDoc()->removeMark( m_view->cursorLine(),
00120         KTextEditor::MarkInterface::markType01 );
00121   else
00122     m_view->getDoc()->addMark( m_view->cursorLine(),
00123         KTextEditor::MarkInterface::markType01 );
00124 }
00125 
00126 void KateBookmarks::clearBookmarks ()
00127 {
00128 
00129   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00130   for (uint i=0; i < m.count(); i++)
00131     m_view->getDoc()->removeMark( m.at(i)->line, KTextEditor::MarkInterface::markType01 );
00132 
00133   // just to be sure ;)
00134   marksChanged ();
00135 }
00136 
00137 void KateBookmarks::slotViewGotFocus( Kate::View *v )
00138 {
00139   if ( v == (Kate::View*)m_view )
00140     bookmarkMenuAboutToHide();
00141 }
00142 
00143 void KateBookmarks::slotViewLostFocus( Kate::View *v )
00144 {
00145   if ( v == (Kate::View*)m_view )
00146     m_bookmarksMenu->clear();
00147 }
00148 
00149 void KateBookmarks::insertBookmarks( QPopupMenu& menu )
00150 {
00151   uint line = m_view->cursorLine();
00152   const QRegExp re("&(?!&)");
00153   int idx( -1 );
00154   int old_menu_count = menu.count();
00155   KTextEditor::Mark *next = 0;
00156   KTextEditor::Mark *prev = 0;
00157 
00158   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00159   QMemArray<uint> sortArray( m.count() );
00160   QPtrListIterator<KTextEditor::Mark> it( m );
00161 
00162   if ( it.count() > 0 )
00163     menu.insertSeparator();
00164 
00165   for( int i = 0; *it; ++it, ++i )
00166   {
00167     if( (*it)->type & KTextEditor::MarkInterface::markType01 )
00168     {
00169       QString bText = KStringHandler::rEmSqueeze
00170                       ( m_view->getDoc()->textLine( (*it)->line ),
00171                         menu.fontMetrics(), 32 );
00172       bText.replace(re, "&&"); // kill undesired accellerators!
00173 
00174       if ( m_sorting == Position )
00175       {
00176         sortArray[i] = (*it)->line;
00177         ssort( sortArray, i );
00178         idx = sortArray.find( (*it)->line ) + 3;
00179       }
00180 
00181       menu.insertItem(
00182           QString("%1 - \"%2\"").arg( (*it)->line+1 ).arg( bText ),
00183           m_view, SLOT(gotoLineNumber(int)), 0, (*it)->line, idx );
00184 
00185       if ( (*it)->line < line )
00186       {
00187         if ( ! prev || prev->line < (*it)->line )
00188           prev = (*it);
00189       }
00190 
00191       else if ( (*it)->line > line )
00192       {
00193         if ( ! next || next->line > (*it)->line )
00194           next = (*it);
00195       }
00196     }
00197   }
00198 
00199   idx = ++old_menu_count;
00200   if ( next )
00201   {
00202     m_goNext->setText( i18n("&Next: %1 - \"%2\"").arg( next->line + 1 )
00203         .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( next->line ), 24 ) ) );
00204     m_goNext->plug( &menu, idx );
00205     idx++;
00206   }
00207   if ( prev )
00208   {
00209     m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"").arg(prev->line + 1 )
00210         .arg( KStringHandler::rsqueeze( m_view->getDoc()->textLine( prev->line ), 24 ) ) );
00211     m_goPrevious->plug( &menu, idx );
00212     idx++;
00213   }
00214   if ( next || prev )
00215     menu.insertSeparator( idx );
00216 
00217 }
00218 
00219 void KateBookmarks::bookmarkMenuAboutToShow()
00220 {
00221 
00222   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00223 
00224   m_bookmarksMenu->clear();
00225   m_bookmarkToggle->setChecked( m_view->getDoc()->mark( m_view->cursorLine() )
00226                                 & KTextEditor::MarkInterface::markType01 );
00227   m_bookmarkToggle->plug( m_bookmarksMenu );
00228   m_bookmarkClear->plug( m_bookmarksMenu );
00229 
00230 
00231   insertBookmarks(*m_bookmarksMenu);
00232 }
00233 
00234 /*
00235    Make sure next/prev actions are plugged, and have a clean text
00236 */
00237 void KateBookmarks::bookmarkMenuAboutToHide()
00238 {
00239   m_bookmarkToggle->plug( m_bookmarksMenu );
00240   m_bookmarkClear->plug( m_bookmarksMenu );
00241   m_goNext->setText( i18n("Next Bookmark") );
00242   m_goNext->plug( m_bookmarksMenu );
00243   m_goPrevious->setText( i18n("Previous Bookmark") );
00244   m_goPrevious->plug( m_bookmarksMenu );
00245 }
00246 
00247 void KateBookmarks::goNext()
00248 {
00249   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00250   if (m.isEmpty())
00251     return;
00252 
00253   uint line = m_view->cursorLine();
00254   int found = -1;
00255 
00256   for (uint z=0; z < m.count(); z++)
00257     if ( (m.at(z)->line > line) && ((found == -1) || (uint(found) > m.at(z)->line)) )
00258       found = m.at(z)->line;
00259 
00260   if (found != -1)
00261     m_view->gotoLineNumber ( found );
00262 }
00263 
00264 void KateBookmarks::goPrevious()
00265 {
00266   QPtrList<KTextEditor::Mark> m = m_view->getDoc()->marks();
00267   if (m.isEmpty())
00268     return;
00269 
00270   uint line = m_view->cursorLine();
00271   int found = -1;
00272 
00273   for (uint z=0; z < m.count(); z++)
00274     if ((m.at(z)->line < line) && ((found == -1) || (uint(found) < m.at(z)->line)))
00275       found = m.at(z)->line;
00276 
00277   if (found != -1)
00278     m_view->gotoLineNumber ( found );
00279 }
00280 
00281 void KateBookmarks::marksChanged ()
00282 {
00283   m_bookmarkClear->setEnabled( !m_view->getDoc()->marks().isEmpty() );
00284 }
00285 
00286 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Nov 27 13:52:31 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003