2 * \file GuiIdListModel.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Some of this code is based upon qstringlistmodel.{h,cpp}, which is
9 * part of the Qt toolkit, copyright (C) 1992-2007 Trolltech ASA, and
10 * released under the General Public License.
12 * Full author contact details are available in file CREDITS.
17 #include "GuiIdListModel.h"
25 // Note: Any role that is added here must also be added to setData().
26 QVariant GuiIdListModel::data(QModelIndex const & index, int role) const
28 int const row = index.row();
31 if (role == Qt::DisplayRole || role == Qt::EditRole)
32 return userData_[row].uiString;
33 if (role == Qt::ToolTipRole) {
34 QString const ttstr = userData_[row].ttString.toString();
35 return !ttstr.isEmpty() ? ttstr : userData_[row].uiString;
37 if (role == Qt::UserRole)
38 return userData_[row].idString;
43 bool GuiIdListModel::setData (QModelIndex const & index,
44 const QVariant & value, int role)
46 int const row = index.row();
49 if (role == Qt::DisplayRole || role == Qt::EditRole) {
50 userData_[row].uiString = value;
51 dataChanged(index, index);
54 if (role == Qt::UserRole) {
55 userData_[row].idString = value;
56 dataChanged(index, index);
59 if (role == Qt::ToolTipRole) {
60 userData_[row].ttString = value;
61 dataChanged(index, index);
68 bool GuiIdListModel::insertRows(int row, int count,
69 QModelIndex const & /*parent*/)
73 vector<OurData>::iterator it = userData_.begin() + row;
75 beginInsertRows(QModelIndex(), row, row + count - 1);
76 userData_.insert(it, count, v);
82 bool GuiIdListModel::removeRows(int row, int count,
83 QModelIndex const & /*parent*/)
85 if (!rowIsValid(row) || row + count > int(userData_.size()) ||
90 vector<OurData>::iterator it = userData_.begin() + row;
91 beginRemoveRows(QModelIndex(), row, row + count - 1);
92 userData_.erase(it, it + count);
98 void GuiIdListModel::insertRow(int const i, QString const & uiString,
99 std::string const & idString)
101 insertRow(i, uiString, idString, uiString);
105 void GuiIdListModel::insertRow(int const i, QString const & uiString,
106 std::string const & idString, QString const & ttString)
109 setUIString(i, uiString);
110 setIDString(i, idString);
111 setTTString(i, ttString);
115 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
117 int const row = index.row();
118 if (!rowIsValid(row))
119 return QMap<int, QVariant>();
120 QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
121 qm[Qt::UserRole] = userData_[row].idString;
126 int GuiIdListModel::findIDString(std::string const & idString)
128 vector<OurData>::const_iterator it = userData_.begin();
129 vector<OurData>::const_iterator end = userData_.end();
130 for (; it != end; ++it)
131 if (fromqstr(it->idString.toString()) == idString)
132 return it - userData_.begin();
138 // The following function is currently unused but is retained here in
139 // case it should at some point be useful.
140 QStringList GuiIdListModel::getIDStringList() const
143 vector<OurData>::const_iterator it = userData_.begin();
144 vector<OurData>::const_iterator end = userData_.end();
145 for (; it != end; ++it)
146 qsl.append(it->idString.toString());
150 } // namespace frontend