~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/gui/qgspastetransformations.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
    qgspastetransformations.cpp - set up how source fields are transformed to
3
 
                                  destination fields in copy/paste operations
4
 
                             -------------------
5
 
    begin                : 8 July 2005
6
 
    copyright            : (C) 2005 by Brendan Morley
7
 
    email                : morb at ozemail dot com dot au
8
 
 ***************************************************************************/
9
 
 
10
 
/***************************************************************************
11
 
 *                                                                         *
12
 
 *   This program is free software; you can redistribute it and/or modify  *
13
 
 *   it under the terms of the GNU General Public License as published by  *
14
 
 *   the Free Software Foundation; either version 2 of the License, or     *
15
 
 *   (at your option) any later version.                                   *
16
 
 *                                                                         *
17
 
 ***************************************************************************/
18
 
 /* $Id: qgspastetransformations.cpp 5608 2006-07-17 05:26:48Z g_j_m $ */
19
 
 
20
 
#include <iostream>
21
 
 
22
 
#include <QSettings>
23
 
#include <QComboBox>
24
 
#include <qglobal.h>
25
 
 
26
 
#include "qgspastetransformations.h"
27
 
#include "qgsmaplayerregistry.h"
28
 
 
29
 
QgsPasteTransformations::QgsPasteTransformations()
30
 
  : QgsPasteTransformationsBase()
31
 
{
32
 
 
33
 
  // Populate the dialog with the loaded layers
34
 
  std::map<QString, QgsMapLayer*> mapLayers =
35
 
    QgsMapLayerRegistry::instance()->mapLayers();
36
 
 
37
 
  for (std::map<QString, QgsMapLayer*>::iterator it  = mapLayers.begin();
38
 
                                                 it != mapLayers.end();
39
 
                                               ++it )
40
 
  {
41
 
#ifdef QGISDEBUG
42
 
        std::cerr << "QgsPasteTransformations::QgsPasteTransformations: QgsMapLayerRegistry has "
43
 
          << it->second->name().toLocal8Bit().data() << "."
44
 
          << std::endl;
45
 
#endif
46
 
 
47
 
    // TODO: Test if a VECTOR or DATABASE layer only (not RASTER)
48
 
 
49
 
    sourceLayerComboBox     ->insertItem( it->second->name() );
50
 
    destinationLayerComboBox->insertItem( it->second->name() );
51
 
 
52
 
    // store the lookup from the name to the map layer object
53
 
    mMapNameLookup[ it->second->name() ] = it->second;
54
 
  }
55
 
 
56
 
 
57
 
}
58
 
 
59
 
QgsPasteTransformations::~QgsPasteTransformations()
60
 
{
61
 
 
62
 
}
63
 
 
64
 
 
65
 
void QgsPasteTransformations::accept()
66
 
{
67
 
 
68
 
  QSettings settings;
69
 
  QString baseKey        = "/Qgis/paste-transformations";             // TODO: promote to static member
70
 
  QString sourceKey      = sourceLayerComboBox     ->currentText();
71
 
  QString destinationKey = destinationLayerComboBox->currentText();
72
 
 
73
 
  for (int i = 0; i < mSourceTransfers.size(); i++)
74
 
  {
75
 
    settings.writeEntry(
76
 
                        baseKey + "/" + sourceKey + "/" + destinationKey + "/" +
77
 
                        mSourceTransfers[i]     ->currentText(),
78
 
                        mDestinationTransfers[i]->currentText()
79
 
                       );
80
 
  }
81
 
 
82
 
  QDialog::accept();
83
 
 
84
 
}
85
 
 
86
 
 
87
 
void QgsPasteTransformations::addNewTransfer()
88
 
{
89
 
  // This ends up being a wrapper for addTransfer, but with no preselected fields
90
 
  addTransfer();
91
 
}
92
 
 
93
 
 
94
 
void QgsPasteTransformations::sourceChanged(const QString& layerName)
95
 
{
96
 
#ifdef QGISDEBUG
97
 
        std::cerr << "QgsPasteTransformations::sourceChanged: Source changed to "
98
 
          << layerName.toLocal8Bit().data() << "."
99
 
          << std::endl;
100
 
#endif
101
 
 
102
 
  layerChanged(layerName, &mSourceFields);
103
 
 
104
 
}
105
 
 
106
 
 
107
 
void QgsPasteTransformations::destinationChanged(const QString& layerName)
108
 
