Skip to content

Commit 4f65b2b

Browse files
author
gjm
committed
Actions now provide access to the coordinates for points and the
start/end points for linestrings. Also includes some raionalisation of, and subtle bug fies for, the action code. git-svn-id: http://svn.osgeo.org/qgis/trunk@9031 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 4040ddd commit 4f65b2b

File tree

3 files changed

+79
-40
lines changed

3 files changed

+79
-40
lines changed

src/app/qgsidentifyresults.cpp

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
5454

5555
connect( lstResults, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
5656
this, SLOT(handleCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
57+
58+
// The label to use for the Derived node in the identify results
59+
mDerivedLabel = tr("(Derived)");
5760
}
5861

5962
QgsIdentifyResults::~QgsIdentifyResults()
@@ -121,30 +124,7 @@ void QgsIdentifyResults::contextMenuEvent(QContextMenuEvent* event)
121124
}
122125
// Save the attribute values as these are needed for substituting into
123126
// the action.
124-
// A little bit complicated because the user could of right-clicked
125-
// on a parent or a child in the dialog box. We also want to keep
126-
// track of which row in the identify results table was actually
127-
// clicked on. This is stored as an index into the mValues vector.
128-
129-
QTreeWidgetItem* parent = item->parent();
130-
if (parent == 0)
131-
parent = item;
132-
133-
mValues.clear();
134-
for (int j = 0; j < parent->childCount(); ++j)
135-
{
136-
if ( parent->child(j)->text(0) != "action" ) {
137-
mValues.push_back(std::make_pair(parent->child(j)->text(0),
138-
parent->child(j)->text(1)));
139-
// Need to do the comparison on the text strings rather than the
140-
// pointers because if the user clicked on the parent, we need
141-
// to pick up which child that actually is (the parent in the
142-
// identify results dialog box is just one of the children
143-
// that has been chosen by some method).
144-
if (parent->child(j)->text(0) == item->text(0))
145-
mClickedOnValue = j;
146-
}
147-
}
127+
extractAllItemData(item);
148128

149129
if (mActions.size() > 0)
150130
mActionPopup->popup(event->globalPos());
@@ -194,7 +174,7 @@ void QgsIdentifyResults::addDerivedAttribute(QTreeWidgetItem * fnode, QString fi
194174
else
195175
{
196176
// Create new derived-attribute root node
197-
daRootNode = new QTreeWidgetItem(fnode, QStringList(tr("(Derived)")));
177+
daRootNode = new QTreeWidgetItem(fnode, QStringList(mDerivedLabel));
198178
QFont font = daRootNode->font(0);
199179
font.setItalic(true);
200180
daRootNode->setFont(0, font);
@@ -277,21 +257,7 @@ void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
277257

278258
int id = item->text(3).toInt();
279259

280-
QTreeWidgetItem* parent = item->parent();
281-
if (parent == 0)
282-
parent = item;
283-
284-
mValues.clear();
285-
286-
for (int j = 0; j < parent->childCount(); ++j)
287-
{
288-
if ( parent->child(j)->text(0) != "action" ) {
289-
mValues.push_back(std::make_pair(parent->child(j)->text(0),
290-
parent->child(j)->text(1)));
291-
if (parent->child(j)->text(0) == item->text(0))
292-
mClickedOnValue = j;
293-
}
294-
}
260+
extractAllItemData(item);
295261

296262
mActions.doAction(id, mValues, mClickedOnValue);
297263
}
@@ -337,3 +303,53 @@ void QgsIdentifyResults::handleCurrentItemChanged(QTreeWidgetItem* current, QTre
337303
mCurrentFeatureId = fid2;
338304
emit selectedFeatureChanged(mCurrentFeatureId);
339305
}
306+
307+
void QgsIdentifyResults::extractAllItemData(QTreeWidgetItem* item)
308+
{
309+
// Extracts the name/value pairs from the given item. This includes data
310+
// under the (Derived) item.
311+
312+
// A little bit complicated because the user could of right-clicked
313+
// on any item in the dialog box. We want a toplevel item, so walk upwards
314+
// as far as possible.
315+
// We also want to keep track of which row in the identify results table was
316+
// actually clicked on. This is stored as an index into the mValues vector.
317+
318+
QTreeWidgetItem* child = item;
319+
QTreeWidgetItem* parent = child->parent();
320+
while (parent != 0)
321+
{
322+
child = parent;
323+
parent = parent->parent();
324+
}
325+
parent = child;
326+
327+
mValues.clear();
328+
329+
// For the code below we
330+
// need to do the comparison on the text strings rather than the
331+
// pointers because if the user clicked on the parent, we need
332+
// to pick up which child that actually is (the parent in the
333+
// identify results dialog box is just one of the children
334+
// that has been chosen by some method).
335+
336+
for (int j = 0; j < parent->childCount(); ++j)
337+
{
338+
if ( parent->child(j)->text(0) != "action" ) {
339+
// For derived attributes, build up a virtual name
340+
if (parent->child(j)->text(0) == mDerivedLabel ) {
341+
for (int k = 0; k < parent->child(j)->childCount(); ++k)
342+
mValues.push_back(
343+
std::make_pair(mDerivedLabel + "."
344+
+ parent->child(j)->child(k)->text(0),
345+
parent->child(j)->child(k)->text(1)));
346+
}
347+
else // do the actual feature attributes
348+
mValues.push_back(std::make_pair(parent->child(j)->text(0),
349+
parent->child(j)->text(1)));
350+
351+
if (parent->child(j)->text(0) == item->text(0))
352+
mClickedOnValue = j;
353+
}
354+
}
355+
}

src/app/qgsidentifyresults.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
116116
std::vector<std::pair<QString, QString> > mValues;
117117
static const int context_id = 689216579;
118118
int mCurrentFeatureId;
119+
QString mDerivedLabel;
119120

120121
/**
121122
Keeps track of what derived-attribute (e.g. Length, Area)
@@ -125,6 +126,10 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
125126
Second item: Derived-attribute root node for that feature
126127
*/
127128
std::map<QTreeWidgetItem *, QTreeWidgetItem *> mDerivedAttributeRootNodes;
129+
130+
// Convenience function to populate mValues with all of the item names and
131+
// values for a item, including the derived ones.
132+
void extractAllItemData(QTreeWidgetItem* item);
128133
};
129134

