implement simple identify results table view

This commit is contained in:
Etienne Tourigny 2014-04-05 17:11:44 -03:00 committed by Nathan Woodrow
parent 2102a80021
commit f95f64e335
2 changed files with 204 additions and 14 deletions

View File

@ -467,6 +467,65 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
}
}
// table
int j = tblResults->rowCount();
for ( int i = 0; i < attrs.count(); ++i )
{
if ( i >= fields.count() )
continue;
QString value = fields[i].displayString( attrs[i] );
QString value2 = value;
switch ( vlayer->editType( i ) )
{
case QgsVectorLayer::Hidden:
// skip the item
continue;
case QgsVectorLayer::ValueMap:
value2 = vlayer->valueMap( i ).key( value, QString( "(%1)" ).arg( value ) );
break;
case QgsVectorLayer::Calendar:
if ( attrs[i].canConvert( QVariant::Date ) )
value2 = attrs[i].toDate().toString( vlayer->dateFormat( i ) );
break;
default:
break;
}
tblResults->setRowCount( j + 1 );
QgsDebugMsg( QString( "adding item #%1 / %2 / %3 / %4" ).arg( j ).arg( vlayer->name() ).arg( vlayer->attributeDisplayName( i ) ).arg( value2 ) );
QTableWidgetItem *item = new QTableWidgetItem( vlayer->name() );
item->setData( Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( vlayer ) ) );
tblResults->setItem( j, 0, item );
item = new QTableWidgetItem( FID_TO_STRING( f.id() ) );
item->setData( Qt::UserRole, FID_TO_STRING( f.id() ) );
item->setData( Qt::UserRole + 1, mFeatures.size() );
tblResults->setItem( j, 1, item );
item = new QTableWidgetItem( QString::number( i ) );
if ( fields[i].name() == vlayer->displayField() )
item->setData( Qt::DisplayRole, vlayer->attributeDisplayName( i ) + " *" );
else
item->setData( Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
item->setData( Qt::UserRole, fields[i].name() );
item->setData( Qt::UserRole + 1, i );
tblResults->setItem( j, 2, item );
item = new QTableWidgetItem( value );
item->setData( Qt::UserRole, value );
item->setData( Qt::DisplayRole, value2 );
tblResults->setItem( j, 3, item );
j++;
}
tblResults->resizeColumnToContents( 1 );
highlightFeature( featItem );
}
@ -591,6 +650,24 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
derivedItem->addChild( new QTreeWidgetItem( QStringList() << it.key() << it.value() ) );
}
}
// table
int j = tblResults->rowCount();
tblResults->setRowCount( j + attributes.count() );
int i = 1;
for ( QMap<QString, QString>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
{
QgsDebugMsg( QString( "adding item #%1 / %1 / %2 / %3" ).arg( j ).arg( layer->name() ).arg( it.key() ).arg( it.value() ) );
QTableWidgetItem *item = new QTableWidgetItem( layer->name() );
item->setData( Qt::UserRole, QVariant::fromValue( qobject_cast<QObject *>( layer ) ) );
tblResults->setItem( j, 0, item );
tblResults->setItem( j, 1, new QTableWidgetItem( QString::number( i ) ) );
tblResults->setItem( j, 2, new QTableWidgetItem( it.key() ) );
tblResults->setItem( j, 3, new QTableWidgetItem( it.value() ) );
j++; i++;
}
tblResults->resizeColumnToContents( 1 );
}
void QgsIdentifyResultsDialog::editingToggled()
@ -851,6 +928,9 @@ void QgsIdentifyResultsDialog::clear()
lstResults->clear();
clearHighlights();
tblResults->clearContents();
tblResults->setRowCount( 0 );
// keep it visible but disabled, it can switch from disabled/enabled
// after raster format change
mPrintToolButton->setDisabled( true );
@ -1112,6 +1192,23 @@ void QgsIdentifyResultsDialog::layerDestroyed()
disconnectLayer( theSender );
delete layerItem( theSender );
// remove items, starting from last
for ( int i = tblResults->rowCount() - 1; i >= 0; i-- )
{
QgsDebugMsg( QString( "item %1 / %2" ).arg( i ).arg( tblResults->rowCount() ) );
QTableWidgetItem *layItem = tblResults->item( i, 0 );
if ( layItem && layItem->data( Qt::UserRole ).value<QObject *>() == sender() )
{
QgsDebugMsg( QString( "removing row %1" ).arg( i ) );
tblResults->removeRow( i );
}
}
if ( lstResults->topLevelItemCount() == 0 )
{
close();
}
}
void QgsIdentifyResultsDialog::disconnectLayer( QObject *layer )
@ -1158,6 +1255,24 @@ void QgsIdentifyResultsDialog::featureDeleted( QgsFeatureId fid )
{
delete layItem;
}
for ( int i = tblResults->rowCount() - 1; i >= 0; i-- )
{
QgsDebugMsg( QString( "item %1 / %2" ).arg( i ).arg( tblResults->rowCount() ) );
QTableWidgetItem *layItem = tblResults->item( i, 0 );
QTableWidgetItem *featItem = tblResults->item( i, 1 );
if ( layItem && layItem->data( Qt::UserRole ).value<QObject *>() == sender() &&
featItem && STRING_TO_FID( featItem->data( Qt::UserRole ) ) == fid )
{
QgsDebugMsg( QString( "removing row %1" ).arg( i ) );
tblResults->removeRow( i );
}
}
if ( lstResults->topLevelItemCount() == 0 )
{
close();
}
}
void QgsIdentifyResultsDialog::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &val )

View File

@ -18,21 +18,88 @@
<number>2</number>
</property>
<item>
<widget class="QTreeWidget" name="lstResults">
<property name="lineWidth">
<number>2</number>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tree</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="lstResults">
<property name="lineWidth">
<number>2</number>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Table</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QTableWidget" name="tblResults">
<column>
<property name="text">
<string>Layer</string>
</property>
</column>
<column>
<property name="text">
<string>FID</string>
</property>
</column>
<column>
<property name="text">
<string>Attribute</string>
</property>
</column>
<column>
<property name="text">
<string>Value</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Graph</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QwtPlot" name="mPlot"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
@ -212,6 +279,14 @@
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QwtPlot</class>
<extends>QFrame</extends>
<header>qwt_plot.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../images/images.qrc"/>
</resources>