Skip to content

Commit b9083f3

Browse files
author
g_j_m
committedOct 4, 2006
Move the identify results dialog from a Qt3 to a Qt4 widget
Make the derived item italics Due to the change to Qt4 the derived item now also sorts to the top (which neatly fixes ticket #302) git-svn-id: http://svn.osgeo.org/qgis/trunk@5911 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 7ee9b7c commit b9083f3

File tree

3 files changed

+112
-105
lines changed

3 files changed

+112
-105
lines changed
 

‎src/gui/qgsidentifyresults.cpp

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
#include <QCloseEvent>
2525
#include <QLabel>
2626
#include <QAction>
27-
#include <Q3ListView>
27+
#include <QTreeWidgetItem>
2828
#include <QPixmap>
2929
#include <QSettings>
3030
#include <QMenu>
3131

32+
#include <iostream>
33+
3234
QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
3335
QWidget *parent, Qt::WFlags f)
3436
: QDialog(parent, f),
@@ -37,23 +39,33 @@ QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
3739
mActionPopup(0)
3840
{
3941
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"));
4345

4446
connect( buttonCancel, SIGNAL(clicked()),
4547
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*)));
5052
}
5153

5254
QgsIdentifyResults::~QgsIdentifyResults()
5355
{
5456
saveWindowLocation();
5557
delete mActionPopup;
5658
}
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+
}
5769
// Slot called when user clicks the Close button
5870
// (saves the current window size/position)
5971
void QgsIdentifyResults::close()
@@ -73,21 +85,23 @@ void QgsIdentifyResults::closeEvent(QCloseEvent *e)
7385
// Popup (create if necessary) a context menu that contains a list of
7486
// actions that can be applied to the data in the identify results
7587
// dialog box.
76-
void QgsIdentifyResults::popupContextMenu(Q3ListViewItem* item,
77-
const QPoint& p, int i)
88+
89+
void QgsIdentifyResults::contextMenuEvent(QContextMenuEvent* event)
7890
{
91+
QTreeWidgetItem* item = lstResults->itemAt(lstResults->viewport()->mapFrom(this, event->pos()));
7992
// if the user clicked below the end of the attribute list, just return
8093
if (item == NULL)
8194
return;
82-
95+
8396
// The assumption is made that an instance of QgsIdentifyResults is
8497
// created for each new Identify Results dialog box, and that the
8598
// contents of the popup menu doesn't change during the time that
8699
// such a dialog box is around.
87100
if (mActionPopup == 0)
88101
{
89102
mActionPopup = new QMenu();
90-
QAction *a = mActionPopup->addAction( tr("Run action") );
103+
QAction* a = mActionPopup->addAction( tr("Run action") );
104+
a->setEnabled(false);
91105
mActionPopup->addSeparator();
92106

93107
QgsAttributeAction::aIter iter = mActions.begin();
@@ -108,35 +122,30 @@ void QgsIdentifyResults::popupContextMenu(Q3ListViewItem* item,
108122
// track of which row in the identify results table was actually
109123
// clicked on. This is stored as an index into the mValues vector.
110124

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;
118128

119129
mValues.clear();
120-
int j = 0;
121-
while (child != 0)
130+
for (int j = 0; j < parent->childCount(); ++j)
122131
{
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)));
125135
// Need to do the comparison on the text strings rather than the
126136
// pointers because if the user clicked on the parent, we need
127137
// to pick up which child that actually is (the parent in the
128138
// identify results dialog box is just one of the children
129139
// 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))
131141
mClickedOnValue = j;
132-
++j;
133142
}
134-
child = child->nextSibling();
135143
}
136144

137145
if (mActions.size() > 0)
138-
mActionPopup->popup(p);
146+
mActionPopup->popup(event->globalPos());
139147
}
148+
140149
// Restore last window position/size and show the window
141150
void QgsIdentifyResults::restorePosition()
142151
{
@@ -165,22 +174,23 @@ void QgsIdentifyResults::saveWindowLocation()
165174
}
166175

