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_[row].uiString;
35 if (role == Qt::ToolTipRole) {
36 QString const ttstr = userData_[row].ttString.toString();
37 return !ttstr.isEmpty() ? ttstr : userData_[row].uiString;
39 if (role == Qt::UserRole)
40 return userData_[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_[row].uiString = value;
53 dataChanged(index, index);
56 if (role == Qt::UserRole) {
57 userData_[row].idString = value;
58 dataChanged(index, index);
61 if (role == Qt::ToolTipRole) {
62 userData_[row].ttString = value;
63 dataChanged(index, index);
66 // If we assert here, it's because we're trying to set an
68 LASSERT(false, return false);
69 return false; // silence the warning
73 bool GuiIdListModel::insertRows(int row, int count,
74 QModelIndex const & /*parent*/)
78 vector<OurData>::iterator it = userData_.begin() + row;
80 beginInsertRows(QModelIndex(), row, row + count - 1);
81 userData_.insert(it, count, v);
87 bool GuiIdListModel::removeRows(int row, int count,
88 QModelIndex const & /*parent*/)
90 if (!rowIsValid(row) || row + count > int(userData_.size()) ||
95 vector<OurData>::iterator it = userData_.begin() + row;
96 beginRemoveRows(QModelIndex(), row, row + count - 1);
97 userData_.erase(it, it + count);
103 void GuiIdListModel::insertRow(int const i, QString const & uiString,
104 std::string const & idString)
106 insertRow(i, uiString, idString, uiString);
110 void GuiIdListModel::insertRow(int const i, QString const & uiString,
111 std::string const & idString, QString const & ttString)
114 setUIString(i, uiString);
115 setIDString(i, idString);
116 setTTString(i, ttString);
120 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
122 int const row = index.row();
123 if (!rowIsValid(row))
124 return QMap<int, QVariant>();
125 QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
126 qm[Qt::UserRole] = userData_[row].idString;
131 int GuiIdListModel::findIDString(std::string const & idString)
133 vector<OurData>::const_iterator it = userData_.begin();
134 vector<OurData>::const_iterator end = userData_.end();
135 for (; it != end; ++it)
136 if (fromqstr(it->idString.toString()) == idString)
137 return it - userData_.begin();
141 } // namespace frontend