summaryrefslogtreecommitdiffstats
path: root/quanta/utility/quantabookmarks.cpp
blob: ba312f8052a775924cf590662174e71d089ad092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* This file is part of the KDE libraries
   Copyright (C) 2002, 2003, 2004 Anders Lund <anders.lund@lund.tdcadsl.dk>
   Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
   Copyright (C) 2005 Andras Mantia <amantia@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "quantabookmarks.h"

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <klocale.h>
#include <tdeaction.h>
#include <kdebug.h>
#include <tdepopupmenu.h>
#include <kstringhandler.h>
#include <tdetexteditor/markinterface.h>
#include <tdetexteditor/editinterface.h>
#include <tdetexteditor/viewcursorinterface.h>

#include <tqregexp.h>
#include <tqmemarray.h>
#include <tqevent.h>

#include "viewmanager.h"
#include "document.h"

/**
   Utility: selection sort
   sort a TQMemArray<uint> in ascending order.
   max it the largest (zerobased) index to sort.
   To sort the entire array: ssort( *array, array.size() -1 );
   This is only efficient if ran only once.
*/
static void ssort( TQMemArray<uint> &a, int max )
{
  uint tmp, j, maxpos;
  for ( uint h = max; h >= 1; h-- )
  {
    maxpos = 0;
    for ( j = 0; j <= h; j++ )
      maxpos = a[j] > a[maxpos] ? j : maxpos;
    tmp = a[maxpos];
    a[maxpos] = a[h];
    a[h] = tmp;
  }
}

// TODO add a insort() or bubble_sort - more efficient for aboutToShow() ?

QuantaBookmarks::QuantaBookmarks(ViewManager *parent,Sorting sort, bool onlyFromActualDocument )
  : TQObject( parent, "bookmarks" )
  , m_sorting(sort)
  , m_onlyFromActualDocument(onlyFromActualDocument)
{
  m_viewManager = parent;
  _tries=0;
  m_bookmarksMenu = 0L;
  m_doc = 0L;
}

QuantaBookmarks::~QuantaBookmarks()
{
}

void QuantaBookmarks::createActions( TDEActionCollection* ac )
{
  m_bookmarksMenu = (new TDEActionMenu(i18n("&Bookmarks"), ac, "bookmarks"))->popupMenu();
  init(ac);
}

void QuantaBookmarks::init(TDEActionCollection* ac)
{
  m_bookmarkToggle = new TDEToggleAction(
      i18n("Set &Bookmark"), "bookmark", CTRL+Key_B,
  this, TQT_SLOT(toggleBookmark()),
  ac, "bookmarks_toggle" );
  m_bookmarkToggle->setWhatsThis(i18n("If a line has no bookmark then add one, otherwise remove it."));
  m_bookmarkToggle->setCheckedState( i18n("Clear &Bookmark") );

  m_bookmarkClear = new TDEAction(
      i18n("Clear &All Bookmarks"), 0,
  this, TQT_SLOT(clearBookmarks()),
  ac, "bookmarks_clear");
  m_bookmarkClear->setWhatsThis(i18n("Remove all bookmarks of the current document."));

  m_goNext = new TDEAction(
      i18n("Next Bookmark"), "next", ALT + Key_PageDown,
  this, TQT_SLOT(goNext()),
  ac, "bookmarks_next");
  m_goNext->setWhatsThis(i18n("Go to the next bookmark."));

  m_goPrevious = new TDEAction(
      i18n("Previous Bookmark"), "previous", ALT + Key_PageUp,
  this, TQT_SLOT(goPrevious()),
  ac, "bookmarks_previous");
  m_goPrevious->setWhatsThis(i18n("Go to the previous bookmark."));

  //connect the aboutToShow() and aboutToHide() signals with
  //the bookmarkMenuAboutToShow() and bookmarkMenuAboutToHide() slots
  connect( m_bookmarksMenu, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(bookmarkMenuAboutToShow()));
  connect( m_bookmarksMenu, TQT_SIGNAL(aboutToHide()), this, TQT_SLOT(bookmarkMenuAboutToHide()) );

  marksChanged ();
}

void QuantaBookmarks::setBookmarksMenu(TQPopupMenu* bookmarksMenu)
    
{
  m_bookmarksMenu = bookmarksMenu;
  init();
}

