Commit 632fba2a authored by Frédéric Larue's avatar Frédéric Larue

ValueString() and SetValueString() functions moved to GradientWidget class.

parent a5de84ae
......@@ -29,7 +29,7 @@ public:
const QString& description,
const QList<QColor>& colors,
const QList<float>& values );
inline QColor Value( float v ) const { return Widget()->getColor(v); }
inline QString GLSLFunction( const QString& fName) const { return Widget()->getShaderFunctionGLSL(fName); }
......@@ -79,67 +79,12 @@ inline void UIParamGradient::ConnectModificationSignal()
inline QString UIParamGradient::ValueString() const
{
QString str;
QVector<float> values;
QVector<QColor> colors;
Widget()->getStops( values, colors );
for( int i=0; i<values.size(); ++i )
{
QTextStream stream( &str );
if( i != 0 )
stream << ", ";
stream << values[i] << ": ";
stream << "#";
stream.setFieldWidth(2);
stream.setPadChar('0');
stream << QString::number( colors[i].red() , 16 ).toUpper();
stream << QString::number( colors[i].green(), 16 ).toUpper();
stream << QString::number( colors[i].blue() , 16 ).toUpper();
stream << QString::number( colors[i].alpha(), 16 ).toUpper();
}
return str;
return Widget()->ValueString();
}
inline bool UIParamGradient::SetValueString( const QString &valueString )
{
QStringList stopStrings = valueString.split( ',' );
QList<float> values;
QList<QColor> colors;
QRegExp rgbaExp( "#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" );
for( auto &stop : stopStrings )
{
QStringList valueAndColor = stop.remove( ' ' ).split( ':' );
if( valueAndColor.size() != 2 )
return false;
bool ok;
float value = valueAndColor.front().toFloat( &ok );
if( !ok )
return false;
if( rgbaExp.indexIn(valueAndColor.back()) < 0 )
return false;
int r = rgbaExp.cap(1).toInt( NULL, 16 );
int g = rgbaExp.cap(2).toInt( NULL, 16 );
int b = rgbaExp.cap(3).toInt( NULL, 16 );
int a = rgbaExp.cap(4).toInt( NULL, 16 );
values.push_back( value );
colors.push_back( QColor(r,g,b,a) );
}
Widget()->setStops( colors, values );
return true;
return Widget()->SetValueString( valueString );
}
......
......@@ -265,7 +265,7 @@ QString GradientWidget::getShaderFunctionBodyGLSL( const QVector< QPair<int,int>
{
int p = beg + (end-beg)/2;
res += indent + "if( value <= " + QString::number( m_Stops[ uniqueStops[p].first ].value() ) + " )\n";
res += indent + "if( value <= " + QString::number( (double) m_Stops[ uniqueStops[p].first ].value() ) + " )\n";
res += getShaderFunctionBodyGLSL( uniqueStops, beg, p, indent+" " );
res += indent + "else\n";
res += getShaderFunctionBodyGLSL( uniqueStops, p+1, end, indent+" " );
......@@ -293,7 +293,7 @@ QString GradientWidget::getShaderFunctionBodyGLSL( const QVector< QPair<int,int>
res += indent + "return mix( ";
res += "vec3(" + QString::number(c0.redF()) + "," + QString::number(c0.greenF()) + "," + QString::number(c0.blueF()) + "), ";
res += "vec3(" + QString::number(c1.redF()) + "," + QString::number(c1.greenF()) + "," + QString::number(c1.blueF()) + "), ";
res += "(value-" + QString::number(s0.value()) + ")*" + QString::number(1.0f / (s1.value() - s0.value()));
res += "(value-" + QString::number((double) s0.value()) + ")*" + QString::number(1.0 / (s1.value() - s0.value()));
res += " );\n";
}
}
......@@ -533,3 +533,66 @@ void GradientWidget::getStops( QVector<float> &values, QVector<QColor> &colors )
colors.push_back( s.color() );
}
}
QString GradientWidget::ValueString() const
{
QString str;
for( auto &s : m_Stops )
{
QTextStream stream( &str );
if( &s != &m_Stops[0] )
stream << ", ";
stream << (double) s.value() << ": ";
stream << "#";
stream.setFieldWidth(2);
stream.setPadChar('0');
stream << QString::number( s.color().red() , 16 ).toUpper();
stream << QString::number( s.color().green(), 16 ).toUpper();
stream << QString::number( s.color().blue() , 16 ).toUpper();
stream << QString::number( s.color().alpha(), 16 ).toUpper();
}
return str;
}
bool GradientWidget::SetValueString( const QString &valueString )
{
QStringList stopStrings = valueString.split( ',' );
QList<float> values;
QList<QColor> colors;
QRegExp rgbaExp( "#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})" );
for( auto &stop : stopStrings )
{
QStringList valueAndColor = stop.remove( ' ' ).split( ':' );
if( valueAndColor.size() != 2 )
return false;
bool ok;
float value = valueAndColor.front().toFloat( &ok );
if( !ok )
return false;
if( rgbaExp.indexIn(valueAndColor.back()) < 0 )
return false;
int r = rgbaExp.cap(1).toInt( NULL, 16 );
int g = rgbaExp.cap(2).toInt( NULL, 16 );
int b = rgbaExp.cap(3).toInt( NULL, 16 );
int a = rgbaExp.cap(4).toInt( NULL, 16 );
values.push_back( value );
colors.push_back( QColor(r,g,b,a) );
}
setStops( colors, values );
return true;
}
......@@ -76,6 +76,9 @@ public:
QString getShaderFunctionGLSL( const QString& funcName ) const;
QString ValueString() const;
bool SetValueString( const QString &valueString );
signals:
void gradientModified();
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment