~adamreichold/qpdfview/trunk

1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
1
/*
2
3
Copyright 2014 S. Razi Alavizadeh
4
Copyright 2014 Adam Reichold
5
6
This file is part of qpdfview.
7
8
qpdfview is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 2 of the License, or
11
(at your option) any later version.
12
13
qpdfview is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
20
21
*/
22
23
#ifndef SEARCHMODEL_H
24
#define SEARCHMODEL_H
25
26
#include <QAbstractItemModel>
1734.1.2 by Razi Alavizadeh
Cache fetched surrounding text of results.
27
#include <QCache>
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
28
#include <QFutureWatcher>
1742.1.1 by Adam Reichold
Add prototype implementation of asynchronous fetching of surrounding text.
29
#include <QRectF>
1734.1.2 by Razi Alavizadeh
Cache fetched surrounding text of results.
30
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
31
namespace qpdfview
32
{
33
34
class DocumentView;
35
36
class SearchModel : public QAbstractItemModel
37
{
38
    Q_OBJECT
39
40
public:
1719 by Adam Reichold
Merge central search model.
41
    static SearchModel* instance();
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
42
    ~SearchModel();
43
44
45
    QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const;
1719 by Adam Reichold
Merge central search model.
46
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
47
    QModelIndex parent(const QModelIndex& child) const;
48
49
    int rowCount(const QModelIndex& parent = QModelIndex()) const;
50
    int columnCount(const QModelIndex& parent = QModelIndex()) const;
51
1719 by Adam Reichold
Merge central search model.
52
    enum
53
    {
54
        CountRole = Qt::UserRole + 1,
1781 by Adam Reichold
Fix a compilation warning and expose search progress via item model data role.
55
        ProgressRole,
1719 by Adam Reichold
Merge central search model.
56
        PageRole,
57
        RectRole,
1740 by Adam Reichold
Merge displaying surrounding text in extended search model.
58
        TextRole,
59
        MatchCaseRole,
1824 by Adam Reichold
Add a partial implementation of a whole-words search option currently respected only by the DjVu backend.
60
        WholeWordsRole,
1839 by Adam Reichold
Try to decouple highlighted matched text from the matching itself done by the backends.
61
        MatchedTextRole,
1734.1.4 by Razi Alavizadeh
Implemented SearchDelegate and installed it on search tree view.
62
        SurroundingTextRole
1719 by Adam Reichold
Merge central search model.
63
    };
64
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
65
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
66
1719 by Adam Reichold
Merge central search model.
67
1726 by Adam Reichold
Add search model method to map index to view pointer.
68
    DocumentView* viewForIndex(const QModelIndex& index) const;
69
70
1719 by Adam Reichold
Merge central search model.
71
    bool hasResults(DocumentView* view) const;
72
    bool hasResultsOnPage(DocumentView* view, int page) const;
1740 by Adam Reichold
Merge displaying surrounding text in extended search model.
73
    int numberOfResultsOnPage(DocumentView* view, int page) const;
1719 by Adam Reichold
Merge central search model.
74
    QList< QRectF > resultsOnPage(DocumentView* view, int page) const;
75
76
    enum FindDirection
77
    {
78
        FindNext,
79
        FindPrevious
80
    };
81
82
    QPersistentModelIndex findResult(DocumentView* view, const QPersistentModelIndex& currentResult, int currentPage, FindDirection direction) const;
83
84
    void insertResults(DocumentView* view, int page, const QList< QRectF >& resultsOnPage);
85
    void clearResults(DocumentView* view);
1717.1.5 by Razi Alavizadeh
Track the current result of views and implement "find". (now using central search model)
86
1782 by Adam Reichold
Paint search progress in the document items of the extended search dock view.
87
    void updateProgress(DocumentView* view);
88
1742.1.1 by Adam Reichold
Add prototype implementation of asynchronous fetching of surrounding text.
89
protected slots:
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
90
    void on_fetchSurroundingText_finished();
1742.1.1 by Adam Reichold
Add prototype implementation of asynchronous fetching of surrounding text.
91
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
92
private:
1719 by Adam Reichold
Merge central search model.
93
    Q_DISABLE_COPY(SearchModel)
94
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
95
    static SearchModel* s_instance;
1719 by Adam Reichold
Merge central search model.
96
    SearchModel(QObject* parent = 0);
97
2041.2.12 by Adam Reichold
Fix usage of QList in the bookmark and search models and various small performance issues reported by the clazy tool using the checks of "level0,level1,level2,no-missing-qobject-macro,no-qstring-allocations,no-copyable-polymorphic,no-ctor-missing-parent-argument,no-reserve-candidates".
98
    QVector< DocumentView* > m_views;
1719 by Adam Reichold
Merge central search model.
99
100
    QModelIndex findView(DocumentView* view) const;
101
    QModelIndex findOrInsertView(DocumentView* view);
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
102
1740 by Adam Reichold
Merge displaying surrounding text in extended search model.
103
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
104
    typedef QPair< int, QRectF > Result;
1742.1.2 by Adam Reichold
Minimal clean up to the search model itself (use QList as it is the default until it is proven that QVector is faster and make the cache key function freestanding).
105
    typedef QList< Result > Results;
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
106
1742.1.2 by Adam Reichold
Minimal clean up to the search model itself (use QList as it is the default until it is proven that QVector is faster and make the cache key function freestanding).
107
    QHash< DocumentView*, Results* > m_results;
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
108
1734.1.1 by Razi Alavizadeh
Extract surrounding text of search results.
109
1742.1.4 by Adam Reichold
Clean up the asynchronuous surrounding text functionality and begin to populate the cache immediately upon receiving results.
110
    typedef QPair< DocumentView*, QByteArray > TextCacheKey;
1839 by Adam Reichold
Try to decouple highlighted matched text from the matching itself done by the backends.
111
    typedef QPair< QString, QString > TextCacheObject;
1742.1.4 by Adam Reichold
Clean up the asynchronuous surrounding text functionality and begin to populate the cache immediately upon receiving results.
112
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
113
    struct TextJob
1742.1.1 by Adam Reichold
Add prototype implementation of asynchronous fetching of surrounding text.
114
    {
1747 by Adam Reichold
Optimize text job memory layout to remove redundant cache key calculations.
115
        TextCacheKey key;
116
        TextCacheObject* object;
117
118
        TextJob() : key(), object(0) {}
119
        TextJob(const TextCacheKey& key, TextCacheObject* object) : key(key), object(object) {}
1742.1.4 by Adam Reichold
Clean up the asynchronuous surrounding text functionality and begin to populate the cache immediately upon receiving results.
120
1742.1.1 by Adam Reichold
Add prototype implementation of asynchronous fetching of surrounding text.
121
    };
122
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
123
    typedef QFutureWatcher< TextJob > TextWatcher;
124
1742.1.4 by Adam Reichold
Clean up the asynchronuous surrounding text functionality and begin to populate the cache immediately upon receiving results.
125
    mutable QCache< TextCacheKey, TextCacheObject > m_textCache;
1748 by Adam Reichold
Add back checked parallelism to the surrounding text extraction.
126
    mutable QHash< TextCacheKey, TextWatcher* > m_textWatchers;
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
127
1839 by Adam Reichold
Try to decouple highlighted matched text from the matching itself done by the backends.
128
    QString fetchMatchedText(DocumentView* view, const Result& result) const;
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
129
    QString fetchSurroundingText(DocumentView* view, const Result& result) const;
130
1839 by Adam Reichold
Try to decouple highlighted matched text from the matching itself done by the backends.
131
    const TextCacheObject* fetchText(DocumentView* view, const Result& result) const;
132
1742.1.14 by Adam Reichold
Revert the removal of QtConcurrent usage in the search model.
133
    static TextCacheKey textCacheKey(DocumentView* view, const Result& result);
1747 by Adam Reichold
Optimize text job memory layout to remove redundant cache key calculations.
134
    static TextJob textJob(const TextCacheKey& key, const Result& result);
1734.1.5 by Razi Alavizadeh
Add informative tooltips for search model indices.
135
1717.1.2 by Razi Alavizadeh
Add a central search model to store search results.
136
};
137
138
} // qpdfview
139
140
#endif // SEARCHMODEL_H