Skip to content

Commit d19b4aa

Browse files
author
Hugo Mercier
authoredJul 26, 2017
Merge pull request #4905 from pblottiere/bugfix_scrolllocked
Fixes value relation widget to keep scrollbar activated, fixes #16654
2 parents f0a6376 + cdfca0a commit d19b4aa

File tree

6 files changed

+142
-2
lines changed

6 files changed

+142
-2
lines changed
 

‎python/gui/editorwidgets/core/qgseditorwidgetwrapper.sip

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class QgsEditorWidgetWrapper : QgsWidgetWrapper
104104
%End
105105

106106
virtual void setEnabled( bool enabled );
107-
108107
%Docstring
109108
Is used to enable or disable the edit functionality of the managed widget.
110109
By default this will enable or disable the whole widget

‎src/gui/editorwidgets/core/qgseditorwidgetwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
122122
*
123123
* \param enabled Enable or Disable?
124124
*/
125-
void setEnabled( bool enabled ) override;
125+
virtual void setEnabled( bool enabled ) override;
126126

127127
/** Sets the widget to display in an indeterminate "mixed value" state.
128128
* \since QGIS 2.16

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer *vl
3333
, mListWidget( nullptr )
3434
, mLineEdit( nullptr )
3535
, mLayer( nullptr )
36+
, mUpdating( false )
3637
{
3738
}
3839

@@ -198,3 +199,26 @@ void QgsValueRelationWidgetWrapper::showIndeterminateState()
198199
whileBlocking( mLineEdit )->clear();
199200
}
200201
}
202+
203+
void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
204+
{
205+
if ( mUpdating )
206+
return;
207+
208+
if ( mListWidget )
209+
{
210+
mUpdating = true;
211+
for ( int i = 0; i < mListWidget->count(); ++i )
212+
{
213+
QListWidgetItem *item = mListWidget->item( i );
214+
215+
if ( enabled )
216+
item->setFlags( item->flags() | Qt::ItemIsEnabled );
217+
else
218+
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
219+
}
220+
mUpdating = false;
221+
}
222+
else
223+
QgsEditorWidgetWrapper::setEnabled( enabled );
224+
}

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
5959

6060
void showIndeterminateState() override;
6161

62+
void setEnabled( bool enabled ) override;
63+
6264
protected:
6365
QWidget *createWidget( QWidget *parent ) override;
6466
void initWidget( QWidget *editor ) override;
@@ -75,7 +77,10 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
7577
QgsValueRelationFieldFormatter::ValueRelationCache mCache;
7678
QgsVectorLayer *mLayer = nullptr;
7779

80+
bool mUpdating;
81+
7882
friend class QgsValueRelationWidgetFactory;
83+
friend class TestQgsValueRelationWidgetWrapper;
7984
};
8085

8186
#endif // QGSVALUERELATIONWIDGETWRAPPER_H

‎tests/src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
2222
${CMAKE_SOURCE_DIR}/src/core/metadata
2323
${CMAKE_SOURCE_DIR}/src/core/raster
2424
${CMAKE_SOURCE_DIR}/src/core/symbology-ng
25+
${CMAKE_SOURCE_DIR}/src/core/fieldformatter
2526
${CMAKE_SOURCE_DIR}/src/test
2627
${CMAKE_SOURCE_DIR}/src/native
2728

@@ -134,3 +135,4 @@ ADD_QGIS_TEST(listwidgettest testqgslistwidget.cpp)
134135
ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
135136
ADD_QGIS_TEST(composergui testqgscomposergui.cpp)
136137
ADD_QGIS_TEST(layoutview testqgslayoutview.cpp)
138+
ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
17+
#include "qgstest.h"
18+
#include <QScrollBar>
19+
20+
#include <editorwidgets/core/qgseditorwidgetregistry.h>
21+
#include <qgsapplication.h>
22+
#include <qgsproject.h>
23+
#include <qgsvectorlayer.h>
24+
#include "qgseditorwidgetwrapper.h"
25+
#include <editorwidgets/qgsvaluerelationwidgetwrapper.h>
26+
#include "qgsgui.h"
27+
28+
class TestQgsValueRelationWidgetWrapper : public QObject
29+
{
30+
Q_OBJECT
31+
public:
32+
TestQgsValueRelationWidgetWrapper() {}
33+
34+
private slots:
35+
void initTestCase(); // will be called before the first testfunction is executed.
36+
void cleanupTestCase(); // will be called after the last testfunction was executed.
37+
void init(); // will be called before each testfunction is executed.
38+
void cleanup(); // will be called after every testfunction.
39+
40+
void testScrollBarUnlocked();
41+
};
42+
43+
void TestQgsValueRelationWidgetWrapper::initTestCase()
44+
{
45+
QgsApplication::init();
46+
QgsApplication::initQgis();
47+
QgsGui::editorWidgetRegistry()->initEditors();
48+
}
49+
50+
void TestQgsValueRelationWidgetWrapper::cleanupTestCase()
51+
{
52+
QgsApplication::exitQgis();
53+
}
54+
55+
void TestQgsValueRelationWidgetWrapper::init()
56+
{
57+
}
58+
59+
void TestQgsValueRelationWidgetWrapper::cleanup()
60+
{
61+
}
62+
63+
void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
64+
{
65+
// create a vector layer
66+
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
67+
QgsProject::instance()->addMapLayer( &vl1, false, false );
68+
69+
// build a value relation widget wrapper
70+
QListWidget lw;
71+
QWidget editor;
72+
QgsValueRelationWidgetWrapper w( &vl1, 0, &editor, nullptr );
73+
w.setEnabled( true );
74+
w.initWidget( &lw );
75+
76+
// add an item virtually
77+
QListWidgetItem item;
78+
item.setText( "MyText" );
79+
w.mListWidget->addItem( &item );
80+
QCOMPARE( w.mListWidget->item( 0 )->text(), QString( "MyText" ) );
81+
82+
// when the widget wrapper is enabled, the container should be enabled
83+
// as well as items
84+
w.setEnabled( true );
85+
86+
QCOMPARE( w.widget()->isEnabled(), true );
87+
88+
bool itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
89+
QCOMPARE( itemEnabled, true );
90+
91+
// when the widget wrapper is disabled, the container should still be enabled
92+
// to keep the scrollbar available but items should be disabled to avoid
93+
// edition
94+
w.setEnabled( false );
95+
96+
itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
97+
QCOMPARE( itemEnabled, false );
98+
99+
QCOMPARE( w.widget()->isEnabled(), true );
100+
101+
// recheck after re-enabled
102+
w.setEnabled( true );
103+
104+
QCOMPARE( w.widget()->isEnabled(), true );
105+
itemEnabled = w.mListWidget->item( 0 )->flags() & Qt::ItemIsEnabled;
106+
QCOMPARE( itemEnabled, true );
107+
}
108+
109+
QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )
110+
#include "testqgsvaluerelationwidgetwrapper.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.