QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 <QHBoxLayout>
17#include <QStackedWidget>
18
19#include "qgsattributeform.h"
21
23 : QWidget( form )
24 , mForm( form )
25 , mWidget( widget )
26{
27 mEditPage = new QWidget();
28 QHBoxLayout *l = new QHBoxLayout();
29 l->setContentsMargins( 0, 0, 0, 0 );
30 mEditPage->setLayout( l );
31
32 l = new QHBoxLayout();
33 l->setContentsMargins( 0, 0, 0, 0 );
34 mSearchFrame = new QWidget();
35 mSearchFrame->setLayout( l );
36
37 mSearchPage = new QWidget();
38 l = new QHBoxLayout();
39 l->setContentsMargins( 0, 0, 0, 0 );
40 mSearchPage->setLayout( l );
41 l->addWidget( mSearchFrame, 1 );
42 mSearchWidgetToolButton = new QgsSearchWidgetToolButton();
43 mSearchWidgetToolButton->setObjectName( QStringLiteral( "SearchWidgetToolButton" ) );
44 connect( mSearchWidgetToolButton, &QgsSearchWidgetToolButton::activeFlagsChanged,
45 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 )
174 && !( flags & QgsSearchWidgetWrapper::IsNotNull ) );
175 if ( !mSearchWidgetToolButton->isActive() )
176 {
177 widget->clearWidget();
178 }
179 }
180
181 if ( mSearchWidgets.count() >= 2 )
182 {
183 mSearchWidgets.at( 1 )->widget()->setVisible( flags & QgsSearchWidgetWrapper::Between ||
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
231void QgsAttributeFormWidget::setSearchWidgetToolButtonVisible( bool searchWidgetToolButtonVisible )
232{
233 mSearchWidgetToolButton->setVisible( searchWidgetToolButtonVisible );
234}
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...
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.
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