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