51 changes: 44 additions & 7 deletions src/app/qgsattributeactiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ QgsAttributeActionDialog::QgsAttributeActionDialog( QgsAttributeAction* actions,

connect( attributeActionTable, SIGNAL( itemSelectionChanged() ),
this, SLOT( itemSelectionChanged() ) );
connect( actionName, SIGNAL( textChanged( QString ) ), this, SLOT( updateButtons() ) );
connect( actionAction, SIGNAL( textChanged( QString ) ), this, SLOT( updateButtons() ) );

connect( moveUpButton, SIGNAL( clicked() ), this, SLOT( moveUp() ) );
connect( moveDownButton, SIGNAL( clicked() ), this, SLOT( moveDown() ) );
connect( removeButton, SIGNAL( clicked() ), this, SLOT( remove() ) );
Expand Down Expand Up @@ -72,6 +75,8 @@ void QgsAttributeActionDialog::init()
const QgsAction action = ( *mActions )[i];
insertRow( i, action.type(), action.name(), action.action(), action.capture() );
}

updateButtons();
}

void QgsAttributeActionDialog::insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &action, bool capture )
Expand All @@ -87,6 +92,8 @@ void QgsAttributeActionDialog::insertRow( int row, QgsAction::ActionType type, c
item->setFlags( item->flags() & ~( Qt::ItemIsEditable | Qt::ItemIsUserCheckable ) );
item->setCheckState( capture ? Qt::Checked : Qt::Unchecked );
attributeActionTable->setItem( row, 3, item );

updateButtons();
}

void QgsAttributeActionDialog::moveUp()
Expand All @@ -97,7 +104,7 @@ void QgsAttributeActionDialog::moveUp()
QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems();
if ( !selection.isEmpty() )
{
row1 = attributeActionTable->row( selection.first() );
row1 = selection.first()->row();
}

if ( row1 > 0 )
Expand All @@ -118,7 +125,7 @@ void QgsAttributeActionDialog::moveDown()
QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems();
if ( !selection.isEmpty() )
{
row1 = attributeActionTable->row( selection.first() );
row1 = selection.first()->row();
}

if ( row1 < attributeActionTable->rowCount() - 1 )
Expand Down Expand Up @@ -181,13 +188,15 @@ void QgsAttributeActionDialog::remove()
if ( !selection.isEmpty() )
{
// Remove the selected row.
int row = attributeActionTable->row( selection.first() );
int row = selection.first()->row();
attributeActionTable->removeRow( row );

// And select the row below the one that was selected or the last one.
if ( row >= attributeActionTable->rowCount() )
row = attributeActionTable->rowCount() - 1;
attributeActionTable->selectRow( row );

updateButtons();
}
}

Expand Down Expand Up @@ -249,11 +258,35 @@ void QgsAttributeActionDialog::update()
QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems();
if ( !selection.isEmpty() )
{
int i = attributeActionTable->row( selection.first() );
insert( i );
insert( selection.first()->row() );
}
}

void QgsAttributeActionDialog::updateButtons()
{
bool validNewAction = !actionName->text().isEmpty() && !actionAction->text().isEmpty();

QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems();
bool hasSelection = !selection.isEmpty();

if ( hasSelection )
{
int row = selection.first()->row();
moveUpButton->setEnabled( row >= 1 );
moveDownButton->setEnabled( row >= 0 && row < attributeActionTable->rowCount() - 1 );
}
else
{
moveUpButton->setEnabled( false );
moveDownButton->setEnabled( false );
}

removeButton->setEnabled( hasSelection );

insertButton->setEnabled( validNewAction );
updateButton->setEnabled( hasSelection && validNewAction );
}

void QgsAttributeActionDialog::insertField()
{
// Convert the selected field to an expression and
Expand Down Expand Up @@ -289,10 +322,14 @@ void QgsAttributeActionDialog::apply()
void QgsAttributeActionDialog::itemSelectionChanged()
{
QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems();
if ( !selection.isEmpty() )
bool hasSelection = !selection.isEmpty();
if ( hasSelection )
{
rowSelected( attributeActionTable->row( selection.first() ) );
int row = selection.first()->row();
rowSelected( row );
}

updateButtons();
}

void QgsAttributeActionDialog::rowSelected( int row )
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsattributeactiondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class QgsAttributeActionDialog: public QWidget, private Ui::QgsAttributeActionDi
void update();
void itemSelectionChanged();

private slots:
void updateButtons();

private:

void insertRow( int row, QgsAction::ActionType type, const QString &name, const QString &action, bool capture );
Expand Down
33 changes: 21 additions & 12 deletions src/app/qgsnewspatialitelayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
}
settings.endGroup();

buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( false );

connect( buttonBox->button( QDialogButtonBox::Apply ), SIGNAL( clicked() ), this, SLOT( apply() ) );

buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
mOkButton->setEnabled( false );

// Set the SRID box to a default of WGS84
QgsCoordinateReferenceSystem srs;
Expand All @@ -81,6 +77,12 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
leSRID->setText( srs.authid() + " - " + srs.description() );

pbnFindSRID->setEnabled( mDatabaseComboBox->count() );

connect( mNameEdit, SIGNAL( textChanged( QString ) ), this, SLOT( nameChanged( QString ) ) );
connect( mAttributeView, SIGNAL( itemSelectionChanged() ), this, SLOT( selectionChanged() ) );

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
}

QgsNewSpatialiteLayerDialog::~QgsNewSpatialiteLayerDialog()
Expand Down Expand Up @@ -154,8 +156,7 @@ void QgsNewSpatialiteLayerDialog::on_leLayerName_textChanged( QString text )
{
Q_UNUSED( text );
bool created = leLayerName->text().length() > 0 && mAttributeView->topLevelItemCount() > 0 && createDb();
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( created );
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( created );
mOkButton->setEnabled( created );
}

void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
Expand All @@ -169,8 +170,7 @@ void QgsNewSpatialiteLayerDialog::on_mAddAttributeButton_clicked()
if ( mAttributeView->topLevelItemCount() > 0 && leLayerName->text().length() > 0 )
{
bool created = createDb();
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( created );
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( created );
mOkButton->setEnabled( created );
}
mNameEdit->clear();
}
Expand All @@ -181,8 +181,7 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
delete mAttributeView->currentItem();
if ( mAttributeView->topLevelItemCount() == 0 )
{
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
buttonBox->button( QDialogButtonBox::Apply )->setEnabled( false );
mOkButton->setEnabled( false );
}
}

Expand Down Expand Up @@ -251,6 +250,16 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
delete mySelector;
}

void QgsNewSpatialiteLayerDialog::nameChanged( QString name )
{
mAddAttributeButton->setDisabled( name.isEmpty() || mAttributeView->findItems( name, Qt::MatchExactly ).size() > 0 );
}

void QgsNewSpatialiteLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().size() == 0 );
}

bool QgsNewSpatialiteLayerDialog::createDb()
{
QString dbPath = mDatabaseComboBox->currentText();
Expand Down
7 changes: 5 additions & 2 deletions src/app/qgsnewspatialitelayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,26 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
void on_pbnFindSRID_clicked();
void on_leLayerName_textChanged( QString text );
void on_toolButtonNewDatabase_clicked();
void nameChanged( QString );
void selectionChanged();

void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
void on_buttonBox_accepted();
void on_buttonBox_rejected();

bool apply();

private:
/**Returns the selected geometry type*/
QString selectedType() const;

/** Create a new database */
bool createDb();

bool apply();

static QString quotedIdentifier( QString id );
static QString quotedValue( QString value );

QPushButton *mOkButton;
int mCrsId;
};

Expand Down
2 changes: 0 additions & 2 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,6 @@ QString QgsVectorFileWriter::errorMessage()