167176
/** 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)
169178
{
170-
new Q3ListViewItem(fnode, field, value);
179+
QStringList labels;
180+
labels << field << value;
181+
new QTreeWidgetItem(fnode, labels);
171182
}
172183

173184
void QgsIdentifyResults::addAttribute(QString field, QString value)
174185
{
175-
new Q3ListViewItem(lstResults, field, value);
186+
QStringList labels;
187+
labels << field << value;
188+
new QTreeWidgetItem(lstResults, labels);
176189
}
177190

178-
void QgsIdentifyResults::addDerivedAttribute(Q3ListViewItem * fnode, QString field, QString value)
191+
void QgsIdentifyResults::addDerivedAttribute(QTreeWidgetItem * fnode, QString field, QString value)
179192
{
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;
184194

185195
// Determine if this is the first derived attribute for this
186196
// feature or not
@@ -192,24 +202,31 @@ void QgsIdentifyResults::addDerivedAttribute(Q3ListViewItem * fnode, QString fie
192202
else
193203
{
194204
// 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);
196209
}
197210

198-
new Q3ListViewItem(daRootNode, field, value);
211+
QStringList labels;
212+
labels << field << value;
213+
new QTreeWidgetItem(daRootNode, labels);
199214
}
200215

201-
void QgsIdentifyResults::addAction(Q3ListViewItem * fnode, int id, QString field, QString value)
216+
void QgsIdentifyResults::addAction(QTreeWidgetItem * fnode, int id, QString field, QString value)
202217
{
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 );
204221

205222
QPixmap pm ( QgsApplication::themePath() + "/mAction.png" );
206-
item->setPixmap ( 0, pm );
223+
item->setIcon ( 0, QIcon(pm) );
207224
}
208225

209226
/** Add a feature node to the list */
210-
Q3ListViewItem *QgsIdentifyResults::addNode(QString label)
227+
QTreeWidgetItem *QgsIdentifyResults::addNode(QString label)
211228
{
212-
return (new Q3ListViewItem(lstResults, label));
229+
return (new QTreeWidgetItem(lstResults, QStringList(label)));
213230
}
214231

215232
void QgsIdentifyResults::setTitle(QString title)
@@ -219,7 +236,8 @@ void QgsIdentifyResults::setTitle(QString title)
219236

220237
void QgsIdentifyResults::setColumnText ( int column, const QString & label )
221238
{
222-
lstResults->setColumnText ( column, label );
239+
QTreeWidgetItem* header = lstResults->headerItem();
240+
header->setText ( column, label );
223241
}
224242

225243
// Run the action that was selected in the popup menu
@@ -231,9 +249,15 @@ void QgsIdentifyResults::popupItemSelected(QAction* menuAction)
231249

232250
/** Expand all the identified features (show their attributes). */
233251
void QgsIdentifyResults::showAllAttributes() {
234-
Q3ListViewItemIterator qlvii(lstResults);
252+
QTreeWidgetItemIterator qlvii(lstResults);
235253
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);
237261
}
238262

239263
void QgsIdentifyResults::clear()
@@ -243,45 +267,38 @@ void QgsIdentifyResults::clear()
243267

244268
void QgsIdentifyResults::setMessage( QString shortMsg, QString longMsg )
245269
{
246-
new Q3ListViewItem(lstResults, shortMsg, longMsg );
270+
QStringList labels;
271+
labels << shortMsg << longMsg;
272+
new QTreeWidgetItem(lstResults, labels );
247273
}
248274

249275
void QgsIdentifyResults::setActions( const QgsAttributeAction& actions )
250276
{
251277
mActions = actions;
252278
}
253279

254-
void QgsIdentifyResults::clicked ( Q3ListViewItem *item )
280+
void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
255281
{
256282
if ( !item ) return;
257283

258284
if ( item->text(2) != "action" ) return;
259285

260286
int id = item->text(3).toInt();
261287

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;
269291

270292
mValues.clear();
271293

272-
int j = 0;
273-
274-
while (child != 0)
294+
for (int j = 0; j < parent->childCount(); ++j)
275295
{
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))
280300
mClickedOnValue = j;
281-
282-
++j;
283301
}
284-
child = child->nextSibling();
285302
}
286303

287304
mActions.doAction(id, mValues, mClickedOnValue);
@@ -290,3 +307,8 @@ void QgsIdentifyResults::on_buttonHelp_clicked()
290307
{
291308
QgsContextHelp::run(context_id);
292309
}
310+
311+
void QgsIdentifyResults::itemExpanded(QTreeWidgetItem* item)
312+
{
313+
expandColumnsToFit();
314+
}

‎src/gui/qgsidentifyresults.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <map>
2727

2828
class QCloseEvent;
29-
class Q3ListViewItem;
29+
class QTreeWidgetItem;
3030
class QAction;
3131
class QMenu;
3232

@@ -46,19 +46,19 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
4646
~QgsIdentifyResults();
4747

