QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsattributeformwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeformwidget.cpp
3 ---------------------
4 begin : November 2017
5 copyright : (C) 2017 by Matthias Kuhn
6 email : matthias at opengis dot ch
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 ***************************************************************************/
16#include "moc_qgsattributeformwidget.cpp"
17#include <QHBoxLayout>
18#include <QStackedWidget>
19
20#include "qgsattributeform.h"
22
24 : QWidget( form )
25 , mForm( form )
26 , mWidget( widget )
27{
28 mEditPage = new QWidget();
29 QHBoxLayout *l = new QHBoxLayout();
30 l->setContentsMargins( 0, 0, 0, 0 );
31 mEditPage->setLayout( l );
32
33 l = new QHBoxLayout();
34 l->setContentsMargins( 0, 0, 0, 0 );
35 mSearchFrame = new QWidget();
36 mSearchFrame->setLayout( l );
37
38 mSearchPage = new QWidget();
39 l = new QHBoxLayout();
40 l->setContentsMargins( 0, 0, 0, 0 );
41 mSearchPage->setLayout( l );
42 l->addWidget( mSearchFrame, 1 );
43 mSearchWidgetToolButton = new QgsSearchWidgetToolButton();
44 mSearchWidgetToolButton->setObjectName( QStringLiteral( "SearchWidgetToolButton" ) );
45 connect( mSearchWidgetToolButton, &QgsSearchWidgetToolButton::activeFlagsChanged, this, &QgsAttributeFormWidget::searchWidgetFlagsChanged );
46 l->addWidget( mSearchWidgetToolButton, 0 );
47
48 mStack = new QStackedWidget();
49 // IMPORTANT!
50 // We do NOT add pages to mStack here, as QStackedWidgets will always inherit the minimum size
51 // of their largest page. This can cause attribute form sizes to needlessly blow out in certain modes,
52 // eg when the form is in the "Add feature" mode we do NOT need the extra horizontal space requirements
53 // that the search widgets enfore. Doing so forces all editor widgets in all modes to have a very wide
54 // minimum width, preventing attribute forms from being shrunk to reasonable sizes without horizontal
55 // scroll bars appearing.
56 // Instead, the pages are added and removed from the stack whenever the visible page is changed (in updateWidgets()).
57 // This ensures that the stack, and this widget too, only inherit the size requirements of the actual visible
58 // page.
59
60 l = new QHBoxLayout();
61 l->setContentsMargins( 0, 0, 0, 0 );
62 setLayout( l );
63 l->addWidget( mStack );
64
65 if ( !mWidget || !mForm )
66 return;
67
68 mEditPage->layout()->addWidget( mWidget->widget() );
69
70 // Respect size policy of embedded widget
71 setSizePolicy( mWidget->widget()->sizePolicy() );
72
73 setVisiblePageForMode( mMode );
74}
75
77{
78 // depending on the current page in the stacked widget, these pages NOT
79 // be parented to the stacked widget or this widget. Clean them up manually to avoid leaks.
80 delete mEditPage;
81 mEditPage = nullptr;
82 delete mSearchPage;
83 mSearchPage = nullptr;
84}
85
87{
88 mMode = mode;
89 updateWidgets();
90}
91
93{
94 return mForm;
95}
96
98{
99 return mSearchFrame;
100}
101
103{
104 mSearchWidgets.clear();
105 mSearchWidgets << wrapper;
106 mSearchFrame->layout()->addWidget( wrapper->widget() );
107 mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
108 mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
109 mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
110 connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
111 connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
112}
113
115{
116 mSearchWidgets << wrapper;
117
118 mSearchFrame->layout()->addWidget( wrapper->widget() );
119 wrapper->widget()->hide();
120}
121
123{
124 return mSearchWidgets;
125}
126
128{
129 if ( mSearchWidgets.isEmpty() )
130 return QString();
131
132 if ( !mSearchWidgetToolButton->isActive() )
133 return QString();
134
135 if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
136 {
137 // special case: Between search
138 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
139 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
140 return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
141 }
142 else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
143 {
144 // special case: Is Not Between search
145 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
146 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
147 return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
148 }
149
150 return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
151}
152
154{
155 mSearchWidgetToolButton->setInactive();
156 const auto constMSearchWidgets = mSearchWidgets;
157 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
158 {
159 widget->clearWidget();
160 }
161}
162
164{
165 return mWidget->layer();
166}
167
168void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
169{
170 const auto constMSearchWidgets = mSearchWidgets;
171 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
172 {
173 widget->setEnabled( !( flags & QgsSearchWidgetWrapper::IsNull ) && !( flags & QgsSearchWidgetWrapper::IsNotNull ) );
174 if ( !mSearchWidgetToolButton->isActive() )
175 {
176 widget->clearWidget();
177 }
178 }
179
180 if ( mSearchWidgets.count() >= 2 )
181 {
182 mSearchWidgets.at( 1 )->widget()->setVisible( flags & QgsSearchWidgetWrapper::Between || flags & QgsSearchWidgetWrapper::IsNotBetween );
183 }
184}
185
186void QgsAttributeFormWidget::updateWidgets()
187{
188 setVisiblePageForMode( mMode );
189}
190
192{
193 QWidget *currentVisibleWidget = mStack->currentWidget();
194
195 QWidget *newVisibleWidget = nullptr;
196 switch ( mode )
197 {
198 case DefaultMode:
199 case MultiEditMode:
200 newVisibleWidget = mEditPage;
201 break;
202
203 case SearchMode:
205 {
206 newVisibleWidget = mSearchPage;
207 break;
208 }
209 }
210
211 if ( newVisibleWidget != currentVisibleWidget )
212 {
213 if ( currentVisibleWidget )
214 {
215 // as per Qt docs, this does NOT delete the page, it just removes it from the stack
216 mStack->removeWidget( currentVisibleWidget );
217 }
218
219 mStack->addWidget( newVisibleWidget );
220 mStack->setCurrentWidget( newVisibleWidget );
221 }
222}
223
225{
226 return mSearchWidgetToolButton->isVisible();
227}
228
229void QgsAttributeFormWidget::setSearchWidgetToolButtonVisible( bool searchWidgetToolButtonVisible )
230{
231 mSearchWidgetToolButton->setVisible( searchWidgetToolButtonVisible );
232}
233
235{
236 return mSearchPage;
237}
238
239QStackedWidget *QgsAttributeFormWidget::stack() const
240{
241 return mStack;
242}
243
245{
246 return mEditPage;
247}
void setSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Sets the search widget wrapper for the widget used when the form is in search mode.
bool searchWidgetToolButtonVisible() const
The visibility of the search widget tool button, that allows (de)activating this search widgte or def...
QList< QgsSearchWidgetWrapper * > searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
QWidget * searchPage() const
Returns a pointer to the search page widget.
QgsAttributeForm * form() const
The form on which this widget is shown.
void setVisiblePageForMode(QgsAttributeFormWidget::Mode mode)
Sets the visible page in the widget to the page matching the specified mode.
void addAdditionalSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Adds an additional search widget wrapper.
void resetSearch()
Resets the search/filter value of the widget.
void setMode(Mode mode)
Sets the current mode for the widget.
QWidget * searchWidgetFrame()
Returns the widget which should be used as a parent during construction of the search widget wrapper.
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
QStackedWidget * stack() const
Returns a pointer to the stacked widget managing edit and search page.
Mode mode() const
Returns the current mode for the widget.
virtual QString currentFilterExpression() const
Creates an expression matching the current search filter value and search properties represented in t...
@ SearchMode
Layer search/filter mode.
@ MultiEditMode
Multi edit mode, both the editor widget and a QgsMultiEditToolButton is shown.
@ DefaultMode
Default mode, only the editor widget is shown.
@ AggregateSearchMode
Embedded in a search form, show additional aggregate function toolbutton.
QgsAttributeFormWidget(QgsWidgetWrapper *widget, QgsAttributeForm *form)
A new form widget for the wrapper widget on form.
void setSearchWidgetToolButtonVisible(bool searchWidgetToolButtonVisible)
The visibility of the search widget tool button, that allows (de)activating this search widgte or def...
A tool button widget which is displayed next to search widgets in forms, and allows for controlling h...
QgsSearchWidgetWrapper::FilterFlags activeFlags() const
Returns the active filter flags shown in the widget.
void setInactive()
Sets the search widget as inactive, ie do not search the corresponding field.
void activeFlagsChanged(QgsSearchWidgetWrapper::FilterFlags flags)
Emitted when the active flags selected in the widget is changed.
bool isActive() const
Returns true if the widget is set to be included in the search.
void setActive()
Sets the search widget as active by selecting the first available search type.
Shows a search widget on a filter form.
@ IsNotBetween
Supports searching for values outside of a set range.
@ LessThan
Supports less than.
@ IsNull
Supports searching for null values.
@ GreaterThan
Supports greater than.
@ IsNotNull
Supports searching for non-null values.
@ Between
Supports searches between two values.
void valueChanged()
Emitted when a user changes the value of the search widget.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state.
QFlags< FilterFlag > FilterFlags
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
#define SIP_SKIP
Definition qgis_sip.h:126