]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.h
Fix the tab ordering of GuiDocument components.
[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 "qt_helpers.h"
16
17 #include <QHash>
18 #include <QSortFilterProxyModel>
19 #include <QStandardItemModel>
20
21 namespace lyx {
22
23 class Buffer;
24 class BufferView;
25 class DocIterator;
26 class Toc;
27 class TocItem;
28
29 namespace frontend {
30
31 class TocTypeModel;
32
33 /// A class that adapt the TocBackend of a Buffer into standard Qt models for
34 /// GUI visualisation.
35 /// There is one TocModel per list in the TocBackend.
36 class TocModel
37 {
38 public:
39         ///
40         TocModel(QObject * parent);
41         ///
42         void reset(Toc const & toc);
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         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 updateBackend() 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 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