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