-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathgeometrycollection.cpp
155 lines (128 loc) · 4.15 KB
/
geometrycollection.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
// Copyright 2015, Christopher J. Foster and the other displaz contributors.
// Use of this code is governed by the BSD-style license found in LICENSE.txt
#include "geometrycollection.h"
#include "tinyformat.h"
#include "fileloader.h"
#include <QRegExp>
#include <QThread>
#include <QDebug>
GeometryCollection::GeometryCollection(QObject* parent)
: QAbstractListModel(parent)
{ }
void GeometryCollection::clear()
{
if (m_geometries.size())
{
emit beginRemoveRows(QModelIndex(), 0, (int)m_geometries.size()-1);
m_geometries.clear();
emit endRemoveRows();
}
}
int GeometryCollection::findMatchingRow(const QRegExp & filenameRegex)
{
for (unsigned row = 0; row < m_geometries.size(); row++)
{
if (filenameRegex.exactMatch(m_geometries[row]->label()))
return row;
}
return -1;
}
void GeometryCollection::unloadFiles(const QRegExp & filenameRegex)
{
while (true)
{
int row = findMatchingRow(filenameRegex);
if (row == -1)
break;
this->removeRows(row, 1, QModelIndex());
}
}
QModelIndex GeometryCollection::findLabel(const QRegExp & labelPattern)
{
int row = findMatchingRow(labelPattern);
return (row == -1) ? QModelIndex() : createIndex(row, 0);
}
int GeometryCollection::rowCount(const QModelIndex & parent) const
{
if (parent.isValid())
return 0;
return (int)m_geometries.size();
}
QVariant GeometryCollection::data(const QModelIndex & index, int role) const
{
if (!index.isValid())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
return m_geometries[index.row()]->label();
case Qt::ToolTipRole:
return m_geometries[index.row()]->fileName();
}
return QVariant();
}
Qt::ItemFlags GeometryCollection::flags(const QModelIndex& index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
bool GeometryCollection::setData(const QModelIndex & index, const QVariant & value, int role)
{
return false;
}
bool GeometryCollection::removeRows(int row, int count, const QModelIndex& parent)
{
if (parent.isValid())
return false;
if (row < 0 || row+count > (int)m_geometries.size())
return false;
emit beginRemoveRows(QModelIndex(), row, row+count-1);
m_geometries.erase(m_geometries.begin() + row,
m_geometries.begin() + row + count);
emit endRemoveRows();
return true;
}
void GeometryCollection::addGeometry(std::shared_ptr<Geometry> geom,
bool replaceLabel, bool reloaded)
{
geom->moveToThread(QThread::currentThread());
if (replaceLabel || reloaded)
{
// FIXME: Doing it this way seems a bit nasty. The geometry should
// probably reload itself (or provide a way to create a new clone of
// the geometry object containing the required metadata which is then
// reloaded?)
for (size_t i = 0; i < m_geometries.size(); ++i)
{
if ( (replaceLabel && m_geometries[i]->label() == geom->label()) ||
(reloaded && m_geometries[i]->fileName() == geom->fileName()) )
{
m_geometries[i] = geom;
QModelIndex idx = createIndex((int)i, 0);
emit dataChanged(idx, idx);
return;
}
}
}
emit beginInsertRows(QModelIndex(), (int)m_geometries.size(),
(int)m_geometries.size());
m_geometries.push_back(geom);
emit endInsertRows();
}
void GeometryCollection::mutateGeometry(std::shared_ptr<GeometryMutator> mutator)
{
mutator->moveToThread(QThread::currentThread());
g_logger.info("Attempting to mutate data with label \"%s\"", mutator->label());
for (size_t i = 0; i < m_geometries.size(); ++i)
{
if ( m_geometries[i]->label() == mutator->label() )
{
m_geometries[i]->mutate(mutator);
QModelIndex idx = createIndex((int)i, 0);
emit dataChanged(idx, idx);
return;
}
}
g_logger.error("Didn't match mutation label \"%s\"\n", mutator->label());
}