]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIdListModel.cpp
GuiIdListModel: ignore DecorationRole (icons of available list)
[lyx.git] / src / frontends / qt4 / GuiIdListModel.cpp
1 /**
2  * \file GuiIdListModel.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Richard Heck
7  *
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.
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "GuiIdListModel.h"
18
19 #include "support/lassert.h"
20
21 using std::vector;
22
23 namespace lyx {
24 namespace frontend {
25
26
27 // Note: Any role that is added here must also be added to setData().
28 QVariant GuiIdListModel::data(QModelIndex const & index, int role) const
29 {
30         int const row = index.row();
31         if (!rowIsValid(row))
32                 return QVariant();
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;
38         }
39         if (role == Qt::UserRole)
40                 return userData_[ulong(row)].idString;
41         return QVariant();
42 }
43
44
45 bool GuiIdListModel::setData(QModelIndex const & index,
46                 const QVariant & value, int role)
47 {
48         int const row = index.row();
49         if (!rowIsValid(row))
50                 return false;
51         if (role == Qt::DisplayRole || role == Qt::EditRole) {
52                 userData_[ulong(row)].uiString = value;
53                 dataChanged(index, index);
54                 return true;
55         }
56         if (role == Qt::UserRole) {
57                 userData_[ulong(row)].idString = value;
58                 dataChanged(index, index);
59                 return true;
60         }
61         if (role == Qt::ToolTipRole) {
62                 userData_[ulong(row)].ttString = value;
63                 dataChanged(index, index);
64                 return true;
65         }
66         if (role == Qt::DecorationRole)
67                 // nothing to do
68                 return true;
69         // If we assert here, it's because we're trying to set an
70         // unrecognized role.
71         LATTEST(false);
72         return false;
73 }
74
75
76 bool GuiIdListModel::insertRows(int row, int count,
77                 QModelIndex const & /*parent*/)
78 {
79         if (!rowIsValid(row))
80                 return false;
81         vector<OurData>::iterator it = userData_.begin() + row;
82         beginInsertRows(QModelIndex(), row, row + count - 1);
83         userData_.insert(it, ulong(count), OurData());
84         endInsertRows();
85         return true;
86 }
87
88
89 bool GuiIdListModel::removeRows(int row, int count,
90                 QModelIndex const & /*parent*/)
91 {
92         if (!rowIsValid(row) || row + count > int(userData_.size()) ||
93             count < 0)
94                 return false;
95         if (count == 0)
96                 return true;
97         vector<OurData>::iterator it = userData_.begin() + row;
98         beginRemoveRows(QModelIndex(), row, row + count - 1);
99         userData_.erase(it, it + count);
100         endRemoveRows();
101         return true;
102 }
103
104
105 void GuiIdListModel::insertRow(int const i, QString const & uiString,
106                 std::string const & idString)
107 {
108         insertRow(i, uiString, idString, uiString);
109 }
110
111
112 void GuiIdListModel::insertRow(int const i, QString const & uiString,
113         std::string const & idString, QString const & ttString)
114 {
115         insertRows(i, 1);
116         setUIString(i, uiString);
117         setIDString(i, idString);
118         setTTString(i, ttString);
119 }
120
121
122 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
123 {
124         int const row = index.row();
125         if (!rowIsValid(row))
126                 return QMap<int, QVariant>();
127         QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
128         qm[Qt::UserRole] = userData_[ulong(row)].idString;
129         return qm;
130 }
131
132
133 int GuiIdListModel::findIDString(std::string const & idString)
134 {
135         vector<OurData>::const_iterator it  = userData_.begin();
136         vector<OurData>::const_iterator end = userData_.end();
137         for (; it != end; ++it)
138                 if (fromqstr(it->idString.toString()) == idString)
139                         return it - userData_.begin();
140         return -1;
141 }
142
143 } // namespace frontend
144 } // namespace lyx
145