]> git.lyx.org Git - features.git/blob - src/frontends/qt4/TocModel.h
Replace support/shared_ptr.h and boost::shared_ptr with std::shared_ptr
[features.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
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         /// noncopyable
36         TocModel(TocModel const &);
37         void operator=(TocModel const &);
38 public:
39         ///
40         TocModel(QObject * parent);
41         ///
42         void reset(std::shared_ptr<Toc const>);
43         ///
44         void reset();
45         ///
46         void updateItem(DocIterator const & dit);
47         ///
48         void clear();
49         ///
50         QAbstractItemModel * model();
51         ///
52         QAbstractItemModel const * model() const;
53         ///
54         void sort(bool sort_it);
55         ///
56         bool isSorted() const { return is_sorted_; }
57         ///
58         TocItem const & tocItem(QModelIndex const & index) const;
59         ///
60         QModelIndex modelIndex(DocIterator const & dit) const;
61         ///
62         int modelDepth() const;
63
64 private:
65         ///
66         void populate(unsigned int & index, QModelIndex const & parent);
67         ///
68         TocTypeModel * model_;
69         ///
70         QSortFilterProxyModel * sorted_model_;
71         ///
72         bool is_sorted_;
73         ///
74         std::shared_ptr<Toc const> toc_;
75         ///
76         int maxdepth_;
77         ///
78         int mindepth_;
79 };
80
81
82 /// A filter to sort the models alphabetically but with
83 /// the table of contents on top.
84 class TocModelSortProxyModel : public QSortFilterProxyModel
85 {
86 public:
87         TocModelSortProxyModel(QObject * w) 
88                 : QSortFilterProxyModel(w)
89         {}
90
91         bool lessThan (const QModelIndex & left, const QModelIndex & right) const
92         {
93                 if (left.model()->data(left, Qt::UserRole).toString()
94                           == QString("tableofcontents"))
95                         return true;
96                 else if (right.model()->data(right, Qt::UserRole).toString()
97                           == QString("tableofcontents"))
98                         return false;
99                 else
100                         return QSortFilterProxyModel::lessThan(left, right);
101         }
102 };
103
104
105
106 /// A container for the different TocModels.
107 class TocModels : public QObject
108 {
109         Q_OBJECT
110 public:
111         ///
112         TocModels();
113         ///
114         void reset(BufferView const * bv);
115         ///
116         int depth(QString const & type);
117         ///
118         QAbstractItemModel * model(QString const & type);
119         ///
120         QAbstractItemModel * nameModel();
121         ///
122         QModelIndex currentIndex(QString const & type) const;
123         ///
124         void goTo(QString const & type, QModelIndex const & index) const;
125         ///
126         void init(Buffer const & buffer);
127         ///
128         void updateItem(QString const & type, DocIterator const & dit);
129         ///
130         void sort(QString const & type, bool sort_it);
131         ///
132         bool isSorted(QString const & type) const;
133         /// the item that is currently selected
134         TocItem const currentItem(QString const & type,
135                 QModelIndex const & index) const;
136
137 private:
138         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
139         typedef QHash<QString, TocModel *>::iterator iterator;
140         ///
141         void clear();
142         ///
143         BufferView const * bv_;
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