Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pasting unsaved changes as temporary scratch layers #60474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/vector/qgsvectorlayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,
QString providerDefault = layer->dataProvider()->defaultValueClause( providerIndex );
if ( !providerDefault.isEmpty() )
{
v = providerDefault;
v = QgsUnsetAttributeValue( providerDefault );
checkUnique = false;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/gui/editorwidgets/qgstexteditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ QVariant QgsTextEditWrapper::value() const

if ( !QgsVariantUtils::isNull( defaultValue() ) && v == defaultValue().toString() )
{
return defaultValue();
return QVariant::fromValue( QgsUnsetAttributeValue( defaultValue().toString() ) );
}

QVariant res( v );
Expand Down Expand Up @@ -302,6 +302,10 @@ void QgsTextEditWrapper::setWidgetValue( const QVariant &val )
{
v = QgsApplication::nullRepresentation();
}
else if ( val.userType() == qMetaTypeId<QgsUnsetAttributeValue>() )
{
v = defaultValue().toString();
}
else
{
v = field().displayString( val );
Expand Down
51 changes: 49 additions & 2 deletions tests/src/gui/testqgstexteditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
#include <nlohmann/json.hpp>
#include "qgsjsonutils.h"

class TestQgsTextEditWrapper : public QObject
class TestQgsTextEditWrapper : public QgsTest
{
Q_OBJECT
public:
TestQgsTextEditWrapper() = default;
TestQgsTextEditWrapper()
: QgsTest( QStringLiteral( "Text Edit Wrapper" ) ) {};

private:
QTemporaryDir tempDir;
Expand All @@ -44,6 +45,7 @@ class TestQgsTextEditWrapper : public QObject
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void defaultValueClause();
void testWithJsonInPostgres();
void testWithJsonBInPostgres();
};
Expand All @@ -69,6 +71,51 @@ void TestQgsTextEditWrapper::cleanup()
{
}

void TestQgsTextEditWrapper::defaultValueClause()
{
QgsVectorLayer vl( copyTestData( QStringLiteral( "points_gpkg.gpkg" ) ) + QStringLiteral( "|layername=points_gpkg" ) );
QVERIFY( vl.isValid() );

QgsTextEditWrapper wrapper( &vl, vl.fields().indexOf( QLatin1String( "fid" ) ), nullptr, nullptr );
QLineEdit *widget = qobject_cast<QLineEdit *>( wrapper.widget() );
wrapper.setEnabled( true );
QCOMPARE( wrapper.defaultValue().toString(), QStringLiteral( "Autogenerate" ) );
QCOMPARE( widget->placeholderText(), QStringLiteral( "Autogenerate" ) );

wrapper.setValues( QgsUnsetAttributeValue( QStringLiteral( "Autogenerate" ) ), {} );
QCOMPARE( widget->text(), QStringLiteral( "Autogenerate" ) );
QCOMPARE( wrapper.value().userType(), qMetaTypeId< QgsUnsetAttributeValue >() );

// set explicit text
widget->setText( QStringLiteral( "11" ) );
QCOMPARE( wrapper.value().userType(), qMetaTypeId< long long >() );
QCOMPARE( wrapper.value().toInt(), 11 );

// reset to unset value (this time without the default value clause, should still work)
wrapper.setValues( QgsUnsetAttributeValue(), {} );
QCOMPARE( widget->text(), QStringLiteral( "Autogenerate" ) );
QCOMPARE( wrapper.value().userType(), qMetaTypeId< QgsUnsetAttributeValue >() );

// set to null
widget->clear();
QVERIFY( QgsVariantUtils::isNull( wrapper.value() ) );

// reset to unset value (this time without the default value clause, should still work)
wrapper.setValues( QgsUnsetAttributeValue(), {} );
QCOMPARE( widget->text(), QStringLiteral( "Autogenerate" ) );
QCOMPARE( wrapper.value().userType(), qMetaTypeId< QgsUnsetAttributeValue >() );

// null -> valid value
widget->clear();
QVERIFY( QgsVariantUtils::isNull( wrapper.value() ) );
widget->setText( QStringLiteral( "11" ) );
QCOMPARE( wrapper.value().userType(), qMetaTypeId< long long >() );
QCOMPARE( wrapper.value().toInt(), 11 );
// back to null
widget->clear();
QVERIFY( QgsVariantUtils::isNull( wrapper.value() ) );
}

void TestQgsTextEditWrapper::testWithJsonInPostgres()
{
#ifdef ENABLE_PGTEST
Expand Down
40 changes: 40 additions & 0 deletions tests/src/python/test_qgsvectorlayerutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import shutil
import tempfile
import os

from qgis.PyQt.QtCore import QVariant
from qgis.core import (
Expand Down Expand Up @@ -995,6 +996,45 @@ def test_unique_pk_when_subset(self):
vl.addFeatures(features)
self.assertTrue(vl.commitChanges())

def test_create_feature_provider_default_value_clause(self):
"""Test create feature with a provider side defaultValueClause"""
src = unitTestDataPath("points_gpkg.gpkg")

with tempfile.TemporaryDirectory() as temp_dir:
dest = os.path.join(temp_dir, "points.gpkg")
shutil.copy(src, dest)
vl = QgsVectorLayer(dest, "vl", "ogr")
self.assertTrue(vl.isValid())
self.assertEqual(vl.dataProvider().defaultValueClause(0), "Autogenerate")
context = vl.createExpressionContext()
feature = QgsVectorLayerUtils.createFeature(
vl, QgsGeometry.fromPointXY(QgsPointXY(1, 2)), {1: "aaa"}, context
)
# result should be a QgsUnsetValueAttribute for fid field
self.assertTrue(feature.isUnsetValue(0))
self.assertEqual(feature[1], "aaa")
self.assertTrue(vl.startEditing())
vl.addFeature(feature)
self.assertTrue(vl.commitChanges())
# with explicit fid
feature = QgsVectorLayerUtils.createFeature(
vl,
QgsGeometry.fromPointXY(QgsPointXY(1, 2)),
{0: 111, 1: "bbb"},
context,
)
self.assertFalse(feature.isUnsetValue(0))
self.assertEqual(feature[0], 111)
self.assertEqual(feature[1], "bbb")
self.assertTrue(vl.startEditing())
vl.addFeature(feature)
self.assertTrue(vl.commitChanges())

res = [f.attributes() for f in vl.getFeatures() if f[1] == "aaa"][0]
self.assertEqual(res, [18, "aaa", NULL, NULL, NULL, NULL, NULL])
res = [f.attributes() for f in vl.getFeatures() if f[1] == "bbb"][0]
self.assertEqual(res, [111, "bbb", NULL, NULL, NULL, NULL, NULL])

def testGuessFriendlyIdentifierField(self):
"""
Test guessing a user friendly identifier field
Expand Down
5 changes: 4 additions & 1 deletion tests/src/python/test_qgsvectorlayerutils_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
QgsDefaultValue,
QgsVectorLayer,
QgsVectorLayerUtils,
QgsUnsetAttributeValue,
)
import unittest
from qgis.testing import start_app, QgisTestCase
Expand Down Expand Up @@ -53,7 +54,9 @@ def testCreateFeature(self):
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
self.assertTrue(QgsVectorLayerUtils.valueExists(pg_layer, 0, default_clause))
self.assertTrue(
QgsVectorLayerUtils.valueExists(pg_layer, 0, QgsUnsetAttributeValue())
)
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
Expand Down