3 * \file GuiIdListModel.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
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.
13 * Full author contact details are available in file CREDITS.
16 #ifndef GUIIDLISTMODEL_H
17 #define GUIIDLISTMODEL_H
19 #include "support/qstring_helpers.h"
21 #include <QAbstractListModel>
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.
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.
39 class GuiIdListModel : public QAbstractListModel {
42 explicit GuiIdListModel() {};
43 //////////////////////////////////////////////////////////////////////
44 // Methods overridden from QAbstractListModel
45 //////////////////////////////////////////////////////////////////////
47 inline int rowCount(QModelIndex const & parent = QModelIndex()) const;
49 virtual QVariant data(QModelIndex const & index,
50 int role = Qt::DisplayRole) const;
52 bool insertRows(int row, int count, QModelIndex const & parent = QModelIndex());
54 bool removeRows(int row, int count, QModelIndex const & parent = QModelIndex());
56 void clear() { removeRows(0, rowCount()); };
58 virtual bool setData (QModelIndex const & index,
59 const QVariant & value, int role = Qt::EditRole );
61 virtual QMap<int, QVariant> itemData(QModelIndex const & index ) const;
62 //////////////////////////////////////////////////////////////////////
64 //////////////////////////////////////////////////////////////////////
66 void setUIString(QModelIndex const & index, QString const & value)
67 { setData(index, value); };
69 void setUIString(int const i, QString const & value)
70 { setUIString(index(i), value); };
72 void setIDString(QModelIndex const & index, QString const & value)
73 { setData(index, value, Qt::UserRole); };
75 void setIDString(int const i, std::string const & value)
76 { setIDString(index(i), toqstr(value)); };
78 virtual QString getIDString(QModelIndex const & index) const
79 { return data(index, Qt::UserRole).toString(); };
81 virtual std::string getIDString(int const i) const
82 { return fromqstr(getIDString(index(i))); };
84 void insertRow(int const i, QString 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.
89 void setUIString(int const i, std::string const & value)
90 { setUIString(index(i), value); };
92 void setIDString(int const i, QString const & value)
93 { setIDString(index(i), value); };
95 QStringList getIDStringList() const;
97 void insertRow(int const i, QString const & uiString,
98 QString const & idString);
100 void insertRow(int const i, std::string const & uiString,
101 std::string const & idString);
102 /// Returns whether the model contains an item with the given ID
103 bool containsID(QVariant const &) const;
107 GuiIdListModel(GuiIdListModel const &);
114 std::vector<OurData> userData_;
116 inline bool rowIsValid(int const i) const
118 return i >= 0 && i <= int(userData_.size());
124 //definition is here to silence a warning
125 inline int GuiIdListModel::rowCount(QModelIndex const &) const
126 { return userData_.size(); }
130 #endif //GUIIDLISTMODEL_H