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"
19 #include "support/lassert.h"
27 // Note: Any role that is added here must also be added to setData().
28 QVariant GuiIdListModel::data(QModelIndex const & index, int role) const
30 int const row = index.row();
33 if (role == Qt::DisplayRole || role == Qt::EditRole)
34 return userData_[ulong(row)].uiString;
35 if (role == Qt::ToolTipRole) {
36 QString const ttstr = userData_[ulong(row)].ttString.toString();
37 return !ttstr.isEmpty() ? ttstr : userData_[ulong(row)].uiString;
39 if (role == Qt::UserRole)
40 return userData_[ulong(row)].idString;
45 bool GuiIdListModel::setData(QModelIndex const & index,
46 const QVariant & value, int role)
48 int const row = index.row();
51 if (role == Qt::DisplayRole || role == Qt::EditRole) {
52 userData_[ulong(row)].uiString = value;
53 dataChanged(index, index);
56 if (role == Qt::UserRole) {
57 userData_[ulong(row)].idString = value;
58 dataChanged(index, index);
61 if (role == Qt::ToolTipRole) {
62 userData_[ulong(row)].ttString = value;
63 dataChanged(index, index);
66 if (role == Qt::DecorationRole)
69 if (role == Qt::UserRole - 1)
70 // This role is set by Qt <= 5.5 if setEditable()
71 // of the item is false
73 // If we assert here, it's because we're trying to set an
80 bool GuiIdListModel::insertRows(int row, int count,
81 QModelIndex const & /*parent*/)
85 vector<OurData>::iterator it = userData_.begin() + row;
86 beginInsertRows(QModelIndex(), row, row + count - 1);
87 userData_.insert(it, ulong(count), OurData());
93 bool GuiIdListModel::removeRows(int row, int count,
94 QModelIndex const & /*parent*/)
96 if (!rowIsValid(row) || row + count > int(userData_.size()) ||
101 vector<OurData>::iterator it = userData_.begin() + row;
102 beginRemoveRows(QModelIndex(), row, row + count - 1);
103 userData_.erase(it, it + count);
109 void GuiIdListModel::insertRow(int const i, QString const & uiString,
110 std::string const & idString)
112 insertRow(i, uiString, idString, uiString);
116 void GuiIdListModel::insertRow(int const i, QString const & uiString,
117 std::string const & idString, QString const & ttString)
120 setUIString(i, uiString);
121 setIDString(i, idString);
122 setTTString(i, ttString);
126 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
128 int const row = index.row();
129 if (!rowIsValid(row))
130 return QMap<int, QVariant>();
131 QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
132 qm[Qt::UserRole] = userData_[ulong(row)].idString;
137 int GuiIdListModel::findIDString(std::string const & idString)
139 vector<OurData>::const_iterator it = userData_.begin();
140 vector<OurData>::const_iterator end = userData_.end();
141 for (; it != end; ++it)
142 if (fromqstr(it->idString.toString()) == idString)
143 return it - userData_.begin();
147 } // namespace frontend