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 {
43 //////////////////////////////////////////////////////////////////////
44 // Methods overridden from QAbstractListModel
45 //////////////////////////////////////////////////////////////////////
47 int rowCount(QModelIndex const & = QModelIndex()) const
48 { return userData_.size(); }
51 virtual QVariant data(QModelIndex const & index,
52 int role = Qt::DisplayRole) const;
54 bool insertRows(int row, int count, QModelIndex const & parent = QModelIndex());
56 bool removeRows(int row, int count, QModelIndex const & parent = QModelIndex());
58 void clear() { removeRows(0, rowCount()); }
60 virtual bool setData (QModelIndex const & index,
61 const QVariant & value, int role = Qt::EditRole );
63 virtual QMap<int, QVariant> itemData(QModelIndex const & index ) const;
64 //////////////////////////////////////////////////////////////////////
66 //////////////////////////////////////////////////////////////////////
68 void insertRow(int const i, QString const & uiString,
69 std::string const & idString, QString const & ttString);
70 /// A convenience method, setting ttString to the same as uiString
71 void insertRow(int const i, QString const & uiString,
72 std::string const & idString);
73 /// \return the index of the (first) item with idString
74 /// \return -1 if not found
75 int findIDString(std::string const & idString);
77 virtual QString getIDString(QModelIndex const & index) const
78 { return data(index, Qt::UserRole).toString(); }
80 virtual std::string getIDString(int const i) const
81 { return fromqstr(getIDString(index(i))); }
85 GuiIdListModel(GuiIdListModel const &);
87 void operator=(GuiIdListModel const &);
89 void setUIString(QModelIndex const & index, QString const & value)
90 { setData(index, value); }
92 void setUIString(int const i, QString const & value)
93 { setUIString(index(i), value); }
95 void setIDString(QModelIndex const & index, QString const & value)
96 { setData(index, value, Qt::UserRole); }
98 void setIDString(int const i, std::string const & value)
99 { setIDString(index(i), toqstr(value)); }
101 void setTTString(QModelIndex const & index, QString const & value)
102 { setData(index, value, Qt::ToolTipRole); }
104 void setTTString(int const i, QString const & value)
105 { setTTString(index(i), value); }
107 /// Qt::DisplayRole and Qt::EditRole
115 std::vector<OurData> userData_;
117 bool rowIsValid(int const i) const
119 return i >= 0 && i <= int(userData_.size());
126 #endif //GUIIDLISTMODEL_H