]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiIdListModel.cpp
fix warning.
[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 using std::vector;
20
21 namespace lyx {
22 namespace frontend {
23
24 QVariant GuiIdListModel::data(QModelIndex const & index, int role) const
25 {
26         int const row = index.row();
27         if (!rowIsValid(row))
28                 return QVariant();
29         if (role == Qt::DisplayRole || role == Qt::EditRole)
30                 return userData_[row].uiString;
31         if (role == Qt::UserRole)
32                 return userData_[row].idString;
33         return QVariant();
34 }
35
36
37 bool GuiIdListModel::setData (QModelIndex const & index, 
38                 const QVariant & value, int role)
39 {
40         int const row = index.row();
41         if (!rowIsValid(row))
42                 return false;
43         if (role == Qt::DisplayRole || role == Qt::EditRole) {
44                 userData_[row].uiString = value;
45                 dataChanged(index, index);
46                 return true;
47         }
48         if (role == Qt::UserRole) {
49                 userData_[row].idString = value;
50                 dataChanged(index, index);
51                 return true;
52         }
53         return false;
54 }
55
56
57 bool GuiIdListModel::insertRows(int row, int count, 
58                 QModelIndex const & /*parent*/)
59 {
60         if (!rowIsValid(row))
61                 return false;
62         vector<OurData>::iterator it = userData_.begin() + row;
63         OurData const v;
64         beginInsertRows(QModelIndex(), row, row + count - 1);
65         userData_.insert(it, count, v);
66         endInsertRows();
67         return true;
68 }
69
70
71 bool GuiIdListModel::removeRows(int row, int count, 
72                 QModelIndex const & /*parent*/)
73 {
74         if (!rowIsValid(row) || row + count > int(userData_.size()) ||
75             count < 0)
76                 return false;
77         if (count == 0)
78                 return true;
79         vector<OurData>::iterator it = userData_.begin() + row;
80         beginRemoveRows(QModelIndex(), row, row + count - 1);
81         userData_.erase(it, it + count);        
82         endRemoveRows();
83         return true;
84 }
85
86
87 void GuiIdListModel::insertRow(int const i, std::string const & uiString, 
88                 std::string const & idString)
89 {
90         insertRows(i, 1);
91         setUIString(i, uiString);
92         setIDString(i, idString);
93 }
94
95
96 QMap<int, QVariant> 
97         GuiIdListModel::itemData(QModelIndex const & index ) const 
98 {
99         int const row = index.row();
100         if (!rowIsValid(row))
101                 return QMap<int, QVariant>();
102         QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
103         qm[Qt::UserRole] = userData_[row].idString;
104         return qm;
105 }
106
107 /* The following functions are currently unused but are retained here in
108    case they should at some point be useful.
109    
110 QStringList GuiIdListModel::getIDStringList() const {
111         QStringList qsl;
112         vector<OurData>::const_iterator it  = userData_.begin();
113         vector<OurData>::const_iterator end = userData_.end();
114         for (; it != end; ++it)
115                 qsl.append(it->idString.toString());
116         return qsl;
117 }
118
119
120 void GuiIdListModel::insertRow(int const i, QString const & uiString, 
121                 QString const & idString)
122 {
123         insertRows(i, 1);
124         setUIString(i, uiString);
125         setIDString(i, idString);
126 }
127
128 bool GuiIdListModel::containsID(QVariant const & q) const
129 {
130         vector<OurData>::const_iterator it  = userData_.begin();
131         vector<OurData>::const_iterator end = userData_.end();
132         for (; it != end; ++it)
133                 if (it->idString == q)
134                         return true;
135         return false;
136 }
137 */
138
139 }
140 }
141