-
Notifications
You must be signed in to change notification settings - Fork 0
/
listmodel.cpp
224 lines (178 loc) · 5.41 KB
/
listmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "listmodel.h"
#include <QDebug>
ListModel::ListModel(PlateItem* prototype, QObject *parent) :
QAbstractListModel(parent), m_prototype(prototype)
{
setRoleNames(m_prototype->roleNames());
}
int ListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_list.size();
}
QVariant ListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_list.size())
return QVariant();
return m_list.at(index.row())->data(role);
}
ListModel::~ListModel() {
delete m_prototype;
clear();
}
void ListModel::appendRow(PlateItem *item)
{
appendRows(QList<PlateItem*>() << item);
}
void ListModel::appendRows(const QList<PlateItem *> &items)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
foreach (PlateItem *item, items) {
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.append(item);
}
endInsertRows();
}
void ListModel::insertRow(int row, PlateItem *item)
{
beginInsertRows(QModelIndex(), row, row);
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
m_list.insert(row, item);
endInsertRows();
}
void ListModel::handleItemChange()
{
PlateItem* item = static_cast<PlateItem*>(sender());
QModelIndex index = indexFromItem(item);
if (index.isValid())
emit dataChanged(index, index);
}
PlateItem * ListModel::find(const QString &id) const
{
foreach (PlateItem* item, m_list)
if(item->id() == id) return item;
return 0;
}
int ListModel::checkIfNumbersNow(QString data)
{
if (data.length() > 2 && data.at(2).isDigit())
return 3;
if (data.length() > 3 && data.at(3).isDigit())
return 4;
return -1;
}
void ListModel::searchFor(const QString &value, bool mode)
{
int number = checkIfNumbersNow(value);
int chopAt = -1;
QString newValue = value;
if (number == 3)
chopAt = newValue.length() - 2;
else if (number == 4)
chopAt = newValue.length() - 3;
if (number != -1)
newValue.chop(chopAt);
QList<PlateItem*> cacheList;
ListModel* cache = new ListModel(new PlateItem, this);
if (mode) {
searchModel->clear();
cacheList = QList<PlateItem*>(m_list);
}
else
cacheList = QList<PlateItem*>(searchModel->m_list);
PlateItem *newItem;
bool add = false;
if (number == -1) {
foreach (PlateItem* item, cacheList) {
add = false;
if (settings->getEnableSearchingByDistrict()) {
if (item->powiat().startsWith(newValue, Qt::CaseInsensitive))
add = true;
}
if (!add) {
if (settings->getEnableSearchingByCity()) {
if (item->miasto().startsWith(newValue, Qt::CaseInsensitive))
add = true;
}
}
if (!add) {
if (settings->getEnableSearchingByDistrictB()) {
if (item->wojewodztwo().startsWith(newValue, Qt::CaseInsensitive))
add = true;
}
}
if (add) {
newItem = new PlateItem(item->name(), item->wojewodztwo());
newItem->setMiasto(item->miasto());
newItem->setPowiat(item->powiat());
cache->appendRow(newItem);
}
else if (item->name().startsWith(newValue, Qt::CaseInsensitive)) {
newItem = new PlateItem(item->name(), item->wojewodztwo());
newItem->setMiasto(item->miasto());
newItem->setPowiat(item->powiat());
cache->appendRow(newItem);
}
}
}
else {
foreach (PlateItem* item, cacheList) {
if (QString::compare(item->name(), newValue, Qt::CaseInsensitive) == 0){
newItem = new PlateItem(item->name(), item->wojewodztwo());
newItem->setMiasto(item->miasto());
newItem->setPowiat(item->powiat());
cache->appendRow(newItem);
}
}
}
searchModel->clear();
searchModel->m_list = QList<PlateItem*>(cache->m_list);
if (searchModel->m_list.length() == 0)
emit this->emptyList(true);
else
emit this->emptyList(false);
}
QModelIndex ListModel::indexFromItem(const PlateItem *item) const
{
Q_ASSERT(item);
for(int row=0; row<m_list.size(); ++row)
if(m_list.at(row) == item) return index(row);
return QModelIndex();
}
void ListModel::clear()
{
qDeleteAll(m_list);
m_list.clear();
}
bool ListModel::removeRow(int row, const QModelIndex &parent)
{
Q_UNUSED(parent);
if (row < 0 || row >= m_list.size())
return false;
beginRemoveRows(QModelIndex(), row, row);
delete m_list.takeAt(row);
endRemoveRows();
return true;
}
bool ListModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
if (row < 0 || (row+count) >= m_list.size())
return false;
beginRemoveRows(QModelIndex(), row, row+count-1);
for (int i=0; i<count; ++i)
delete m_list.takeAt(row);
endRemoveRows();
return true;
}
PlateItem * ListModel::takeRow(int row)
{
beginRemoveRows(QModelIndex(), row, row);
PlateItem* item = m_list.takeAt(row);
endRemoveRows();
return item;
}
void ListModel::setSettings(Settings *settings)
{
this->settings = settings;
}