QColor QgsColorDialog::getLiveColor( const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent, const QString &title, const bool allowAlpha ) { QColor returnColor( initialColor ); QSettings settings; //using native color dialogs? bool useNative = settings.value( QStringLiteral( "/qgis/native_color_dialogs" ), false ).toBool(); if ( useNative ) { QColorDialog* liveDialog = new QColorDialog( initialColor, parent ); liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title ); liveDialog->setOptions( allowAlpha ? QColorDialog::ShowAlphaChannel : ( QColorDialog::ColorDialogOption )0 ); connect( liveDialog, SIGNAL( currentColorChanged( const QColor& ) ), updateObject, updateSlot ); if ( liveDialog->exec() ) { returnColor = liveDialog->currentColor(); } delete liveDialog; } else { QgsColorDialog* liveDialog = new QgsColorDialog( parent, 0, initialColor ); liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title ); if ( !allowAlpha ) { liveDialog->setAllowAlpha( false ); } connect( liveDialog, SIGNAL( currentColorChanged( const QColor& ) ), updateObject, updateSlot ); if ( liveDialog->exec() ) { returnColor = liveDialog->color(); } delete liveDialog; } return returnColor; }
QColor QgsColorDialog::getLiveColor( const QColor& initialColor, QObject* updateObject, const char* updateSlot, QWidget* parent, const QString& title, const QColorDialog::ColorDialogOptions& options ) { QColor returnColor( initialColor ); QColorDialog* liveDialog = new QColorDialog( initialColor, parent ); liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title ); liveDialog->setOptions( options ); connect( liveDialog, SIGNAL( currentColorChanged( const QColor& ) ), updateObject, updateSlot ); if ( liveDialog->exec() ) { returnColor = liveDialog->currentColor(); } delete liveDialog; liveDialog = nullptr; return returnColor; }
QColor QgsColorDialogV2::getLiveColor( const QColor &initialColor, QObject *updateObject, const char *updateSlot, QWidget *parent, const QString &title, const bool allowAlpha ) { QColor returnColor( initialColor ); QgsColorDialogV2* liveDialog = new QgsColorDialogV2( parent, 0, initialColor ); liveDialog->setWindowTitle( title.isEmpty() ? tr( "Select Color" ) : title ); if ( !allowAlpha ) { liveDialog->setAllowAlpha( false ); } connect( liveDialog, SIGNAL( currentColorChanged( const QColor& ) ), updateObject, updateSlot ); if ( liveDialog->exec() ) { returnColor = liveDialog->color(); } delete liveDialog; liveDialog = nullptr; return returnColor; }
QColor GeneralConfig::getColorFromString(QString colorString) { /* -converts a color defined in a string to a 'QColor' object, using regexp validation, and returnes it */ if ( colorString.isEmpty() ) return QColor(); //return if no color is specified //cleen up the string colorString = colorString.simplified(); colorString = colorString.trimmed(); QColor returnColor(0, 0, 0); //dummy color to be returned int p = 0; //used for regexp validator //regexps to check color value QRegularExpressionValidator isRGB( QRegularExpression("\\d{1,3} \\d{1,3} \\d{1,3}") ); //check if RGB QRegularExpressionValidator isRGBA( QRegularExpression("\\d{1,3} \\d{1,3} \\d{1,3} \\d{1,3}") ); //check if RGBA QRegularExpressionValidator isHEX( QRegularExpression("^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8}|[0-9A-Fa-f]{9}|[0-9A-Fa-f]{12})$") );//check if HEX code QRegularExpressionValidator isName( QRegularExpression("\\w+") ); //check if color name //check color value if ( isRGB.validate(colorString, p) == 2) { QStringList rgb = colorString.split( QRegularExpression("\\s") ); returnColor.setRgb( rgb.at(0).toInt(), rgb.at(1).toInt(), rgb.at(2).toInt() ); } else if ( isRGBA.validate(colorString, p) == 2) { QStringList rgb = colorString.split( QRegularExpression("\\s") ); returnColor.setRgb( rgb.at(0).toInt(), rgb.at(1).toInt(), rgb.at(2).toInt(), rgb.at(3).toInt() ); } else if ( isHEX.validate(colorString, p) == 2) { returnColor.setNamedColor(colorString); } else if ( isName.validate(colorString, p) ) { returnColor.setNamedColor( colorString.toLower().remove( QRegularExpression("\\s+") ) ); } //check color is valid and return if ( returnColor.isValid() ) return returnColor; else return QColor(); }