|
QGIS API Documentation
master-3f58142
|
00001 /*************************************************************************** 00002 qgsvectorgradientcolorrampv2dialog.cpp 00003 --------------------- 00004 begin : November 2009 00005 copyright : (C) 2009 by Martin Dobias 00006 email : wonder dot sk at gmail dot 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 "qgsvectorgradientcolorrampv2dialog.h" 00017 00018 #include "qgsvectorcolorrampv2.h" 00019 #include "qgsdialog.h" 00020 #include "qgscolordialog.h" 00021 #include "qgscptcityarchive.h" 00022 00023 #include <QColorDialog> 00024 #include <QInputDialog> 00025 #include <QPainter> 00026 #include <QSettings> 00027 #include <QTableWidget> 00028 #include <QTextEdit> 00029 00030 QgsVectorGradientColorRampV2Dialog::QgsVectorGradientColorRampV2Dialog( QgsVectorGradientColorRampV2* ramp, QWidget* parent ) 00031 : QDialog( parent ), mRamp( ramp ), mCurrentItem( 0 ) 00032 { 00033 setupUi( this ); 00034 #ifdef Q_WS_MAC 00035 setWindowModality( Qt::WindowModal ); 00036 #endif 00037 00038 btnColor1->setColorDialogOptions( QColorDialog::ShowAlphaChannel ); 00039 btnColor2->setColorDialogOptions( QColorDialog::ShowAlphaChannel ); 00040 connect( btnColor1, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor1( const QColor& ) ) ); 00041 connect( btnColor2, SIGNAL( colorChanged( const QColor& ) ), this, SLOT( setColor2( const QColor& ) ) ); 00042 00043 // handle stops 00044 updateStops(); 00045 connect( groupStops, SIGNAL( toggled( bool ) ), this, SLOT( toggledStops( bool ) ) ); 00046 connect( btnAddStop, SIGNAL( clicked() ), this, SLOT( addStop() ) ); 00047 connect( btnRemoveStop, SIGNAL( clicked() ), this, SLOT( removeStop() ) ); 00048 connect( treeStops, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), this, SLOT( stopDoubleClicked( QTreeWidgetItem*, int ) ) ); 00049 00050 // fill type combobox 00051 cboType->blockSignals( true ); 00052 cboType->addItem( tr( "Discrete" ) ); 00053 cboType->addItem( tr( "Continuous" ) ); 00054 if ( mRamp->isDiscrete() ) 00055 cboType->setCurrentIndex( 0 ); 00056 else 00057 cboType->setCurrentIndex( 1 ); 00058 cboType->blockSignals( false ); 00059 00060 // information button if needed 00061 // QPushButton* button = buttonBox->addButton( tr( "Information" ), QDialogButtonBox::ActionRole ); 00062 if ( mRamp->info().isEmpty() ) 00063 btnInformation->setEnabled( false ); 00064 // else 00065 // connect( button, SIGNAL( pressed() ), this, SLOT( showInformation() ) ); 00066 00067 updatePreview(); 00068 } 00069 00070 void QgsVectorGradientColorRampV2Dialog::on_cboType_currentIndexChanged( int index ) 00071 { 00072 if (( index == 0 && mRamp->isDiscrete() ) || 00073 ( index == 1 && !mRamp->isDiscrete() ) ) 00074 return; 00075 mRamp->convertToDiscrete( index == 0 ); 00076 updateStops(); 00077 updatePreview(); 00078 } 00079 00080 void QgsVectorGradientColorRampV2Dialog::on_btnInformation_pressed() 00081 { 00082 if ( mRamp->info().isEmpty() ) 00083 return; 00084 00085 QgsDialog *dlg = new QgsDialog( this ); 00086 QLabel *label = 0; 00087 00088 // information table 00089 QTableWidget *tableInfo = new QTableWidget( dlg ); 00090 tableInfo->verticalHeader()->hide(); 00091 tableInfo->horizontalHeader()->hide(); 00092 tableInfo->setRowCount( mRamp->info().count() ); 00093 tableInfo->setColumnCount( 2 ); 00094 int i = 0; 00095 for ( QgsStringMap::const_iterator it = mRamp->info().constBegin(); 00096 it != mRamp->info().constEnd(); ++it ) 00097 { 00098 if ( it.key().startsWith( "cpt-city" ) ) 00099 continue; 00100 tableInfo->setItem( i, 0, new QTableWidgetItem( it.key() ) ); 00101 tableInfo->setItem( i, 1, new QTableWidgetItem( it.value() ) ); 00102 tableInfo->resizeRowToContents( i ); 00103 i++; 00104 } 00105 tableInfo->resizeColumnToContents( 0 ); 00106 tableInfo->horizontalHeader()->setStretchLastSection( true ); 00107 tableInfo->setRowCount( i ); 00108 tableInfo->setFixedHeight( tableInfo->rowHeight( 0 ) * i + 5 ); 00109 dlg->layout()->addWidget( tableInfo ); 00110 dlg->resize( 600, 250 ); 00111 00112 dlg->layout()->addSpacing( 5 ); 00113 00114 // gradient file 00115 QString gradientFile = mRamp->info().value( "cpt-city-gradient" ); 00116 if ( ! gradientFile.isNull() ) 00117 { 00118 QString fileName = gradientFile; 00119 fileName.replace( "<cpt-city>", QgsCptCityArchive::defaultBaseDir() ); 00120 if ( ! QFile::exists( fileName ) ) 00121 { 00122 fileName = gradientFile; 00123 fileName.replace( "<cpt-city>", "http://soliton.vm.bytemark.co.uk/pub/cpt-city" ); 00124 } 00125 label = new QLabel( tr( "Gradient file : %1" ).arg( fileName ), dlg ); 00126 label->setTextInteractionFlags( Qt::TextBrowserInteraction ); 00127 dlg->layout()->addSpacing( 5 ); 00128 dlg->layout()->addWidget( label ); 00129 } 00130 00131 // license file 00132 QString licenseFile = mRamp->info().value( "cpt-city-license" ); 00133 if ( !licenseFile.isNull() ) 00134 { 00135 QString fileName = licenseFile; 00136 fileName.replace( "<cpt-city>", QgsCptCityArchive::defaultBaseDir() ); 00137 if ( ! QFile::exists( fileName ) ) 00138 { 00139 fileName = licenseFile; 00140 fileName.replace( "<cpt-city>", "http://soliton.vm.bytemark.co.uk/pub/cpt-city" ); 00141 } 00142 label = new QLabel( tr( "License file : %1" ).arg( fileName ), dlg ); 00143 label->setTextInteractionFlags( Qt::TextBrowserInteraction ); 00144 dlg->layout()->addSpacing( 5 ); 00145 dlg->layout()->addWidget( label ); 00146 if ( QFile::exists( fileName ) ) 00147 { 00148 QTextEdit *textEdit = new QTextEdit( dlg ); 00149 textEdit->setReadOnly( true ); 00150 QFile file( fileName ); 00151 if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) 00152 { 00153 textEdit->setText( file.readAll() ); 00154 file.close(); 00155 dlg->layout()->addSpacing( 5 ); 00156 dlg->layout()->addWidget( textEdit ); 00157 dlg->resize( 600, 500 ); 00158 } 00159 } 00160 } 00161 00162 dlg->show(); //non modal 00163 } 00164 00165 void QgsVectorGradientColorRampV2Dialog::updateStops() 00166 { 00167 QgsGradientStopsList stops = mRamp->stops(); 00168 groupStops->setChecked( !stops.isEmpty() ); 00169 00170 QList<QTreeWidgetItem *> items; 00171 for ( QgsGradientStopsList::iterator it = stops.begin(); 00172 it != stops.end(); ++it ) 00173 { 00174 double val = it->offset * 100.0; 00175 QStringList lst; 00176 lst << "." << QString(( val < 10 ) ? '0' + QString::number( val ) : QString::number( val ) ); 00177 QTreeWidgetItem* item = new QTreeWidgetItem( lst ); 00178 00179 setStopColor( item, it->color ); 00180 item->setData( 0, StopOffsetRole, it->offset ); 00181 00182 items.append( item ); 00183 } 00184 treeStops->clear(); 00185 treeStops->insertTopLevelItems( 0, items ); 00186 treeStops->resizeColumnToContents( 0 ); 00187 treeStops->setColumnWidth( 0, treeStops->columnWidth( 0 ) + 20 ); 00188 treeStops->sortByColumn( 1, Qt::AscendingOrder ); 00189 treeStops->setSortingEnabled( true ); 00190 } 00191 00192 void QgsVectorGradientColorRampV2Dialog::updatePreview() 00193 { 00194 // update ramp stops from the tree widget 00195 QgsGradientStopsList stops; 00196 if ( groupStops->isChecked() ) 00197 { 00198 int count = treeStops->topLevelItemCount(); 00199 for ( int i = 0; i < count; i++ ) 00200 { 00201 QTreeWidgetItem* item = treeStops->topLevelItem( i ); 00202 double offset = item->data( 0, StopOffsetRole ).toDouble(); 00203 QColor color = item->data( 0, StopColorRole ).value<QColor>(); 00204 stops.append( QgsGradientStop( offset, color ) ); 00205 } 00206 } 00207 mRamp->setStops( stops ); 00208 00209 // generate the preview 00210 QSize size( 300, 40 ); 00211 lblPreview->setPixmap( QgsSymbolLayerV2Utils::colorRampPreviewPixmap( mRamp, size ) ); 00212 00213 btnColor1->setColor( mRamp->color1() ); 00214 btnColor2->setColor( mRamp->color2() ); 00215 } 00216 00217 void QgsVectorGradientColorRampV2Dialog::setColor1( const QColor& color ) 00218 { 00219 mRamp->setColor1( color ); 00220 updatePreview(); 00221 } 00222 00223 void QgsVectorGradientColorRampV2Dialog::setColor2( const QColor& color ) 00224 { 00225 mRamp->setColor2( color ); 00226 updatePreview(); 00227 } 00228 00229 void QgsVectorGradientColorRampV2Dialog::setStopColor( QTreeWidgetItem* item, QColor color ) 00230 { 00231 QSize iconSize( 16, 16 ); 00232 QPixmap pixmap( iconSize ); 00233 pixmap.fill( QColor( 0, 0, 0, 0 ) ); 00234 QRect rect( 1, 1, iconSize.width() - 2, iconSize.height() - 2 ); 00235 00236 // draw a slightly rounded rectangle 00237 QPainter p; 00238 p.begin( &pixmap ); 00239 p.setPen( Qt::NoPen ); 00240 p.setRenderHint( QPainter::Antialiasing ); 00241 p.setBrush( color ); 00242 p.drawRoundedRect( rect, 2, 2 ); 00243 p.end(); 00244 00245 item->setIcon( 0, QIcon( pixmap ) ); 00246 item->setData( 0, StopColorRole, color ); 00247 item->setText( 0, color.name() ); 00248 } 00249 00250 void QgsVectorGradientColorRampV2Dialog::setItemStopColor( const QColor& newColor ) 00251 { 00252 if ( mCurrentItem ) 00253 { 00254 setStopColor( mCurrentItem, newColor ); 00255 updatePreview(); 00256 } 00257 } 00258 00259 void QgsVectorGradientColorRampV2Dialog::stopDoubleClicked( QTreeWidgetItem* item, int column ) 00260 { 00261 if ( column == 0 ) 00262 { 00263 QColor color; 00264 00265 QSettings settings; 00266 if ( settings.value( "/qgis/live_color_dialogs", false ).toBool() ) 00267 { 00268 mCurrentItem = item; 00269 color = QgsColorDialog::getLiveColor( 00270 item->data( 0, StopColorRole ).value<QColor>(), 00271 this, SLOT( setItemStopColor( const QColor& ) ), this ); 00272 mCurrentItem = 0; 00273 } 00274 else 00275 { 00276 color = QColorDialog::getColor( item->data( 0, StopColorRole ).value<QColor>(), this, tr( "Edit Stop Color" ), QColorDialog::ShowAlphaChannel ); 00277 } 00278 if ( !color.isValid() ) 00279 return; 00280 setStopColor( item, color ); 00281 00282 updatePreview(); 00283 } 00284 else 00285 { 00286 bool ok; 00287 double key = item->data( 0, StopOffsetRole ).toDouble(); 00288 // allow for floating-point values 00289 double val = key * 100; 00290 #if QT_VERSION >= 0x40500 00291 val = QInputDialog::getDouble( this, tr( "Offset of the stop" ), 00292 tr( "Please enter offset in percents (%) of the new stop" ), 00293 val, 0, 100, 2, &ok ); 00294 #else 00295 QString res = QInputDialog::getText( this, tr( "Offset of the stop" ), 00296 tr( "Please enter offset in percents (%) of the new stop" ), 00297 QLineEdit::Normal, QString::number( val ), &ok ); 00298 if ( ok ) 00299 val = res.toDouble( &ok ); 00300 if ( ok ) 00301 ok = val >= 0 && val <= 100; 00302 #endif 00303 if ( !ok ) 00304 return; 00305 00306 double newkey = val / 100.0; 00307 item->setText( 1, ( val < 10 ) ? '0' + QString::number( val ) : QString::number( val ) ); 00308 item->setData( 0, StopOffsetRole, newkey ); 00309 00310 updatePreview(); 00311 } 00312 } 00313 00314 void QgsVectorGradientColorRampV2Dialog::addStop() 00315 { 00316 // Native Mac dialog works only for Qt Carbon 00317 // Qt bug: http://bugreports.qt.nokia.com/browse/QTBUG-14889 00318 // Qt 4.7 Mac Cocoa bug: calling QInputDialog::getInt after QColorDialog::getColor will freeze app 00319 // workaround: call QColorDialog::getColor below instead of here, 00320 // but not needed at this time because of the other Qt bug 00321 // FIXME need to also check max QT_VERSION when Qt bug(s) fixed 00322 #ifndef Q_WS_MAC 00323 QColor color = QColorDialog::getColor( QColor(), this, tr( "Add Color Stop" ), QColorDialog::ShowAlphaChannel ); 00324 00325 if ( !color.isValid() ) 00326 return; 00327 activateWindow(); 00328 #endif 00329 00330 bool ok; 00331 double val = 50.0; 00332 #if QT_VERSION >= 0x40500 00333 val = QInputDialog::getDouble( this, tr( "Offset of the stop" ), 00334 tr( "Please enter offset in percents (%) of the new stop" ), 00335 val, 0, 100, 2, &ok ); 00336 #else 00337 QString res = QInputDialog::getText( this, tr( "Offset of the stop" ), 00338 tr( "Please enter offset in percents (%) of the new stop" ), 00339 QLineEdit::Normal, QString::number( val ), &ok ); 00340 if ( ok ) 00341 val = res.toDouble( &ok ); 00342 if ( ok ) 00343 ok = val >= 0 && val <= 100; 00344 #endif 00345 if ( !ok ) 00346 return; 00347 activateWindow(); 00348 00349 double key = val / 100.0; 00350 QStringList lst; 00351 lst << "." << QString(( val < 10 ) ? '0' + QString::number( val ) : QString::number( val ) ); 00352 00353 #ifdef Q_WS_MAC 00354 QColor color = QColorDialog::getColor( QColor(), this, tr( "Add Color Stop" ), QColorDialog::ShowAlphaChannel ); 00355 00356 if ( !color.isValid() ) 00357 return; 00358 activateWindow(); 00359 #endif 00360 00361 QTreeWidgetItem* item = new QTreeWidgetItem( lst ); 00362 00363 setStopColor( item, color ); 00364 item->setData( 0, StopOffsetRole, key ); 00365 00366 treeStops->addTopLevelItem( item ); 00367 00368 treeStops->resizeColumnToContents( 0 ); 00369 treeStops->setColumnWidth( 0, treeStops->columnWidth( 0 ) + 20 ); 00370 00371 updatePreview(); 00372 } 00373 00374 void QgsVectorGradientColorRampV2Dialog::removeStop() 00375 { 00376 QTreeWidgetItem* item = treeStops->currentItem(); 00377 if ( !item ) 00378 return; 00379 int index = treeStops->indexOfTopLevelItem( item ); 00380 treeStops->takeTopLevelItem( index ); 00381 00382 updatePreview(); 00383 } 00384 00385 void QgsVectorGradientColorRampV2Dialog::toggledStops( bool on ) 00386 { 00387 Q_UNUSED( on ); 00388 updatePreview(); 00389 }