void QgsGrassNewMapset::setLocations()
{
  QgsDebugMsg( "entered" );

  mLocationComboBox->clear();

  QSettings settings;
  QString lastLocation = settings.value( "/GRASS/lastLocation" ).toString();

  if ( gisdbaseExists() )
  {
    // Get available locations with write permissions
    QDir gisdbaseDir( gisdbase() );

    // Add all subdirs containing PERMANENT/DEFAULT_WIND
    int idx = 0;
    int sel = -1;
    for ( unsigned int i = 0; i < gisdbaseDir.count(); i++ )
    {
      if ( gisdbaseDir[i] == "." || gisdbaseDir[i] == ".." )
        continue;

      QString windName = mDatabaseLineEdit->text() + "/" + gisdbaseDir[i] + "/PERMANENT/DEFAULT_WIND";
      QString locationName = mDatabaseLineEdit->text() + "/" + gisdbaseDir[i];
      QFileInfo locationInfo( locationName );

      if ( QFile::exists( windName ) && locationInfo.isWritable() )
      {
        mLocationComboBox->insertItem( -1, QString( gisdbaseDir[i] ) );
        if ( QString( gisdbaseDir[i] ) == lastLocation )
        {
          sel = idx;
        }
        idx++;
      }
    }
    if ( sel >= 0 )
    {
      mLocationComboBox->setCurrentIndex( sel );
    }
  }

  if ( mLocationComboBox->count() == 0 )
  {
    mCreateLocationRadioButton->setChecked( true );
    mSelectLocationRadioButton->setEnabled( false );
  }
  else
  {
    mSelectLocationRadioButton->setEnabled( true );
  }

  locationRadioSwitched(); // calls also checkLocation()
}
示例#2
0
void QgsGrassNewMapset::createMapset()
{
    QgsDebugMsg( "entered." );

    // TODO: handle all possible errors better, especially
    //       half created location/mapset

    QString location;

    if ( mCreateLocationRadioButton->isChecked() )
    {
        location = mLocationLineEdit->text().trimmed();

        // TODO: add QgsGrass::setLocation or G_make_location with
        //       database path
        QgsGrass::activeMode(); // because it calls private gsGrass::init()
#if defined(WIN32)
        G__setenv(( char * ) "GISDBASE", QgsGrass::shortPath( mDatabaseLineEdit->text() ).toUtf8().data() );
#else
        G__setenv(( char * ) "GISDBASE", mDatabaseLineEdit->text().toUtf8().data() );
#endif

        int ret = 0;

        try
        {
            ret = G_make_location( location.toUtf8().data(), &mCellHead, mProjInfo, mProjUnits, stdout );
        }
        catch ( QgsGrass::Exception &e )
        {
            ret = -1;
            Q_UNUSED( e );
        }

        if ( ret != 0 )
        {
            QMessageBox::warning( this, tr( "Create location" ),
                                  tr( "Cannot create new location: %1" ).arg( QgsGrass::errorMessage() ) );
            return;
        }

        // Location created -> reset widgets
        setLocations();
        mSelectLocationRadioButton->setChecked( true );
        mLocationComboBox->setItemText( mLocationComboBox->currentIndex(), location );
        mLocationLineEdit->setText( "" );
        locationRadioSwitched(); // calls also checkLocation()
    }
    else
    {
        location = mLocationComboBox->currentText();
    }

    // Create mapset
    QString mapset = mMapsetLineEdit->text().trimmed();

    if ( mapset != "PERMANENT" )
    {
        QString locationPath = mDatabaseLineEdit->text() + "/" + location;
        QDir d( locationPath );

        if ( !d.mkdir( mapset ) )
        {
            QMessageBox::warning( this, tr( "Create mapset" ),
                                  tr( "Cannot create new mapset directory" ) );
            return;
        }

        // Copy WIND Better way to copy file in Qt?
        QStringList lines;
        QFile in( locationPath + "/PERMANENT/DEFAULT_WIND" );
        if ( !in.open( QIODevice::ReadOnly ) )
        {
            QMessageBox::warning( this, tr( "Create mapset" ), tr( "Cannot open DEFAULT_WIND" ) );
            return;
        }

        QFile out( locationPath + "/" + mapset + "/WIND" );
        if ( !out.open( QIODevice::WriteOnly ) )
        {
            QMessageBox::warning( this, tr( "Create mapset" ), tr( "Cannot open WIND" ) );
            return;
        }
        QTextStream stream( &out );

        //QTextStream stream( &file );
        QString line;
        char buf[100];
        while ( in.readLine( buf, 100 ) != -1 )
        {
            stream << buf;
        }

        in.close();
        out.close();
    }

    QString err = QgsGrass::openMapset(
                      mDatabaseLineEdit->text(), location, mapset );

    if ( err.length() > 0 )
    {
        QMessageBox::information( this, tr( "New mapset" ),
                                  tr( "New mapset successfully created, but cannot be opened: %1" ).arg( err ) );
    }
    else
    {
        QMessageBox::information( this, tr( "New mapset" ),
                                  tr( "New mapset successfully created and set as current working mapset." ) );

        mPlugin->mapsetChanged();
    }

    deleteLater();
}