1
/***************************************************************************
2
qgsidentifyresults.cpp - description
4
begin : Fri Oct 25 2002
5
copyright : (C) 2002 by Gary E.Sherman
6
email : sherman at mrcc dot com
7
Romans 3:23=>Romans 6:23=>Romans 5:8=>Romans 10:9,10=>Romans 12
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: qgsidentifyresults.cpp 6255 2006-12-15 09:21:43Z g_j_m $ */
20
#include "qgsidentifyresults.h"
21
#include "qgscontexthelp.h"
22
#include "qgsapplication.h"
24
#include <QCloseEvent>
27
#include <QTreeWidgetItem>
34
QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
35
QWidget *parent, Qt::WFlags f)
42
lstResults->setColumnCount(2);
43
setColumnText(0, tr("Feature"));
44
setColumnText(1, tr("Value"));
46
connect( buttonCancel, SIGNAL(clicked()),
47
this, SLOT(close()) );
48
connect( lstResults, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
49
this, SLOT(clicked(QTreeWidgetItem *)) );
50
connect( lstResults, SIGNAL(itemExpanded(QTreeWidgetItem*)),
51
this, SLOT(itemExpanded(QTreeWidgetItem*)));
54
QgsIdentifyResults::~QgsIdentifyResults()
59
// Call to show the dialog box.
60
void QgsIdentifyResults::show()
62
// Enfore a few things before showing the dialog box
63
lstResults->sortItems(0, Qt::Ascending);
68
// Slot called when user clicks the Close button
69
// (saves the current window size/position)
70
void QgsIdentifyResults::close()
75
// Save the current window size/position before closing
76
// from window menu or X in titlebar
77
void QgsIdentifyResults::closeEvent(QCloseEvent *e)
79
// We'll close in our own good time thanks...
84
// Popup (create if necessary) a context menu that contains a list of
85
// actions that can be applied to the data in the identify results
88
void QgsIdentifyResults::contextMenuEvent(QContextMenuEvent* event)
90
QTreeWidgetItem* item = lstResults->itemAt(lstResults->viewport()->mapFrom(this, event->pos()));
91
// if the user clicked below the end of the attribute list, just return
95
// The assumption is made that an instance of QgsIdentifyResults is
96
// created for each new Identify Results dialog box, and that the
97
// contents of the popup menu doesn't change during the time that
98
// such a dialog box is around.
99
if (mActionPopup == 0)
101
mActionPopup = new QMenu();
102
QAction* a = mActionPopup->addAction( tr("Run action") );
103
a->setEnabled(false);
104
mActionPopup->addSeparator();
106
QgsAttributeAction::aIter iter = mActions.begin();
107
for (int j = 0; iter != mActions.end(); ++iter, ++j)
109
QAction* a = mActionPopup->addAction(iter->name());
110
// The menu action stores an integer that is used later on to
111
// associate an menu action with an actual qgis action.
112
a->setData(QVariant::fromValue(j));
114
connect(mActionPopup, SIGNAL(triggered(QAction*)),
115
this, SLOT(popupItemSelected(QAction*)));
117
// Save the attribute values as these are needed for substituting into
119
// A little bit complicated because the user could of right-clicked
120
// on a parent or a child in the dialog box. We also want to keep
121
// track of which row in the identify results table was actually
122
// clicked on. This is stored as an index into the mValues vector.
124
QTreeWidgetItem* parent = item->parent();
129
for (int j = 0; j < parent->childCount(); ++j)
131
if ( parent->child(j)->text(0) != "action" ) {
132
mValues.push_back(std::make_pair(parent->child(j)->text(0),
133
parent->child(j)->text(1)));
134
// Need to do the comparison on the text strings rather than the
135
// pointers because if the user clicked on the parent, we need
136
// to pick up which child that actually is (the parent in the
137
// identify results dialog box is just one of the children
138
// that has been chosen by some method).
139
if (parent->child(j)->text(0) == item->text(0))
144
if (mActions.size() > 0)
145
mActionPopup->popup(event->globalPos());
148
// Restore last window position/size and show the window
149
void QgsIdentifyResults::restorePosition()
153
QPoint pos = settings.value("/Windows/Identify/pos",
154
QPoint(100,100)).toPoint();
155
QSize size = settings.value("/Windows/Identify/size",
156
QSize(281,316)).toSize();
157
//std::cerr << "Setting geometry: " << wx << ", " << wy << ", " << ww << ", " << wh << std::endl;
161
//std::cerr << "Current geometry: " << x() << ", " << y() << ", " << width() << ", " << height() << std::endl;
163
// Save the current window location (store in ~/.qt/qgisrc)
164
void QgsIdentifyResults::saveWindowLocation()
167
settings.setValue("/Windows/Identify/pos", this->pos());
168
settings.setValue("/Windows/Identify/size", this->size());
171
/** add an attribute and its value to the list */
172
void QgsIdentifyResults::addAttribute(QTreeWidgetItem * fnode, QString field, QString value)
175
labels << field << value;
176
new QTreeWidgetItem(fnode, labels);
179
void QgsIdentifyResults::addAttribute(QString field, QString value)
182
labels << field << value;
183
new QTreeWidgetItem(lstResults, labels);
186
void QgsIdentifyResults::addDerivedAttribute(QTreeWidgetItem * fnode, QString field, QString value)
188
QTreeWidgetItem * daRootNode;
190
// Determine if this is the first derived attribute for this
192
if (mDerivedAttributeRootNodes.find(fnode) != mDerivedAttributeRootNodes.end())
194
// Reuse existing derived-attribute root node
195
daRootNode = mDerivedAttributeRootNodes[fnode];
199
// Create new derived-attribute root node
200
daRootNode = new QTreeWidgetItem(fnode, QStringList(tr("(Derived)")));
201
QFont font = daRootNode->font(0);
202
font.setItalic(true);
203
daRootNode->setFont(0, font);
207
labels << field << value;
208
new QTreeWidgetItem(daRootNode, labels);
211
void QgsIdentifyResults::addAction(QTreeWidgetItem * fnode, int id, QString field, QString value)
214
labels << field << value << "action" << QString::number(id);
215
QTreeWidgetItem *item = new QTreeWidgetItem(fnode, labels );
217
QPixmap pm ( QgsApplication::themePath() + "/mAction.png" );
218
item->setIcon ( 0, QIcon(pm) );
221
/** Add a feature node to the list */
222
QTreeWidgetItem *QgsIdentifyResults::addNode(QString label)
224
return (new QTreeWidgetItem(lstResults, QStringList(label)));
227
void QgsIdentifyResults::setTitle(QString title)
229
setWindowTitle(tr("Identify Results - ") + title);
232
void QgsIdentifyResults::setColumnText ( int column, const QString & label )
234
QTreeWidgetItem* header = lstResults->headerItem();
235
header->setText ( column, label );
238
// Run the action that was selected in the popup menu
239
void QgsIdentifyResults::popupItemSelected(QAction* menuAction)
241
int id = menuAction->data().toInt();
242
mActions.doAction(id, mValues, mClickedOnValue);
245
/** Expand all the identified features (show their attributes). */
246
void QgsIdentifyResults::showAllAttributes()
248
// Easy now with Qt 4.2...
249
lstResults->expandAll();
252
void QgsIdentifyResults::expandColumnsToFit()
254
lstResults->resizeColumnToContents(0);
255
lstResults->resizeColumnToContents(1);
258
void QgsIdentifyResults::clear()
263
void QgsIdentifyResults::setMessage( QString shortMsg, QString longMsg )
266
labels << shortMsg << longMsg;
267
new QTreeWidgetItem(lstResults, labels );
270
void QgsIdentifyResults::setActions( const QgsAttributeAction& actions )
275
void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
279
if ( item->text(2) != "action" ) return;
281
int id = item->text(3).toInt();
283
QTreeWidgetItem* parent = item->parent();
289
for (int j = 0; j < parent->childCount(); ++j)
291
if ( parent->child(j)->text(0) != "action" ) {
292
mValues.push_back(std::make_pair(parent->child(j)->text(0),
293
parent->child(j)->text(1)));
294
if (parent->child(j)->text(0) == item->text(0))
299
mActions.doAction(id, mValues, mClickedOnValue);
301
void QgsIdentifyResults::on_buttonHelp_clicked()
303
QgsContextHelp::run(context_id);
306
void QgsIdentifyResults::itemExpanded(QTreeWidgetItem* item)
308
expandColumnsToFit();