~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/gui/qgsidentifyresults.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                     qgsidentifyresults.cpp  -  description
 
3
                              -------------------
 
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
 ***************************************************************************/
 
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: qgsidentifyresults.cpp 6255 2006-12-15 09:21:43Z g_j_m $ */
 
19
 
 
20
#include "qgsidentifyresults.h"
 
21
#include "qgscontexthelp.h"
 
22
#include "qgsapplication.h"
 
23
 
 
24
#include <QCloseEvent>
 
25
#include <QLabel>
 
26
#include <QAction>
 
27
#include <QTreeWidgetItem>
 
28
#include <QPixmap>
 
29
#include <QSettings>
 
30
#include <QMenu>
 
31
 
 
32
#include <iostream>
 
33
 
 
34
QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
 
35
    QWidget *parent, Qt::WFlags f)
 
36
: QDialog(parent, f),
 
37
  mActions(actions),
 
38
  mClickedOnValue(0),
 
39
  mActionPopup(0)
 
40
{
 
41
  setupUi(this);
 
42
  lstResults->setColumnCount(2);
 
43
  setColumnText(0, tr("Feature"));
 
44
  setColumnText(1, tr("Value"));
 
45
 
 
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*)));
 
52
}
 
53
 
 
54
QgsIdentifyResults::~QgsIdentifyResults()
 
55
{
 
56
  delete mActionPopup;
 
57
}
 
58
 
 
59
// Call to show the dialog box.
 
60
void QgsIdentifyResults::show()
 
61
{
 
62
  // Enfore a few things before showing the dialog box
 
63
  lstResults->sortItems(0, Qt::Ascending);
 
64
  expandColumnsToFit();
 
65
 
 
66
  QDialog::show();
 
67
}
 
68
// Slot called when user clicks the Close button
 
69
// (saves the current window size/position)
 
70
void QgsIdentifyResults::close()
 
71
{
 
72
  saveWindowLocation();
 
73
  done(0);
 
74
}
 
75
// Save the current window size/position before closing 
 
76
// from window menu or X in titlebar
 
77
void QgsIdentifyResults::closeEvent(QCloseEvent *e)
 
78
{
 
79
  // We'll close in our own good time thanks...
 
80
  e->ignore();
 
81
  close();
 
82
}
 
83
 
 
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
 
86
// dialog box.
 
87
 
 
88
void QgsIdentifyResults::contextMenuEvent(QContextMenuEvent* event)
 
89
{
 
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
 
92
  if (item == NULL)
 
93
    return;
 
94
  
 
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)
 
100
  {
 
101
    mActionPopup = new QMenu();
 
102
    QAction* a = mActionPopup->addAction( tr("Run action") );
 
103
    a->setEnabled(false);
 
104
    mActionPopup->addSeparator();
 
105
 
 
106
    QgsAttributeAction::aIter iter = mActions.begin();
 
107
    for (int j = 0; iter != mActions.end(); ++iter, ++j)
 
108
    {
 
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));
 
113
    }
 
114
    connect(mActionPopup, SIGNAL(triggered(QAction*)),
 
115
            this, SLOT(popupItemSelected(QAction*)));
 
116
  }
 
117
  // Save the attribute values as these are needed for substituting into
 
118
  // the action. 
 
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.
 
123
 
 
124
  QTreeWidgetItem* parent = item->parent();
 
125
  if (parent == 0)
 
126
    parent = item;
 
127
 
 
128
  mValues.clear();
 
129
  for (int j = 0; j < parent->childCount(); ++j)
 
130
  {
 
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))
 
140
        mClickedOnValue = j;
 
141
    }
 
142
  }
 
143
 
 
144
  if (mActions.size() > 0)
 
145
    mActionPopup->popup(event->globalPos());
 
146
}
 
147
 
 
148
// Restore last window position/size and show the window
 
149
void QgsIdentifyResults::restorePosition()
 
150
{
 
151
 
 
152
  QSettings settings;
 
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;
 
158
  resize(size);
 
159
  move(pos);
 
160
  show();
 
161
  //std::cerr << "Current geometry: " << x() << ", " << y() << ", " << width() << ", " << height() << std::endl; 
 
162
}
 
163
// Save the current window location (store in ~/.qt/qgisrc)
 
164
void QgsIdentifyResults::saveWindowLocation()
 
