23 #include <QHeaderView> 
   24 #include <QPushButton> 
   26 #include <QToolButton> 
   27 #include <QFileDialog> 
   28 #include <QMessageBox> 
   29 #include <QDesktopWidget> 
   30 #include <QMouseEvent> 
   32 #include <QInputDialog> 
   33 #include <QVBoxLayout> 
   34 #include <QRegularExpression> 
   40   connect( mHueRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mHueRadio_toggled );
 
   41   connect( mSaturationRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mSaturationRadio_toggled );
 
   42   connect( mValueRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mValueRadio_toggled );
 
   43   connect( mRedRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mRedRadio_toggled );
 
   44   connect( mGreenRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mGreenRadio_toggled );
 
   45   connect( mBlueRadio, &QRadioButton::toggled, 
this, &QgsCompoundColorWidget::mBlueRadio_toggled );
 
   46   connect( mAddColorToSchemeButton, &QPushButton::clicked, 
this, &QgsCompoundColorWidget::mAddColorToSchemeButton_clicked );
 
   47   connect( mAddCustomColorButton, &QPushButton::clicked, 
this, &QgsCompoundColorWidget::mAddCustomColorButton_clicked );
 
   48   connect( mSampleButton, &QPushButton::clicked, 
this, &QgsCompoundColorWidget::mSampleButton_clicked );
 
   49   connect( mTabWidget, &QTabWidget::currentChanged, 
this, &QgsCompoundColorWidget::mTabWidget_currentChanged );
 
   50   connect( mActionShowInButtons, &QAction::toggled, 
this, &QgsCompoundColorWidget::mActionShowInButtons_toggled );
 
   55     QVBoxLayout *newLayout = 
new QVBoxLayout();
 
   56     newLayout->setContentsMargins( 0, 0, 0, 0 );
 
   57     newLayout->addWidget( mTabWidget );
 
   58     newLayout->addWidget( mSlidersWidget );
 
   59     newLayout->addWidget( mPreviewWidget );
 
   60     newLayout->addWidget( mSwatchesWidget );
 
   62     setLayout( newLayout );
 
   67   mSchemeList->header()->hide();
 
   68   mSchemeList->setColumnWidth( 0, 
static_cast< int >( 
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 
'X' ) * 6 ) );
 
   71   refreshSchemeComboBox();
 
   75   int activeScheme = settings.
value( QStringLiteral( 
"Windows/ColorDialog/activeScheme" ), 0 ).toInt();
 
   76   activeScheme = activeScheme >= mSchemeComboBox->count() ? 0 : activeScheme;
 
   78   mSchemeList->setScheme( schemeList.at( activeScheme ) );
 
   80   mSchemeComboBox->setCurrentIndex( activeScheme );
 
   81   updateActionsForCurrentScheme();
 
   84   connect( mSchemeList->selectionModel(), &QItemSelectionModel::selectionChanged, 
this, &QgsCompoundColorWidget::listSelectionChanged );
 
   86   mActionCopyColors->setEnabled( 
false );
 
   92   connect( mActionImportPalette, &QAction::triggered, 
this, &QgsCompoundColorWidget::importPalette );
 
   93   connect( mActionRemovePalette, &QAction::triggered, 
this, &QgsCompoundColorWidget::removePalette );
 
   94   connect( mActionNewPalette, &QAction::triggered, 
this, &QgsCompoundColorWidget::newPalette );
 
   97   QMenu *schemeMenu = 
