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