Skip to content

Commit 23c2be8

Browse files
author
Hugo Mercier
authored
Merge pull request #4925 from pblottiere/bugfix_scrolllocked_218
Fixes value relation widget to keep scrollbar activated, fixes #16654 (backport)
2 parents 8ccc84e + 399f01a commit 23c2be8

File tree

5 files changed

+138
-2
lines changed

5 files changed

+138
-2
lines changed

src/gui/editorwidgets/core/qgseditorwidgetwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
103103
*
104104
* @param enabled Enable or Disable?
105105
*/
106-
void setEnabled( bool enabled ) override;
106+
virtual void setEnabled( bool enabled ) override;
107107

108108
/** Sets the widget to display in an indeterminate "mixed value" state.
109109
* @note added in QGIS 2.16

src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer* vl
4343
, mListWidget( nullptr )
4444
, mLineEdit( nullptr )
4545
, mLayer( nullptr )
46+
, mUpdating( false )
4647
{
4748
}
4849

@@ -248,3 +249,26 @@ void QgsValueRelationWidgetWrapper::showIndeterminateState()
248249
whileBlocking( mLineEdit )->clear();
249250
}
250251
}
252+
253+
void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
254+
{
255+
if ( mUpdating )
256+
return;
257+
258+
if ( mListWidget )
259+
{
260+
mUpdating = true;
261+
for ( int i = 0; i < mListWidget->count(); ++i )
262+
{
263+
QListWidgetItem *item = mListWidget->item( i );
264+
265+
if ( enabled )
266+
item->setFlags( item->flags() | Qt::ItemIsEnabled );
267+
else
268+
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
269+
}
270+
mUpdating = false;
271+
}
272+
else
273+
QgsEditorWidgetWrapper::setEnabled( enabled );
274+
}

src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
6868
static ValueRelationCache createCache( const QgsEditorWidgetConfig& config );
6969
void showIndeterminateState() override;
7070

71+
void setEnabled( bool enabled ) override;
72+
7173
protected:
7274
QWidget* createWidget( QWidget* parent ) override;
7375
void initWidget( QWidget* editor ) override;
@@ -84,7 +86,10 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
8486
ValueRelationCache mCache;
8587
QgsVectorLayer* mLayer;
8688

89+
bool mUpdating;
90+
8791
friend class QgsValueRelationWidgetFactory;
92+
friend class TestQgsValueRelationWidgetWrapper;
8893
};
8994

9095
Q_DECLARE_METATYPE( QgsValueRelationWidgetWrapper::ValueRelationCache )

tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@ ADD_QGIS_TEST(spinbox testqgsspinbox.cpp)
144144
ADD_QGIS_TEST(sqlcomposerdialog testqgssqlcomposerdialog.cpp)
145145
ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
146146
ADD_QGIS_TEST(composergui testqgscomposergui.cpp)
147-
147+
ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/***************************************************************************
2+
testqgsvaluerelationwidgetwrapper.cpp
3+
--------------------------------------
4+
Date : 21 07 2017
5+
Copyright : (C) 2017 Paul Blottiere
6+
Email : paul dot blottiere at oslandia dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QtTest/QtTest>
17+
#include <QScrollBar>
18+
19+
#include <editorwidgets/core/qgseditorwidgetregistry.h>
20+
#include <qgsapplication.h>
21+
#include <qgsproject.h>
22+
#include <qgsvectorlayer.h>
23+
#include "qgseditorwidgetwrapper.h"
24+
#include <editorwidgets/qgsvaluerelationwidgetwrapper.h>
25+
26+
class TestQgsValueRelationWidgetWrapper : public QObject
27+
{
28+
Q_OBJECT
29+
public:
30+
TestQgsValueRelationWidgetWrapper() {}
31+
32+
private slots:
33+
void initTestCase(); // will be called before the first testfunction is executed.
34+
void cleanupTestCase(); // will be called after the last testfunction was executed.
35+
void init(); // will be called before each testfunction is executed.
36+
void cleanup(); // will be called after every testfunction.
37+
38+
void testScrollBarUnlocked();
39+
};
40+
41+
void TestQgsValueRelationWidgetWrapper::initTestCase()
42+
{
43+
QgsApplication::init();
44+
QgsApplication::initQgis();
45+
QgsEditorWidgetRegistry::initEditors();
46+
}
47+
48+
void TestQgsValueRelationWidgetWrapper::cleanupTestCase()
49+
{
50+
QgsApplication::exitQgis();
51+
}
52+
53+
void TestQgsValueRelationWidgetWrapper::init()
54+
{
55+
}
56+
57+
void TestQgsValueRelationWidgetWrapper::cleanup()
58+
{
59+
}
60+
61+
void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
62+
{
63+
// create a vector layer
64+
QgsVectorLayer vl1( QString( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QString( "vl1" ), QString( "memory" ) );
65+
66+
// build a value relation widget wrapper
67+
QListWidget lw;
68+
QWidget editor;
69+
QgsValueRelationWidgetWrapper w( &vl1, 0, &editor, nullptr );
70+
w.setEnabled( true );
71+
w.initWidget( &lw );
72+
73+
// add an item virtually
74+
QListWidgetItem item;
75+
item.setText( "MyText" );
76+
w.mListWidget->addItem( &item );
77+
QCOMPARE( w.mListWidget->item( 0 )->text(), QString( "MyText" ) );
78+
79+
// when the widget wrapper is enabled, the container should be enabled
80+
// as well as items
81+
w.setEnabled( true );
82+
83+
QCOMPARE( w.widget()->isEnabled(), true );
84+
85+
bool itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
86+
QCOMPARE( itemEnabled, true );
87+
88+
// when the widget wrapper is disabled, the container should still be enabled
89+
// to keep the scrollbar available but items should be disabled to avoid
90+
// edition
91+
w.setEnabled( false );
92+
93+
itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
94+
QCOMPARE( itemEnabled, false );
95+
96+
QCOMPARE( w.widget()->isEnabled(), true );
97+
98+
// recheck after re-enabled
99+
w.setEnabled( true );
100+
101+
QCOMPARE( w.widget()->isEnabled(), true );
102+
itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
103+
QCOMPARE( itemEnabled, true );
104+
}
105+
106+
QTEST_MAIN( TestQgsValueRelationWidgetWrapper )
107+
#include "testqgsvaluerelationwidgetwrapper.moc"

0 commit comments

Comments
 (0)