new QMenu( mSchemeToolButton );
 
   98   schemeMenu->addAction( mActionCopyColors );
 
   99   schemeMenu->addAction( mActionPasteColors );
 
  100   schemeMenu->addSeparator();
 
  101   schemeMenu->addAction( mActionImportColors );
 
  102   schemeMenu->addAction( mActionExportColors );
 
  103   schemeMenu->addSeparator();
 
  104   schemeMenu->addAction( mActionNewPalette );
 
  105   schemeMenu->addAction( mActionImportPalette );
 
  106   schemeMenu->addAction( mActionRemovePalette );
 
  107   schemeMenu->addAction( mActionShowInButtons );
 
  108   mSchemeToolButton->setMenu( schemeMenu );
 
  110   connect( mSchemeComboBox, 
static_cast<void ( QComboBox::* )( 
int )
>( &QComboBox::currentIndexChanged ), 
this, &QgsCompoundColorWidget::schemeIndexChanged );
 
  113   mOldColorLabel->hide();
 
  116   mVerticalRamp->setInteriorMargin( 2 );
 
  117   mVerticalRamp->setShowFrame( 
true );
 
  127   mSwatchButton1->setShowMenu( 
false );
 
  129   mSwatchButton2->setShowMenu( 
false );
 
  131   mSwatchButton3->setShowMenu( 
false );
 
  133   mSwatchButton4->setShowMenu( 
false );
 
  135   mSwatchButton5->setShowMenu( 
false );
 
  137   mSwatchButton6->setShowMenu( 
false );
 
  139   mSwatchButton7->setShowMenu( 
false );
 
  141   mSwatchButton8->setShowMenu( 
false );
 
  143   mSwatchButton9->setShowMenu( 
false );
 
  145   mSwatchButton10->setShowMenu( 
false );
 
  147   mSwatchButton11->setShowMenu( 
false );
 
  149   mSwatchButton12->setShowMenu( 
false );
 
  151   mSwatchButton13->setShowMenu( 
false );
 
  153   mSwatchButton14->setShowMenu( 
false );
 
  155   mSwatchButton15->setShowMenu( 
false );
 
  157   mSwatchButton16->setShowMenu( 
false );
 
  160   mSwatchButton1->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor1" ), QVariant( QColor() ) ).value<QColor>() );
 
  161   mSwatchButton2->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor2" ), QVariant( QColor() ) ).value<QColor>() );
 
  162   mSwatchButton3->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor3" ), QVariant( QColor() ) ).value<QColor>() );
 
  163   mSwatchButton4->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor4" ), QVariant( QColor() ) ).value<QColor>() );
 
  164   mSwatchButton5->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor5" ), QVariant( QColor() ) ).value<QColor>() );
 
  165   mSwatchButton6->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor6" ), QVariant( QColor() ) ).value<QColor>() );
 
  166   mSwatchButton7->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor7" ), QVariant( QColor() ) ).value<QColor>() );
 
  167   mSwatchButton8->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor8" ), QVariant( QColor() ) ).value<QColor>() );
 
  168   mSwatchButton9->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor9" ), QVariant( QColor() ) ).value<QColor>() );
 
  169   mSwatchButton10->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor10" ), QVariant( QColor() ) ).value<QColor>() );
 
  170   mSwatchButton11->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor11" ), QVariant( QColor() ) ).value<QColor>() );
 
  171   mSwatchButton12->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor12" ), QVariant( QColor() ) ).value<QColor>() );
 
  172   mSwatchButton13->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor13" ), QVariant( QColor() ) ).value<QColor>() );
 
  173   mSwatchButton14->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor14" ), QVariant( QColor() ) ).value<QColor>() );
 
  174   mSwatchButton15->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor15" ), QVariant( QColor() ) ).value<QColor>() );
 
  175   mSwatchButton16->setColor( settings.
value( QStringLiteral( 
"Windows/ColorDialog/customColor16" ), QVariant( QColor() ) ).value<QColor>() );
 
  178   mSpinBoxRadius->setValue( settings.
value( QStringLiteral( 
"Windows/ColorDialog/sampleRadius" ), 1 ).toInt() );
 
  179   mSamplePreview->setColor( QColor() );
 
  182   const int swatchWidth = 
static_cast< int >( std::round( std::max( 
Qgis::UI_SCALE_FACTOR * 1.9 * mSwatchButton1->fontMetrics().height(), 38.0 ) ) );
 
  183   const int swatchHeight = 
