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