QGIS API Documentation  master-59fd5e0
src/gui/symbology-ng/qgscptcitycolorrampv2dialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgscptcitycolorrampv2dialog.cpp
00003     ---------------------
00004     begin                : July 2012
00005     copyright            : (C) 2012 by Etienne Tourigny
00006     email                : etourigny dot dev at gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgscptcitycolorrampv2dialog.h"
00017 
00018 #include "qgscptcityarchive.h"
00019 #include "qgsvectorcolorrampv2.h"
00020 #include "qgslogger.h"
00021 #include "qgsapplication.h"
00022 #include "qgsdialog.h"
00023 
00024 #include <QPushButton>
00025 #include <QTextEdit>
00026 #include <QTime>
00027 #include <QMessageBox>
00028 #include <QSortFilterProxyModel>
00029 
00031 
00032 // TODO
00033 // - fix Diverging children when first show Selections
00034 // - fix crash on Diverging?
00035 
00036 class TreeFilterProxyModel : public QSortFilterProxyModel
00037 {
00038     //  Q_OBJECT
00039 
00040   public:
00041     TreeFilterProxyModel( QObject *parent, QgsCptCityBrowserModel* model )
00042         : QSortFilterProxyModel( parent ), mModel( model )
00043     { setSourceModel( mModel ); }
00044 
00045   protected:
00046     bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
00047     {
00048       QgsCptCityDataItem* item = mModel->dataItem( mModel->index( sourceRow, 0, sourceParent ) );
00049       return ( item && ! item->type() == QgsCptCityDataItem::ColorRamp );
00050     }
00051     // bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
00052 
00053   private:
00054     QgsCptCityBrowserModel* mModel;
00055 };
00056 
00057 
00058 // ----------------------
00059 
00060 QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2* ramp, QWidget* parent )
00061     : QDialog( parent ), mRamp( 0 )
00062 {
00063   setupUi( this );
00064 
00065   buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
00066 
00067   QSettings settings;
00068   restoreGeometry( settings.value( "/Windows/CptCityColorRampV2Dialog/geometry" ).toByteArray() );
00069   mSplitter->setSizes( QList<int>() << 250 << 550 );
00070   mSplitter->restoreState( settings.value( "/Windows/CptCityColorRampV2Dialog/splitter" ).toByteArray() );
00071 
00072   mModel = mAuthorsModel = mSelectionsModel = 0; //mListModel = 0;
00073   mTreeFilter = 0;
00074 
00075   QgsCptCityArchive::initDefaultArchive();
00076   mArchive = QgsCptCityArchive::defaultArchive();
00077 
00078   // show information on how to install cpt-city files if none are found
00079   if ( ! mArchive || mArchive->isEmpty() )
00080   {
00081     // QgsDialog dlg( this );
00082     // dlg.setWindowTitle( tr( "cpt-city gradient files not found" ) );
00083     QTextEdit *edit = new QTextEdit( 0 );
00084     edit->setReadOnly( true );
00085     // not sure if we want this long string to be translated
00086     QString helpText = tr( "Error - cpt-city gradient files not found.\n\n"
00087                            "You have two means of installing them:\n\n"
00088                            "1) Install the \"Color Ramp Manager\" python plugin "
00089                            "(you must enable Experimental plugins in the plugin manager) "
00090                            "and use it to download latest cpt-city package.\n"
00091                            "You can install the entire cpt-city archive or a selection for QGIS.\n\n"
00092                            "2) Download the complete archive (in svg format) "
00093                            "and unzip it to your QGIS settings directory [%1] .\n\n"
00094                            "This file can be found at [%2]\nand current file is [%3]"
00095                          ).arg( QgsApplication::qgisSettingsDirPath()
00096                               ).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/"
00097                                    ).arg( "http://soliton.vm.bytemark.co.uk/pub/cpt-city/pkg/cpt-city-svg-2.07.zip" );
00098     edit->setText( helpText );
00099     mStackedWidget->addWidget( edit );
00100     mStackedWidget->setCurrentIndex( 1 );
00101     tabBar->setVisible( false );
00102     // dlg.layout()->addWidget( edit );
00103     // dlg.resize(500,400);
00104     // dlg.exec();
00105     return;
00106   }
00107 
00108   if ( ! mArchive )
00109     return;
00110   QgsDebugMsg( "archive: " + mArchive->archiveName() );
00111 
00112   if ( ramp )
00113   {
00114     mRamp = ramp;
00115   }
00116   else
00117   {
00118     mRamp = new QgsCptCityColorRampV2( "", "", false );
00119     ramp = mRamp;
00120   }
00121   QgsDebugMsg( QString( "ramp name= %1 variant= %2 - %3 variants" ).arg( ramp->schemeName() ).arg( ramp->variantName() ).arg( ramp->variantList().count() ) );
00122 
00123   // model / view
00124   QgsDebugMsg( "loading model/view objects" );
00125   if ( mAuthorsModel )
00126     delete mAuthorsModel;
00127   mAuthorsModel = new QgsCptCityBrowserModel( this, mArchive, QgsCptCityBrowserModel::Authors );
00128   if ( mSelectionsModel )
00129     delete mSelectionsModel;
00130   mSelectionsModel = new QgsCptCityBrowserModel( this, mArchive, QgsCptCityBrowserModel::Selections );
00131   setTreeModel( mSelectionsModel );
00132 
00133   mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
00134   mTreeView->setColumnHidden( 1, true );
00135   QgsDebugMsg( "done loading model/view objects" );
00136 
00137   // setup ui
00138   tabBar->blockSignals( true );
00139   tabBar->addTab( tr( "Selections by theme" ) );
00140   tabBar->addTab( tr( "All by author" ) );
00141   cboVariantName->setIconSize( QSize( 100, 15 ) );
00142   lblPreview->installEventFilter( this ); // mouse click on preview label shows svg render
00143 
00144   // look for item, if not found in selections archive, look for in authors
00145   QgsDebugMsg( "looking for ramp " + mRamp->schemeName() );
00146   if ( mRamp->schemeName() != "" )
00147   {
00148     bool found = false;
00149     found = updateRamp();
00150     if ( ! found )
00151     {
00152       tabBar->setCurrentIndex( 1 );
00153       setTreeModel( mAuthorsModel );
00154       found = updateRamp();
00155       // if not found, go back to selections model
00156       if ( ! found )
00157       {
00158         tabBar->setCurrentIndex( 0 );
00159         setTreeModel( mSelectionsModel );
00160       }
00161     }
00162     if ( found )
00163       buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
00164   }
00165 
00166   tabBar->blockSignals( false );
00167 
00168   connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished() ) );
00169 
00170 }
00171 
00172 QgsCptCityColorRampV2Dialog::~QgsCptCityColorRampV2Dialog()
00173 {
00174 }
00175 
00176 void QgsCptCityColorRampV2Dialog::populateVariants()
00177 {
00178   QStringList variantList = mRamp->variantList();
00179 
00180   QgsDebugMsg( QString( "ramp %1%2 has %3 variants" ).arg( mRamp->schemeName() ).arg( mRamp->variantName() ).arg( variantList.count() ) );
00181 
00182   cboVariantName->blockSignals( true );
00183   cboVariantName->clear();
00184 
00185   if ( variantList.isEmpty() )
00186   {
00187     cboVariantName->setEnabled( false );
00188     cboVariantName->setVisible( false );
00189     on_cboVariantName_currentIndexChanged( -1 );
00190   }
00191   else
00192   {
00193     // populate variant combobox
00194     QString oldVariant = cboVariantName->currentText();
00195     QgsCptCityColorRampV2 ramp( mRamp->schemeName(), mRamp->variantList(), QString() );
00196     QPixmap blankPixmap( cboVariantName->iconSize() );
00197     blankPixmap.fill( Qt::white );
00198     QIcon blankIcon( blankPixmap );
00199     int index;
00200 
00201     foreach ( QString variant, variantList )
00202     {
00203       QString variantStr = variant;
00204       if ( variantStr.startsWith( "-" ) || variantStr.startsWith( "_" ) )
00205         variantStr.remove( 0, 1 );
00206       cboVariantName->addItem( " " + variantStr );
00207       index = cboVariantName->count() - 1;
00208       cboVariantName->setItemData( index, variant, Qt::UserRole );
00209 
00210       ramp.setVariantName( variant );
00211       if ( ramp.loadFile() )
00212         cboVariantName->setItemIcon( index,
00213                                      QgsSymbolLayerV2Utils::colorRampPreviewIcon( &ramp, cboVariantName->iconSize() ) );
00214       else
00215         cboVariantName->setItemIcon( index, blankIcon );
00216       cboVariantName->setItemData( index, Qt::AlignHCenter, Qt::TextAlignmentRole );
00217     }
00218 
00219     cboVariantName->blockSignals( false );
00220 
00221     // try to set the original variant again (if exists)
00222     int idx = -1;
00223     QString newVariant = mRamp->variantName();
00224     QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp->variantName() ).arg( mRamp->variantList().count() ) );
00225     if ( newVariant != QString() )
00226     {
00227       if ( newVariant.startsWith( "-" ) || newVariant.startsWith( "_" ) )
00228         newVariant.remove( 0, 1 );
00229       newVariant = " " + newVariant;
00230       idx = cboVariantName->findText( newVariant );
00231     }
00232     else
00233       idx = cboVariantName->findText( oldVariant );
00234 
00235     // if not found use the item in the middle
00236     if ( idx == -1 )
00237     {
00238       idx = cboVariantName->count() / 2;
00239     }
00240     cboVariantName->setCurrentIndex( idx );
00241     // updatePreview();
00242 
00243     cboVariantName->setEnabled( true );
00244     cboVariantName->setVisible( true );
00245   }
00246 
00247 }
00248 
00249 void QgsCptCityColorRampV2Dialog::on_mTreeView_clicked( const QModelIndex &index )
00250 {
00251   const QModelIndex &sourceIndex = mTreeFilter->mapToSource( index );
00252   QgsCptCityDataItem *item = mModel->dataItem( sourceIndex );
00253   if ( ! item )
00254     return;
00255   QgsDebugMsg( QString( "item %1 clicked" ).arg( item->name() ) );
00256   buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
00257   updateTreeView( item );
00258 }
00259 
00260 void QgsCptCityColorRampV2Dialog::updateTreeView( QgsCptCityDataItem *item, bool resetRamp )
00261 {
00262   if ( ! item )
00263   {
00264     QgsDebugMsg( "invalid item" );
00265     return;
00266   }
00267   if ( item->type() == QgsCptCityDataItem::Directory )
00268   {
00269     if ( resetRamp )
00270     {
00271       mRamp->setName( "", "" );
00272       QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp->variantName() ).arg( mRamp->variantList().count() ) );
00273       lblSchemeName->setText( "" );
00274       populateVariants();
00275     }
00276     updateListWidget( item );
00277     lblSchemePath->setText( item->path() );
00278     lblCollectionInfo->setText( QString( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
00279     updateCopyingInfo( mArchive->copyingInfo( mArchive->copyingFileName( item->path() ) ) );
00280   }
00281   else if ( item->type() == QgsCptCityDataItem::Selection )
00282   {
00283     lblSchemePath->setText( "" );
00284     clearCopyingInfo( );
00285     updateListWidget( item );
00286     lblCollectionInfo->setText( QString( "%1 (%2)" ).arg( item->info() ).arg( item->leafCount() ) );
00287   }
00288   else if ( item->type() == QgsCptCityDataItem::AllRamps )
00289   {
00290     lblSchemePath->setText( "" );
00291     clearCopyingInfo( );
00292     updateListWidget( item );
00293     lblCollectionInfo->setText( tr( "All Ramps (%1)" ).arg( item->leafCount() ) );
00294   }
00295   else
00296   {
00297     QgsDebugMsg( QString( "item %1 has invalid type %2" ).arg( item->path() ).arg(( int )item->type() ) );
00298   }
00299 }
00300 
00301 void QgsCptCityColorRampV2Dialog::on_mListWidget_itemClicked( QListWidgetItem * item )
00302 {
00303   QgsCptCityColorRampItem *rampItem = mListRamps.at( item->data( Qt::UserRole ).toInt() );
00304   if ( rampItem )
00305   {
00306     buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
00307     lblSchemeName->setText( QFileInfo( rampItem->name() ).fileName() );
00308     mRamp->copy( &rampItem->ramp() );
00309     QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp->variantName() ).arg( mRamp->variantList().count() ) );
00310     populateVariants();
00311   }
00312   else
00313   {
00314     QgsDebugMsg( QString( "item %1 has invalid type %2" ).arg( rampItem->path() ).arg(( int )rampItem->type() ) );
00315   }
00316 }
00317 
00318 void QgsCptCityColorRampV2Dialog::on_tabBar_currentChanged( int index )
00319 {
00320   if ( index == 0 )
00321   {
00322     setTreeModel( mSelectionsModel );
00323     mArchiveViewType = QgsCptCityBrowserModel::Selections;
00324   }
00325   else if ( index == 1 )
00326   {
00327     setTreeModel( mAuthorsModel );
00328     mArchiveViewType = QgsCptCityBrowserModel::Authors;
00329   }
00330   else
00331   {
00332     QgsDebugMsg( QString( "invalid index %1" ).arg( index ) );
00333     setTreeModel( mAuthorsModel );
00334     mArchiveViewType = QgsCptCityBrowserModel::Authors;
00335   }
00336 
00337   updateRamp();
00338 }
00339 
00340 
00341 void QgsCptCityColorRampV2Dialog::on_pbtnLicenseDetails_pressed()
00342 {
00343   QString path, title, copyFile, descFile;
00344 
00345   // get basic information, depending on if is color ramp or directory
00346   QgsCptCityDataItem *item =
00347     dynamic_cast< QgsCptCityDataItem* >( mModel->dataItem( mTreeFilter->mapToSource( mTreeView->currentIndex() ) ) );
00348   if ( ! item )
00349     return;
00350 
00351   path = item->path();
00352   if ( item->type() == QgsCptCityDataItem::Directory )
00353   {
00354     title = tr( "%1 directory details" ).arg( item->path() );
00355   }
00356   else if ( item->type() == QgsCptCityColorRampItem::Directory )
00357   {
00358     title = tr( "%1 gradient details" ).arg( path );
00359   }
00360   else
00361   {
00362     return;
00363   }
00364   copyFile = mArchive->copyingFileName( path );
00365   descFile = mArchive->descFileName( path );
00366 
00367   // prepare dialog
00368   QgsDialog dlg( this, 0, QDialogButtonBox::Close );
00369   QVBoxLayout *layout = dlg.layout();
00370   dlg.setWindowTitle( title );
00371   QTextEdit *textEdit = new QTextEdit( &dlg );
00372   textEdit->setReadOnly( true );
00373   layout->addWidget( textEdit );
00374 
00375   // add contents of DESC.xml and COPYING.xml
00376   QString copyText;
00377   if ( ! copyFile.isNull() )
00378   {
00379     QFile file( copyFile );
00380     if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
00381     {
00382       copyText = QString( file.readAll() );
00383       file.close();
00384     }
00385   }
00386   QString descText;
00387   if ( ! descFile.isNull() )
00388   {
00389     QFile file( descFile );
00390     if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
00391     {
00392       descText = QString( file.readAll() );
00393       file.close();
00394     }
00395   }
00396   textEdit->insertPlainText( title + "\n\n" );
00397   textEdit->insertPlainText( "===================" );
00398   textEdit->insertPlainText( " copying " );
00399   textEdit->insertPlainText( "===================\n" );
00400   textEdit->insertPlainText( copyText );
00401   textEdit->insertPlainText( "\n" );
00402   textEdit->insertPlainText( "==================" );
00403   textEdit->insertPlainText( " description " );
00404   textEdit->insertPlainText( "==================\n" );
00405   textEdit->insertPlainText( descText );
00406   textEdit->moveCursor( QTextCursor::Start );
00407 
00408   dlg.resize( 600, 600 );
00409   dlg.exec();
00410 }
00411 
00412 void QgsCptCityColorRampV2Dialog::updatePreview( bool clear )
00413 {
00414   QSize size = lblPreview->size();
00415 
00416   if ( clear || mRamp->schemeName() == "" )
00417   {
00418     lblSchemeName->setText( "" );
00419     lblSchemePath->setText( "" );
00420     lblLicensePreview->setText( "" );
00421     QPixmap blankPixmap( size );
00422     blankPixmap.fill( Qt::transparent );
00423     lblPreview->setPixmap( blankPixmap );
00424     return;
00425   }
00426 
00427   mRamp->loadFile();
00428 
00429   lblSchemePath->setText( mRamp->schemeName() + mRamp->variantName() );
00430 
00431   // update pixmap
00432   // TODO draw checker-board/transparent background
00433   // for transparent, add  [ pixmap.fill( Qt::transparent ); ] to QgsSymbolLayerV2Utils::colorRampPreviewPixmap
00434   QPixmap pixmap = QgsSymbolLayerV2Utils::colorRampPreviewPixmap( mRamp, size );
00435   lblPreview->setPixmap( pixmap );
00436 
00437   // add copyright information from COPYING.xml file
00438   updateCopyingInfo( mRamp->copyingInfo() );
00439 }
00440 
00441 void QgsCptCityColorRampV2Dialog::clearCopyingInfo()
00442 {
00443   updateCopyingInfo( QMap< QString, QString >() );
00444 }
00445 
00446 void QgsCptCityColorRampV2Dialog::updateCopyingInfo( const QMap< QString, QString >& copyingMap )
00447 {
00448   QString authorStr = copyingMap.value( "authors" );
00449   if ( authorStr.length() > 80 )
00450     authorStr.replace( authorStr.indexOf( " ", 80 ), 1, "\n" );
00451   lblAuthorName->setText( authorStr );
00452   QString licenseStr = copyingMap.value( "license/informal" );
00453   if ( copyingMap.contains( "license/year" ) )
00454     licenseStr += " (" + copyingMap.value( "license/year" ) + ")";
00455   if ( licenseStr.length() > 80 )
00456     licenseStr.replace( licenseStr.indexOf( " ", 80 ), 1, "\n" );
00457   if ( copyingMap.contains( "license/url" ) )
00458     licenseStr += "\n[ " + copyingMap.value( "license/url" ) + " ]";
00459   else
00460     licenseStr += "\n";
00461   lblLicenseName->setText( licenseStr );
00462   licenseStr.replace( "\n", "  " );
00463   lblLicensePreview->setText( licenseStr );
00464   lblLicensePreview->setCursorPosition( 0 );
00465   if ( copyingMap.contains( "src/link" ) )
00466     lblSrcLink->setText( copyingMap.value( "src/link" ) );
00467   else
00468     lblSrcLink->setText( "" );
00469 }
00470 
00471 void QgsCptCityColorRampV2Dialog::on_cboVariantName_currentIndexChanged( int index )
00472 {
00473   Q_UNUSED( index );
00474   if ( cboVariantName->currentIndex() != -1 )
00475     mRamp->setVariantName( cboVariantName->itemData( cboVariantName->currentIndex(), Qt::UserRole ).toString() );
00476   QgsDebugMsg( QString( "variant= %1 - %2 variants" ).arg( mRamp->variantName() ).arg( mRamp->variantList().count() ) );
00477   updatePreview();
00478 }
00479 
00480 void QgsCptCityColorRampV2Dialog::onFinished()
00481 {
00482   // save settings
00483   QSettings settings;
00484   settings.setValue( "/Windows/CptCityColorRampV2Dialog/geometry", saveGeometry() );
00485   settings.setValue( "/Windows/CptCityColorRampV2Dialog/splitter", mSplitter->saveState() );
00486 }
00487 
00488 void QgsCptCityColorRampV2Dialog::on_buttonBox_helpRequested()
00489 {
00490   // show error message to use color ramp manager to get more gradients
00491   QString helpText = tr( "You can download a more complete set of cpt-city gradients "
00492                          "by installing the \"Color Ramp Manager\" plugin "
00493                          "(you must enable Experimental plugins in the plugin manager).\n\n"
00494                        );
00495   QMessageBox* msg = new QMessageBox( this );
00496   msg->setText( helpText );
00497   msg->exec();
00498 }
00499 
00500 bool QgsCptCityColorRampV2Dialog::saveAsGradientRamp() const
00501 {
00502   QgsDebugMsg( QString( "result: %1 checked: %2" ).arg( result() ).arg( cboConvertStandard->isChecked() ) );
00503   // if "save as standard gradient" is checked, convert to QgsVectorGradientColorRampV2
00504   return ( result() == Accepted && cboConvertStandard->isChecked() );
00505 }
00506 
00507 void QgsCptCityColorRampV2Dialog::updateListWidget( QgsCptCityDataItem *item )
00508 {
00509   mListWidget->clear();
00510   mListRamps.clear();
00511   QgsCptCityCollectionItem* colItem = dynamic_cast<QgsCptCityCollectionItem*>( item );
00512   if ( colItem )
00513   {
00514     QgsDebugMsg( "path= " + item->path() );
00515     // recursively get children ramps
00516     QVector<QgsCptCityDataItem*> childrenRamps = colItem->childrenRamps( true );
00517     for ( int i = 0; i < childrenRamps.count(); i++ )
00518     {
00519       QgsCptCityColorRampItem* rampItem = dynamic_cast<QgsCptCityColorRampItem*>( childrenRamps[i] );
00520       if ( ! rampItem )
00521       {
00522         QgsDebugMsg( "invalid item " + childrenRamps[i]->path() );
00523         continue;
00524       }
00525       QListWidgetItem* listItem = new QListWidgetItem();
00526       listItem->setText( rampItem->shortInfo() );
00527       listItem->setIcon( rampItem->icon( QSize( 75, 50 ) ) );
00528       listItem->setToolTip( rampItem->path() + "\n" + rampItem->info() );
00529       listItem->setData( Qt::UserRole, QVariant( i ) );
00530       mListWidget->addItem( listItem );
00531       mListRamps << rampItem;
00532     }
00533   }
00534   else
00535   {
00536     QgsDebugMsg( "invalid item" );
00537   }
00538 }
00539 
00540 // this function is for a svg preview, available if the svg files have been processed with svgx
00541 // e.g. for f in `ls */*/*/*/*.svg`; do echo $f ; svgx -p -t svg $f > tmp1.svg; mv tmp1.svg $f; done
00542 // perhaps a future version of the cpt-city svg gradients will have them by default
00543 bool QgsCptCityColorRampV2Dialog::eventFilter( QObject *obj, QEvent *event )
00544 {
00545   QSize size = lblPreview->size();
00546 
00547   if ( event->type() == QEvent::MouseButtonPress )
00548   {
00549     // create preview from svg file if supported - depends on file versions
00550     QPixmap pixmap( mRamp->fileName() );
00551     if ( ! pixmap.isNull() )
00552       lblPreview->setPixmap( pixmap.scaled( size ) );
00553     return true;
00554   }
00555   else if ( event->type() == QEvent::MouseButtonRelease )
00556   {
00557     // restore preview
00558     QPixmap pixmap = QgsSymbolLayerV2Utils::colorRampPreviewPixmap( mRamp, size );
00559     lblPreview->setPixmap( pixmap );
00560     return true;
00561   }
00562   else
00563   {
00564     // standard event processing
00565     return QObject::eventFilter( obj, event );
00566   }
00567 }
00568 
00569 bool QgsCptCityColorRampV2Dialog::updateRamp()
00570 {
00571   mListWidget->clear();
00572   mListRamps.clear();
00573   cboVariantName->clear();
00574   clearCopyingInfo( );
00575   lblCollectionInfo->clear();
00576 
00577   buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
00578   updatePreview( true );
00579 
00580   QgsDebugMsg( "schemeName= " + mRamp->schemeName() );
00581   if ( mRamp->schemeName() == "" )
00582     return false;
00583 
00584   // search for item in model
00585   QModelIndex modelIndex = mModel->findPath( mRamp->schemeName() );
00586   if ( modelIndex == QModelIndex() )
00587     return false;
00588   QgsCptCityColorRampItem* childItem =
00589     dynamic_cast<QgsCptCityColorRampItem*>( mModel->dataItem( modelIndex ) );
00590   if ( ! childItem )
00591     return false;
00592   if ( mRamp->schemeName() != childItem->ramp().schemeName() )
00593     return false;
00594 
00595   // found child, set mRamp variantList
00596   // mRamp->copy( &childItem->ramp() );
00597   mRamp->setVariantList( childItem->ramp().variantList() );
00598 
00599   // found child, update tree
00600   QgsDebugMsg( QString( "found item %1" ).arg( mRamp->schemeName() ) );
00601   lblSchemeName->setText( QFileInfo( mRamp->schemeName() ).fileName() );
00602   QModelIndex parentIndex = modelIndex.parent();
00603   QModelIndex selIndex = mTreeFilter->mapFromSource( parentIndex );
00604 
00605   // QgsDebugMsg(QString("parent row=%1 path=%2 parentRow=%3").arg(parentIndex.row()).arg(mModel->dataItem( parentIndex )->path()).arg(parentIndex.parent().row()));
00606   mTreeView->setCurrentIndex( selIndex );
00607   mTreeView->setExpanded( selIndex, true );
00608   mTreeView->scrollTo( selIndex, QAbstractItemView::PositionAtCenter );
00609   updateTreeView( mModel->dataItem( parentIndex ), false );
00610 
00611   // update listWidget, find appropriate item in mListRamps
00612   for ( int i = 0; i < mListRamps.count(); i++ )
00613   {
00614     if ( mListRamps[i] == childItem )
00615     {
00616       QgsDebugMsg( QString( "found matching item %1 target=%2" ).arg( mListRamps[i]->path() ).arg( childItem->path() ) );
00617       QListWidgetItem* listItem = mListWidget->item( i );
00618       mListWidget->setCurrentItem( listItem );
00619       // on_mListWidget_itemClicked( listItem );
00620       populateVariants();
00621       mListWidget->scrollToItem( listItem, QAbstractItemView::EnsureVisible );
00622       // mListView->selectionModel()->select( childIndex, QItemSelectionModel::Select );
00623       buttonBox->button( QDialogButtonBox::Ok )->setEnabled( true );
00624       return true;
00625     }
00626   }
00627 
00628   return false;
00629 }
00630 
00631 void QgsCptCityColorRampV2Dialog::setTreeModel( QgsCptCityBrowserModel* model )
00632 {
00633   mModel = model;
00634 
00635   if ( mTreeFilter )
00636     delete mTreeFilter;
00637   mTreeFilter = new TreeFilterProxyModel( this, mModel );
00638   mTreeView->setModel( mTreeFilter );
00639 }
00640 
00641 #if 0
00642 void QgsCptCityColorRampV2Dialog::refresh()
00643 {
00644   QApplication::setOverrideCursor( Qt::WaitCursor );
00645   refreshModel( QModelIndex() );
00646   QApplication::restoreOverrideCursor();
00647 }
00648 
00649 void QgsCptCityColorRampV2Dialog::refreshModel( const QModelIndex& index )
00650 {
00651   if ( index.isValid() )
00652   {
00653     QgsCptCityDataItem *item = mModel->dataItem( index );
00654     if ( item )
00655     {
00656       QgsDebugMsg( "path = " + item->path() );
00657     }
00658     else
00659     {
00660       QgsDebugMsg( "invalid item" );
00661     }
00662   }
00663 
00664   mModel->refresh( index );
00665 
00666   for ( int i = 0 ; i < mModel->rowCount( index ); i++ )
00667   {
00668     QModelIndex idx = mModel->index( i, 0, index );
00669     if ( mTreeView->isExpanded( idx ) || !mModel->hasChildren( idx ) )
00670     {
00671       refreshModel( idx );
00672     }
00673   }
00674 }
00675 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines