]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiIdListModel.cpp
Fix broken Apple speller interface
[features.git] / src / frontends / qt / 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 Kimberly 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 (role == Qt::UserRole - 1)
70                 // This role is set by Qt <= 5.5 if setEditable()
71                 // of the item is false
72                 return true;
73         // If we assert here, it's because we're trying to set an
74         // unrecognized role.
75         LATTEST(false);
76         return false;
77 }
78
79
80 bool GuiIdListModel::insertRows(int row, int count,
81                 QModelIndex const & /*parent*/)
82 {
83         if (!rowIsValid(row))
84                 return false;
85         vector<OurData>::iterator it = userData_.begin() + row;
86         beginInsertRows(QModelIndex(), row, row + count - 1);
87         userData_.insert(it, ulong(count), OurData());
88         endInsertRows();
89         return true;
90 }
91
92
93 bool GuiIdListModel::removeRows(int row, int count,
94                 QModelIndex const & /*parent*/)
95 {
96         if (!rowIsValid(row) || row + count > int(userData_.size()) ||
97             count < 0)
98                 return false;
99         if (count == 0)
100                 return true;
101         vector<OurData>::iterator it = userData_.begin() + row;
102         beginRemoveRows(QModelIndex(), row, row + count - 1);
103         userData_.erase(it, it + count);
104         endRemoveRows();
105         return true;
106 }
107
108
109 void GuiIdListModel::insertRow(int const i, QString const & uiString,
110                 std::string const & idString)
111 {
112         insertRow(i, uiString, idString, uiString);
113 }
114
115
116 void GuiIdListModel::insertRow(int const i, QString const & uiString,
117         std::string const & idString, QString const & ttString)
118 {
119         insertRows(i, 1);
120         setUIString(i, uiString);
121         setIDString(i, idString);
122         setTTString(i, ttString);
123 }
124
125
126 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
127 {
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;
133         return qm;
134 }
135
136
137 int GuiIdListModel::findIDString(std::string const & idString)
138 {
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();
144         return -1;
145 }
146
147 } // namespace frontend
148 } // namespace lyx
149