{
109
 
#ifdef QGISDEBUG
110
 
        std::cerr << "QgsPasteTransformations::destinationChanged: Destination changed to "
111
 
          << layerName.toLocal8Bit().data() << "."
112
 
          << std::endl;
113
 
#endif
114
 
 
115
 
  layerChanged(layerName, &mDestinationFields);
116
 
 
117
 
}
118
 
 
119
 
 
120
 
void QgsPasteTransformations::addTransfer(const QString& sourceSelectedFieldName,
121
 
                                          const QString& destinationSelectedFieldName)
122
 
{
123
 
#ifdef QGISDEBUG
124
 
        std::cerr << "QgsPasteTransformations::addTransfer: From " << sourceSelectedFieldName.toLocal8Bit().data()
125
 
                                                         << " to " << destinationSelectedFieldName.toLocal8Bit().data() << "."
126
 
          << std::endl;
127
 
#endif
128
 
 
129
 
  int newRow = gridLayout->numRows();
130
 
 
131
 
// TODO: Do not add the transfer if neither the sourceSelectedFieldName nor the destinationSelectedFieldName could be found.
132
 
 
133
 
  // For some reason Qt4's uic3 only outputs generic names for layout items
134
 
  QComboBox* newSourceFields      = new QComboBox(FALSE, gridLayout->mainWidget() );
135
 
  QComboBox* newDestinationFields = new QComboBox(FALSE, gridLayout->mainWidget() );
136
 
 
137
 
  int count = 0;
138
 
 
139
 
  // Populate source fields
140
 
  for (std::vector<QString>::iterator it  = mSourceFields.begin();
141
 
                                      it != mSourceFields.end();
142
 
                                    ++it )
143
 
  {
144
 
    newSourceFields->insertItem( (*it) );
145
 
 
146
 
    // highlight this item if appropriate
147
 
    if (sourceSelectedFieldName == (*it))
148
 
    {
149
 
      newSourceFields->setCurrentItem(count);
150
 
    }
151
 
 
152
 
    count++;
153
 
  }
154
 
 
155
 
  count = 0;
156
 
 
157
 
  // Populate destination fields
158
 
  for (std::vector<QString>::iterator it  = mDestinationFields.begin();
159
 
                                      it != mDestinationFields.end();
160
 
                                    ++it )
161
 
  {
162
 
    newDestinationFields->insertItem( (*it) );
163
 
 
164
 
    // highlight this item if appropriate
165
 
    if (destinationSelectedFieldName == (*it))
166
 
    {
167
 
      newDestinationFields->setCurrentItem(count);
168
 
    }
169
 
 
170
 
    count++;
171
 
  }
172
 
 
173
 
  // Append to dialog layout
174
 
  gridLayout->addWidget(newSourceFields,      newRow, 0);
175
 
  gridLayout->addWidget(newDestinationFields, newRow, 1);
176
 
 
177
 
  // Keep a reference to them so that we can read from them
178
 
  // when the dialog is dismissed
179
 
  mSourceTransfers     .push_back(newSourceFields);
180
 
  mDestinationTransfers.push_back(newDestinationFields);
181
 
 
182
 
  // Reveal the new sub-widgets
183
 
  newSourceFields->show();
184
 
  newDestinationFields->show();
185
 
 
186
 
}
187
 
 
188
 
 
189
 
void QgsPasteTransformations::layerChanged(const QString& layerName, std::vector<QString>* fields)
190
 
{
191
 
  // Fetch the fields that will be populated into the Transfer rows.
192
 
#ifdef QGISDEBUG
193
 
        std::cerr << "QgsPasteTransformations::layerChanged: Layer changed to "
194
 
          << layerName.toLocal8Bit().data() << "."
195
 
          << std::endl;
196
 
#endif
197
 
 
198
 
  std::vector<QgsField> layerFields = 
199
 
    (mMapNameLookup[ layerName ])->fields();
200
 
 
201
 
  fields->clear();
202
 
 
203
 
  for (std::vector<QgsField>::iterator it  = layerFields.begin();
204
 
                                       it != layerFields.end();
205
 
                                     ++it )
206
 
  {
207
 
#ifdef QGISDEBUG
208
 
        std::cerr << "QgsPasteTransformations::layerChanged: Got field "
209
 
          << it->name().toLocal8Bit().data() << "."
210
 
          << std::endl;
211
 
#endif
212
 
 
213
 
    fields->push_back(it->name());
214
 
  }
215
 
 
216
 
  restoreTransfers( sourceLayerComboBox     ->currentText(),
217
 
                    destinationLayerComboBox->currentText() );
218
 
}
219
 
 
220
 
void QgsPasteTransformations::restoreTransfers(const QString& sourceLayerName,
221
 
                                               const QString& destinationLayerName)
222
 
{
223
 
#ifdef QGISDEBUG
224
 
        std::cerr << "QgsPasteTransformations::restoreTransfers: Entered."
225
 
          << std::endl;
226
 
#endif
227
 
  QSettings settings;
228
 
  QString baseKey = "/Qgis/paste-transformations";             // TODO: promote to static member
229
 
 
230
 
  QStringList sourceLayers = settings.subkeyList(baseKey);
231
 
 
232
 
  for (QStringList::Iterator it  = sourceLayers.begin(); 
233
 
                             it != sourceLayers.end();
234
 
                           ++it )
235
 
  {
236
 
#ifdef QGISDEBUG
237
 
        std::cerr << "QgsPasteTransformations::restoreTransfers: Testing source '"
238
 
          << (*it).toLocal8Bit().data() << "' with '"
239
 
          << sourceLayerName.toLocal8Bit().data() << "'."
240
 
          << std::endl;
241
 
#endif
242
 
    if ((sourceLayerName == (*it)))
243
 
    {
244
 
      // Go through destination layers defined for this source layer.
245
 
      QStringList destinationLayers = settings.subkeyList( baseKey + "/" + (*it) );
246
 
      for (QStringList::Iterator it2  = destinationLayers.begin(); 
247
 
                                 it2 != destinationLayers.end();
248
 
                               ++it2 )
249
 
      {
250
 
#ifdef QGISDEBUG
251
 
        std::cerr << "QgsPasteTransformations::restoreTransfers: Testing destination '"
252
 
          << (*it2).toLocal8Bit().data() << "' with '"
253
 
          << destinationLayerName.toLocal8Bit().data() << "'."
254
 
          << std::endl;
255
 
#endif
256
 
        if ((destinationLayerName == (*it2)))
257
 
        {
258
 
#ifdef QGISDEBUG
259
 
        std::cerr << "QgsPasteTransformations::restoreTransfers:going through transfers."
260
 
          << std::endl;
261
 
#endif
262
 
          // Go through Transfers for this source/destination layer pair.
263
 
          QStringList transfers = settings.entryList( baseKey + "/" + (*it) + "/" + (*it2) );
264
 
          for (QStringList::Iterator it3  = transfers.begin(); 
265
 
                                     it3 != transfers.end();
266
 
                                   ++it3 )
267
 
          {
268
 
#ifdef QGISDEBUG
269
 
        std::cerr << "QgsPasteTransformations::restoreTransfers: setting transfer for "
270
 
          << (*it3).toLocal8Bit().data() << "."
271
 
          << std::endl;
272
 
#endif
273
 
            QString destinationField = 
274
 
              settings.readEntry( baseKey + "/" + (*it) + "/" + (*it2) + "/" + (*it3) );
275
 
            addTransfer( (*it3), destinationField );
276
 
          }
277
 
        }
278
 
      }
279
 
    }
280
 
  }
281
 
}
282
 
 
283
 
 
284
 
QString QgsPasteTransformations::pasteTo(const QString& sourceLayerName,
285
 
                                         const QString& destinationLayerName,
286
 
                                         const QString& sourceFieldName)
287
 
{
288
 
 
289
 
// TODO: Adjust QgsVectorLayer::addFeature to complete the usefulness of this function
290
 
// TODO: Cache previous results as this will be called once per pasted feature.
291
 
 
292
 
#ifdef QGISDEBUG
293
 
        std::cerr << "QgsPasteTransformations::pasteTo: Entered."
294
 
          << std::endl;
295
 
#endif
296
 
  QSettings settings;
297
 
  QString baseKey = "/Qgis/paste-transformations";             // TODO: promote to static member
298
 
 
299
 
  QString destinationField = 
300
 
    settings.readEntry( baseKey + "/" + sourceLayerName + "/" + destinationLayerName + "/" + sourceFieldName );
301
 
 
302
 
  if (QString::null == destinationField)
303
 
  {
304
 
    destinationField = sourceFieldName;
305
 
  }
306
 
 
307
 
#ifdef QGISDEBUG
308
 
        std::cerr << "QgsPasteTransformations::pasteTo: Returning '" << destinationField.toLocal8Bit().data() << "'."
309
 
          << std::endl;
310
 
#endif
311
 
 
312
 
  return destinationField;
313
 
}
314