]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIdListModel.cpp
* fix spelling in comments to please John.
[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         LASSERT(false, return false);
69         return false; // silence the warning
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         OurData const v;
80         beginInsertRows(QModelIndex(), row, row + count - 1);
81         userData_.insert(it, count, v);
82         endInsertRows();
83         return true;
84 }
85
86
87 bool GuiIdListModel::removeRows(int row, int count, 
88                 QModelIndex const & /*parent*/)
89 {
90         if (!rowIsValid(row) || row + count > int(userData_.size()) ||
91             count < 0)
92                 return false;
93         if (count == 0)
94                 return true;
95         vector<OurData>::iterator it = userData_.begin() + row;
96         beginRemoveRows(QModelIndex(), row, row + count - 1);
97         userData_.erase(it, it + count);        
98         endRemoveRows();
99         return true;
100 }
101
102
103 void GuiIdListModel::insertRow(int const i, QString const & uiString, 
104                 std::string const & idString)
105 {
106         insertRow(i, uiString, idString, uiString);
107 }
108
109
110 void GuiIdListModel::insertRow(int const i, QString const & uiString, 
111         std::string const & idString, QString const & ttString)
112 {
113         insertRows(i, 1);
114         setUIString(i, uiString);
115         setIDString(i, idString);
116         setTTString(i, ttString);
117 }
118
119
120 QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
121 {
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;
127         return qm;
128 }
129
130
131 int GuiIdListModel::findIDString(std::string const & idString) 
132 {
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();
138         return -1;
139 }
140
141 } // namespace frontend
142 } // namespace lyx
143