Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/itemmodels/qstringlistmodel.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||||||||
2 | - | |||||||||||||||||||||||||
3 | - | |||||||||||||||||||||||||
4 | - | |||||||||||||||||||||||||
5 | - | |||||||||||||||||||||||||
6 | - | |||||||||||||||||||||||||
7 | - | |||||||||||||||||||||||||
8 | QStringListModel::QStringListModel(QObject *parent) | - | ||||||||||||||||||||||||
9 | : QAbstractListModel(parent) | - | ||||||||||||||||||||||||
10 | { | - | ||||||||||||||||||||||||
11 | } | - | ||||||||||||||||||||||||
12 | - | |||||||||||||||||||||||||
13 | - | |||||||||||||||||||||||||
14 | - | |||||||||||||||||||||||||
15 | - | |||||||||||||||||||||||||
16 | - | |||||||||||||||||||||||||
17 | - | |||||||||||||||||||||||||
18 | QStringListModel::QStringListModel(const QStringList &strings, QObject *parent) | - | ||||||||||||||||||||||||
19 | : QAbstractListModel(parent), lst(strings) | - | ||||||||||||||||||||||||
20 | { | - | ||||||||||||||||||||||||
21 | } | - | ||||||||||||||||||||||||
22 | int QStringListModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
23 | { | - | ||||||||||||||||||||||||
24 | if (parent.isValid()) | - | ||||||||||||||||||||||||
25 | return 0; | - | ||||||||||||||||||||||||
26 | - | |||||||||||||||||||||||||
27 | return lst.count(); | - | ||||||||||||||||||||||||
28 | } | - | ||||||||||||||||||||||||
29 | - | |||||||||||||||||||||||||
30 | - | |||||||||||||||||||||||||
31 | - | |||||||||||||||||||||||||
32 | - | |||||||||||||||||||||||||
33 | QModelIndex QStringListModel::sibling(int row, int column, const QModelIndex &idx) const | - | ||||||||||||||||||||||||
34 | { | - | ||||||||||||||||||||||||
35 | if (!idx.isValid() || column != 0 || row >= lst.count()) | - | ||||||||||||||||||||||||
36 | return QModelIndex(); | - | ||||||||||||||||||||||||
37 | - | |||||||||||||||||||||||||
38 | return createIndex(row, 0); | - | ||||||||||||||||||||||||
39 | } | - | ||||||||||||||||||||||||
40 | QVariant QStringListModel::data(const QModelIndex &index, int role) const | - | ||||||||||||||||||||||||
41 | { | - | ||||||||||||||||||||||||
42 | if (index.row() < 0 || index.row() >= lst.size()) | - | ||||||||||||||||||||||||
43 | return QVariant(); | - | ||||||||||||||||||||||||
44 | - | |||||||||||||||||||||||||
45 | if (role == Qt::DisplayRole || role == Qt::EditRole) | - | ||||||||||||||||||||||||
46 | return lst.at(index.row()); | - | ||||||||||||||||||||||||
47 | - | |||||||||||||||||||||||||
48 | return QVariant(); | - | ||||||||||||||||||||||||
49 | } | - | ||||||||||||||||||||||||
50 | Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const | - | ||||||||||||||||||||||||
51 | { | - | ||||||||||||||||||||||||
52 | if (!index.isValid()) | - | ||||||||||||||||||||||||
53 | return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled; | - | ||||||||||||||||||||||||
54 | - | |||||||||||||||||||||||||
55 | return QAbstractListModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; | - | ||||||||||||||||||||||||
56 | } | - | ||||||||||||||||||||||||
57 | bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||||||||||||||
58 | { | - | ||||||||||||||||||||||||
59 | if (index.row() >= 0 && index.row() < lst.size() | - | ||||||||||||||||||||||||
60 | && (role == Qt::EditRole || role == Qt::DisplayRole)) { | - | ||||||||||||||||||||||||
61 | lst.replace(index.row(), value.toString()); | - | ||||||||||||||||||||||||
62 | QVector<int> roles; | - | ||||||||||||||||||||||||
63 | roles.reserve(2); | - | ||||||||||||||||||||||||
64 | roles.append(Qt::DisplayRole); | - | ||||||||||||||||||||||||
65 | roles.append(Qt::EditRole); | - | ||||||||||||||||||||||||
66 | dataChanged(index, index, roles); | - | ||||||||||||||||||||||||
67 | - | |||||||||||||||||||||||||
68 | - | |||||||||||||||||||||||||
69 | return true; | - | ||||||||||||||||||||||||
70 | } | - | ||||||||||||||||||||||||
71 | return false; | - | ||||||||||||||||||||||||
72 | } | - | ||||||||||||||||||||||||
73 | bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent) | - | ||||||||||||||||||||||||
74 | { | - | ||||||||||||||||||||||||
75 | if (count < 1 || row < 0 || row > rowCount(parent)) | - | ||||||||||||||||||||||||
76 | return false; | - | ||||||||||||||||||||||||
77 | - | |||||||||||||||||||||||||
78 | beginInsertRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||||||||||||||
79 | - | |||||||||||||||||||||||||
80 | for (int r = 0; r < count; ++r) | - | ||||||||||||||||||||||||
81 | lst.insert(row, QString()); | - | ||||||||||||||||||||||||
82 | - | |||||||||||||||||||||||||
83 | endInsertRows(); | - | ||||||||||||||||||||||||
84 | - | |||||||||||||||||||||||||
85 | return true; | - | ||||||||||||||||||||||||
86 | } | - | ||||||||||||||||||||||||
87 | bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent) | - | ||||||||||||||||||||||||
88 | { | - | ||||||||||||||||||||||||
89 | if (count <= 0
| 6-39 | ||||||||||||||||||||||||
90 | return executed 31 times by 2 tests: false;return false; Executed by:
executed 31 times by 2 tests: return false; Executed by:
| 31 | ||||||||||||||||||||||||
91 | - | |||||||||||||||||||||||||
92 | beginRemoveRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||||||||||||||
93 | - | |||||||||||||||||||||||||
94 | for (int rconst auto it = 0; r < countlst.begin() + row; | - | ||||||||||||||||||||||||
95 | ++r)lst.removeAterase(rowit, it + count); | - | ||||||||||||||||||||||||
96 | - | |||||||||||||||||||||||||
97 | endRemoveRows(); | - | ||||||||||||||||||||||||
98 | - | |||||||||||||||||||||||||
99 | return executed 24 times by 5 tests: true;return true; Executed by:
executed 24 times by 5 tests: return true; Executed by:
| 24 | ||||||||||||||||||||||||
100 | } | - | ||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||
102 | static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - | ||||||||||||||||||||||||
103 | { | - | ||||||||||||||||||||||||
104 | return s1.first < s2.first; | - | ||||||||||||||||||||||||
105 | } | - | ||||||||||||||||||||||||
106 | - | |||||||||||||||||||||||||
107 | static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - | ||||||||||||||||||||||||
108 | { | - | ||||||||||||||||||||||||
109 | return s1.first > s2.first; | - | ||||||||||||||||||||||||
110 | } | - | ||||||||||||||||||||||||
111 | - | |||||||||||||||||||||||||
112 | - | |||||||||||||||||||||||||
113 | - | |||||||||||||||||||||||||
114 | - | |||||||||||||||||||||||||
115 | void QStringListModel::sort(int, Qt::SortOrder order) | - | ||||||||||||||||||||||||
116 | { | - | ||||||||||||||||||||||||
117 | layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - | ||||||||||||||||||||||||
118 | - | |||||||||||||||||||||||||
119 | QListQVector<QPair<QString, int> > list; | - | ||||||||||||||||||||||||
120 | const int lstCount = lst.count(); | - | ||||||||||||||||||||||||
121 | list.reserve(lstCount); | - | ||||||||||||||||||||||||
122 | for (int i = 0; i < lstCount
| 19-347 | ||||||||||||||||||||||||
123 | list.append(QPair<QString, int>(lst.at(i), i)); executed 347 times by 2 tests: list.append(QPair<QString, int>(lst.at(i), i)); Executed by:
| 347 | ||||||||||||||||||||||||
124 | - | |||||||||||||||||||||||||
125 | if (order == Qt::AscendingOrder
| 4-15 | ||||||||||||||||||||||||
126 | std::sort(list.begin(), list.end(), ascendingLessThan); executed 15 times by 2 tests: std::sort(list.begin(), list.end(), ascendingLessThan); Executed by:
| 15 | ||||||||||||||||||||||||
127 | else | - | ||||||||||||||||||||||||
128 | std::sort(list.begin(), list.end(), decendingLessThan); executed 4 times by 1 test: std::sort(list.begin(), list.end(), decendingLessThan); Executed by:
| 4 | ||||||||||||||||||||||||
129 | - | |||||||||||||||||||||||||
130 | lst.clear(); | - | ||||||||||||||||||||||||
131 | QVector<int> forwarding(list.count());lstCount); | - | ||||||||||||||||||||||||
132 | for (int i = 0; i < list.count();lstCount
| 19-347 | ||||||||||||||||||||||||
133 | lst.append(list.at(i).first); | - | ||||||||||||||||||||||||
134 | forwarding[list.at(i).second] = i; | - | ||||||||||||||||||||||||
135 | } executed 347 times by 2 tests: end of block Executed by:
| 347 | ||||||||||||||||||||||||
136 | - | |||||||||||||||||||||||||
137 | QModelIndexList oldList = persistentIndexList(); | - | ||||||||||||||||||||||||
138 | QModelIndexList newList; | - | ||||||||||||||||||||||||
139 | const int numOldIndexes = oldList.count(); | - | ||||||||||||||||||||||||
140 | newList.reserve(numOldIndexes); | - | ||||||||||||||||||||||||
141 | for (int i = 0; i < numOldIndexes
| 0-19 | ||||||||||||||||||||||||
142 | newList.append(index(forwarding.at(oldList.at(i).row()), 0)); never executed: newList.append(index(forwarding.at(oldList.at(i).row()), 0)); | 0 | ||||||||||||||||||||||||
143 | changePersistentIndexList(oldList, newList); | - | ||||||||||||||||||||||||
144 | - | |||||||||||||||||||||||||
145 | layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - | ||||||||||||||||||||||||
146 | } executed 19 times by 2 tests: end of block Executed by:
| 19 | ||||||||||||||||||||||||
147 | - | |||||||||||||||||||||||||
148 | - | |||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | - | |||||||||||||||||||||||||
151 | QStringList QStringListModel::stringList() const | - | ||||||||||||||||||||||||
152 | { | - | ||||||||||||||||||||||||
153 | return lst; | - | ||||||||||||||||||||||||
154 | } | - | ||||||||||||||||||||||||
155 | - | |||||||||||||||||||||||||
156 | - | |||||||||||||||||||||||||
157 | - | |||||||||||||||||||||||||
158 | - | |||||||||||||||||||||||||
159 | - | |||||||||||||||||||||||||
160 | - | |||||||||||||||||||||||||
161 | - | |||||||||||||||||||||||||
162 | void QStringListModel::setStringList(const QStringList &strings) | - | ||||||||||||||||||||||||
163 | { | - | ||||||||||||||||||||||||
164 | beginResetModel(); | - | ||||||||||||||||||||||||
165 | lst = strings; | - | ||||||||||||||||||||||||
166 | endResetModel(); | - | ||||||||||||||||||||||||
167 | } | - | ||||||||||||||||||||||||
168 | - | |||||||||||||||||||||||||
169 | - | |||||||||||||||||||||||||
170 | - | |||||||||||||||||||||||||
171 | - | |||||||||||||||||||||||||
172 | Qt::DropActions QStringListModel::supportedDropActions() const | - | ||||||||||||||||||||||||
173 | { | - | ||||||||||||||||||||||||
174 | return QAbstractItemModel::supportedDropActions() | Qt::MoveAction; | - | ||||||||||||||||||||||||
175 | } | - | ||||||||||||||||||||||||
176 | - | |||||||||||||||||||||||||
177 | - | |||||||||||||||||||||||||
Switch to Source code | Preprocessed file |