]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiIdListModel.h
Fix up 'Reduce metrics updates from 4 to 1 when loading file'
[lyx.git] / src / frontends / qt / GuiIdListModel.h
1 // -*- C++ -*-
2 /**
3  * \file GuiIdListModel.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Kimberly Heck
8  *
9  * Some of this code is based upon qstringlistmodel.{h,cpp}, which is
10  * part of the Qt toolkit, copyright (C) 1992-2007 Trolltech ASA, and
11  * released under the General Public License.
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #ifndef GUIIDLISTMODEL_H
17 #define GUIIDLISTMODEL_H
18
19 #include "support/qstring_helpers.h"
20
21 #include <QAbstractListModel>
22 #include <vector>
23 #include <string>
24
25 namespace lyx {
26 namespace frontend {
27
28 /**
29  * A QAbstractListModel that associates an identifying string with
30  * each item, as well as a display string. The display string is set
31  * with setUIString; the identifying string, with setIDString.
32  *
33  * This is intended to be used, for example, with GuiSelectionManager.
34  * In that case, one needs to recover from selectedModel which items
35  * have been selected. One may not wish to do so using the string that
36  * is there *displayed*, since, among other things, that string may be
37  * translated. So the id can be used to identify the items in this case.
38  */
39 class GuiIdListModel : public QAbstractListModel {
40 public:
41         ///
42         GuiIdListModel() {}
43         //////////////////////////////////////////////////////////////////////
44         // Methods overridden from QAbstractListModel
45         //////////////////////////////////////////////////////////////////////
46         ///
47         int rowCount(QModelIndex const & = QModelIndex()) const override
48                 { return int(userData_.size()); }
49
50         ///
51         QVariant data(QModelIndex const & index, int role = Qt::DisplayRole) const override;
52         ///
53         bool insertRows(int row, int count, QModelIndex const & parent = QModelIndex()) override;
54         ///
55         bool removeRows(int row, int count, QModelIndex const & parent = QModelIndex()) override;
56         ///
57         void clear() { removeRows(0, rowCount()); }
58         ///
59         bool setData (QModelIndex const & index,
60                         const QVariant & value, int role = Qt::EditRole) override;
61         ///
62         QMap<int, QVariant> itemData(QModelIndex const & index ) const override;
63         //////////////////////////////////////////////////////////////////////
64         // New methods
65         //////////////////////////////////////////////////////////////////////
66         ///
67         void insertRow(int const i, QString const & uiString,
68                         std::string const & idString, QString const & ttString);
69         /// A convenience method, setting ttString to the same as uiString
70         void insertRow(int const i, QString const & uiString,
71                         std::string const & idString);
72         /// \return the index of the (first) item with idString
73         /// \return -1 if not found
74         int findIDString(std::string const & idString);
75         ///
76         virtual QString getIDString(QModelIndex const & index) const
77                 { return data(index, Qt::UserRole).toString(); }
78         ///
79         virtual std::string getIDString(int const i) const
80                 { return fromqstr(getIDString(index(i))); }
81
82 private:
83         /// noncopyable
84         GuiIdListModel(GuiIdListModel const &);
85         ///
86         void operator=(GuiIdListModel const &);
87         ///
88         void setUIString(QModelIndex const & index, QString const & value)
89                 { setData(index, value); }
90         ///
91         void setUIString(int const i, QString const & value)
92                 { setUIString(index(i), value); }
93         ///
94         void setIDString(QModelIndex const & index, QString const & value)
95                 { setData(index, value, Qt::UserRole); }
96         ///
97         void setIDString(int const i, std::string const & value)
98                 { setIDString(index(i), toqstr(value)); }
99         ///
100         void setTTString(QModelIndex const & index, QString const & value)
101                 { setData(index, value, Qt::ToolTipRole); }
102         ///
103         void setTTString(int const i, QString const & value)
104                 { setTTString(index(i), value); }
105         struct OurData {
106                 /// Qt::DisplayRole and Qt::EditRole
107                 QVariant uiString;
108                 /// Qt::UserRole
109                 QVariant idString;
110                 /// Qt::ToolTipRole
111                 QVariant ttString;
112         };
113         ///
114         std::vector<OurData> userData_;
115         ///
116         bool rowIsValid(int const i) const
117         {
118                 return i >= 0 && i <= int(userData_.size());
119         }
120 };
121
122
123 } // namespace frontend
124 } // namespace lyx
125 #endif //GUIIDLISTMODEL_H