static_cast< int >( std::round( std::max( 
Qgis::UI_SCALE_FACTOR * 1.5 * mSwatchButton1->fontMetrics().height(), 30.0 ) ) );
 
  184   mSwatchButton1->setMinimumSize( swatchWidth, swatchHeight );
 
  185   mSwatchButton1->setMaximumSize( swatchWidth, swatchHeight );
 
  186   mSwatchButton2->setMinimumSize( swatchWidth, swatchHeight );
 
  187   mSwatchButton2->setMaximumSize( swatchWidth, swatchHeight );
 
  188   mSwatchButton3->setMinimumSize( swatchWidth, swatchHeight );
 
  189   mSwatchButton3->setMaximumSize( swatchWidth, swatchHeight );
 
  190   mSwatchButton4->setMinimumSize( swatchWidth, swatchHeight );
 
  191   mSwatchButton4->setMaximumSize( swatchWidth, swatchHeight );
 
  192   mSwatchButton5->setMinimumSize( swatchWidth, swatchHeight );
 
  193   mSwatchButton5->setMaximumSize( swatchWidth, swatchHeight );
 
  194   mSwatchButton6->setMinimumSize( swatchWidth, swatchHeight );
 
  195   mSwatchButton6->setMaximumSize( swatchWidth, swatchHeight );
 
  196   mSwatchButton7->setMinimumSize( swatchWidth, swatchHeight );
 
  197   mSwatchButton7->setMaximumSize( swatchWidth, swatchHeight );
 
  198   mSwatchButton8->setMinimumSize( swatchWidth, swatchHeight );
 
  199   mSwatchButton8->setMaximumSize( swatchWidth, swatchHeight );
 
  200   mSwatchButton9->setMinimumSize( swatchWidth, swatchHeight );
 
  201   mSwatchButton9->setMaximumSize( swatchWidth, swatchHeight );
 
  202   mSwatchButton10->setMinimumSize( swatchWidth, swatchHeight );
 
  203   mSwatchButton10->setMaximumSize( swatchWidth, swatchHeight );
 
  204   mSwatchButton11->setMinimumSize( swatchWidth, swatchHeight );
 
  205   mSwatchButton11->setMaximumSize( swatchWidth, swatchHeight );
 
  206   mSwatchButton12->setMinimumSize( swatchWidth, swatchHeight );
 
  207   mSwatchButton12->setMaximumSize( swatchWidth, swatchHeight );
 
  208   mSwatchButton13->setMinimumSize( swatchWidth, swatchHeight );
 
  209   mSwatchButton13->setMaximumSize( swatchWidth, swatchHeight );
 
  210   mSwatchButton14->setMinimumSize( swatchWidth, swatchHeight );
 
  211   mSwatchButton14->setMaximumSize( swatchWidth, swatchHeight );
 
  212   mSwatchButton15->setMinimumSize( swatchWidth, swatchHeight );
 
  213   mSwatchButton15->setMaximumSize( swatchWidth, swatchHeight );
 
  214   mSwatchButton16->setMinimumSize( swatchWidth, swatchHeight );
 
  215   mSwatchButton16->setMaximumSize( swatchWidth, swatchHeight );
 
  216   const int previewHeight = 
static_cast< int >( std::round( std::max( 
Qgis::UI_SCALE_FACTOR * 2.0 * mSwatchButton1->fontMetrics().height(), 40.0 ) ) );
 
  217   mColorPreview->setMinimumSize( 0, previewHeight );
 
  218   mPreviewWidget->setMaximumHeight( previewHeight * 2 );
 
  219   const int swatchAddSize = 
static_cast< int >( std::round( std::max( 
Qgis::UI_SCALE_FACTOR * 1.4 * mSwatchButton1->fontMetrics().height(), 28.0 ) ) );
 
  220   mAddCustomColorButton->setMinimumWidth( swatchAddSize );
 
  221   mAddCustomColorButton->setMaximumWidth( swatchAddSize );
 
  226   if ( 
color.isValid() )
 
  232   const int activeRadio = settings.
value( QStringLiteral( 
"Windows/ColorDialog/activeComponent" ), 2 ).toInt();
 
  233   switch ( activeRadio )
 
  236       mHueRadio->setChecked( 
true );
 
  239       mSaturationRadio->setChecked( 
true );
 
  242       mValueRadio->setChecked( 
true );
 
  245       mRedRadio->setChecked( 
true );
 
  248       mGreenRadio->setChecked( 
true );
 
  251       mBlueRadio->setChecked( 
true );
 
  254   const int currentTab = settings.