4848
/** Add an attribute to the feature display node */
49-
void addAttribute(Q3ListViewItem *parent, QString field, QString value);
49+
void addAttribute(QTreeWidgetItem *parent, QString field, QString value);
5050

5151
/** Add an attribute */
5252
void addAttribute(QString field, QString value);
5353

5454
/** Add a derived attribute (e.g. Length, Area) to the feature display node */
55-
void addDerivedAttribute(Q3ListViewItem *parent, QString field, QString value);
55+
void addDerivedAttribute(QTreeWidgetItem *parent, QString field, QString value);
5656

5757
/** Add an action to the feature display node */
58-
void addAction(Q3ListViewItem *parent, int id, QString field, QString value);
58+
void addAction(QTreeWidgetItem *parent, int id, QString field, QString value);
5959

6060
/** Add a feature node to the feature display */
61-
Q3ListViewItem * addNode(QString label);
61+
QTreeWidgetItem * addNode(QString label);
6262
/** Set the title for the identify results dialog */
6363
void setTitle(QString title);
6464
/** Set header column */
@@ -68,6 +68,9 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
6868
void closeEvent(QCloseEvent *e);
6969
void showAllAttributes();
7070

71+
/** Resize all of the columns to fit the data in them */
72+
void expandColumnsToFit();
73+
7174
/** Remove results */
7275
void clear();
7376

@@ -82,16 +85,22 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
8285

8386
public slots:
8487

88+
void show();
89+
8590
void close();
86-
void popupContextMenu(Q3ListViewItem*, const QPoint&, int);
91+
void contextMenuEvent(QContextMenuEvent*);
8792
void popupItemSelected(QAction* menuAction);
8893

8994
/* Item in tree was clicked */
90-
void clicked ( Q3ListViewItem *lvi );
95+
void clicked ( QTreeWidgetItem *lvi );
9196

9297
//! Context help
9398
void on_buttonHelp_clicked();
9499

100+
/* Called when an item is expanded so that we can ensure that the
101+
column width if expanded to show it */
102+
void itemExpanded(QTreeWidgetItem*);
103+
95104
private:
96105

97106
QgsAttributeAction mActions;
@@ -107,7 +116,7 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
107116
First item: Feature root node
108117
Second item: Derived-attribute root node for that feature
109118
*/
110-
std::map<Q3ListViewItem *, Q3ListViewItem *> mDerivedAttributeRootNodes;
119+
std::map<QTreeWidgetItem *, QTreeWidgetItem *> mDerivedAttributeRootNodes;
111120
};
112121

113122
#endif

‎src/ui/qgsidentifyresultsbase.ui

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,24 @@
1515
<property name="windowTitle" >
1616
<string>Identify Results</string>
1717
</property>
18-
<layout class="QGridLayout" >
18+
<layout class="QVBoxLayout" >
1919
<property name="margin" >
2020
<number>9</number>
2121
</property>
2222
<property name="spacing" >
2323
<number>6</number>
2424
</property>
25-
<item row="0" column="0" >
26-
<widget class="QSplitter" name="splitter1" >
27-
<property name="orientation" >
28-
<enum>Qt::Horizontal</enum>
25+
<item>
26+
<widget class="QTreeWidget" name="lstResults" >
27+
<property name="lineWidth" >
28+
<number>2</number>
29+
</property>
30+
<property name="sortingEnabled" >
31+
<bool>true</bool>
2932
</property>
30-
<widget class="Q3ListView" name="lstResults" >
31-
<property name="showSortIndicator" >
32-
<bool>true</bool>
33-
</property>
34-
<property name="rootIsDecorated" >
35-
<bool>true</bool>
36-
</property>
37-
<column>
38-
<property name="text" >
39-
<string>Feature</string>
40-
</property>
41-
</column>
42-
<column>
43-
<property name="text" >
44-
<string>Value</string>
45-
</property>
46-
</column>
47-
</widget>
4833
</widget>
4934
</item>
50-
<item row="1" column="0" >
35+
<item>
5136
<layout class="QHBoxLayout" >
5237
<property name="margin" >
5338
<number>0</number>
@@ -103,15 +88,6 @@
10388
</widget>
10489
<layoutdefault spacing="6" margin="11" />
10590
<pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
106-
<customwidgets>
107-
<customwidget>
108-
<class>Q3ListView</class>
109-
<extends></extends>
110-
<header>q3listview.h</header>
111-
<container>0</container>
112-
<pixmap></pixmap>
113-
</customwidget>
114-
</customwidgets>
11591
<resources/>
11692
<connections/>
11793
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.