130135
#endif

src/app/qgsmaptoolidentify.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,31 @@ void QgsMapToolIdentify::identifyVectorLayer(QgsVectorLayer* layer, const QgsPoi
314314
double dist = calc.measure(f_it->geometry());
315315
QString str = calc.textUnit(dist, 3, mCanvas->mapUnits(), false);
316316
mResults->addDerivedAttribute(featureNode, QObject::tr("Length"), str);
317+
// Add the start and end points in as derived attributes
318+
str.setNum(f_it->geometry()->asPolyline().first().x(), 'g', 10);
319+
mResults->addDerivedAttribute(featureNode, "startX", str);
320+
str.setNum(f_it->geometry()->asPolyline().first().y(), 'g', 10);
321+
mResults->addDerivedAttribute(featureNode, "startY", str);
322+
str.setNum(f_it->geometry()->asPolyline().last().x(), 'g', 10);
323+
mResults->addDerivedAttribute(featureNode, "endX", str);
324+
str.setNum(f_it->geometry()->asPolyline().last().y(), 'g', 10);
325+
mResults->addDerivedAttribute(featureNode, "endY", str);
317326
}
318327
else if (layer->vectorType() == QGis::Polygon)
319328
{
320329
double area = calc.measure(f_it->geometry());
321330
QString str = calc.textUnit(area, 3, mCanvas->mapUnits(), true);
322331
mResults->addDerivedAttribute(featureNode, QObject::tr("Area"), str);
323332
}
333+
else if (layer->vectorType() == QGis::Point)
334+
{
335+
// Include the x and y coordinates of the point as a derived attribute
336+
QString str;
337+
str.setNum(f_it->geometry()->asPoint().x(), 'g', 10);
338+
mResults->addDerivedAttribute(featureNode, "X", str);
339+
str.setNum(f_it->geometry()->asPoint().y(), 'g', 10);
340+
mResults->addDerivedAttribute(featureNode, "Y", str);
341+
}
324342

325343
// Add actions
326344
QgsAttributeAction::aIter iter = actions.begin();

0 commit comments

Comments
 (0)