void QuantaBookmarks::toggleBookmark ()
{
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  if (doc && doc->markIf)
  {
    uint mark = doc->markIf->mark(doc->viewCursorIf->cursorLine());
    if( mark & KTextEditor::MarkInterface::markType01 )
      doc->markIf->removeMark(doc->viewCursorIf->cursorLine(),
          KTextEditor::MarkInterface::markType01 );
    else
      doc->markIf->addMark(doc->viewCursorIf->cursorLine(),
          KTextEditor::MarkInterface::markType01 );
  }
  marksChanged();
}

void QuantaBookmarks::clearBookmarks ()
{
  Document *doc = m_viewManager->activeDocument();
  if (doc && doc->markIf)
  {
    TQPtrList<KTextEditor::Mark> m = doc->markIf->marks();
    for (uint i=0; i < m.count(); i++)
      doc->markIf->removeMark( m.at(i)->line, KTextEditor::MarkInterface::markType01 );
  
    // just to be sure ;)
    marksChanged ();
  }
}

int QuantaBookmarks::insertBookmarks(TQPopupMenu& menu, Document *doc, bool insertNavigationItems )
{
  int insertedItems = 0;
  if (doc->markIf)
  {
    uint line = doc->viewCursorIf->cursorLine();
    const TQRegExp re("&(?!&)");
    int idx( -1 );
    int old_menu_count = menu.count();
    KTextEditor::Mark *next = 0;
    KTextEditor::Mark *prev = 0;
  
    TQPtrList<KTextEditor::Mark> m = doc->markIf->marks();
    TQMemArray<uint> sortArray( m.count() );
    TQPtrListIterator<KTextEditor::Mark> it( m );
  
    if ( it.count() > 0 && insertNavigationItems)
      menu.insertSeparator();
  
    for( int i = 0; *it; ++it)
    {
      if( (*it)->type & KTextEditor::MarkInterface::markType01 )
      {
        TQString bText = KStringHandler::rEmSqueeze
            ( doc->editIf->textLine( (*it)->line ),
                          menu.fontMetrics(), 32 );
        bText.replace(re, "&&"); // kill undesired accellerators!
        bText.replace('\t', ' '); // kill tabs, as they are interpreted as shortcuts
          
        if ( m_sorting == Position )
        {
          sortArray[i] = (*it)->line;
          ssort( sortArray, i );
          idx = sortArray.find( (*it)->line );
          if (insertNavigationItems)
            idx += 3;
          i++;
        }
  
        menu.insertItem(
            TQString("%1 - \"%2\"").arg( (*it)->line+1 ).arg( bText ),
             0, (*it)->line, idx );
        insertedItems++;
  
        if ( (*it)->line < line )
        {
          if ( ! prev || prev->line < (*it)->line )
            prev = (*it);
        }
  
        else if ( (*it)->line > line )
        {
          if ( ! next || next->line > (*it)->line )
            next = (*it);
        }
      }
    }
 
    if (insertNavigationItems) 
    {
      idx = ++old_menu_count;
      if ( next )
      {
        m_goNext->setText( i18n("&Next: %1 - \"%2\"").arg( next->line + 1 )
            .arg( KStringHandler::rsqueeze( doc->editIf->textLine( next->line ), 24 ) ) );
        m_goNext->plug( &menu, idx );
        idx++;
      }
      if ( prev )
      {
        m_goPrevious->setText( i18n("&Previous: %1 - \"%2\"").arg(prev->line + 1 )
            .arg( KStringHandler::rsqueeze( doc->editIf->textLine( prev->line ), 24 ) ) );
        m_goPrevious->plug( &menu, idx );
        idx++;
      }
      if ( next || prev )
        menu.insertSeparator( idx );
    }
    connect(&menu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(gotoLineNumber(int)));
  }
  return insertedItems;
}

