]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIdListModel.h
Doxy bit.
[lyx.git] / src / frontends / qt4 / 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 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
48                 { return userData_.size(); }
49
50         ///
51         virtual QVariant data(QModelIndex const & index, 
52                               int role = Qt::DisplayRole) const;
53         ///
54         bool insertRows(int row, int count, QModelIndex const & parent = QModelIndex());
55         ///
56         bool removeRows(int row, int count, QModelIndex const & parent = QModelIndex());
57         /// 
58         void clear() { removeRows(0, rowCount()); }
59         ///
60         virtual bool setData (QModelIndex const & index, 
61                         const QVariant & value, int role = Qt::EditRole );
62         ///
63         virtual QMap<int, QVariant> itemData(QModelIndex const & index ) const;
64         //////////////////////////////////////////////////////////////////////
65         // New methods
66         //////////////////////////////////////////////////////////////////////
67         ///
68         void setUIString(QModelIndex const & index, QString const & value)
69                         { setData(index, value); }
70         ///
71         void setUIString(int const i, QString const & value)
72                         { setUIString(index(i), value); }
73         ///
74         void setIDString(QModelIndex const & index, QString const & value)
75                         { setData(index, value, Qt::UserRole); }
76         ///
77         void setIDString(int const i, std::string const & value)
78                         { setIDString(index(i), toqstr(value)); }
79         ///
80         virtual QString getIDString(QModelIndex const & index) const
81                         { return data(index, Qt::UserRole).toString(); }
82         ///
83         virtual std::string getIDString(int const i) const
84                         { return fromqstr(getIDString(index(i))); }
85         ///
86         void insertRow(int const i, QString const & uiString, 
87                         std::string const & idString);
88         /* The following functions are currently unused but are retained here in
89            case they should at some point be useful.
90         ///
91         void setUIString(int const i, std::string const & value)
92                         { setUIString(index(i), value); }
93         ///
94         void setIDString(int const i, QString const & value)
95                         { setIDString(index(i), value); }
96         ///
97         QStringList getIDStringList() const;
98         ///
99         void insertRow(int const i, QString const & uiString, 
100                         QString const & idString);
101         ///
102         void insertRow(int const i, std::string const & uiString, 
103                         std::string const & idString);
104         /// Returns whether the model contains an item with the given ID
105         bool containsID(QVariant const &) const;
106         */
107 private:
108         /// noncopyable
109         GuiIdListModel(GuiIdListModel const &);
110         ///
111         void operator=(GuiIdListModel const &);
112         ///
113         struct OurData {
114                 QVariant uiString;
115                 QVariant idString;
116         };
117         ///
118         std::vector<OurData> userData_;
119         ///
120         bool rowIsValid(int const i) const
121         {
122                 return i >= 0 && i <= int(userData_.size());
123         }
124 };
125
126
127 }
128 }
129 #endif //GUIIDLISTMODEL_H