]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/TocModel.h
Prevent Outliner crash due to emptied toc value
[lyx.git] / src / frontends / qt / 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 BufferView;
23 class DocIterator;
24 class FuncRequest;
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         bool empty() { return toc_->empty(); }
51         ///
52         QAbstractItemModel * model();
53         ///
54         QAbstractItemModel const * model() const;
55         ///
56         void sort(bool sort_it);
57         ///
58         bool isSorted() const { return is_sorted_; }
59         ///
60         TocItem const & tocItem(QModelIndex const & index) const;
61         ///
62         QModelIndex modelIndex(DocIterator const & dit) const;
63         ///
64         int modelDepth() const;
65
66 private:
67         ///
68         void populate(unsigned int & index, QModelIndex const & parent);
69         ///
70         void setString(TocItem const & item, QModelIndex index);
71         ///
72         TocTypeModel * model_;
73         ///
74         QSortFilterProxyModel * sorted_model_;
75         ///
76         bool is_sorted_;
77         ///
78         std::shared_ptr<Toc const> toc_;
79         ///
80         int maxdepth_;
81         ///
82         int mindepth_;
83 };
84
85
86 /// A filter to sort the models alphabetically but with
87 /// the table of contents on top.
88 class TocModelSortProxyModel : public QSortFilterProxyModel
89 {
90 public:
91         TocModelSortProxyModel(QObject * w)
92                 : QSortFilterProxyModel(w)
93         {}
94
95         bool lessThan(const QModelIndex & left, const QModelIndex & right) const override
96         {
97                 if (left.model()->data(left, Qt::UserRole).toString()
98                           == QString("tableofcontents"))
99                         return true;
100                 else if (right.model()->data(right, Qt::UserRole).toString()
101                           == QString("tableofcontents"))
102                         return false;
103                 else
104                         return QSortFilterProxyModel::lessThan(left, right);
105         }
106 };
107
108
109
110 /// A container for the different TocModels.
111 class TocModels : public QObject
112 {
113         Q_OBJECT
114 public:
115         ///
116         TocModels();
117         ~TocModels();
118         ///
119         void reset(BufferView const * bv);
120         ///
121         int depth(QString const & type);
122         ///
123         QAbstractItemModel * model(QString const & type);
124         ///
125         QAbstractItemModel * nameModel();
126         ///
127         QModelIndex currentIndex(QString const & type,
128                                  DocIterator const & dit) const;
129         ///
130         FuncRequest goTo(QString const & type, QModelIndex const & index) const;
131         ///
132         void updateItem(QString const & type, DocIterator const & dit);
133         ///
134         void sort(QString const & type, bool sort_it);
135         ///
136         bool isSorted(QString const & type) const;
137         /// the item that is currently selected
138         TocItem const currentItem(QString const & type,
139                 QModelIndex const & index) const;
140
141 private:
142         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
143         typedef QHash<QString, TocModel *>::iterator iterator;
144         ///
145         void clear();
146         ///
147         QHash<QString, TocModel *> models_;
148         ///
149         TocTypeModel * names_;
150         ///
151         TocModelSortProxyModel * names_sorted_;
152 };
153
154 } // namespace frontend
155 } // namespace lyx
156
157 #endif // TOCMODEL_H