void QuantaBookmarks::bookmarkMenuAboutToShow()
{
  TDEConfig *config = kapp->config();
  if (config->hasGroup("Kate View Defaults"))
  {
    config->setGroup("Kate View Defaults");
    m_sorting = config->readNumEntry("Bookmark Menu Sorting", 0) == 0 ? Position : Creation;
  }
  for (uint i = 0; i < m_othersMenuList.count(); i++)
  {
    delete m_othersMenuList[i];
  }
  m_othersMenuList.clear();
  m_others.clear();
  m_bookmarksMenu->clear();
  marksChanged();
  
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  TQValueList<Document*> openedDocuments = m_viewManager->openedDocuments();
  if (doc && doc->markIf)
  {
    TQPtrList<KTextEditor::Mark> m = doc->markIf->marks();
  
    if (!m_onlyFromActualDocument)
    {
      m_bookmarkToggle->setChecked( doc->markIf->mark( doc->viewCursorIf->cursorLine() )
                                    & KTextEditor::MarkInterface::markType01 );
      m_bookmarkToggle->plug( m_bookmarksMenu );
      m_bookmarkClear->plug( m_bookmarksMenu );
    }
  
    insertBookmarks(*m_bookmarksMenu, doc, !m_onlyFromActualDocument);
    if (openedDocuments.count() > 1 && !m_onlyFromActualDocument)
      m_bookmarksMenu->insertSeparator();
  } 
  if (!m_onlyFromActualDocument)
  {
    int i = 0;
    for (TQValueList<Document*>::Iterator it = openedDocuments.begin(); it != openedDocuments.end(); ++it)
    {
      if (*it != doc)
      {
        TQPopupMenu *menu = new TQPopupMenu(m_bookmarksMenu);
        m_bookmarksMenu->insertItem((*it)->url().fileName(), menu);
        if (insertBookmarks(*menu, *it, false) > 0)
        {        
          m_othersMenuList.append(menu);
          m_others.append(*it);
          i++;
        } else
          delete menu;
      }     
    }
  }
}

/*
   Make sure next/prev actions are plugged, and have a clean text
*/
void QuantaBookmarks::bookmarkMenuAboutToHide()
{
  m_bookmarkToggle->plug( m_bookmarksMenu );
  m_bookmarkClear->plug( m_bookmarksMenu );
  m_goNext->setText( i18n("Next Bookmark") );
  m_goNext->plug( m_bookmarksMenu );
  m_goPrevious->setText( i18n("Previous Bookmark") );
  m_goPrevious->plug( m_bookmarksMenu );
}

void QuantaBookmarks::goNext()
{
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  if (doc && doc->markIf)
  {
    TQPtrList<KTextEditor::Mark> m = doc->markIf->marks();
    if (m.isEmpty())
      return;
  
    uint line = doc->viewCursorIf->cursorLine();
    int found = -1;
  
    for (uint z=0; z < m.count(); z++)
      if ( (m.at(z)->line > line) && ((found == -1) || (uint(found) > m.at(z)->line)) )
        found = m.at(z)->line;
  
    if (found != -1)
      doc->viewCursorIf->setCursorPositionReal(found, 0);
  }
}

void QuantaBookmarks::goPrevious()
{
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  if (doc && doc->markIf)
  {
    TQPtrList<KTextEditor::Mark> m = doc->markIf->marks();
    if (m.isEmpty())
      return;
  
    uint line = doc->viewCursorIf->cursorLine();
    int found = -1;
  
    for (uint z=0; z < m.count(); z++)
      if ((m.at(z)->line < line) && ((found == -1) || (uint(found) < m.at(z)->line)))
        found = m.at(z)->line;
  
    if (found != -1)
      doc->viewCursorIf->setCursorPositionReal(found, 0);
  }
}

void QuantaBookmarks::gotoLineNumber(int line)
{
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  TQObject *s = TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()));
  for (uint i = 0; i < m_othersMenuList.count(); i++)
  {
    if (TQT_BASE_OBJECT(s) == TQT_BASE_OBJECT(m_othersMenuList[i]))
    {
      doc = m_others[i];
      break;
    }
  }
  if (doc)
  {
    if (doc->isUntitled())
    {
       emit gotoFileAndLine("file:" + doc->url().path(), line, 0);
    } else
    {
      emit gotoFileAndLine(doc->url().url(), line, 0);
    }
  }     
}


void QuantaBookmarks::marksChanged ()
{
  Document *doc = m_doc;
  if (!doc)
    doc = m_viewManager->activeDocument();
  if (doc && doc->markIf)
  {
    m_bookmarkClear->setEnabled( !doc->markIf->marks().isEmpty() );
  }
}

#include "quantabookmarks.moc"

// kate: space-indent on; indent-width 2; replace-tabs on;