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