1 /***************************************************************************
2 qgscolorbrewercolorrampdialog.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16 #include "qgscolorbrewercolorrampdialog.h"
17
18 #include "qgscolorramp.h"
19 #include "qgssymbollayerutils.h"
20 #include "qgshelp.h"
21
22 #include <QAbstractButton>
23 #include <QDialogButtonBox>
24
25 #if 0 // unused
26 static void updateColorButton( QAbstractButton *button, QColor color )
27 {
28 QPixmap p( 20, 20 );
29 p.fill( color );
30 button->setIcon( QIcon( p ) );
31 }
32 #endif
33
34 /////////
35
36
QgsColorBrewerColorRampWidget(const QgsColorBrewerColorRamp & ramp,QWidget * parent)37 QgsColorBrewerColorRampWidget::QgsColorBrewerColorRampWidget( const QgsColorBrewerColorRamp &ramp, QWidget *parent )
38 : QgsPanelWidget( parent )
39 , mRamp( ramp )
40 {
41
42 setupUi( this );
43
44 QSize iconSize( 50, 16 );
45 cboSchemeName->setIconSize( iconSize );
46
47 QStringList schemes = QgsColorBrewerColorRamp::listSchemeNames();
48 const auto constSchemes = schemes;
49 for ( const QString &schemeName : constSchemes )
50 {
51 // create a preview icon using five color variant
52 QgsColorBrewerColorRamp *r = new QgsColorBrewerColorRamp( schemeName, 5 );
53 QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( r, iconSize );
54 delete r;
55 cboSchemeName->addItem( icon, schemeName );
56 }
57
58 updateUi();
59 connect( cboSchemeName, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setSchemeName );
60 connect( cboColors, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsColorBrewerColorRampWidget::setColors );
61 }
62
setRamp(const QgsColorBrewerColorRamp & ramp)63 void QgsColorBrewerColorRampWidget::setRamp( const QgsColorBrewerColorRamp &ramp )
64 {
65 mRamp = ramp;
66 updateUi();
67 emit changed();
68 }
69
populateVariants()70 void QgsColorBrewerColorRampWidget::populateVariants()
71 {
72 QString oldVariant = cboColors->currentText();
73
74 cboColors->clear();
75 QString schemeName = cboSchemeName->currentText();
76 QList<int> variants = QgsColorBrewerColorRamp::listSchemeVariants( schemeName );
77 const auto constVariants = variants;
78 for ( int variant : constVariants )
79 {
80 cboColors->addItem( QString::number( variant ) );
81 }
82
83 // try to set the original variant again (if exists)
84 int idx = cboColors->findText( oldVariant );
85 if ( idx == -1 ) // not found?
86 {
87 // use the last item
88 idx = cboColors->count() - 1;
89 }
90 cboColors->setCurrentIndex( idx );
91 }
92
updatePreview()93 void QgsColorBrewerColorRampWidget::updatePreview()
94 {
95 QSize size( 300, 40 );
96 lblPreview->setPixmap( QgsSymbolLayerUtils::colorRampPreviewPixmap( &mRamp, size ) );
97 }
98
updateUi()99 void QgsColorBrewerColorRampWidget::updateUi()
100 {
101 whileBlocking( cboSchemeName )->setCurrentIndex( cboSchemeName->findText( mRamp.schemeName() ) );
102 populateVariants();
103 whileBlocking( cboColors )->setCurrentIndex( cboColors->findText( QString::number( mRamp.colors() ) ) );
104 updatePreview();
105 }
106
setSchemeName()107 void QgsColorBrewerColorRampWidget::setSchemeName()
108 {
109 // populate list of variants
110 populateVariants();
111
112 mRamp.setSchemeName( cboSchemeName->currentText() );
113 updatePreview();
114 emit changed();
115 }
116
setColors()117 void QgsColorBrewerColorRampWidget::setColors()
118 {
119 int num = cboColors->currentText().toInt();
120 mRamp.setColors( num );
121 updatePreview();
122 emit changed();
123 }
124
QgsColorBrewerColorRampDialog(const QgsColorBrewerColorRamp & ramp,QWidget * parent)125 QgsColorBrewerColorRampDialog::QgsColorBrewerColorRampDialog( const QgsColorBrewerColorRamp &ramp, QWidget *parent )
126 : QDialog( parent )
127 {
128 QVBoxLayout *vLayout = new QVBoxLayout();
129 mWidget = new QgsColorBrewerColorRampWidget( ramp );
130 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QDialog::reject );
131 vLayout->addWidget( mWidget );
132 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Ok, Qt::Horizontal );
133 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
134 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
135 connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsColorBrewerColorRampDialog::showHelp );
136 vLayout->addWidget( mButtonBox );
137 setLayout( vLayout );
138 setWindowTitle( tr( "ColorBrewer Ramp" ) );
139 connect( mWidget, &QgsColorBrewerColorRampWidget::changed, this, &QgsColorBrewerColorRampDialog::changed );
140 }
141
buttonBox() const142 QDialogButtonBox *QgsColorBrewerColorRampDialog::buttonBox() const
143 {
144 return mButtonBox;
145 }
146
showHelp()147 void QgsColorBrewerColorRampDialog::showHelp()
148 {
149 QgsHelp::openHelp( QStringLiteral( "style_library/style_manager.html#setting-a-color-ramp" ) );
150 }
151