]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.h
Amend f441590c
[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         /// noncopyable
38         TocModel(TocModel const &);
39         void operator=(TocModel const &);
40 public:
41         ///
42         TocModel(QObject * parent);
43         ///
44         void reset(shared_ptr<Toc const>);
45         ///
46         void reset();
47         ///
48         void updateItem(DocIterator const & dit);
49         ///
50         void clear();
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         TocTypeModel * model_;
71         ///
72         QSortFilterProxyModel * sorted_model_;
73         ///
74         bool is_sorted_;
75         ///
76         shared_ptr<Toc const> toc_;
77         ///
78         int maxdepth_;
79         ///
80         int mindepth_;
81 };
82
83
84 /// A filter to sort the models alphabetically but with
85 /// the table of contents on top.
86 class TocModelSortProxyModel : public QSortFilterProxyModel
87 {
88 public:
89         TocModelSortProxyModel(QObject * w) 
90                 : QSortFilterProxyModel(w)
91         {}
92
93         bool lessThan (const QModelIndex & left, const QModelIndex & right) const
94         {
95                 if (left.model()->data(left, Qt::UserRole).toString()
96                           == QString("tableofcontents"))
97                         return true;
98                 else if (right.model()->data(right, Qt::UserRole).toString()
99                           == QString("tableofcontents"))
100                         return false;
101                 else
102                         return QSortFilterProxyModel::lessThan(left, right);
103         }
104 };
105
106
107
108 /// A container for the different TocModels.
109 class TocModels : public QObject
110 {
111         Q_OBJECT
112 public:
113         ///
114         TocModels();
115         ///
116         void reset(BufferView const * bv);
117         ///
118         int depth(QString const & type);
119         ///
120         QAbstractItemModel * model(QString const & type);
121         ///
122         QAbstractItemModel * nameModel();
123         ///
124         QModelIndex currentIndex(QString const & type) const;
125         ///
126         void goTo(QString const & type, QModelIndex const & index) const;
127         ///
128         void init(Buffer const & buffer);
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 Q_SIGNALS:
140         /// Signal that the internal toc_models_ has been reset.
141         void modelReset();
142
143 private:
144         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
145         typedef QHash<QString, TocModel *>::iterator iterator;
146         ///
147         void clear();
148         ///
149         BufferView const * bv_;
150         ///
151         QHash<QString, TocModel *> models_;
152         ///
153         TocTypeModel * names_;
154         ///
155         TocModelSortProxyModel * names_sorted_;
156 };
157
158 } // namespace frontend
159 } // namespace lyx
160
161 #endif // TOCMODEL_H