Skip to content
Permalink
Browse files

Merge pull request #4905 from pblottiere/bugfix_scrolllocked

Fixes value relation widget to keep scrollbar activated, fixes #16654
  • Loading branch information
Hugo Mercier
Hugo Mercier committed Jul 26, 2017
2 parents f0a6376 + cdfca0a commit d19b4aa2bf59017515adb635c5dc11708cd63edf
@@ -104,7 +104,6 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
%End

virtual void setEnabled( bool enabled );

%Docstring
Is used to enable or disable the edit functionality of the managed widget.
By default this will enable or disable the whole widget
@@ -122,7 +122,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.
* \since QGIS 2.16
@@ -33,6 +33,7 @@ QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer *vl
, mListWidget( nullptr )
, mLineEdit( nullptr )
, mLayer( nullptr )
, mUpdating( false )
{
}

@@ -198,3 +199,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 );
}
@@ -59,6 +59,8 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper

void showIndeterminateState() override;

void setEnabled( bool enabled ) override;

protected:
QWidget *createWidget( QWidget *parent ) override;
void initWidget( QWidget *editor ) override;
@@ -75,7 +77,10 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
QgsValueRelationFieldFormatter::ValueRelationCache mCache;
QgsVectorLayer *mLayer = nullptr;

bool mUpdating;

friend class QgsValueRelationWidgetFactory;
friend class TestQgsValueRelationWidgetWrapper;
};

#endif // QGSVALUERELATIONWIDGETWRAPPER_H
@@ -22,6 +22,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/symbology-ng
${CMAKE_SOURCE_DIR}/src/core/fieldformatter
${CMAKE_SOURCE_DIR}/src/test
${CMAKE_SOURCE_DIR}/src/native

@@ -134,3 +135,4 @@ ADD_QGIS_TEST(listwidgettest testqgslistwidget.cpp)
ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
ADD_QGIS_TEST(composergui testqgscomposergui.cpp)
ADD_QGIS_TEST(layoutview testqgslayoutview.cpp)
ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
@@ -0,0 +1,110 @@
/***************************************************************************
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 "qgstest.h"
#include <QScrollBar>

#include <editorwidgets/core/qgseditorwidgetregistry.h>
#include <qgsapplication.h>
#include <qgsproject.h>
#include <qgsvectorlayer.h>
#include "qgseditorwidgetwrapper.h"
#include <editorwidgets/qgsvaluerelationwidgetwrapper.h>
#include "qgsgui.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();
QgsGui::editorWidgetRegistry()->initEditors();
}

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

void TestQgsValueRelationWidgetWrapper::init()
{
}

void TestQgsValueRelationWidgetWrapper::cleanup()
{
}

void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
{
// create a vector layer
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &vl1, false, false );

// 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 );
}

QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )
#include "testqgsvaluerelationwidgetwrapper.moc"

0 comments on commit d19b4aa

Please sign in to comment.
You can’t perform that action at this time.