~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to src/TagModel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <algorithm>
 
2
#include "TagModel.h"
 
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"
 
9
 
 
10
TagModel::TagModel(MainWindow* aMain)
 
11
: Main(aMain)
 
12
{
 
13
}
 
14
 
 
15
TagModel::~TagModel(void)
 
16
{
 
17
}
 
18
 
 
19
void TagModel::setFeature(const QList<MapFeature*> Features)
 
20
{
 
21
        if (theFeatures.size())
 
22
        {
 
23
                beginRemoveRows(QModelIndex(),0,Tags.size());
 
24
                Tags.clear();
 
25
                endRemoveRows();
 
26
        }
 
27
        theFeatures = Features;
 
28
        if (theFeatures.size())
 
29
        {
 
30
                MapFeature* F = theFeatures[0];
 
31
                for (int i=0; i<F->tagSize(); ++i)
 
32
                {
 
33
                        int j=0;
 
34
                        for (j=1; j<theFeatures.size(); ++j)
 
35
                                if (F->tagValue(i) != theFeatures[j]->tagValue(F->tagKey(i),""))
 
36
                                        break;
 
37
                        if (j == theFeatures.size())
 
38
                                if (!F->tagKey(i).startsWith("%kml:"))
 
39
                                        Tags.push_back(qMakePair(F->tagKey(i),F->tagValue(i)));
 
40
                }
 
41
                std::sort(Tags.begin(), Tags.end());
 
42
                beginInsertRows(QModelIndex(),0,Tags.size());
 
43
                endInsertRows();
 
44
        }
 
45
}
 
46
 
 
47
int TagModel::rowCount(const QModelIndex &) const
 
48
{
 
49
        if (!theFeatures.size()) return 0;
 
50
        return Tags.size()+1;
 
51
}
 
52
 
 
53
int TagModel::columnCount(const QModelIndex &) const
 
54
{
 
55
        return 2;
 
56
}
 
57
 
 
58
QVariant TagModel::data(const QModelIndex &index, int role) const
 
59
{
 
60
        if (!theFeatures.size())
 
61
                return QVariant();
 
62
        if (!index.isValid())
 
63
                return QVariant();
 
64
        if (index.row() > Tags.size())
 
65
                return QVariant();
 
66
        if (role == Qt::DisplayRole)
 
67
        {
 
68
                if (index.row() >= Tags.size())
 
69
                {
 
70
                        if (index.column() == 0)
 
71
                                return newKeyText();
 
72
                        else
 
73
                                return "";
 
74
                }
 
75
                else
 
76
                {
 
77
                        if (index.column() == 0)
 
78
                                return Tags[index.row()].first;
 
79
                        else
 
80
                                return Tags[index.row()].second;
 
81
                }
 
82
        }
 
83
        else if (role == Qt::EditRole)
 
84
        {
 
85
                if (index.row() >= Tags.size())
 
86
                        return "";
 
87
                else
 
88
                {
 
89
                        if (index.column() == 0)
 
90
                                return Tags[index.row()].first;
 
91
                        else
 
92
                                return Tags[index.row()].second;
 
93
                }
 
94
        }
 
95
        return QVariant();
 
96
}
 
97
 
 
98
QVariant TagModel::headerData(int section, Qt::Orientation orientation, int role) const
 
99
{
 
100
        if (role != Qt::DisplayRole)
 
101
                return QVariant();
 
102
        if (orientation == Qt::Horizontal)
 
103
        {
 
104
                if (section == 0)
 
105
                        return tr("Key");
 
106
                else
 
107
                        return tr("Value");
 
108
        }
 
109
        return QVariant();
 
110
}
 
111
 
 
112
Qt::ItemFlags TagModel::flags(const QModelIndex &index) const
 
113
{
 
114
        if (!index.isValid())
 
115
                return Qt::ItemIsEnabled;
 
116
        return QAbstractTableModel::flags(index) | Qt::ItemIsEditable  | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
117
}
 
118
 
 
119
bool TagModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
120
{
 
121
        if (!theFeatures.size()) return false;
 
122
        if (index.isValid() && role == Qt::EditRole)
 
123
        {
 
124
                if ((int)index.row() == Tags.size())
 
125
                {
 
126
                        if (index.column() == 0)
 
127
                        {
 
128
                                beginInsertRows(QModelIndex(), Tags.size()+1, Tags.size()+1);
 
129
                                CommandList* L;
 
130
                                if (theFeatures.size() > 1) 
 
131
                                        L = new CommandList(MainWindow::tr("Set Tags on multiple features"), NULL);
 
132
                                else
 
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)
 
135
                                {
 
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));
 
139
                                        }
 
140
                                        L->add(new SetTagCommand(theFeatures[i],value.toString(),"", Main->document()->getDirtyOrOriginLayer(theFeatures[i]->layer())));
 
141
                                        theFeatures[i]->setLastUpdated(MapFeature::User);
 
142
                                }
 
143
                                Tags.push_back(qMakePair(value.toString(),QString("")));
 
144
                                Main->document()->addHistory(L);
 
145
                                endInsertRows();
 
146
                        }
 
147
                        else
 
148
                                return false;
 
149
                }
 
150
                else
 
151
                {
 
152
                        QString Original(Tags[index.row()].first);
 
153
                        if (index.column() == 0)
 
154
                                Tags[index.row()].first = value.toString();
 
155
                        else
 
156
                                Tags[index.row()].second = value.toString();
 
157
                                CommandList* L;
 
158
                                if (theFeatures.size() > 1) 
 
159
                                        L = new CommandList(MainWindow::tr("Set Tags on multiple features"), NULL);
 
160
                                else
 
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)
 
163
                        {
 
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));
 
169
                                        }
 
170
                                        L->add(new SetTagCommand(theFeatures[i],j , Tags[index.row()].first, Tags[index.row()].second, Main->document()->getDirtyOrOriginLayer(theFeatures[i]->layer())));
 
171
                                }
 
172
                                theFeatures[i]->setLastUpdated(MapFeature::User);
 
173
                        }
 
174
                        Main->document()->addHistory(L);
 
175
                        Main->invalidateView(false);
 
176
                }
 
177
                emit dataChanged(index, index);
 
178
                return true;
 
179
        }
 
180
        return false;
 
181
}