]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIdListModel.cpp
Do not compute caret geometry when we are not ready to do so.
[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_[row].uiString;
35         if (role == Qt::ToolTipRole) {
36                 QString const ttstr = userData_[row].ttString.toString();
37                 return !ttstr.isEmpty() ? ttstr : userData_[row].uiString;
38         }
39         if (role == Qt::UserRole)
40                 return userData_[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_[row].uiString = value;
53                 dataChanged(index, index);
54                 return true;
55         }
56         if (role == Qt::UserRole) {
57                 userData_[row].idString = value;
58                 dataChanged(index, index);
59                 return true;
60         }
61         if (role == Qt::ToolTipRole) {
62                 userData_[row].ttString = value;
63                 dataChanged(index, index);
64                 return true;
65         }
66         // If we assert here, it's because we're trying to set an
67         // unrecognized role.
68         LATTEST(false);
69         return false;
70 }
71
72
73 bool GuiIdListModel::insertRows(int row, int count,
74                 QModelIndex const & /*parent*/)
75 {
76         if (!rowIsValid(row))
77                 return false;
78         vector<OurData>::iterator it = userData_.begin() + row;
79         beginInsertRows(QModelIndex(), row, row + count - 1);
80         userData_.insert(it, count, OurData());
81         endInsertRows();
82         return true;
83 }
84
85
86 bool GuiIdListModel::removeRows(int row, int count,
87                 QModelIndex const & /*parent*/)
88 {
89         if (!rowIsValid(row) || row + count > int(userData_.size()) ||
90             count < 0)
91                 return false;
92         if (count == 0)
93                 return true;
94         vector<OurData>::iterator it = userData_.begin() + row;
95         beginRemoveRows(QModelIndex(), row, row + count - 1);
96         userData_.erase(it, it + count);
97         endRemoveRows();
98         return true;
99 }
100
101
102 void GuiIdListModel::insertRow(int const i, QString const & uiString,
103                 std::string const & idString)
104 {
105         insertRow(i, uiString, idString, uiString);
106 }
107
108
109 void GuiIdListModel::insertRow(int const i, QString const & uiString,
110         std::string const & idString, QString const & ttString)
111 {
112         insertRows(i, 1);
113         setUIString(i, uiString);
114         setIDString(i, idString);
115         setTTString(i, ttString);
116 }
117
118
119 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
120 {
121         int const row = index.row();
122         if (!rowIsValid(row))
123                 return QMap<int, QVariant>();
124         QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
125         qm[Qt::UserRole] = userData_[row].idString;
126         return qm;
127 }
128
129
130 int GuiIdListModel::findIDString(std::string const & idString)
131 {
132         vector<OurData>::const_iterator it  = userData_.begin();
133         vector<OurData>::const_iterator end = userData_.end();
134         for (; it != end; ++it)
135                 if (fromqstr(it->idString.toString()) == idString)
136                         return it - userData_.begin();
137         return -1;
138 }
139
140 } // namespace frontend
141 } // namespace lyx
142