3
#include "MainWindow.h"
4
#include "Command/DocumentCommands.h"
5
#include "Command/FeatureCommands.h"
6
#include "Maps/MapDocument.h"
7
#include "Maps/MapFeature.h"
8
#include "Maps/MapLayer.h"
10
TagModel::TagModel(MainWindow* aMain)
15
TagModel::~TagModel(void)
19
void TagModel::setFeature(const QList<MapFeature*> Features)
21
if (theFeatures.size())
23
beginRemoveRows(QModelIndex(),0,Tags.size());
27
theFeatures = Features;
28
if (theFeatures.size())
30
MapFeature* F = theFeatures[0];
31
for (int i=0; i<F->tagSize(); ++i)
34
for (j=1; j<theFeatures.size(); ++j)
35
if (F->tagValue(i) != theFeatures[j]->tagValue(F->tagKey(i),""))
37
if (j == theFeatures.size())
38
if (!F->tagKey(i).startsWith("%kml:"))
39
Tags.push_back(qMakePair(F->tagKey(i),F->tagValue(i)));
41
std::sort(Tags.begin(), Tags.end());
42
beginInsertRows(QModelIndex(),0,Tags.size());
47
int TagModel::rowCount(const QModelIndex &) const
49
if (!theFeatures.size()) return 0;
53
int TagModel::columnCount(const QModelIndex &) const
58
QVariant TagModel::data(const QModelIndex &index, int role) const
60
if (!theFeatures.size())
64
if (index.row() > Tags.size())
66
if (role == Qt::DisplayRole)
68
if (index.row() >= Tags.size())
70
if (index.column() == 0)
77
if (index.column() == 0)
78
return Tags[index.row()].first;
80
return Tags[index.row()].second;
83
else if (role == Qt::EditRole)
85
if (index.row() >= Tags.size())
89
if (index.column() == 0)
90
return Tags[index.row()].first;
92
return Tags[index.row()].second;
98
QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const
100
if (role != Qt::DisplayRole)
102
if (orientation == Qt::Horizontal)
112
Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
114
if (!index.isValid())
115
return Qt::ItemIsEnabled;
116
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
119
bool TagModel::setData(const QModelIndex &index, const QVariant &value, int role)
121
if (!theFeatures.size()) return false;
122
if (index.isValid() && role == Qt::EditRole)
124
if ((int)index.row() == Tags.size())
126
if (index.column() == 0)
128
beginInsertRows(QModelIndex(), Tags.size()+1, Tags.size()+1);
130
if (theFeatures.size() > 1)
131
L = new CommandList(MainWindow::tr("Set Tags on multiple features"), NULL);
133
L = new CommandList(MainWindow::tr("Set Tags on %1").arg(theFeatures[0]->id()), theFeatures[0]);
134
for (int i=0; i<theFeatures.size(); ++i)
136
if (!theFeatures[i]->isDirty() && !theFeatures[i]->hasOSMId() && theFeatures[i]->isUploadable()) {
137
bool userAdded = !theFeatures[i]->id().startsWith("conflict_");
138
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),theFeatures[i],userAdded));
140
L->add(new SetTagCommand(theFeatures[i],value.toString(),"", Main->document()->getDirtyOrOriginLayer(theFeatures[i]->layer())));
141
theFeatures[i]->setLastUpdated(MapFeature::User);
143
Tags.push_back(qMakePair(value.toString(),QString("")));
144
Main->document()->addHistory(L);
152
QString Original(Tags[index.row()].first);
153
if (index.column() == 0)
154
Tags[index.row()].first = value.toString();
156
Tags[index.row()].second = value.toString();
158
if (theFeatures.size() > 1)
159
L = new CommandList(MainWindow::tr("Set Tags on multiple features"), NULL);
161
L = new CommandList(MainWindow::tr("Set Tags on %1").arg(theFeatures[0]->id()), theFeatures[0]);
162
for (int i=0; i<theFeatures.size(); ++i)
164
int j = theFeatures[i]->findKey(Original);
165
if (j<theFeatures[i]->tagSize()) {
166
if (!theFeatures[i]->isDirty() && !theFeatures[i]->hasOSMId() && theFeatures[i]->isUploadable()) {
167
bool userAdded = !theFeatures[i]->id().startsWith("conflict_");
168
L->add(new AddFeatureCommand(Main->document()->getDirtyOrOriginLayer(),theFeatures[i],userAdded));
170
L->add(new SetTagCommand(theFeatures[i],j , Tags[index.row()].first, Tags[index.row()].second, Main->document()->getDirtyOrOriginLayer(theFeatures[i]->layer())));
172
theFeatures[i]->setLastUpdated(MapFeature::User);
174
Main->document()->addHistory(L);
175
Main->invalidateView(false);
177
emit dataChanged(index, index);