Skip to content

Commit

Permalink
Merge pull request #4925 from pblottiere/bugfix_scrolllocked_218
Browse files Browse the repository at this point in the history
Fixes value relation widget to keep scrollbar activated, fixes #16654 (backport)
  • Loading branch information
Hugo Mercier committed Jul 27, 2017
2 parents 8ccc84e + 399f01a commit 23c2be8
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
Expand Up @@ -103,7 +103,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*
* @param enabled Enable or Disable?
*/
void setEnabled( bool enabled ) override;
virtual void setEnabled( bool enabled ) override;

/** Sets the widget to display in an indeterminate "mixed value" state.
* @note added in QGIS 2.16
Expand Down
24 changes: 24 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -43,6 +43,7 @@ QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer* vl
, mListWidget( nullptr )
, mLineEdit( nullptr )
, mLayer( nullptr )
, mUpdating( false )
{
}

Expand Down Expand Up @@ -248,3 +249,26 @@ void QgsValueRelationWidgetWrapper::showIndeterminateState()
whileBlocking( mLineEdit )->clear();
}
}

void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
{
if ( mUpdating )
return;

if ( mListWidget )
{
mUpdating = true;
for ( int i = 0; i < mListWidget->count(); ++i )
{
QListWidgetItem *item = mListWidget->item( i );

if ( enabled )
item->setFlags( item->flags() | Qt::ItemIsEnabled );
else
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
}
mUpdating = false;
}
else
QgsEditorWidgetWrapper::setEnabled( enabled );
}
5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h
Expand Up @@ -68,6 +68,8 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
static ValueRelationCache createCache( const QgsEditorWidgetConfig& config );
void showIndeterminateState() override;

void setEnabled( bool enabled ) override;

protected:
QWidget* createWidget( QWidget* parent ) override;
void initWidget( QWidget* editor ) override;
Expand All @@ -84,7 +86,10 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
ValueRelationCache mCache;
QgsVectorLayer* mLayer;

bool mUpdating;

friend class QgsValueRelationWidgetFactory;
friend class TestQgsValueRelationWidgetWrapper;
};

Q_DECLARE_METATYPE( QgsValueRelationWidgetWrapper::ValueRelationCache )
Expand Down
2 changes: 1 addition & 1 deletion tests/src/gui/CMakeLists.txt
Expand Up @@ -144,4 +144,4 @@ ADD_QGIS_TEST(spinbox testqgsspinbox.cpp)
ADD_QGIS_TEST(sqlcomposerdialog testqgssqlcomposerdialog.cpp)
ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
ADD_QGIS_TEST(composergui testqgscomposergui.cpp)

ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
107 changes: 107 additions & 0 deletions tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
@@ -0,0 +1,107 @@
/***************************************************************************
testqgsvaluerelationwidgetwrapper.cpp
--------------------------------------
Date : 21 07 2017
Copyright : (C) 2017 Paul Blottiere
Email : paul dot blottiere at oslandia dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QtTest/QtTest>
#include <QScrollBar>

#include <editorwidgets/core/qgseditorwidgetregistry.h>
#include <qgsapplication.h>
#include <qgsproject.h>
#include <qgsvectorlayer.h>
#include "qgseditorwidgetwrapper.h"
#include <editorwidgets/qgsvaluerelationwidgetwrapper.h>

class TestQgsValueRelationWidgetWrapper : public QObject
{
Q_OBJECT
public:
TestQgsValueRelationWidgetWrapper() {}

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void testScrollBarUnlocked();
};

void TestQgsValueRelationWidgetWrapper::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsEditorWidgetRegistry::initEditors();
}

void TestQgsValueRelationWidgetWrapper::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsValueRelationWidgetWrapper::init()
{
}

void TestQgsValueRelationWidgetWrapper::cleanup()
{
}

void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
{
// create a vector layer
QgsVectorLayer vl1( QString( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QString( "vl1" ), QString( "memory" ) );

// build a value relation widget wrapper
QListWidget lw;
QWidget editor;
QgsValueRelationWidgetWrapper w( &vl1, 0, &editor, nullptr );
w.setEnabled( true );
w.initWidget( &lw );

// add an item virtually
QListWidgetItem item;
item.setText( "MyText" );
w.mListWidget->addItem( &item );
QCOMPARE( w.mListWidget->item( 0 )->text(), QString( "MyText" ) );

// when the widget wrapper is enabled, the container should be enabled
// as well as items
w.setEnabled( true );

QCOMPARE( w.widget()->isEnabled(), true );

bool itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
QCOMPARE( itemEnabled, true );

// when the widget wrapper is disabled, the container should still be enabled
// to keep the scrollbar available but items should be disabled to avoid
// edition
w.setEnabled( false );

itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
QCOMPARE( itemEnabled, false );

QCOMPARE( w.widget()->isEnabled(), true );

// recheck after re-enabled
w.setEnabled( true );

QCOMPARE( w.widget()->isEnabled(), true );
itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
QCOMPARE( itemEnabled, true );
}

QTEST_MAIN( TestQgsValueRelationWidgetWrapper )
#include "testqgsvaluerelationwidgetwrapper.moc"

0 comments on commit 23c2be8

Please sign in to comment.