Esempio n. 1
0
bool QgsGplColorScheme::setColors( const QgsNamedColorList &colors, const QString &context, const QColor &baseColor )
{
  Q_UNUSED( context );
  Q_UNUSED( baseColor );

  QString destFilePath = gplFilePath();
  if ( destFilePath.isEmpty() )
  {
    return false;
  }

  QFile destFile( destFilePath );
  if ( QgsSymbolLayerUtils::saveColorsToGpl( destFile, schemeName(), colors ) )
  {
    if ( QgsApplication::colorSchemeRegistry()->randomStyleColorScheme() == this )
    {
      // force a re-generation of the random style color list, since the color list has changed
      QgsApplication::colorSchemeRegistry()->setRandomStyleColorScheme( this );
    }
    return true;
  }
  else
  {
    return false;
  }
}
Esempio n. 2
0
bool QgsUserColorScheme::erase()
{
  QString filePath = gplFilePath();
  if ( filePath.isEmpty() )
  {
    return false;
  }

  //try to erase gpl file
  return QFile::remove( filePath );
}
Esempio n. 3
0
QgsUserColorScheme::QgsUserColorScheme( const QString &filename )
  : mFilename( filename )
{
  QFile sourceFile( gplFilePath() );

  //read in name
  if ( sourceFile.open( QIODevice::ReadOnly ) )
  {
    QTextStream in( &sourceFile );

    //find name line
    QString line;
    while ( !in.atEnd() && !line.startsWith( QLatin1String( "Name:" ) ) )
    {
      line = in.readLine();
    }
    if ( !in.atEnd() )
    {
      QRegExp rx( "Name:\\s*(\\S.*)$" );
      if ( rx.indexIn( line ) != -1 )
      {
        mName = rx.cap( 1 );
      }
    }
  }
  if ( mName.isEmpty() )
  {
    mName = mFilename;
  }

  // we consider this scheme writable if the user has permission, OR
  // if it DOESN'T already exist (since new schemes are only created when
  // first written to)
  QFileInfo sourceFileInfo( gplFilePath() );
  mEditable = !sourceFileInfo.exists() || sourceFileInfo.isWritable();
}
Esempio n. 4
0
QgsNamedColorList QgsGplColorScheme::fetchColors( const QString &context, const QColor &baseColor )
{
  Q_UNUSED( context );
  Q_UNUSED( baseColor );

  QString sourceFilePath = gplFilePath();
  if ( sourceFilePath.isEmpty() )
  {
    QgsNamedColorList noColors;
    return noColors;
  }

  bool ok;
  QString name;
  QFile sourceFile( sourceFilePath );
  return QgsSymbolLayerUtils::importColorsFromGpl( sourceFile, ok, name );
}
Esempio n. 5
0
void QgsCompoundColorWidget::newPalette()
{
  bool ok = false;
  QString name = QInputDialog::getText( this, tr( "Create New Palette" ), tr( "Enter a name for the new palette:" ),
                                        QLineEdit::Normal, tr( "New palette" ), &ok );

  if ( !ok || name.isEmpty() )
  {
    //user canceled
    return;
  }

  //generate file name for new palette
  QDir palettePath( gplFilePath() );
  QRegExp badChars( "[,^@={}\\[\\]~!?:&*\"|#%<>$\"'();`' /\\\\]" );
  QString filename = name.simplified().toLower().replace( badChars, QStringLiteral( "_" ) );
  if ( filename.isEmpty() )
  {
    filename = tr( "new_palette" );
  }
  QFileInfo destFileInfo( palettePath.filePath( filename + ".gpl" ) );
  int fileNumber = 1;
  while ( destFileInfo.exists() )
  {
    //try to generate a unique file name
    destFileInfo = QFileInfo( palettePath.filePath( filename + QStringLiteral( "%1.gpl" ).arg( fileNumber ) ) );
    fileNumber++;
  }

  QgsUserColorScheme *newScheme = new QgsUserColorScheme( destFileInfo.fileName() );
  newScheme->setName( name );

  QgsApplication::colorSchemeRegistry()->addColorScheme( newScheme );

  //refresh combobox and set new scheme as active
  refreshSchemeComboBox();
  mSchemeComboBox->setCurrentIndex( mSchemeComboBox->count() - 1 );
}