2121#include " qgssymbol.h"
2222#include " qgssymbollayerutils.h"
2323#include " qgscolorramp.h"
24+ #include " qgscolorrampbutton.h"
2425#include " qgsstyle.h"
2526#include " qgslogger.h"
2627
@@ -410,21 +411,15 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
410411
411412 mExpressionWidget ->setLayer ( mLayer );
412413
413- cboCategorizedColorRamp->populate ( mStyle );
414- int randomIndex = cboCategorizedColorRamp->findText ( tr ( " Random colors" ) );
415- if ( randomIndex != -1 )
416- {
417- cboCategorizedColorRamp->setCurrentIndex ( randomIndex );
418- mButtonEditRamp ->setEnabled ( false );
419- }
414+ // initiate color ramp button to random
415+ btnColorRamp->setShowRandomColorRamp ( true );
416+ btnColorRamp->setRandomColorRamp ();
420417
421418 // set project default color ramp
422419 QString defaultColorRamp = QgsProject::instance ()->readEntry ( QStringLiteral ( " DefaultStyles" ), QStringLiteral ( " /ColorRamp" ), QLatin1String ( " " ) );
423420 if ( defaultColorRamp != QLatin1String ( " " ) )
424421 {
425- int index = cboCategorizedColorRamp->findText ( defaultColorRamp, Qt::MatchCaseSensitive );
426- if ( index >= 0 )
427- cboCategorizedColorRamp->setCurrentIndex ( index );
422+ btnColorRamp->setColorRampFromName ( defaultColorRamp );
428423 }
429424
430425 mCategorizedSymbol = QgsSymbol::defaultSymbol ( mLayer ->geometryType () );
@@ -455,10 +450,8 @@ QgsCategorizedSymbolRendererWidget::QgsCategorizedSymbolRendererWidget( QgsVecto
455450 connect ( btnDeleteCategories, SIGNAL ( clicked () ), this , SLOT ( deleteCategories () ) );
456451 connect ( btnDeleteAllCategories, SIGNAL ( clicked () ), this , SLOT ( deleteAllCategories () ) );
457452 connect ( btnAddCategory, SIGNAL ( clicked () ), this , SLOT ( addCategory () ) );
458- connect ( cbxInvertedColorRamp, SIGNAL ( toggled ( bool ) ), this , SLOT ( applyColorRamp () ) );
459- connect ( cboCategorizedColorRamp, SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( applyColorRamp () ) );
460- connect ( cboCategorizedColorRamp, SIGNAL ( sourceRampEdited () ), this , SLOT ( applyColorRamp () ) );
461- connect ( mButtonEditRamp , SIGNAL ( clicked () ), cboCategorizedColorRamp, SLOT ( editSourceRamp () ) );
453+
454+ connect ( btnColorRamp, &QgsColorRampButton::colorRampChanged, this , &QgsCategorizedSymbolRendererWidget::applyColorRamp );
462455
463456 // menus for data-defined rotation/size
464457 QMenu* advMenu = new QMenu;
@@ -501,17 +494,11 @@ void QgsCategorizedSymbolRendererWidget::updateUiFromRenderer()
501494 updateCategorizedSymbolIcon ();
502495 }
503496
504- // set source color ramp
497+ // if a color ramp attached to the renderer, enable the color ramp button
505498 if ( mRenderer ->sourceColorRamp () )
506499 {
507- cboCategorizedColorRamp->setSourceColorRamp ( mRenderer ->sourceColorRamp () );
508- cbxInvertedColorRamp->setChecked ( mRenderer ->invertedColorRamp () );
500+ btnColorRamp->setColorRamp ( mRenderer ->sourceColorRamp () );
509501 }
510-
511- if ( cboCategorizedColorRamp->currentText () == tr ( " Random colors" ) )
512- mButtonEditRamp ->setEnabled ( false );
513- else
514- mButtonEditRamp ->setEnabled ( true );
515502}
516503
517504QgsFeatureRenderer* QgsCategorizedSymbolRendererWidget::renderer ()
@@ -628,20 +615,6 @@ static void _createCategories( QgsCategoryList& cats, QList<QVariant>& values, Q
628615 }
629616}
630617
631- QgsColorRamp* QgsCategorizedSymbolRendererWidget::getColorRamp ()
632- {
633- QgsColorRamp* ramp = cboCategorizedColorRamp->currentColorRamp ();
634- if ( !ramp )
635- {
636- if ( cboCategorizedColorRamp->count () == 0 )
637- QMessageBox::critical ( this , tr ( " Error" ), tr ( " There are no available color ramps. You can add them in Style Manager." ) );
638- else if ( !cboCategorizedColorRamp->createNewColorRampSelected () )
639- QMessageBox::critical ( this , tr ( " Error" ), tr ( " The selected color ramp is not available." ) );
640- }
641- return ramp;
642- }
643-
644-
645618void QgsCategorizedSymbolRendererWidget::addCategories ()
646619{
647620 QString attrName = mExpressionWidget ->currentField ();
@@ -759,8 +732,7 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
759732 // recreate renderer
760733 QgsCategorizedSymbolRenderer *r = new QgsCategorizedSymbolRenderer ( attrName, cats );
761734 r->setSourceSymbol ( mCategorizedSymbol ->clone () );
762- r->setInvertedColorRamp ( cbxInvertedColorRamp->isChecked () );
763- QScopedPointer< QgsColorRamp > ramp ( getColorRamp () );
735+ QScopedPointer< QgsColorRamp > ramp ( btnColorRamp->colorRamp () );
764736 if ( ramp )
765737 r->setSourceColorRamp ( ramp->clone () );
766738
@@ -777,15 +749,9 @@ void QgsCategorizedSymbolRendererWidget::addCategories()
777749
778750void QgsCategorizedSymbolRendererWidget::applyColorRamp ()
779751{
780- if ( cboCategorizedColorRamp->currentText () == tr ( " Random colors" ) )
781- mButtonEditRamp ->setEnabled ( false );
782- else
783- mButtonEditRamp ->setEnabled ( true );
784-
785- QgsColorRamp* ramp = getColorRamp ();
786- if ( ramp )
752+ if ( !btnColorRamp->isNull () )
787753 {
788- mRenderer ->updateColorRamp ( ramp, cbxInvertedColorRamp-> isChecked () );
754+ mRenderer ->updateColorRamp ( btnColorRamp-> colorRamp ()-> clone () );
789755 }
790756 mModel ->updateSymbology ();
791757}
0 commit comments