Skip to content

Commit

Permalink
Move the identify results dialog from a Qt3 to a Qt4 widget
Browse files Browse the repository at this point in the history
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
  • Loading branch information
g_j_m committed Oct 4, 2006
1 parent 7ee9b7c commit b9083f3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 105 deletions.
150 changes: 86 additions & 64 deletions src/gui/qgsidentifyresults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
#include <QCloseEvent>
#include <QLabel>
#include <QAction>
#include <Q3ListView>
#include <QTreeWidgetItem>
#include <QPixmap>
#include <QSettings>
#include <QMenu>

#include <iostream>

QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
QWidget *parent, Qt::WFlags f)
: QDialog(parent, f),
Expand All @@ -37,23 +39,33 @@ QgsIdentifyResults::QgsIdentifyResults(const QgsAttributeAction& actions,
mActionPopup(0)
{
setupUi(this);
lstResults->setResizeMode(Q3ListView::LastColumn);
lstResults->setColumnWidthMode(0, Q3ListView::Maximum);
lstResults->setColumnWidthMode(1, Q3ListView::Maximum);
lstResults->setColumnCount(2);
setColumnText(0, tr("Feature"));
setColumnText(1, tr("Value"));

connect( buttonCancel, SIGNAL(clicked()),
this, SLOT(close()) );
connect( lstResults, SIGNAL(clicked(Q3ListViewItem *)),
this, SLOT(clicked(Q3ListViewItem *)) );
connect( lstResults, SIGNAL(contextMenuRequested(Q3ListViewItem *, const QPoint &, int)),
this, SLOT(popupContextMenu(Q3ListViewItem *, const QPoint &, int)) );
connect( lstResults, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
this, SLOT(clicked(QTreeWidgetItem *)) );
connect( lstResults, SIGNAL(itemExpanded(QTreeWidgetItem*)),
this, SLOT(itemExpanded(QTreeWidgetItem*)));
}

QgsIdentifyResults::~QgsIdentifyResults()
{
saveWindowLocation();
delete mActionPopup;
}

// Call to show the dialog box.
void QgsIdentifyResults::show()
{
// Enfore a few things before showing the dialog box
lstResults->sortItems(0, Qt::Ascending);
expandColumnsToFit();

QDialog::show();
}
// Slot called when user clicks the Close button
// (saves the current window size/position)
void QgsIdentifyResults::close()
Expand All @@ -73,21 +85,23 @@ void QgsIdentifyResults::closeEvent(QCloseEvent *e)
// Popup (create if necessary) a context menu that contains a list of
// actions that can be applied to the data in the identify results
// dialog box.
void QgsIdentifyResults::popupContextMenu(Q3ListViewItem* item,
const QPoint& p, int i)

void QgsIdentifyResults::contextMenuEvent(QContextMenuEvent* event)
{
QTreeWidgetItem* item = lstResults->itemAt(lstResults->viewport()->mapFrom(this, event->pos()));
// if the user clicked below the end of the attribute list, just return
if (item == NULL)
return;

// The assumption is made that an instance of QgsIdentifyResults is
// created for each new Identify Results dialog box, and that the
// contents of the popup menu doesn't change during the time that
// such a dialog box is around.
if (mActionPopup == 0)
{
mActionPopup = new QMenu();
QAction *a = mActionPopup->addAction( tr("Run action") );
QAction* a = mActionPopup->addAction( tr("Run action") );
a->setEnabled(false);
mActionPopup->addSeparator();

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

Q3ListViewItem* parent = item->parent();
Q3ListViewItem* child;

if (item->parent() == 0)
child = item->firstChild();
else
child = parent->firstChild();
QTreeWidgetItem* parent = item->parent();
if (parent == 0)
parent = item;

mValues.clear();
int j = 0;
while (child != 0)
for (int j = 0; j < parent->childCount(); ++j)
{
if ( child->text(2) != "action" ) {
mValues.push_back(std::make_pair(child->text(0), child->text(1)));
if ( parent->child(j)->text(0) != "action" ) {
mValues.push_back(std::make_pair(parent->child(j)->text(0),
parent->child(j)->text(1)));
// Need to do the comparison on the text strings rather than the
// pointers because if the user clicked on the parent, we need
// to pick up which child that actually is (the parent in the
// identify results dialog box is just one of the children
// that has been chosen by some method).
if (child->text(0) == item->text(0))
if (parent->child(j)->text(0) == item->text(0))
mClickedOnValue = j;
++j;
}
child = child->nextSibling();
}

if (mActions.size() > 0)
mActionPopup->popup(p);
mActionPopup->popup(event->globalPos());
}

// Restore last window position/size and show the window
void QgsIdentifyResults::restorePosition()
{
Expand Down Expand Up @@ -165,22 +174,23 @@ void QgsIdentifyResults::saveWindowLocation()
}

/** add an attribute and its value to the list */
void QgsIdentifyResults::addAttribute(Q3ListViewItem * fnode, QString field, QString value)
void QgsIdentifyResults::addAttribute(QTreeWidgetItem * fnode, QString field, QString value)
{
new Q3ListViewItem(fnode, field, value);
QStringList labels;
labels << field << value;
new QTreeWidgetItem(fnode, labels);
}

void QgsIdentifyResults::addAttribute(QString field, QString value)
{
new Q3ListViewItem(lstResults, field, value);
QStringList labels;
labels << field << value;
new QTreeWidgetItem(lstResults, labels);
}

void QgsIdentifyResults::addDerivedAttribute(Q3ListViewItem * fnode, QString field, QString value)
void QgsIdentifyResults::addDerivedAttribute(QTreeWidgetItem * fnode, QString field, QString value)
{
// TODO: When we migrate this to a Qt4 QTreeViewWidget,
// this should be added as italic text instead

Q3ListViewItem * daRootNode;
QTreeWidgetItem * daRootNode;

// Determine if this is the first derived attribute for this
// feature or not
Expand All @@ -192,24 +202,31 @@ void QgsIdentifyResults::addDerivedAttribute(Q3ListViewItem * fnode, QString fie
else
{
// Create new derived-attribute root node
daRootNode = new Q3ListViewItem(fnode, tr("(Derived)"));
daRootNode = new QTreeWidgetItem(fnode, QStringList(tr("(Derived)")));
QFont font = daRootNode->font(0);
font.setItalic(true);
daRootNode->setFont(0, font);
}

new Q3ListViewItem(daRootNode, field, value);
QStringList labels;
labels << field << value;
new QTreeWidgetItem(daRootNode, labels);
}

void QgsIdentifyResults::addAction(Q3ListViewItem * fnode, int id, QString field, QString value)
void QgsIdentifyResults::addAction(QTreeWidgetItem * fnode, int id, QString field, QString value)
{
Q3ListViewItem *item = new Q3ListViewItem(fnode, field, value, "action", QString::number(id) );
QStringList labels;
labels << field << value << "action" << QString::number(id);
QTreeWidgetItem *item = new QTreeWidgetItem(fnode, labels );

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

/** Add a feature node to the list */
Q3ListViewItem *QgsIdentifyResults::addNode(QString label)
QTreeWidgetItem *QgsIdentifyResults::addNode(QString label)
{
return (new Q3ListViewItem(lstResults, label));
return (new QTreeWidgetItem(lstResults, QStringList(label)));
}

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

void QgsIdentifyResults::setColumnText ( int column, const QString & label )
{
lstResults->setColumnText ( column, label );
QTreeWidgetItem* header = lstResults->headerItem();
header->setText ( column, label );
}

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

/** Expand all the identified features (show their attributes). */
void QgsIdentifyResults::showAllAttributes() {
Q3ListViewItemIterator qlvii(lstResults);
QTreeWidgetItemIterator qlvii(lstResults);
for ( ; *qlvii; ++qlvii)
lstResults->setOpen(*qlvii, true);
lstResults->setItemExpanded(*qlvii, true);
}

void QgsIdentifyResults::expandColumnsToFit()
{
lstResults->resizeColumnToContents(0);
lstResults->resizeColumnToContents(1);
}

void QgsIdentifyResults::clear()
Expand All @@ -243,45 +267,38 @@ void QgsIdentifyResults::clear()

void QgsIdentifyResults::setMessage( QString shortMsg, QString longMsg )
{
new Q3ListViewItem(lstResults, shortMsg, longMsg );
QStringList labels;
labels << shortMsg << longMsg;
new QTreeWidgetItem(lstResults, labels );
}

void QgsIdentifyResults::setActions( const QgsAttributeAction& actions )
{
mActions = actions;
}

void QgsIdentifyResults::clicked ( Q3ListViewItem *item )
void QgsIdentifyResults::clicked ( QTreeWidgetItem *item )
{
if ( !item ) return;

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

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

Q3ListViewItem* parent = item->parent();
Q3ListViewItem* child;

if (item->parent() == 0)
child = item->firstChild();
else
child = parent->firstChild();
QTreeWidgetItem* parent = item->parent();
if (parent == 0)
parent = item;

mValues.clear();

int j = 0;

while (child != 0)
for (int j = 0; j < parent->childCount(); ++j)
{
if ( child->text(2) != "action" ) {
mValues.push_back(std::make_pair(child->text(0), child->text(1)));

if (child->text(0) == item->text(0))
if ( parent->child(j)->text(0) != "action" ) {
mValues.push_back(std::make_pair(parent->child(j)->text(0),
parent->child(j)->text(1)));
if (parent->child(j)->text(0) == item->text(0))
mClickedOnValue = j;

++j;
}
child = child->nextSibling();
}

mActions.doAction(id, mValues, mClickedOnValue);
Expand All @@ -290,3 +307,8 @@ void QgsIdentifyResults::on_buttonHelp_clicked()
{
QgsContextHelp::run(context_id);
}

void QgsIdentifyResults::itemExpanded(QTreeWidgetItem* item)
{
expandColumnsToFit();
}
25 changes: 17 additions & 8 deletions src/gui/qgsidentifyresults.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <map>

class QCloseEvent;
class Q3ListViewItem;
class QTreeWidgetItem;
class QAction;
class QMenu;

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

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

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

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

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

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

/** Resize all of the columns to fit the data in them */
void expandColumnsToFit();

/** Remove results */
void clear();

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

public slots:

void show();

void close();
void popupContextMenu(Q3ListViewItem*, const QPoint&, int);
void contextMenuEvent(QContextMenuEvent*);
void popupItemSelected(QAction* menuAction);

/* Item in tree was clicked */
void clicked ( Q3ListViewItem *lvi );
void clicked ( QTreeWidgetItem *lvi );

//! Context help
void on_buttonHelp_clicked();

/* Called when an item is expanded so that we can ensure that the
column width if expanded to show it */
void itemExpanded(QTreeWidgetItem*);

private:

QgsAttributeAction mActions;
Expand All @@ -107,7 +116,7 @@ class QgsIdentifyResults: public QDialog, private Ui::QgsIdentifyResultsBase
First item: Feature root node
Second item: Derived-attribute root node for that feature
*/
std::map<Q3ListViewItem *, Q3ListViewItem *> mDerivedAttributeRootNodes;
std::map<QTreeWidgetItem *, QTreeWidgetItem *> mDerivedAttributeRootNodes;
};

#endif
Loading

0 comments on commit b9083f3

Please sign in to comment.