value( QStringLiteral( 
"Windows/ColorDialog/activeTab" ), 0 ).toInt();
 
  255   mTabWidget->setCurrentIndex( currentTab );
 
  300   return mColorPreview->color();
 
  305   mAllowAlpha = allowOpacity;
 
  306   mAlphaLabel->setVisible( allowOpacity );
 
  307   mAlphaSlider->setVisible( allowOpacity );
 
  310     mAlphaLayout->setContentsMargins( 0, 0, 0, 0 );
 
  311     mAlphaLayout->setSpacing( 0 );
 
  315 void QgsCompoundColorWidget::refreshSchemeComboBox()
 
  317   mSchemeComboBox->blockSignals( 
true );
 
  318   mSchemeComboBox->clear();
 
  320   QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin();
 
  321   for ( ; schemeIt != schemeList.constEnd(); ++schemeIt )
 
  323     mSchemeComboBox->addItem( ( *schemeIt )->schemeName() );
 
  325   mSchemeComboBox->blockSignals( 
false );
 
  332   const QString lastDir = s.
value( QStringLiteral( 
"/UI/lastGplPaletteDir" ), QDir::homePath() ).toString();
 
  333   const QString filePath = QFileDialog::getOpenFileName( parent, tr( 
"Select Palette File" ), lastDir, QStringLiteral( 
"GPL (*.gpl);;All files (*.*)" ) );
 
  335     parent->activateWindow();
 
  336   if ( filePath.isEmpty() )
 
  342   const QFileInfo fileInfo( filePath );
 
  343   if ( !fileInfo.exists() || !fileInfo.isReadable() )
 
  345     QMessageBox::critical( 
nullptr, tr( 
"Import Color Palette" ), tr( 
"Error, file does not exist or is not readable." ) );
 
  349   s.
setValue( QStringLiteral( 
"/UI/lastGplPaletteDir" ), fileInfo.absolutePath() );
 
  350   QFile file( filePath );
 
  358     QMessageBox::critical( 
nullptr, tr( 
"Import Color Palette" ), tr( 
"Palette file is not readable." ) );
 
  362   if ( importedColors.length() == 0 )
 
  365     QMessageBox::critical( 
nullptr, tr( 
"Import Color Palette" ), tr( 
"No colors found in palette file." ) );
 
  371   importedScheme->
setName( paletteName );
 
  372   importedScheme->