bool QgsVectorFileWriter::addFeature( QgsFeature& feature )
{
QgsAttributeMap::const_iterator it;

// create the feature
OGRFeatureH poFeature = OGR_F_Create( OGR_L_GetLayerDefn( mLayer ) );

Expand Down
83 changes: 49 additions & 34 deletions src/core/qgsvectorlayerimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@
#include "qgsvectorlayerimport.h"
#include "qgsproviderregistry.h"

#include <QFile>
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <QTextCodec>
#include <QTextStream>
#include <QSet>
#include <QMetaType>

#include <cassert>
#include <cstdlib> // size_t
#include <limits> // std::numeric_limits


#define FEATURE_BUFFER_SIZE 200

typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
Expand All @@ -53,16 +39,17 @@ typedef QgsVectorLayerImport::ImportError createEmptyLayer_t(
);


QgsVectorLayerImport::QgsVectorLayerImport(
const QString &uri,
const QString &providerKey,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
bool overwrite,
const QMap<QString, QVariant> *options )
QgsVectorLayerImport::QgsVectorLayerImport( const QString &uri,
const QString &providerKey,
const QgsFieldMap& fields,
QGis::WkbType geometryType,
const QgsCoordinateReferenceSystem* crs,
bool overwrite,
const QMap<QString, QVariant> *options )
: mErrorCount( 0 )
{
mProvider = NULL;

QgsProviderRegistry * pReg = QgsProviderRegistry::instance();

QLibrary *myLib = pReg->providerLibrary( providerKey );
Expand Down Expand Up @@ -170,6 +157,7 @@ bool QgsVectorLayerImport::flushBuffer()
.arg( mFeatureBuffer.first().id() )
.arg( mFeatureBuffer.last().id() );
mError = ErrFeatureWriteFailed;
mErrorCount += mFeatureBuffer.count();

mFeatureBuffer.clear();
QgsDebugMsg( mErrorMessage );
Expand Down Expand Up @@ -212,8 +200,18 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
outputCRS = &layer->crs();
}

QgsFieldMap fields = skipAttributeCreation ? QgsFieldMap() : layer->pendingFields();
if ( layer->providerType() == "ogr" && layer->storageType() == "ESRI Shapefile" )
{
// convert field names to lowercase
for ( QgsFieldMap::iterator fldIt = fields.begin(); fldIt != fields.end(); ++fldIt )
{
fldIt.value().setName( fldIt.value().name().toLower() );
}
}

QgsVectorLayerImport * writer =
new QgsVectorLayerImport( uri, providerKey, skipAttributeCreation ? QgsFieldMap() : layer->pendingFields(), layer->wkbType(), outputCRS, false, options );
new QgsVectorLayerImport( uri, providerKey, fields, layer->wkbType(), outputCRS, false, options );

// check whether file creation was successful
ImportError err = writer->hasError();
Expand Down Expand Up @@ -249,7 +247,12 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
shallTransform = false;
}

int n = 0, errors = 0;
int n = 0;

if ( errorMessage )
{
*errorMessage = QObject::tr( "Feature write errors:" );
}

// write all features
while ( layer->nextFeature( fet ) )
Expand All @@ -275,7 +278,7 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
.arg( fet.typeName() ).arg( e.what() );
QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) );
if ( errorMessage )
*errorMessage = msg;
*errorMessage += "\n" + msg;

return ErrProjection;
}
Expand All @@ -288,19 +291,14 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
{
if ( writer->hasError() && errorMessage )
{
if ( errorMessage->isEmpty() )
{
*errorMessage = QObject::tr( "Feature write errors:" );
}
*errorMessage += "\n" + writer->errorMessage();
}
errors++;

if ( errors > 1000 )
if ( writer->errorCount() > 1000 )
{
if ( errorMessage )
{
*errorMessage += QObject::tr( "Stopping after %1 errors" ).arg( errors );
*errorMessage += "\n" + QObject::tr( "Stopping after %1 errors" ).arg( writer->errorCount() );
}

n = -1;
Expand All @@ -310,16 +308,33 @@ QgsVectorLayerImport::importLayer( QgsVectorLayer* layer,
n++;
}

// flush the buffer to be sure that all features are written
if ( !writer->flushBuffer() )
{
if ( writer->hasError() && errorMessage )
{
*errorMessage += "\n" + writer->errorMessage();
}
}
int errors = writer->errorCount();

delete writer;

if ( shallTransform )
{
delete ct;
}

if ( errors > 0 && errorMessage && n > 0 )
if ( errorMessage )
{
*errorMessage += QObject::tr( "\nOnly %1 of %2 features written." ).arg( n - errors ).arg( n );
if ( n > 0 && errors > 0 )
{
*errorMessage += "\n" + QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n );
}
else
{
errorMessage->clear();
}
}

return errors == 0 ? NoError : ErrFeatureWriteFailed;
Expand Down
Loading