QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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,
46 this, &QgsAttributeFormWidget::searchWidgetFlagsChanged );
47 l->addWidget( mSearchWidgetToolButton, 0 );
48
49 mStack = new QStackedWidget();
50 // IMPORTANT!
51 // We do NOT add pages to mStack here, as QStackedWidgets will always inherit the minimum size
52 // of their largest page. This can cause attribute form sizes to needlessly blow out in certain modes,
53 // eg when the form is in the "Add feature" mode we do NOT need the extra horizontal space requirements
54 // that the search widgets enfore. Doing so forces all editor widgets in all modes to have a very wide
55 // minimum width, preventing attribute forms from being shrunk to reasonable sizes without horizontal
56 // scroll bars appearing.
57 // Instead, the pages are added and removed from the stack whenever the visible page is changed (in updateWidgets()).
58 // This ensures that the stack, and this widget too, only inherit the size requirements of the actual visible
59 // page.
60
61 l = new QHBoxLayout();
62 l->setContentsMargins( 0, 0, 0, 0 );
63 setLayout( l );
64 l->addWidget( mStack );
65
66 if ( !mWidget || !mForm )
67 return;
68
69 mEditPage->layout()->addWidget( mWidget->widget() );
70
71 // Respect size policy of embedded widget
72 setSizePolicy( mWidget->widget()->sizePolicy() );
73
74 setVisiblePageForMode( mMode );
75}
76
78{
79 // depending on the current page in the stacked widget, these pages NOT
80 // be parented to the stacked widget or this widget. Clean them up manually to avoid leaks.
81 delete mEditPage;
82 mEditPage = nullptr;
83 delete mSearchPage;
84 mSearchPage = nullptr;
85}
86
88{
89 mMode = mode;
90 updateWidgets();
91}
92
94{
95 return mForm;
96}
97
99{
100 return mSearchFrame;
101}
102
104{
105 mSearchWidgets.clear();
106 mSearchWidgets << wrapper;
107 mSearchFrame->layout()->addWidget( wrapper->widget() );
108 mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
109 mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
110 mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
111 connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
112 connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
113}
114
116{
117 mSearchWidgets << wrapper;
118
119 mSearchFrame->layout()->addWidget( wrapper->widget() );
120 wrapper->widget()->hide();
121}
122
124{
125 return mSearchWidgets;
126}
127
129{
130 if ( mSearchWidgets.isEmpty() )
131 return QString();
132
133 if ( !mSearchWidgetToolButton->isActive() )
134 return QString();
135
136 if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
137 {
138 // special case: Between search
139 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
140 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
141 return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
142 }
143 else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
144 {
145 // special case: Is Not Between search
146 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
147 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
148 return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
149 }
150
151 return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
152}
153
155{
156 mSearchWidgetToolButton->setInactive();
157 const auto constMSearchWidgets = mSearchWidgets;
158 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
159 {
160 widget->clearWidget();
161 }
162}
163
165{
166 return mWidget->layer();
167}
168
169void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
170{
171 const auto constMSearchWidgets = mSearchWidgets;
172 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
173 {
174 widget->setEnabled( !( flags & QgsSearchWidgetWrapper::IsNull )
175 && !( flags & QgsSearchWidgetWrapper::IsNotNull ) );
176 if ( !mSearchWidgetToolButton->isActive() )
177 {
178 widget->clearWidget();
179 }
180 }
181
182 if ( mSearchWidgets.count() >= 2 )
183 {
184 mSearchWidgets.at( 1 )->widget()->setVisible( flags & QgsSearchWidgetWrapper::Between ||
186 }
187}
188
189void QgsAttributeFormWidget::updateWidgets()
190{
191 setVisiblePageForMode( mMode );
192}
193
195{
196 QWidget *currentVisibleWidget = mStack->currentWidget();
197
198 QWidget *newVisibleWidget = nullptr;
199 switch ( mode )
200 {
201 case DefaultMode:
202 case MultiEditMode:
203 newVisibleWidget = mEditPage;
204 break;
205
206 case SearchMode:
208 {
209 newVisibleWidget = mSearchPage;
210 break;
211 }
212 }
213
214 if ( newVisibleWidget != currentVisibleWidget )
215 {
216 if ( currentVisibleWidget )
217 {
218 // as per Qt docs, this does NOT delete the page, it just removes it from the stack
219 mStack->removeWidget( currentVisibleWidget );
220 }
221
222 mStack->addWidget( newVisibleWidget );
223 mStack->setCurrentWidget( newVisibleWidget );
224 }
225}
226
228{
229 return mSearchWidgetToolButton->isVisible();
230}
231
232void QgsAttributeFormWidget::setSearchWidgetToolButtonVisible( bool searchWidgetToolButtonVisible )
233{
234 mSearchWidgetToolButton->setVisible( searchWidgetToolButtonVisible );
235}
236
238{
239 return mSearchPage;
240}
241
242QStackedWidget *QgsAttributeFormWidget::stack() const
243{
244 return mStack;
245}
246
248{
249 return mEditPage;
250}
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