|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgscolorrampcombobox.cpp 00003 --------------------- 00004 begin : October 2010 00005 copyright : (C) 2010 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 #include "qgscolorrampcombobox.h" 00016 00017 #include "qgssymbollayerv2utils.h" 00018 #include "qgsvectorcolorrampv2.h" 00019 #include "qgsstylev2.h" 00020 #include "qgsstylev2managerdialog.h" 00021 00022 QSize QgsColorRampComboBox::rampIconSize( 50, 16 ); 00023 00024 QgsColorRampComboBox::QgsColorRampComboBox( QWidget *parent ) : 00025 QComboBox( parent ), mStyle( NULL ), mSourceColorRamp( NULL ) 00026 { 00027 } 00028 00029 QgsColorRampComboBox::~QgsColorRampComboBox() 00030 { 00031 delete mSourceColorRamp; 00032 } 00033 00034 void QgsColorRampComboBox::populate( QgsStyleV2* style ) 00035 { 00036 if ( count() != 0 ) 00037 return; // already populated! 00038 00039 mStyle = style; 00040 00041 setIconSize( rampIconSize ); 00042 00043 QStringList rampNames = mStyle->colorRampNames(); 00044 for ( QStringList::iterator it = rampNames.begin(); it != rampNames.end(); ++it ) 00045 { 00046 QgsVectorColorRampV2* ramp = style->colorRamp( *it ); 00047 QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize ); 00048 00049 addItem( icon, *it ); 00050 00051 delete ramp; 00052 } 00053 00054 addItem( tr( "New color ramp..." ) ); 00055 connect( this, SIGNAL( activated( int ) ), SLOT( colorRampChanged( int ) ) ); 00056 } 00057 00058 QgsVectorColorRampV2* QgsColorRampComboBox::currentColorRamp() 00059 { 00060 QString rampName = currentText(); 00061 if ( rampName == "[source]" && mSourceColorRamp ) 00062 return mSourceColorRamp->clone(); 00063 else 00064 return mStyle->colorRamp( rampName ); 00065 } 00066 00067 void QgsColorRampComboBox::setSourceColorRamp( QgsVectorColorRampV2* sourceRamp ) 00068 { 00069 mSourceColorRamp = sourceRamp->clone(); 00070 00071 QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( mSourceColorRamp, rampIconSize ); 00072 if ( itemText( 0 ) == "[source]" ) 00073 setItemIcon( 0, icon ); 00074 else 00075 insertItem( 0, icon, "[source]" ); 00076 setCurrentIndex( 0 ); 00077 } 00078 00079 void QgsColorRampComboBox::colorRampChanged( int index ) 00080 { 00081 if ( index != count() - 1 ) 00082 return; 00083 00084 // last item: "new color ramp..." 00085 QString rampName = QgsStyleV2ManagerDialog::addColorRampStatic( this, mStyle ); 00086 if ( rampName.isEmpty() ) 00087 return; 00088 00089 // put newly added ramp into the combo 00090 QgsVectorColorRampV2* ramp = mStyle->colorRamp( rampName ); 00091 QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, rampIconSize ); 00092 00093 blockSignals( true ); // avoid calling this method again! 00094 insertItem( index, icon, rampName ); 00095 blockSignals( false ); 00096 00097 delete ramp; 00098 00099 // ... and set it as active 00100 setCurrentIndex( index ); 00101 00102 // make sure the color ramp is stored 00103 mStyle->save(); 00104 }