24
24
#include < QCloseEvent>
25
25
#include < QLabel>
26
26
#include < QAction>
27
- #include < Q3ListView >
27
+ #include < QTreeWidgetItem >
28
28
#include < QPixmap>
29
29
#include < QSettings>
30
30
#include < QMenu>
31
31
32
+ #include < iostream>
33
+
32
34
QgsIdentifyResults::QgsIdentifyResults (const QgsAttributeAction& actions,
33
35
QWidget *parent, Qt::WFlags f)
34
36
: QDialog(parent, f),
@@ -37,23 +39,33 @@ QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
37
39
mActionPopup(0 )
38
40
{
39
41
setupUi (this );
40
- lstResults->setResizeMode (Q3ListView::LastColumn );
41
- lstResults-> setColumnWidthMode (0 , Q3ListView::Maximum );
42
- lstResults-> setColumnWidthMode (1 , Q3ListView::Maximum );
42
+ lstResults->setColumnCount ( 2 );
43
+ setColumnText (0 , tr ( " Feature " ) );
44
+ setColumnText (1 , tr ( " Value " ) );
43
45
44
46
connect ( buttonCancel, SIGNAL (clicked ()),
45
47
this , SLOT (close ()) );
46
- connect ( lstResults, SIGNAL (clicked (Q3ListViewItem * )),
47
- this , SLOT (clicked (Q3ListViewItem *)) );
48
- connect ( lstResults, SIGNAL (contextMenuRequested (Q3ListViewItem *, const QPoint &, int )),
49
- this , SLOT ( popupContextMenu (Q3ListViewItem *, const QPoint &, int )) );
48
+ connect ( lstResults, SIGNAL (itemClicked (QTreeWidgetItem*, int )),
49
+ this , SLOT (clicked (QTreeWidgetItem *)) );
50
+ connect ( lstResults, SIGNAL (itemExpanded (QTreeWidgetItem* )),
51
+ this , SLOT ( itemExpanded (QTreeWidgetItem*)) );
50
52
}
51
53
52
54
QgsIdentifyResults::~QgsIdentifyResults ()
53
55
{
54
56
saveWindowLocation ();
55
57
delete mActionPopup ;
56
58
}
59
+
60
+ // Call to show the dialog box.
61
+ void QgsIdentifyResults::show ()
62
+ {
63
+ // Enfore a few things before showing the dialog box
64
+ lstResults->sortItems (0 , Qt::Ascending);
65
+ expandColumnsToFit ();
66
+
67
+ QDialog::show ();
68
+ }
57
69
// Slot called when user clicks the Close button
58
70
// (saves the current window size/position)
59
71
void QgsIdentifyResults::close ()
@@ -73,21 +85,23 @@ void QgsIdentifyResults::closeEvent(QCloseEvent *e)
73
85
// Popup (create if necessary) a context menu that contains a list of
74
86
// actions that can be applied to the data in the identify results
75
87
// dialog box.
76
- void QgsIdentifyResults::popupContextMenu (Q3ListViewItem* item,
77
- const QPoint& p, int i )
88
+
89
+ void QgsIdentifyResults::contextMenuEvent (QContextMenuEvent* event )
78
90
{
91
+ QTreeWidgetItem* item = lstResults->itemAt (lstResults->viewport ()->mapFrom (this , event->pos ()));
79
92
// if the user clicked below the end of the attribute list, just return
80
93
if (item == NULL )
81
94
return ;
82
-
95
+
83
96
// The assumption is made that an instance of QgsIdentifyResults is
84
97
// created for each new Identify Results dialog box, and that the
85
98
// contents of the popup menu doesn't change during the time that
86
99
// such a dialog box is around.
87
100
if (mActionPopup == 0 )
88
101
{
89
102
mActionPopup = new QMenu ();
90
- QAction *a = mActionPopup ->addAction ( tr (" Run action" ) );
103
+ QAction* a = mActionPopup ->addAction ( tr (" Run action" ) );
104
+ a->setEnabled (false );
91
105
mActionPopup ->addSeparator ();
92
106
93
107
QgsAttributeAction::aIter iter = mActions .begin ();
@@ -108,35 +122,30 @@ void QgsIdentifyResults::popupContextMenu(Q3ListViewItem* item,
108
122
// track of which row in the identify results table was actually
109
123
// clicked on. This is stored as an index into the mValues vector.
110
124
111
- Q3ListViewItem* parent = item->parent ();
112
- Q3ListViewItem* child;
113
-
114
- if (item->parent () == 0 )
115
- child = item->firstChild ();
116
- else
117
- child = parent->firstChild ();
125
+ QTreeWidgetItem* parent = item->parent ();
126
+ if (parent == 0 )
127
+ parent = item;
118
128
119
129
mValues .clear ();
120
- int j = 0 ;
121
- while (child != 0 )
130
+ for (int j = 0 ; j < parent->childCount (); ++j)
122
131
{
123
- if ( child->text (2 ) != " action" ) {
124
- mValues .push_back (std::make_pair (child->text (0 ), child->text (1 )));
132
+ if ( parent->child (j)->text (0 ) != " action" ) {
133
+ mValues .push_back (std::make_pair (parent->child (j)->text (0 ),
134
+ parent->child (j)->text (1 )));
125
135
// Need to do the comparison on the text strings rather than the
126
136
// pointers because if the user clicked on the parent, we need
127
137
// to pick up which child that actually is (the parent in the
128
138
// identify results dialog box is just one of the children
129
139
// that has been chosen by some method).
130
- if (child->text (0 ) == item->text (0 ))
140
+ if (parent-> child (j) ->text (0 ) == item->text (0 ))
131
141
mClickedOnValue = j;
132
- ++j;
133
142
}
134
- child = child->nextSibling ();
135
143
}
136
144
137
145
if (mActions .size () > 0 )
138
- mActionPopup ->popup (p );
146
+ mActionPopup ->popup (event-> globalPos () );
139
147
}
148
+
140
149
// Restore last window position/size and show the window
141
150
void QgsIdentifyResults::restorePosition ()
142
151
{
@@ -165,22 +174,23 @@ void QgsIdentifyResults::saveWindowLocation()
165
174
}
166
175
167
176
/* * add an attribute and its value to the list */
168
- void QgsIdentifyResults::addAttribute (Q3ListViewItem * fnode, QString field, QString value)
177
+ void QgsIdentifyResults::addAttribute (QTreeWidgetItem * fnode, QString field, QString value)
169
178
{
170
- new Q3ListViewItem (fnode, field, value);
179
+ QStringList labels;
180
+ labels << field << value;
181
+ new QTreeWidgetItem (fnode, labels);
171
182
}
172
183
173
184
void QgsIdentifyResults::addAttribute (QString field, QString value)
174
185
{
175
- new Q3ListViewItem (lstResults, field, value);
186
+ QStringList labels;
187
+ labels << field << value;
188
+ new QTreeWidgetItem (lstResults, labels);
176
189
}
177
190
178
- void QgsIdentifyResults::addDerivedAttribute (Q3ListViewItem * fnode, QString field, QString value)
191
+ void QgsIdentifyResults::addDerivedAttribute (QTreeWidgetItem * fnode, QString field, QString value)
179
192
{
180
- // TODO: When we migrate this to a Qt4 QTreeViewWidget,
181
- // this should be added as italic text instead
182
-
183
- Q3ListViewItem * daRootNode;
193
+ QTreeWidgetItem * daRootNode;
184
194
185
195
// Determine if this is the first derived attribute for this
186
196
// feature or not
@@ -192,24 +202,31 @@ void QgsIdentifyResults::addDerivedAttribute(Q3ListViewItem * fnode, QString fie
192
202
else
193
203
{
194
204
// Create new derived-attribute root node
195
- daRootNode = new Q3ListViewItem (fnode, tr (" (Derived)" ));
205
+ daRootNode = new QTreeWidgetItem (fnode, QStringList (tr (" (Derived)" )));
206
+ QFont font = daRootNode->font (0 );
207
+ font.setItalic (true );
208
+ daRootNode->setFont (0 , font);
196
209
}
197
210
198
- new Q3ListViewItem (daRootNode, field, value);
211
+ QStringList labels;
212
+ labels << field << value;
213
+ new QTreeWidgetItem (daRootNode, labels);
199
214
}
200
215
201
- void QgsIdentifyResults::addAction (Q3ListViewItem * fnode, int id, QString field, QString value)
216
+ void QgsIdentifyResults::addAction (QTreeWidgetItem * fnode, int id, QString field, QString value)
202
217
{
203
- Q3ListViewItem *item = new Q3ListViewItem (fnode, field, value, " action" , QString::number (id) );
218
+ QStringList labels;
219
+ labels << field << value << " action" << QString::number (id);
220
+ QTreeWidgetItem *item = new QTreeWidgetItem (fnode, labels );
204
221
205
222
QPixmap pm ( QgsApplication::themePath () + " /mAction.png" );
206
- item->setPixmap ( 0 , pm );
223
+ item->setIcon ( 0 , QIcon (pm) );
207
224
}
208
225
209
226
/* * Add a feature node to the list */
210
- Q3ListViewItem *QgsIdentifyResults::addNode (QString label)
227
+ QTreeWidgetItem *QgsIdentifyResults::addNode (QString label)
211
228
{
212
- return (new Q3ListViewItem (lstResults, label));
229
+ return (new QTreeWidgetItem (lstResults, QStringList ( label) ));
213
230
}
214
231
215
232
void QgsIdentifyResults::setTitle (QString title)
@@ -219,7 +236,8 @@ void QgsIdentifyResults::setTitle(QString title)
219
236
220
237
void QgsIdentifyResults::setColumnText ( int column, const QString & label )
221
238
{
222
- lstResults->setColumnText ( column, label );
239
+ QTreeWidgetItem* header = lstResults->headerItem ();
240
+ header->setText ( column, label );
223
241
}
224
242
225
243
// Run the action that was selected in the popup menu
@@ -231,9 +249,15 @@ void QgsIdentifyResults::popupItemSelected(QAction* menuAction)
231
249
232
250
/* * Expand all the identified features (show their attributes). */
233
251
void QgsIdentifyResults::showAllAttributes () {
234
- Q3ListViewItemIterator qlvii (lstResults);
252
+ QTreeWidgetItemIterator qlvii (lstResults);
235
253
for ( ; *qlvii; ++qlvii)
236
- lstResults->setOpen (*qlvii, true );
254
+ lstResults->setItemExpanded (*qlvii, true );
255
+ }
256
+
257
+ void QgsIdentifyResults::expandColumnsToFit ()
258
+ {
259
+ lstResults->resizeColumnToContents (0 );
260
+ lstResults->resizeColumnToContents (1 );
237
261
}
238
262
239
263
void QgsIdentifyResults::clear ()
@@ -243,45 +267,38 @@ void QgsIdentifyResults::clear()
243
267
244
268
void QgsIdentifyResults::setMessage ( QString shortMsg, QString longMsg )
245
269
{
246
- new Q3ListViewItem (lstResults, shortMsg, longMsg );
270
+ QStringList labels;
271
+ labels << shortMsg << longMsg;
272
+ new QTreeWidgetItem (lstResults, labels );
247
273
}
248
274
249
275
void QgsIdentifyResults::setActions ( const QgsAttributeAction& actions )
250
276
{
251
277
mActions = actions;
252
278
}
253
279
254
- void QgsIdentifyResults::clicked ( Q3ListViewItem *item )
280
+ void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
255
281
{
256
282
if ( !item ) return ;
257
283
258
284
if ( item->text (2 ) != " action" ) return ;
259
285
260
286
int id = item->text (3 ).toInt ();
261
287
262
- Q3ListViewItem* parent = item->parent ();
263
- Q3ListViewItem* child;
264
-
265
- if (item->parent () == 0 )
266
- child = item->firstChild ();
267
- else
268
- child = parent->firstChild ();
288
+ QTreeWidgetItem* parent = item->parent ();
289
+ if (parent == 0 )
290
+ parent = item;
269
291
270
292
mValues .clear ();
271
293
272
- int j = 0 ;
273
-
274
- while (child != 0 )
294
+ for (int j = 0 ; j < parent->childCount (); ++j)
275
295
{
276
- if ( child->text (2 ) != " action" ) {
277
- mValues .push_back (std::make_pair (child-> text ( 0 ), child ->text (1 )));
278
-
279
- if (child->text (0 ) == item->text (0 ))
296
+ if ( parent-> child (j) ->text (0 ) != " action" ) {
297
+ mValues .push_back (std::make_pair (parent-> child (j) ->text (0 ),
298
+ parent-> child (j)-> text ( 1 )));
299
+ if (parent-> child (j) ->text (0 ) == item->text (0 ))
280
300
mClickedOnValue = j;
281
-
282
- ++j;
283
301
}
284
- child = child->nextSibling ();
285
302
}
286
303
287
304
mActions .doAction (id, mValues , mClickedOnValue );
@@ -290,3 +307,8 @@ void QgsIdentifyResults::on_buttonHelp_clicked()
290
307
{
291
308
QgsContextHelp::run (context_id);
292
309
}
310
+
311
+ void QgsIdentifyResults::itemExpanded (QTreeWidgetItem* item)
312
+ {
313
+ expandColumnsToFit ();
314
+ }
0 commit comments