QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
17#include "qgsattributeform.h"
19
20#include <QHBoxLayout>
21#include <QStackedWidget>
22
23#include "moc_qgsattributeformwidget.cpp"
24
26 : QWidget( form )
27 , mForm( form )
28 , mWidget( widget )
29{
30 mEditPage = new QWidget();
31 QHBoxLayout *l = new QHBoxLayout();
32 l->setContentsMargins( 0, 0, 0, 0 );
33 mEditPage->setLayout( l );
34
35 l = new QHBoxLayout();
36 l->setContentsMargins( 0, 0, 0, 0 );
37 mSearchFrame = new QWidget();
38 mSearchFrame->setLayout( l );
39
40 mSearchPage = new QWidget();
41 l = new QHBoxLayout();
42 l->setContentsMargins( 0, 0, 0, 0 );
43 mSearchPage->setLayout( l );
44 l->addWidget( mSearchFrame, 1 );
45 mSearchWidgetToolButton = new QgsSearchWidgetToolButton();
46 mSearchWidgetToolButton->setObjectName( QStringLiteral( "SearchWidgetToolButton" ) );
47 connect( mSearchWidgetToolButton, &QgsSearchWidgetToolButton::activeFlagsChanged, this, &QgsAttributeFormWidget::searchWidgetFlagsChanged );
48 l->addWidget( mSearchWidgetToolButton, 0 );
49
50 mStack = new QStackedWidget();
51 // IMPORTANT!
52 // We do NOT add pages to mStack here, as QStackedWidgets will always inherit the minimum size
53 // of their largest page. This can cause attribute form sizes to needlessly blow out in certain modes,
54 // eg when the form is in the "Add feature" mode we do NOT need the extra horizontal space requirements
55 // that the search widgets enfore. Doing so forces all editor widgets in all modes to have a very wide
56 // minimum width, preventing attribute forms from being shrunk to reasonable sizes without horizontal
57 // scroll bars appearing.
58 // Instead, the pages are added and removed from the stack whenever the visible page is changed (in updateWidgets()).
59 // This ensures that the stack, and this widget too, only inherit the size requirements of the actual visible
60 // page.
61
62 l = new QHBoxLayout();
63 l->setContentsMargins( 0, 0, 0, 0 );
64 setLayout( l );
65 l->addWidget( mStack );
66
67 if ( !mWidget || !mForm )
68 return;
69
70 mEditPage->layout()->addWidget( mWidget->widget() );
71
72 // Respect size policy of embedded widget
73 setSizePolicy( mWidget->widget()->sizePolicy() );
74
75 setVisiblePageForMode( mMode );
76}
77
79{
80 // depending on the current page in the stacked widget, these pages NOT
81 // be parented to the stacked widget or this widget. Clean them up manually to avoid leaks.
82 delete mEditPage;
83 mEditPage = nullptr;
84 delete mSearchPage;
85 mSearchPage = nullptr;
86}
87
89{
90 mMode = mode;
91 updateWidgets();
92}
93
95{
96 return mForm;
97}
98
100{
101 return mSearchFrame;
102}
103
105{
106 mSearchWidgets.clear();
107 mSearchWidgets << wrapper;
108 mSearchFrame->layout()->addWidget( wrapper->widget() );
109 mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
110 mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
111 mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
112 connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
113 connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
114}
115
117{
118 mSearchWidgets << wrapper;
119
120 mSearchFrame->layout()->addWidget( wrapper->widget() );
121 wrapper->widget()->hide();
122}
123
125{
126 return mSearchWidgets;
127}
128
130{
131 if ( mSearchWidgets.isEmpty() )
132 return QString();
133
134 if ( !mSearchWidgetToolButton->isActive() )
135 return QString();
136
137 if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
138 {
139 // special case: Between search
140 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
141 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
142 return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
143 }
144 else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
145 {
146 // special case: Is Not Between search
147 const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
148 const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
149 return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
150 }
151
152 return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
153}
154
156{
157 mSearchWidgetToolButton->setInactive();
158 const auto constMSearchWidgets = mSearchWidgets;
159 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
160 {
161 widget->clearWidget();
162 }
163}
164
166{
167 return mWidget->layer();
168}
169
170void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
171{
172 const auto constMSearchWidgets = mSearchWidgets;
173 for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
174 {
175 widget->setEnabled( !( flags & QgsSearchWidgetWrapper::IsNull ) && !( 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 || flags & QgsSearchWidgetWrapper::IsNotBetween );
185 }
186}
187
188void QgsAttributeFormWidget::updateWidgets()
189{
190 setVisiblePageForMode( mMode );
191}
192
194{
195 QWidget *currentVisibleWidget = mStack->currentWidget();
196
197 QWidget *newVisibleWidget = nullptr;
198 switch ( mode )
199 {
200 case DefaultMode:
201 case MultiEditMode:
202 newVisibleWidget = mEditPage;
203 break;
204
205 case SearchMode:
207 {
208 newVisibleWidget = mSearchPage;
209 break;
210 }
211 }
212
213 if ( newVisibleWidget != currentVisibleWidget )
214 {
215 if ( currentVisibleWidget )
216 {
217 // as per Qt docs, this does NOT delete the page, it just removes it from the stack
218 mStack->removeWidget( currentVisibleWidget );
219 }
220
221 mStack->addWidget( newVisibleWidget );
222 mStack->setCurrentWidget( newVisibleWidget );
223 }
224}
225
227{
228 return mSearchWidgetToolButton->isVisible();
229}
230
235
237{
238 return mSearchPage;
239}
240
241QStackedWidget *QgsAttributeFormWidget::stack() const
242{
243 return mStack;
244}
245
247{
248 return mEditPage;
249}
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...
The attribute form widget for vector layer features.
A tool button widget which is displayed next to search widgets in forms, and allows for controlling h...
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 dataset.
Manages an editor widget.
QWidget * widget()
Access the widget managed by this wrapper.
#define SIP_SKIP
Definition qgis_sip.h:134