]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.h
Another try at #6522. This works here, for the most part, with no ill
[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 <QStandardItemModel>
19
20 class QSortFilterProxyModel;
21
22 namespace lyx {
23
24 class Buffer;
25 class BufferView;
26 class DocIterator;
27 class Toc;
28 class TocItem;
29
30 namespace frontend {
31
32 /// A QStandardItemModel that gives access to the reset method.
33 /// This is needed in order to fix http://www.lyx.org/trac/ticket/3740
34 class TocTypeModel : public QStandardItemModel
35 {
36 public:
37         ///
38         TocTypeModel(QObject * parent);
39         ///
40         void reset();
41 #if QT_VERSION >= 0x040600
42         void beginResetModel();
43         void endResetModel();
44 #endif
45 };
46
47 /// A class that adapt the TocBackend of a Buffer into standard Qt models for
48 /// GUI visualisation.
49 /// There is one TocModel per list in the TocBackend.
50 class TocModel
51 {
52 public:
53         ///
54         TocModel(QObject * parent);
55         ///
56         void reset(Toc const & toc);
57         ///
58         void reset();
59         ///
60         void updateItem(DocIterator const & dit);
61         ///
62         void clear();
63         ///
64         QAbstractItemModel * model();
65         ///
66         QAbstractItemModel const * model() const;
67         ///
68         void sort(bool sort_it);
69         ///
70         bool isSorted() const { return is_sorted_; }
71         ///
72         TocItem const & tocItem(QModelIndex const & index) const;
73         ///
74         QModelIndex modelIndex(DocIterator const & dit) const;
75         ///
76         int modelDepth() const;
77
78 private:
79         ///
80         void populate(unsigned int & index, QModelIndex const & parent);
81         ///
82         TocTypeModel * model_;
83         ///
84         QSortFilterProxyModel * sorted_model_;
85         ///
86         bool is_sorted_;
87         ///
88         Toc const * toc_;
89         ///
90         int maxdepth_;
91         ///
92         int mindepth_;
93 };
94
95
96 /// A container for the different TocModels.
97 class TocModels : public QObject
98 {
99         Q_OBJECT
100 public:
101         ///
102         TocModels();
103         ///
104         void reset(BufferView const * bv);
105         ///
106         int depth(QString const & type);
107         ///
108         QAbstractItemModel * model(QString const & type);
109         ///
110         QAbstractItemModel * nameModel();
111         ///
112         QModelIndex currentIndex(QString const & type) const;
113         ///
114         void goTo(QString const & type, QModelIndex const & index) const;
115         ///
116         void init(Buffer const & buffer);
117         ///
118         void updateBackend() const;
119         ///
120         void updateItem(QString const & type, DocIterator const & dit);
121         ///
122         void sort(QString const & type, bool sort_it);
123         ///
124         bool isSorted(QString const & type) const;
125         /// the item that is currently selected
126         TocItem const currentItem(QString const & type,
127                 QModelIndex const & index) const;
128
129 Q_SIGNALS:
130         /// Signal that the internal toc_models_ has been reset.
131         void modelReset();
132
133 private:
134         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
135         typedef QHash<QString, TocModel *>::iterator iterator;
136         ///
137         void clear();
138         ///
139         BufferView const * bv_;
140         ///
141         QHash<QString, TocModel *> models_;
142         ///
143         TocTypeModel * names_;
144         ///
145         QSortFilterProxyModel * names_sorted_;
146 };
147
148 } // namespace frontend
149 } // namespace lyx
150
151 #endif // TOCMODEL_H