]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.h
Make a string translatable
[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 "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         void setString(TocItem const & item, QModelIndex index);
69         ///
70         TocTypeModel * model_;
71         ///
72         QSortFilterProxyModel * sorted_model_;
73         ///
74         bool is_sorted_;
75         ///
76         std::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,
125                                  DocIterator const & dit) const;
126         ///
127         void goTo(QString const & type, QModelIndex const & index) const;
128         ///
129         void init(Buffer const & buffer);
130         ///
131         void updateItem(QString const & type, DocIterator const & dit);
132         ///
133         void sort(QString const & type, bool sort_it);
134         ///
135         bool isSorted(QString const & type) const;
136         /// the item that is currently selected
137         TocItem const currentItem(QString const & type,
138                 QModelIndex const & index) const;
139
140 private:
141         typedef QHash<QString, TocModel *>::const_iterator const_iterator;
142         typedef QHash<QString, TocModel *>::iterator iterator;
143         ///
144         void clear();
145         ///
146         QHash<QString, TocModel *> models_;
147         ///
148         TocTypeModel * names_;
149         ///
150         TocModelSortProxyModel * names_sorted_;
151 };
152
153 } // namespace frontend
154 } // namespace lyx
155
156 #endif // TOCMODEL_H