QgsNamedColorList QgsProjectColorScheme::fetchColors( const QString &context, const QColor &baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); QgsNamedColorList colorList; QStringList colorStrings = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Colors" ) ); QStringList colorLabels = QgsProject::instance()->readListEntry( QStringLiteral( "Palette" ), QStringLiteral( "/Labels" ) ); //generate list from custom colors int colorIndex = 0; for ( QStringList::iterator it = colorStrings.begin(); it != colorStrings.end(); ++it ) { QColor color = QgsSymbolLayerUtils::decodeColor( *it ); QString label; if ( colorLabels.length() > colorIndex ) { label = colorLabels.at( colorIndex ); } colorList.append( qMakePair( color, label ) ); colorIndex++; } return colorList; }
void QgsColorDialogV2::importPalette() { QSettings s; QString lastDir = s.value( "/UI/lastGplPaletteDir", "" ).toString(); QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, "GPL (*.gpl);;All files (*.*)" ); activateWindow(); if ( filePath.isEmpty() ) { return; } //check if file exists QFileInfo fileInfo( filePath ); if ( !fileInfo.exists() || !fileInfo.isReadable() ) { QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) ); return; } s.setValue( "/UI/lastGplPaletteDir", fileInfo.absolutePath() ); QFile file( filePath ); QgsNamedColorList importedColors; bool ok = false; QString paletteName; importedColors = QgsSymbolLayerV2Utils::importColorsFromGpl( file, ok, paletteName ); if ( !ok ) { QMessageBox::critical( 0, tr( "Invalid file" ), tr( "Palette file is not readable" ) ); return; } if ( importedColors.length() == 0 ) { //no imported colors QMessageBox::critical( 0, tr( "Invalid file" ), tr( "No colors found in palette file" ) ); return; } //TODO - handle conflicting file names, name for new palette QgsUserColorScheme* importedScheme = new QgsUserColorScheme( fileInfo.fileName() ); importedScheme->setName( paletteName ); importedScheme->setColors( importedColors ); QgsColorSchemeRegistry::instance()->addColorScheme( importedScheme ); //refresh combobox mSchemeComboBox->blockSignals( true ); mSchemeComboBox->clear(); QList<QgsColorScheme *> schemeList = QgsColorSchemeRegistry::instance()->schemes( QgsColorScheme::ShowInColorDialog ); QList<QgsColorScheme *>::const_iterator schemeIt = schemeList.constBegin(); for ( ; schemeIt != schemeList.constEnd(); ++schemeIt ) { mSchemeComboBox->addItem(( *schemeIt )->schemeName() ); } mSchemeComboBox->blockSignals( false ); mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 ); }
void QgsCompoundColorWidget::importPalette() { QgsSettings s; QString lastDir = s.value( QStringLiteral( "/UI/lastGplPaletteDir" ), QDir::homePath() ).toString(); QString filePath = QFileDialog::getOpenFileName( this, tr( "Select palette file" ), lastDir, QStringLiteral( "GPL (*.gpl);;All files (*.*)" ) ); activateWindow(); if ( filePath.isEmpty() ) { return; } //check if file exists QFileInfo fileInfo( filePath ); if ( !fileInfo.exists() || !fileInfo.isReadable() ) { QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Error, file does not exist or is not readable" ) ); return; } s.setValue( QStringLiteral( "/UI/lastGplPaletteDir" ), fileInfo.absolutePath() ); QFile file( filePath ); QgsNamedColorList importedColors; bool ok = false; QString paletteName; importedColors = QgsSymbolLayerUtils::importColorsFromGpl( file, ok, paletteName ); if ( !ok ) { QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "Palette file is not readable" ) ); return; } if ( importedColors.length() == 0 ) { //no imported colors QMessageBox::critical( nullptr, tr( "Invalid file" ), tr( "No colors found in palette file" ) ); return; } //TODO - handle conflicting file names, name for new palette QgsUserColorScheme *importedScheme = new QgsUserColorScheme( fileInfo.fileName() ); importedScheme->setName( paletteName ); importedScheme->setColors( importedColors ); QgsApplication::colorSchemeRegistry()->addColorScheme( importedScheme ); //refresh combobox refreshSchemeComboBox(); mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 ); }
QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString &context, const QColor &baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); //fetch recent colors QSettings settings; QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList(); //generate list from recent colors QgsNamedColorList colorList; Q_FOREACH ( const QVariant& color, recentColorVariants ) { colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerV2Utils::colorToName( color.value<QColor>() ) ) ); }
QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString context, const QColor baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); //fetch recent colors QSettings settings; QList< QVariant > recentColorVariants = settings.value( QString( "/colors/recent" ) ).toList(); //generate list from recent colors QgsNamedColorList colorList; foreach ( QVariant color, recentColorVariants ) { colorList.append( qMakePair( color.value<QColor>(), QString() ) ); }
QgsNamedColorList QgsRecentColorScheme::fetchColors( const QString &context, const QColor &baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); //fetch recent colors QgsSettings settings; QList< QVariant > recentColorVariants = settings.value( QStringLiteral( "colors/recent" ) ).toList(); //generate list from recent colors QgsNamedColorList colorList; const auto constRecentColorVariants = recentColorVariants; for ( const QVariant &color : constRecentColorVariants ) { colorList.append( qMakePair( color.value<QColor>(), QgsSymbolLayerUtils::colorToName( color.value<QColor>() ) ) ); } return colorList; }
bool QgsCustomColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); // save colors to settings QgsSettings settings; QList< QVariant > customColors; QList< QVariant > customColorLabels; QgsNamedColorList::const_iterator colorIt = colors.constBegin(); for ( ; colorIt != colors.constEnd(); ++colorIt ) { QVariant color = ( *colorIt ).first; QVariant label = ( *colorIt ).second; customColors.append( color ); customColorLabels.append( label ); } settings.setValue( QStringLiteral( "colors/palettecolors" ), customColors ); settings.setValue( QStringLiteral( "colors/palettelabels" ), customColorLabels ); return true; }
QgsNamedColorList QgsCustomColorScheme::fetchColors( const QString &context, const QColor &baseColor ) { Q_UNUSED( context ); Q_UNUSED( baseColor ); //fetch predefined custom colors QgsNamedColorList colorList; QgsSettings settings; //check if settings contains custom palette if ( !settings.contains( QStringLiteral( "/colors/palettecolors" ) ) ) { //no custom palette, return default colors colorList.append( qMakePair( QColor( 0, 0, 0 ), QString() ) ); colorList.append( qMakePair( QColor( 255, 255, 255 ), QString() ) ); colorList.append( qMakePair( QColor( 166, 206, 227 ), QString() ) ); colorList.append( qMakePair( QColor( 31, 120, 180 ), QString() ) ); colorList.append( qMakePair( QColor( 178, 223, 138 ), QString() ) ); colorList.append( qMakePair( QColor( 51, 160, 44 ), QString() ) ); colorList.append( qMakePair( QColor( 251, 154, 153 ), QString() ) ); colorList.append( qMakePair( QColor( 227, 26, 28 ), QString() ) ); colorList.append( qMakePair( QColor( 253, 191, 111 ), QString() ) ); colorList.append( qMakePair( QColor( 255, 127, 0 ), QString() ) ); return colorList; } QList< QVariant > customColorVariants = settings.value( QStringLiteral( "colors/palettecolors" ) ).toList(); QList< QVariant > customColorLabels = settings.value( QStringLiteral( "colors/palettelabels" ) ).toList(); //generate list from custom colors int colorIndex = 0; for ( QList< QVariant >::iterator it = customColorVariants.begin(); it != customColorVariants.end(); ++it ) { QColor color = ( *it ).value<QColor>(); QString label; if ( customColorLabels.length() > colorIndex ) { label = customColorLabels.at( colorIndex ).toString(); } colorList.append( qMakePair( color, label ) ); colorIndex++; } return colorList; }