00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "qgsidentifyresults.h"
00021 #include "qgscontexthelp.h"
00022 #include "qgsapplication.h"
00023 #include "qgisapp.h"
00024
00025 #include <QCloseEvent>
00026 #include <QLabel>
00027 #include <QAction>
00028 #include <QTreeWidgetItem>
00029 #include <QPixmap>
00030 #include <QSettings>
00031 #include <QMenu>
00032
00033 #include <iostream>
00034
00035 QgsIdentifyResults::QgsIdentifyResults( const QgsAttributeAction& actions,
00036 QWidget *parent, Qt::WFlags f )
00037 : QDialog( parent, f ),
00038 mActions( actions ),
00039 mClickedOnValue( 0 ),
00040 mActionPopup( 0 ),
00041 mCurrentFeatureId( 0 )
00042 {
00043 setupUi( this );
00044 lstResults->setColumnCount( 2 );
00045 setColumnText( 0, tr( "Feature" ) );
00046 setColumnText( 1, tr( "Value" ) );
00047
00048 connect( buttonCancel, SIGNAL( clicked() ),
00049 this, SLOT( close() ) );
00050 connect( lstResults, SIGNAL( itemClicked( QTreeWidgetItem*, int ) ),
00051 this, SLOT( clicked( QTreeWidgetItem * ) ) );
00052 connect( lstResults, SIGNAL( itemExpanded( QTreeWidgetItem* ) ),
00053 this, SLOT( itemExpanded( QTreeWidgetItem* ) ) );
00054
00055 connect( lstResults, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
00056 this, SLOT( handleCurrentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );
00057
00058
00059 mDerivedLabel = tr( "(Derived)" );
00060 }
00061
00062 QgsIdentifyResults::~QgsIdentifyResults()
00063 {
00064 delete mActionPopup;
00065 }
00066
00067
00068 void QgsIdentifyResults::show()
00069 {
00070
00071 lstResults->sortItems( 0, Qt::AscendingOrder );
00072 expandColumnsToFit();
00073
00074 QDialog::show();
00075 }
00076
00077
00078 void QgsIdentifyResults::close()
00079 {
00080 saveWindowLocation();
00081 done( 0 );
00082 }
00083
00084
00085 void QgsIdentifyResults::closeEvent( QCloseEvent *e )
00086 {
00087
00088 e->ignore();
00089 close();
00090 }
00091
00092
00093
00094
00095
00096 void QgsIdentifyResults::contextMenuEvent( QContextMenuEvent* event )
00097 {
00098 QTreeWidgetItem* item = lstResults->itemAt( lstResults->viewport()->mapFrom( this, event->pos() ) );
00099
00100 if ( item == NULL )
00101 return;
00102
00103
00104
00105
00106
00107 if ( mActionPopup == 0 )
00108 {
00109 mActionPopup = new QMenu();
00110 QAction* a = mActionPopup->addAction( tr( "Run action" ) );
00111 a->setEnabled( false );
00112 mActionPopup->addSeparator();
00113
00114 QgsAttributeAction::aIter iter = mActions.begin();
00115 for ( int j = 0; iter != mActions.end(); ++iter, ++j )
00116 {
00117 QAction* a = mActionPopup->addAction( iter->name() );
00118
00119
00120 a->setData( QVariant::fromValue( j ) );
00121 }
00122 connect( mActionPopup, SIGNAL( triggered( QAction* ) ),
00123 this, SLOT( popupItemSelected( QAction* ) ) );
00124 }
00125
00126
00127 extractAllItemData( item );
00128
00129 if ( mActions.size() > 0 )
00130 mActionPopup->popup( event->globalPos() );
00131 }
00132
00133
00134 void QgsIdentifyResults::restorePosition()
00135 {
00136 QSettings settings;
00137 restoreGeometry( settings.value( "/Windows/Identify/geometry" ).toByteArray() );
00138 show();
00139 }
00140
00141
00142 void QgsIdentifyResults::saveWindowLocation()
00143 {
00144 QSettings settings;
00145 settings.setValue( "/Windows/Identify/geometry", saveGeometry() );
00146 }
00147
00149 void QgsIdentifyResults::addAttribute( QTreeWidgetItem * fnode, QString field, QString value )
00150 {
00151 QStringList labels;
00152 labels << field << value;
00153 new QTreeWidgetItem( fnode, labels );
00154 }
00155
00156 void QgsIdentifyResults::addAttribute( QString field, QString value )
00157 {
00158 QStringList labels;
00159 labels << field << value;
00160 new QTreeWidgetItem( lstResults, labels );
00161 }
00162
00163 void QgsIdentifyResults::addDerivedAttribute( QTreeWidgetItem * fnode, QString field, QString value )
00164 {
00165 QTreeWidgetItem * daRootNode;
00166
00167
00168 if ( mDerivedAttributeRootNodes.find( fnode ) != mDerivedAttributeRootNodes.end() )
00169 {
00170
00171 daRootNode = mDerivedAttributeRootNodes[fnode];
00172 }
00173 else
00174 {
00175
00176 daRootNode = new QTreeWidgetItem( fnode, QStringList( mDerivedLabel ) );
00177 QFont font = daRootNode->font( 0 );
00178 font.setItalic( true );
00179 daRootNode->setFont( 0, font );
00180 mDerivedAttributeRootNodes[fnode] = daRootNode;
00181 }
00182
00183 QStringList labels;
00184 labels << field << value;
00185 new QTreeWidgetItem( daRootNode, labels );
00186 }
00187
00188 void QgsIdentifyResults::addEdit( QTreeWidgetItem * fnode, int id )
00189 {
00190 QStringList labels;
00191 labels << "edit" << QString::number( id );
00192 QTreeWidgetItem *item = new QTreeWidgetItem( fnode, labels );
00193
00194 item->setIcon( 0, QgisApp::getThemeIcon( "/mIconEditable.png" ) );
00195 }
00196
00197 void QgsIdentifyResults::addAction( QTreeWidgetItem * fnode, int id, QString field, QString value )
00198 {
00199 QStringList labels;
00200 labels << field << value << "action" << QString::number( id );
00201 QTreeWidgetItem *item = new QTreeWidgetItem( fnode, labels );
00202
00203 item->setIcon( 0, QgisApp::getThemeIcon( "/mAction.png" ) );
00204 }
00205
00207 QTreeWidgetItem *QgsIdentifyResults::addNode( QString label )
00208 {
00209 return new QTreeWidgetItem( lstResults, QStringList( label ) );
00210 }
00211
00212 void QgsIdentifyResults::setTitle( QString title )
00213 {
00214 setWindowTitle( tr( "Identify Results - " ) + title );
00215 }
00216
00217 void QgsIdentifyResults::setColumnText( int column, const QString & label )
00218 {
00219 QTreeWidgetItem* header = lstResults->headerItem();
00220 header->setText( column, label );
00221 }
00222
00223
00224 void QgsIdentifyResults::popupItemSelected( QAction* menuAction )
00225 {
00226 int id = menuAction->data().toInt();
00227 mActions.doAction( id, mValues, mClickedOnValue );
00228 }
00229
00231 void QgsIdentifyResults::showAllAttributes()
00232 {
00233
00234 lstResults->expandAll();
00235 }
00236
00237 void QgsIdentifyResults::expandColumnsToFit()
00238 {
00239 lstResults->resizeColumnToContents( 0 );
00240 lstResults->resizeColumnToContents( 1 );
00241 }
00242
00243 void QgsIdentifyResults::clear()
00244 {
00245 mDerivedAttributeRootNodes.clear();
00246 lstResults->clear();
00247 }
00248
00249 void QgsIdentifyResults::setMessage( QString shortMsg, QString longMsg )
00250 {
00251 QStringList labels;
00252 labels << shortMsg << longMsg;
00253 new QTreeWidgetItem( lstResults, labels );
00254 }
00255
00256 void QgsIdentifyResults::setActions( const QgsAttributeAction& actions )
00257 {
00258 mActions = actions;
00259 }
00260
00261 void QgsIdentifyResults::clicked( QTreeWidgetItem *item )
00262 {
00263 if ( !item )
00264 return;
00265
00266 if ( item->text( 2 ) == "action" )
00267 {
00268 int id = item->text( 3 ).toInt();
00269
00270 extractAllItemData( item );
00271
00272 mActions.doAction( id, mValues, mClickedOnValue );
00273 }
00274 else if ( item->text( 0 ) == "edit" )
00275 {
00276 emit editFeature( item->text( 1 ).toInt() );
00277 }
00278 }
00279 void QgsIdentifyResults::on_buttonHelp_clicked()
00280 {
00281 QgsContextHelp::run( context_id );
00282 }
00283
00284 void QgsIdentifyResults::itemExpanded( QTreeWidgetItem* item )
00285 {
00286 expandColumnsToFit();
00287 }
00288
00289 void QgsIdentifyResults::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous )
00290 {
00291 if ( lstResults->model()->rowCount() <= 1 )
00292 return;
00293
00294 if ( current == NULL )
00295 {
00296 mCurrentFeatureId = 0;
00297 emit selectedFeatureChanged( 0 );
00298 return;
00299 }
00300
00301
00302 QTreeWidgetItem* topLevelItem = current;
00303 while ( topLevelItem->parent() != NULL )
00304 {
00305 topLevelItem = topLevelItem->parent();
00306 }
00307
00308 QVariant fid = topLevelItem->data( 0, Qt::UserRole );
00309
00310
00311 if ( fid.type() != QVariant::Int )
00312 return;
00313 int fid2 = fid.toInt();
00314
00315 if ( fid2 == mCurrentFeatureId )
00316 return;
00317
00318 mCurrentFeatureId = fid2;
00319 emit selectedFeatureChanged( mCurrentFeatureId );
00320 }
00321
00322 void QgsIdentifyResults::extractAllItemData( QTreeWidgetItem* item )
00323 {
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 QTreeWidgetItem* child = item;
00334 QTreeWidgetItem* parent = child->parent();
00335 while ( parent != 0 )
00336 {
00337 child = parent;
00338 parent = parent->parent();
00339 }
00340 parent = child;
00341
00342 mValues.clear();
00343
00344
00345
00346
00347
00348
00349
00350
00351 int valuesIndex = 0;
00352
00353 for ( int j = 0; j < parent->childCount(); ++j )
00354 {
00355
00356 if ( parent->child( j )->text( 0 ) == mDerivedLabel )
00357 {
00358 for ( int k = 0; k < parent->child( j )->childCount(); ++k )
00359 {
00360 mValues.push_back(
00361 std::make_pair( mDerivedLabel + "."
00362 + parent->child( j )->child( k )->text( 0 ),
00363 parent->child( j )->child( k )->text( 1 ) ) );
00364
00365 if ( item == parent->child( j )->child( k ) )
00366 {
00367 mClickedOnValue = valuesIndex;
00368 }
00369
00370 valuesIndex++;
00371 }
00372 }
00373 else
00374 {
00375 mValues.push_back( std::make_pair( parent->child( j )->text( 0 ),
00376 parent->child( j )->text( 1 ) ) );
00377
00378 if ( item == parent->child( j ) )
00379 {
00380 mClickedOnValue = valuesIndex;
00381 }
00382
00383 valuesIndex++;
00384 }
00385 }
00386 }