itemmodels/qstringlistmodel.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8QStringListModel::QStringListModel(QObject *parent) -
9 : QAbstractListModel(parent) -
10{ -
11}
-
12 -
13 -
14 -
15 -
16 -
17 -
18QStringListModel::QStringListModel(const QStringList &strings, QObject *parent) -
19 : QAbstractListModel(parent), lst(strings) -
20{ -
21}
-
22int QStringListModel::rowCount(const QModelIndex &parent) const -
23{ -
24 if (parent.isValid())
-
25 return 0;
-
26 -
27 return lst.count();
-
28} -
29 -
30 -
31 -
32 -
33QModelIndex 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} -
40QVariant 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} -
50Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const -
51{ -
52 if (!index.isValid())
-
53 return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
-
54 -
55 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
-
56} -
57bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role) -
58{ -
59 if (index.row() >= 0 && index.row() < lst.size()
evaluated: index.row() >= 0
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:35
partially evaluated: index.row() < lst.size()
TRUEFALSE
yes
Evaluation Count:29
no
Evaluation Count:0
0-35
60 && (role == Qt::EditRole || role == Qt::DisplayRole)) {
partially evaluated: role == Qt::EditRole
TRUEFALSE
yes
Evaluation Count:29
no
Evaluation Count:0
never evaluated: role == Qt::DisplayRole
0-29
61 lst.replace(index.row(), value.toString()); -
62 dataChanged(index, index, QVector<int>() << role); -
63 return true;
executed: return true;
Execution Count:29
29
64 } -
65 return false;
executed: return false;
Execution Count:35
35
66} -
67bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent) -
68{ -
69 if (count < 1 || row < 0 || row > rowCount(parent))
-
70 return false;
-
71 -
72 beginInsertRows(QModelIndex(), row, row + count - 1); -
73 -
74 for (int r = 0; r < count; ++r)
-
75 lst.insert(row, QString());
-
76 -
77 endInsertRows(); -
78 -
79 return true;
-
80} -
81bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent) -
82{ -
83 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
-
84 return false;
-
85 -
86 beginRemoveRows(QModelIndex(), row, row + count - 1); -
87 -
88 for (int r = 0; r < count; ++r)
-
89 lst.removeAt(row);
-
90 -
91 endRemoveRows(); -
92 -
93 return true;
-
94} -
95 -
96static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) -
97{ -
98 return s1.first < s2.first;
-
99} -
100 -
101static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) -
102{ -
103 return s1.first > s2.first;
-
104} -
105 -
106 -
107 -
108 -
109void QStringListModel::sort(int, Qt::SortOrder order) -
110{ -
111 layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint); -
112 -
113 QList<QPair<QString, int> > list; -
114 for (int i = 0; i < lst.count(); ++i)
-
115 list.append(QPair<QString, int>(lst.at(i), i));
-
116 -
117 if (order == Qt::AscendingOrder)
-
118 std::sort(list.begin(), list.end(), ascendingLessThan);
-
119 else -
120 std::sort(list.begin(), list.end(), decendingLessThan);
-
121 -
122 lst.clear(); -
123 QVector<int> forwarding(list.count()); -
124 for (int i = 0; i < list.count(); ++i) {
-
125 lst.append(list.at(i).first); -
126 forwarding[list.at(i).second] = i; -
127 }
-
128 -
129 QModelIndexList oldList = persistentIndexList(); -
130 QModelIndexList newList; -
131 for (int i = 0; i < oldList.count(); ++i)
-
132 newList.append(index(forwarding.at(oldList.at(i).row()), 0));
-
133 changePersistentIndexList(oldList, newList); -
134 -
135 layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint); -
136}
-
137 -
138 -
139 -
140 -
141QStringList QStringListModel::stringList() const -
142{ -
143 return lst;
-
144} -
145 -
146 -
147 -
148 -
149 -
150 -
151 -
152void QStringListModel::setStringList(const QStringList &strings) -
153{ -
154 beginResetModel(); -
155 lst = strings; -
156 endResetModel(); -
157}
-
158 -
159 -
160 -
161 -
162Qt::DropActions QStringListModel::supportedDropActions() const -
163{ -
164 return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
-
165} -
166 -
167 -
168 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial