1
/***************************************************************************
2
qgspastetransformations.cpp - set up how source fields are transformed to
3
destination fields in copy/paste operations
6
copyright : (C) 2005 by Brendan Morley
7
email : morb at ozemail dot com dot au
8
***************************************************************************/
10
/***************************************************************************
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. *
17
***************************************************************************/
18
/* $Id: qgspastetransformations.cpp 5608 2006-07-17 05:26:48Z g_j_m $ */
26
#include "qgspastetransformations.h"
27
#include "qgsmaplayerregistry.h"
29
QgsPasteTransformations::QgsPasteTransformations()
30
: QgsPasteTransformationsBase()
33
// Populate the dialog with the loaded layers
34
std::map<QString, QgsMapLayer*> mapLayers =
35
QgsMapLayerRegistry::instance()->mapLayers();
37
for (std::map<QString, QgsMapLayer*>::iterator it = mapLayers.begin();
38
it != mapLayers.end();
42
std::cerr << "QgsPasteTransformations::QgsPasteTransformations: QgsMapLayerRegistry has "
43
<< it->second->name().toLocal8Bit().data() << "."
47
// TODO: Test if a VECTOR or DATABASE layer only (not RASTER)
49
sourceLayerComboBox ->insertItem( it->second->name() );
50
destinationLayerComboBox->insertItem( it->second->name() );
52
// store the lookup from the name to the map layer object
53
mMapNameLookup[ it->second->name() ] = it->second;
59
QgsPasteTransformations::~QgsPasteTransformations()
65
void QgsPasteTransformations::accept()
69
QString baseKey = "/Qgis/paste-transformations"; // TODO: promote to static member
70
QString sourceKey = sourceLayerComboBox ->currentText();
71
QString destinationKey = destinationLayerComboBox->currentText();
73
for (int i = 0; i < mSourceTransfers.size(); i++)
76
baseKey + "/" + sourceKey + "/" + destinationKey + "/" +
77
mSourceTransfers[i] ->currentText(),
78
mDestinationTransfers[i]->currentText()
87
void QgsPasteTransformations::addNewTransfer()
89
// This ends up being a wrapper for addTransfer, but with no preselected fields
94
void QgsPasteTransformations::sourceChanged(const QString& layerName)
97
std::cerr << "QgsPasteTransformations::sourceChanged: Source changed to "
98
<< layerName.toLocal8Bit().data() << "."
102
layerChanged(layerName, &mSourceFields);
107
void QgsPasteTransformations::destinationChanged(const QString& layerName)
110
std::cerr << "QgsPasteTransformations::destinationChanged: Destination changed to "
111
<< layerName.toLocal8Bit().data() << "."
115
layerChanged(layerName, &mDestinationFields);
120
void QgsPasteTransformations::addTransfer(const QString& sourceSelectedFieldName,
121
const QString& destinationSelectedFieldName)
124
std::cerr << "QgsPasteTransformations::addTransfer: From " << sourceSelectedFieldName.toLocal8Bit().data()
125
<< " to " << destinationSelectedFieldName.toLocal8Bit().data() << "."
129
int newRow = gridLayout->numRows();
131
// TODO: Do not add the transfer if neither the sourceSelectedFieldName nor the destinationSelectedFieldName could be found.
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() );
139
// Populate source fields
140
for (std::vector<QString>::iterator it = mSourceFields.begin();
141
it != mSourceFields.end();
144
newSourceFields->insertItem( (*it) );
146
// highlight this item if appropriate
147
if (sourceSelectedFieldName == (*it))
149
newSourceFields->setCurrentItem(count);
157
// Populate destination fields
158
for (std::vector<QString>::iterator it = mDestinationFields.begin();
159
it != mDestinationFields.end();
162
newDestinationFields->insertItem( (*it) );
164
// highlight this item if appropriate
165
if (destinationSelectedFieldName == (*it))
167
newDestinationFields->setCurrentItem(count);
173
// Append to dialog layout
174
gridLayout->addWidget(newSourceFields, newRow, 0);
175
gridLayout->addWidget(newDestinationFields, newRow, 1);
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);
182
// Reveal the new sub-widgets
183
newSourceFields->show();
184
newDestinationFields->show();
189
void QgsPasteTransformations::layerChanged(const QString& layerName, std::vector<QString>* fields)
191
// Fetch the fields that will be populated into the Transfer rows.
193
std::cerr << "QgsPasteTransformations::layerChanged: Layer changed to "
194
<< layerName.toLocal8Bit().data() << "."
198
std::vector<QgsField> layerFields =
199
(mMapNameLookup[ layerName ])->fields();
203
for (std::vector<QgsField>::iterator it = layerFields.begin();
204
it != layerFields.end();
208
std::cerr << "QgsPasteTransformations::layerChanged: Got field "
209
<< it->name().toLocal8Bit().data() << "."
213
fields->push_back(it->name());
216
restoreTransfers( sourceLayerComboBox ->currentText(),
217
destinationLayerComboBox->currentText() );
220
void QgsPasteTransformations::restoreTransfers(const QString& sourceLayerName,
221
const QString& destinationLayerName)
224
std::cerr << "QgsPasteTransformations::restoreTransfers: Entered."
228
QString baseKey = "/Qgis/paste-transformations"; // TODO: promote to static member
230
QStringList sourceLayers = settings.subkeyList(baseKey);
232
for (QStringList::Iterator it = sourceLayers.begin();
233
it != sourceLayers.end();
237
std::cerr << "QgsPasteTransformations::restoreTransfers: Testing source '"
238
<< (*it).toLocal8Bit().data() << "' with '"
239
<< sourceLayerName.toLocal8Bit().data() << "'."
242
if ((sourceLayerName == (*it)))
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();
251
std::cerr << "QgsPasteTransformations::restoreTransfers: Testing destination '"
252
<< (*it2).toLocal8Bit().data() << "' with '"
253
<< destinationLayerName.toLocal8Bit().data() << "'."
256
if ((destinationLayerName == (*it2)))
259
std::cerr << "QgsPasteTransformations::restoreTransfers:going through transfers."
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();
269
std::cerr << "QgsPasteTransformations::restoreTransfers: setting transfer for "
270
<< (*it3).toLocal8Bit().data() << "."
273
QString destinationField =
274
settings.readEntry( baseKey + "/" + (*it) + "/" + (*it2) + "/" + (*it3) );
275
addTransfer( (*it3), destinationField );
284
QString QgsPasteTransformations::pasteTo(const QString& sourceLayerName,
285
const QString& destinationLayerName,
286
const QString& sourceFieldName)
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.
293
std::cerr << "QgsPasteTransformations::pasteTo: Entered."
297
QString baseKey = "/Qgis/paste-transformations"; // TODO: promote to static member
299
QString destinationField =
300
settings.readEntry( baseKey + "/" + sourceLayerName + "/" + destinationLayerName + "/" + sourceFieldName );
302
if (QString::null == destinationField)
304
destinationField = sourceFieldName;
308
std::cerr << "QgsPasteTransformations::pasteTo: Returning '" << destinationField.toLocal8Bit().data() << "'."
312
return destinationField;