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