summaryrefslogtreecommitdiffstats
path: root/lib/kotext/KoTextBookmark.h
blob: eff1a3533b69e99a361f42c08ca806231b69abb0 (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
/* This file is part of the KDE project
   Copyright (C) 2002-2003 Laurent Montel <montel@kde.org>
   Copyright (C) 2006      David Faure <faure@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 as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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.
*/

#ifndef KOTEXTBOOKMARK_H
#define KOTEXTBOOKMARK_H

#include <koffice_export.h>
#include <tqstring.h>
#include <tqvaluelist.h>
#include <tqmap.h>
class KoTextParag;
class KoTextDocument;

/*
 * A bookmark is a name associated with a single position or a run of text (start and end) in a text document.
 */
class KOTEXT_EXPORT KoTextBookmark {
public:
    KoTextBookmark( const TQString& name = TQString() /*for TQValueList; remove default value when going TQt4*/ );
    KoTextBookmark( const TQString& name,
                    KoTextParag* startParag, KoTextParag* endParag,
                    int start, int end );

    TQString bookmarkName() const { return m_name; }
    void setBookmarkName( const TQString &name ) { m_name = name; }

    // Note: the text document always m_startParag->document(), which is also m_endParag->document().
    KoTextDocument* textDocument() const;

    KoTextParag* startParag() const { return m_startParag; }
    void setStartParag( KoTextParag* parag ) { m_startParag = parag; }

    KoTextParag* endParag() const { return m_endParag; }
    void setEndParag( KoTextParag* parag ) { m_endParag = parag; }

    void setBookmarkStartIndex( int pos ) { m_startIndex = pos; }
    int bookmarkStartIndex() const { return m_startIndex; }

    void setBookmarkEndIndex( int end ) { m_endIndex = end; }
    int bookmarkEndIndex() const { return m_endIndex; }

    bool isSimple() const { return m_startParag == m_endParag && m_startIndex == m_endIndex; }

private:
    TQString m_name;
    KoTextParag* m_startParag;
    KoTextParag* m_endParag;
    int m_startIndex;
    int m_endIndex;
};

class KOTEXT_EXPORT KoTextBookmarkList : public TQValueList<KoTextBookmark>
{
public:
    const_iterator findByName( const TQString& name ) const {
        for ( const_iterator it = begin(), itend = end(); it != itend; ++it )
            if ( (*it).bookmarkName() == name )
                return it;
        return end();
    }
    iterator findByName( const TQString& name ) {
        for ( iterator it = begin(), itend = end(); it != itend; ++it )
            if ( (*it).bookmarkName() == name )
                return it;
        return end();
    }
    bool removeByName( const TQString& name ) {
        for ( iterator it = begin(), itend = end(); it != itend; ++it )
            if ( (*it).bookmarkName() == name ) {
                remove( it );
                return true;
            }
        return false;
    }
    /// return a map of bookmarks per paragraph. Note that multi-paragraph bookmarks
    /// will be present twice in the map.
    TQMap<const KoTextParag*, KoTextBookmarkList> bookmarksPerParagraph() const {
        TQMap<const KoTextParag*, KoTextBookmarkList> ret;
        for ( const_iterator it = begin(), itend = end(); it != itend; ++it ) {
            ret[ (*it).startParag() ].append( *it );
            if ( (*it).startParag() != (*it).endParag() )
                ret[ (*it).endParag() ].append( *it );
        }
        return ret;
    }
};

#endif /* KOTEXTBOOKMARK_H */