165
{
 
166
  QSettings settings;
 
167
  settings.setValue("/Windows/Identify/pos", this->pos());
 
168
  settings.setValue("/Windows/Identify/size", this->size());
 
169
 
170
 
 
171
/** add an attribute and its value to the list */
 
172
void QgsIdentifyResults::addAttribute(QTreeWidgetItem * fnode, QString field, QString value)
 
173
{
 
174
  QStringList labels;
 
175
  labels << field << value;
 
176
  new QTreeWidgetItem(fnode, labels);
 
177
}
 
178
 
 
179
void QgsIdentifyResults::addAttribute(QString field, QString value)
 
180
{
 
181
  QStringList labels;
 
182
  labels << field << value;
 
183
  new QTreeWidgetItem(lstResults, labels);
 
184
}
 
185
 
 
186
void QgsIdentifyResults::addDerivedAttribute(QTreeWidgetItem * fnode, QString field, QString value)
 
187
{
 
188
  QTreeWidgetItem * daRootNode;
 
189
 
 
190
  // Determine if this is the first derived attribute for this
 
191
  // feature or not
 
192
  if (mDerivedAttributeRootNodes.find(fnode) != mDerivedAttributeRootNodes.end())
 
193
  {
 
194
    // Reuse existing derived-attribute root node
 
195
    daRootNode = mDerivedAttributeRootNodes[fnode];
 
196
  }
 
197
  else
 
198
  {
 
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);
 
204
  }
 
205
 
 
206
  QStringList labels;
 
207
  labels << field << value;
 
208
  new QTreeWidgetItem(daRootNode, labels);
 
209
}
 
210
 
 
211
void QgsIdentifyResults::addAction(QTreeWidgetItem * fnode, int id, QString field, QString value)
 
212
{
 
213
  QStringList labels;
 
214
  labels << field << value << "action" << QString::number(id);
 
215
  QTreeWidgetItem *item = new QTreeWidgetItem(fnode, labels );
 
216
 
 
217
  QPixmap pm ( QgsApplication::themePath() + "/mAction.png" );
 
218
  item->setIcon ( 0, QIcon(pm) ); 
 
219
}
 
220
 
 
221
/** Add a feature node to the list */
 
222
QTreeWidgetItem *QgsIdentifyResults::addNode(QString label)
 
223
{
 
224
  return (new QTreeWidgetItem(lstResults, QStringList(label)));
 
225
}
 
226
 
 
227
void QgsIdentifyResults::setTitle(QString title)
 
228
{
 
229
  setWindowTitle(tr("Identify Results - ") + title);
 
230
}
 
231
 
 
232
void QgsIdentifyResults::setColumnText ( int column, const QString & label )
 
233
{
 
234
  QTreeWidgetItem* header = lstResults->headerItem();
 
235
  header->setText ( column, label );
 
236
}
 
237
 
 
238
// Run the action that was selected in the popup menu
 
239
void QgsIdentifyResults::popupItemSelected(QAction* menuAction)
 
240
{
 
241
  int id = menuAction->data().toInt();
 
242
  mActions.doAction(id, mValues, mClickedOnValue);
 
243
}
 
244
 
 
245
/** Expand all the identified features (show their attributes). */
 
246
void QgsIdentifyResults::showAllAttributes() 
 
247
{
 
248
  // Easy now with Qt 4.2...
 
249
  lstResults->expandAll();
 
250
}
 
251
 
 
252
void QgsIdentifyResults::expandColumnsToFit()
 
253
{
 
254
  lstResults->resizeColumnToContents(0);
 
255
  lstResults->resizeColumnToContents(1);
 
256
}
 
257
 
 
258
void QgsIdentifyResults::clear()
 
259
{
 
260
  lstResults->clear();
 
261
}
 
262
 
 
263
void QgsIdentifyResults::setMessage( QString shortMsg, QString longMsg )
 
264
{
 
265
  QStringList labels;
 
266
  labels << shortMsg << longMsg;
 
267
  new QTreeWidgetItem(lstResults, labels );
 
268
}
 
269
 
 
270
void QgsIdentifyResults::setActions( const QgsAttributeAction& actions  )
 
271
{
 
272
  mActions = actions;
 
273
}
 
274
 
 
275
void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
 
276
{
 
277
  if ( !item ) return;
 
278
 
 
279
  if ( item->text(2) != "action" ) return;
 
280
 
 
281
  int id = item->text(3).toInt();
 
282
 
 
283
  QTreeWidgetItem* parent = item->parent();
 
284
  if (parent == 0)
 
285
    parent = item;
 
286
 
 
287
  mValues.clear();
 
288
 
 
289
  for (int j = 0; j < parent->childCount(); ++j)
 
290
  {
 
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))
 
295
        mClickedOnValue = j;
 
296
    }
 
297
  }
 
298
 
 
299
  mActions.doAction(id, mValues, mClickedOnValue);
 
300
}
 
301
void QgsIdentifyResults::on_buttonHelp_clicked()
 
302
{
 
303
  QgsContextHelp::run(context_id);
 
304
}
 
305
 
 
306
void QgsIdentifyResults::itemExpanded(QTreeWidgetItem* item)
 
307
{
 
308
  expandColumnsToFit();
 
309
}