]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.h
On Linux show in crash message box the backtrace
[lyx.git] / src / frontends / qt4 / TocModel.h
1 // -*- C++ -*-
2 /**
3  * \file TocModel.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef TOCMODEL_H
13 #define TOCMODEL_H
14
15 #include <QHash>
16 #include <QSortFilterProxyModel>
17
18 namespace lyx {
19
20 class Buffer;
21 class BufferView;
22 class DocIterator;
23 class Toc;
24 class TocItem;
25
26 namespace frontend {
27
28 class TocTypeModel;
29
30 /// A class that adapt the TocBackend of a Buffer into standard Qt models for
31 /// GUI visualisation.
32 /// There is one TocModel per list in the TocBackend.
33 class TocModel
34 {
35 public:
36         ///
37         TocModel(QObject * parent);
38         ///
39         void reset(Toc const & toc);
40         ///
41         void reset();
42         ///
43         void updateItem(DocIterator const & dit);
44         ///
45         void clear();
46         ///
47         QAbstractItemModel * model();
48         ///
49         QAbstractItemModel const * model() const;
50         ///
51         void sort(bool sort_it);
52         ///
53         bool isSorted() const { return is_sorted_; }
54         ///
55         TocItem const & tocItem(QModelIndex const & index) const;
56         ///
57         QModelIndex modelIndex(DocIterator const & dit) const;
58         ///
59         int modelDepth() const;
60
61 private:
62         ///
63         void populate(unsigned int & index, QModelIndex const & parent);
64         ///
65         TocTypeModel * model_;
66         ///
67         QSortFilterProxyModel * sorted_model_;
68         ///
69         bool is_sorted_;
70         ///
71         Toc const * toc_;
72         ///
73         int maxdepth_;
74         ///
75         int mindepth_;
76 };
77
78
79 /// A filter to sort the models alphabetically but with
80 /// the table of contents on top.
81 class TocModelSortProxyModel : public QSortFilterProxyModel
82 {
83 public:
84         TocModelSortProxyModel(QObject * w) 
85                 : QSortFilterProxyModel(w)
86         {}
87
88         bool lessThan (const QModelIndex & left, const QModelIndex & right) const
89         {
90                 if (left.model()->data(left, Qt::UserRole).toString()
91                           == QString("tableofcontents"))
92                         return true;
93                 else if (right.model()->data(right, Qt::UserRole).toString()
94                           == QString("tableofcontents"))
95                         return false;
96                 else
97                         return QSortFilterProxyModel::lessThan(left, right);
98         }
99 };
100
101
102
103 /// A container for the different TocModels.
104 class TocModels : public QObject
105 {
106         Q_OBJECT
107 public:
108         ///
109         TocModels();
110         ///
111         void reset(BufferView const * bv);
112         ///
113         int depth(QString const & type);
114         ///
115         QAbstractItemModel * model(QString const & type);
116         ///
117         QAbstractItemModel * nameModel();
118         ///
119         QModelIndex currentIndex(QString const & type) const;
120         ///
121         void goTo(QString const & type, QModelIndex const & index) const;
122         ///
123         void init(Buffer const & buffer);
124         ///
125         void updateItem(QString const & type, DocIterator const & dit);
126         ///
127         void sort(QString const & type, bool sort_it);
128         ///
129         bool isSorted(QString const & type) const;
130         /// the item that is currently selected
131         TocItem const currentItem(QString const & type,
132                 QModelIndex const & index) const;
133
134 Q_SIGNALS:
135         /// Signal that the internal toc_models_ has been reset.
136         void modelReset();
137
138 private:
139         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
140         typedef QHash<QString, TocModel *>::iterator iterator;
141         ///
142         void clear();
143         ///
144         BufferView const * bv_;
145         ///
146         QHash<QString, TocModel *> models_;
147         ///
148         TocTypeModel * names_;
149         ///
150         TocModelSortProxyModel * names_sorted_;
151 };
152
153 } // namespace frontend
154 } // namespace lyx
155
156 #endif // TOCMODEL_H