setColors( importedColors );
 
  375   return importedScheme;
 
  378 void QgsCompoundColorWidget::importPalette()
 
  383     refreshSchemeComboBox();
 
  384     mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
 
  391   if ( QMessageBox::question( parent, tr( 
"Remove Color Palette" ),
 
  392                               tr( 
"Are you sure you want to remove %1?" ).arg( scheme->
schemeName() ),
 
  393                               QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
 
  400   if ( !scheme->
erase() )
 
  411 void QgsCompoundColorWidget::removePalette()
 
  415   int prevIndex = mSchemeComboBox->currentIndex();
 
  416   if ( prevIndex >= schemeList.length() )
 
  430     refreshSchemeComboBox();
 
  431     prevIndex = std::max( std::min( prevIndex, mSchemeComboBox->count() - 1 ), 0 );
 
  432     mSchemeComboBox->setCurrentIndex( prevIndex );
 
  439   const QString name = QInputDialog::getText( parent, tr( 
"Create New Palette" ), tr( 
"Enter a name for the new palette:" ),
 
  440                        QLineEdit::Normal, tr( 
"New palette" ), &ok );
 
  442   if ( !ok || name.isEmpty() )
 
  449   const QDir palettePath( gplFilePath() );
 
  450   const thread_local QRegularExpression badChars( 
"[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
 
  451   QString filename = name.simplified().toLower().replace( badChars, QStringLiteral( 
"_" ) );
 
  452   if ( filename.isEmpty() )
 
  454     filename = tr( 
"new_palette" );
 
  456   QFileInfo destFileInfo( palettePath.filePath( filename + 
".gpl" ) );
 
  458   while ( destFileInfo.exists() )
 
  461     destFileInfo = QFileInfo( palettePath.filePath( filename + QStringLiteral( 
"%1.gpl" ).arg( fileNumber ) ) );
 
  472 void QgsCompoundColorWidget::newPalette()
 
  477     refreshSchemeComboBox();
 
  478     mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
 
  482 QString QgsCompoundColorWidget::gplFilePath()
 
  487   if ( !localDir.mkpath( palettesDir ) )
 
  495 void QgsCompoundColorWidget::schemeIndexChanged( 
int index )
 
  498   if ( mSchemeList->isDirty() )
 
  500     mSchemeList->saveColorsToScheme();
 
  505   if ( index >= schemeList.length() )
 
  511   mSchemeList->setScheme( scheme );
 
  513   updateActionsForCurrentScheme();
 
  516   mActionCopyColors->setEnabled( 
false );
 
  519 void QgsCompoundColorWidget::listSelectionChanged( 
const QItemSelection &selected, 
const QItemSelection &deselected )
 
  521   Q_UNUSED( deselected )
 
  522   mActionCopyColors->setEnabled( selected.length() > 0 );
 
  525 void QgsCompoundColorWidget::mAddCustomColorButton_clicked()
 
  527   switch ( mLastCustomColorIndex )
 
  530       mSwatchButton1->setColor( mColorPreview->color() );
 
  533       mSwatchButton2->setColor( mColorPreview->color() );
 
  536       mSwatchButton3->setColor( mColorPreview->color() );
 
  539       mSwatchButton4->setColor( mColorPreview->color() );
 
  542       mSwatchButton5->setColor( mColorPreview->color() );
 
  545       mSwatchButton6->setColor( mColorPreview->color() );
 
  548       mSwatchButton7->setColor( mColorPreview->color() );
 
  551       mSwatchButton8->setColor( mColorPreview->color() );
 
  554       mSwatchButton9->setColor( mColorPreview->color() );
 
  557       mSwatchButton10->setColor( mColorPreview->color() );
 
  560       mSwatchButton11->setColor( mColorPreview->color() );
 
  563       mSwatchButton12->setColor( mColorPreview->color() );
 
  566       mSwatchButton13->setColor( mColorPreview->color() );
 
  569       mSwatchButton14->setColor( mColorPreview->color() );
 
  572       mSwatchButton15->setColor( mColorPreview->color() );
 
  575       mSwatchButton16->setColor( mColorPreview->color() );
 
  578   mLastCustomColorIndex++;
 
  579   if ( mLastCustomColorIndex >= 16 )
 
  581     mLastCustomColorIndex = 0;
 
  585 void QgsCompoundColorWidget::mSampleButton_clicked()
 
  591   mPickingColor = 
true;
 
  592   setMouseTracking( 
true );
 
  595 void QgsCompoundColorWidget::mTabWidget_currentChanged( 
int index )
 
  598   const bool enabled = index == 0;
 
  599   mRedRadio->setEnabled( enabled );
 
  600   mBlueRadio->setEnabled( enabled );
 
  601   mGreenRadio->setEnabled( enabled );
 
  602   mHueRadio->setEnabled( enabled );
 
  603   mSaturationRadio->setEnabled( enabled );
 
  604   mValueRadio->setEnabled( enabled );
 
  607 void QgsCompoundColorWidget::mActionShowInButtons_toggled( 
bool state )
 
  616 QScreen *QgsCompoundColorWidget::findScreenAt( QPoint pos )
 
  618   const QList< QScreen * > screens = QGuiApplication::screens();
 
  619   for ( QScreen *screen : screens )
 
  621     if ( screen->geometry().contains( pos ) )
 
  629 void QgsCompoundColorWidget::saveSettings()
 
  632   if ( mSchemeList->isDirty() )
 
  634     mSchemeList->saveColorsToScheme();
 
  641   if ( mHueRadio->isChecked() )
 
  643   if ( mSaturationRadio->isChecked() )
 
  645   if ( mValueRadio->isChecked() )
 
  647   if ( mRedRadio->isChecked() )
 
  649   if ( mGreenRadio->isChecked() )
 
  651   if ( mBlueRadio->isChecked() )
 
  653   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/activeComponent" ), activeRadio );
 
  656   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/activeScheme" ), mSchemeComboBox->currentIndex() );
 
  659   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/activeTab" ), mTabWidget->currentIndex() );
 
  662   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor1" ), QVariant( mSwatchButton1->color() ) );
 
  663   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor2" ), QVariant( mSwatchButton2->color() ) );
 
  664   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor3" ), QVariant( mSwatchButton3->color() ) );
 
  665   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor4" ), QVariant( mSwatchButton4->color() ) );
 
  666   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor5" ), QVariant( mSwatchButton5->color() ) );
 
  667   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor6" ), QVariant( mSwatchButton6->color() ) );
 
  668   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor7" ), QVariant( mSwatchButton7->color() ) );
 
  669   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor8" ), QVariant( mSwatchButton8->color() ) );
 
  670   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor9" ), QVariant( mSwatchButton9->color() ) );
 
  671   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor10" ), QVariant( mSwatchButton10->color() ) );
 
  672   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor11" ), QVariant( mSwatchButton11->color() ) );
 
  673   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor12" ), QVariant( mSwatchButton12->color() ) );
 
  674   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor13" ), QVariant( mSwatchButton13->color() ) );
 
  675   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor14" ), QVariant( mSwatchButton14->color() ) );
 
  676   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor15" ), QVariant( mSwatchButton15->color() ) );
 
  677   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/customColor16" ), QVariant( mSwatchButton16->color() ) );
 
  680   settings.
setValue( QStringLiteral( 
"Windows/ColorDialog/sampleRadius" ), mSpinBoxRadius->value() );
 
  683 void QgsCompoundColorWidget::stopPicking( QPoint eventPos, 
const bool takeSample )
 
  689   setMouseTracking( 
false );
 
  690   mPickingColor = 
false;
 
  699   const QColor snappedColor = sampleColor( eventPos );
 
  700   mSamplePreview->setColor( snappedColor );
 
  701   mColorPreview->setColor( snappedColor, 
true );
 
  706   if ( !
color.isValid() )
 
  711   QColor fixedColor = QColor( 
color );
 
  715     fixedColor.setAlpha( 255 );
 
  717   const QList<QgsColorWidget *> colorWidgets = this->findChildren<QgsColorWidget *>();
 
  718   const auto constColorWidgets = colorWidgets;
 
  721     if ( widget == mSamplePreview )
 
  725     widget->blockSignals( 
true );
 
  726     widget->setColor( fixedColor );
 
  727     widget->blockSignals( 
false );
 
  734   mOldColorLabel->setVisible( 
color.isValid() );
 
  735   mColorPreview->setColor2( 
color );
 
  741   QWidget::hideEvent( e );
 
  753   QWidget::mousePressEvent( e );
 
  756 QColor QgsCompoundColorWidget::averageColor( 
const QImage &image )
 const 
  764   for ( 
int heightIndex = 0; heightIndex < image.height(); ++heightIndex )
 
  766     const QRgb *scanLine = 
reinterpret_cast< const QRgb * 
>( image.constScanLine( heightIndex ) );
 
  767     for ( 
int widthIndex = 0; widthIndex < image.width(); ++widthIndex )
 
  769       tmpRgb = scanLine[widthIndex];
 
  770       sumRed += qRed( tmpRgb );
 
  771       sumBlue += qBlue( tmpRgb );
 
  772       sumGreen += qGreen( tmpRgb );
 
  777   const double avgRed = 
static_cast<double>( sumRed ) / ( 255.0 * colorCount );
 
  778   const double avgGreen = 
static_cast<double>( sumGreen ) / ( 255.0 * colorCount );
 
  779   const double avgBlue = 
static_cast<double>( sumBlue ) / ( 255.0 * colorCount );
 
  782   return QColor::fromRgbF( avgRed, avgGreen, avgBlue );
 
  785 QColor QgsCompoundColorWidget::sampleColor( QPoint point )
 const 
  787   const int sampleRadius = mSpinBoxRadius->value() - 1;
 
  788   QScreen *screen = findScreenAt( point );
 
  793   const QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(),
 
  794                                 point.x() - sampleRadius,
 
  795                                 point.y() - sampleRadius,
 
  796                                 1 + sampleRadius * 2,
 
  797                                 1 + sampleRadius * 2 );
 
  798   const QImage snappedImage = snappedPixmap.toImage();
 
  800   return averageColor( snappedImage );
 
  809     const QColor hoverColor = sampleColor( e->globalPos() );
 
  810     mSamplePreview->setColor( hoverColor );
 
  816   QWidget::mouseMoveEvent( e );
 
  824     stopPicking( e->globalPos() );
 
  829   QWidget::mouseReleaseEvent( e );
 
  834   if ( !mPickingColor )
 
  842   stopPicking( QCursor::pos(), e->key() == Qt::Key_Space );
 
  845 void QgsCompoundColorWidget::mHueRadio_toggled( 
bool checked )
 
  854 void QgsCompoundColorWidget::mSaturationRadio_toggled( 
bool checked )
 
  863 void QgsCompoundColorWidget::mValueRadio_toggled( 
bool checked )
 
  872 void QgsCompoundColorWidget::mRedRadio_toggled( 
bool checked )
 
  881 void QgsCompoundColorWidget::mGreenRadio_toggled( 
bool checked )
 
  890 void QgsCompoundColorWidget::mBlueRadio_toggled( 
bool checked )
 
  899 void QgsCompoundColorWidget::mAddColorToSchemeButton_clicked()
 
  904 void QgsCompoundColorWidget::updateActionsForCurrentScheme()
 
  908   mActionImportColors->setEnabled( scheme->
isEditable() );
 
  909   mActionPasteColors->setEnabled( scheme->
isEditable() );
 
  910   mAddColorToSchemeButton->setEnabled( scheme->
isEditable() );
 
  911   mRemoveColorsFromSchemeButton->setEnabled( scheme->
isEditable() );
 
  914   mActionRemovePalette->setEnabled( 
static_cast< bool >( userScheme ) );
 
  917     mActionShowInButtons->setEnabled( 
true );
 
  923     mActionShowInButtons->setEnabled( 
false );
 
static const double UI_SCALE_FACTOR
UI scaling factor.
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
void pasteColors()
Pastes colors from clipboard to the list.
void removeSelection()
Removes any selected colors from the list.
void copyColors()
Copies colors from the list to the clipboard.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
void addColorScheme(QgsColorScheme *scheme)
Adds a color scheme to the registry.
QList< QgsColorScheme * > schemes() const
Returns all color schemes in the registry.
bool removeColorScheme(QgsColorScheme *scheme)
Removes all matching color schemes from the registry.
Abstract base class for color schemes.
@ ShowInColorButtonMenu
Show scheme in color button drop-down menu.
@ ShowInColorDialog
Show scheme in color picker dialog.
virtual bool isEditable() const
Returns whether the color scheme is editable.
bool setColors(const QgsNamedColorList &colors, const QString &context=QString(), const QColor &baseColor=QColor()) override
Sets the colors for the scheme.
static void addRecentColor(const QColor &color)
Adds a color to the list of recent colors.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsNamedColorList importColorsFromGpl(QFile &file, bool &ok, QString &name)
Imports colors from a gpl GIMP palette file.
static QString colorToName(const QColor &color)
Returns a friendly display name for a color.
A color scheme which stores its colors in a gpl palette file within the "palettes" subfolder off the ...
QString schemeName() const override
Gets the name for the color scheme.
void setName(const QString &name)
Sets the name for the scheme.
QgsColorScheme::SchemeFlags flags() const override
Returns the current flags for the color scheme.
bool erase()
Erases the associated gpl palette file from the users "palettes" folder.
void setShowSchemeInMenu(bool show)
Sets whether a this scheme should be shown in color button menus.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.