QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  ***************************************************************************/
15 #include "qgsattributeformwidget.h"
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 
49  mStack = new QStackedWidget;
50  mStack->addWidget( mEditPage );
51  mStack->addWidget( mSearchPage );
52 
53  l = new QHBoxLayout();
54  l->setContentsMargins( 0, 0, 0, 0 );
55  setLayout( l );
56  l->addWidget( mStack );
57 
58  if ( !mWidget || !mForm )
59  return;
60 
61  mEditPage->layout()->addWidget( mWidget->widget() );
62 
63  // Respect size policy of embedded widget
64  setSizePolicy( mWidget->widget()->sizePolicy() );
65 
66  updateWidgets();
67 }
68 
70 {
71  mMode = mode;
72  updateWidgets();
73 }
74 
76 {
77  return mForm;
78 }
79 
81 {
82  return mSearchFrame;
83 }
84 
86 {
87  mSearchWidgets.clear();
88  mSearchWidgets << wrapper;
89  mSearchFrame->layout()->addWidget( wrapper->widget() );
90  mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
91  mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
92  mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
93  connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
94  connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
95 }
96 
98 {
99  mSearchWidgets << wrapper;
100 
101  mSearchFrame->layout()->addWidget( wrapper->widget() );
102  wrapper->widget()->hide();
103 }
104 
106 {
107  return mSearchWidgets;
108 }
109 
111 {
112  if ( mSearchWidgets.isEmpty() )
113  return QString();
114 
115  if ( !mSearchWidgetToolButton->isActive() )
116  return QString();
117 
118  if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
119  {
120  // special case: Between search
121  const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
122  const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
123  return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
124  }
125  else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
126  {
127  // special case: Is Not Between search
128  const QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
129  const QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
130  return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
131  }
132 
133  return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
134 }
135 
137 {
138  mSearchWidgetToolButton->setInactive();
139  const auto constMSearchWidgets = mSearchWidgets;
140  for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
141  {
142  widget->clearWidget();
143  }
144 }
145 
147 {
148  return mWidget->layer();
149 }
150 
151 void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
152 {
153  const auto constMSearchWidgets = mSearchWidgets;
154  for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
155  {
156  widget->setEnabled( !( flags & QgsSearchWidgetWrapper::IsNull )
157  && !( flags & QgsSearchWidgetWrapper::IsNotNull ) );
158  if ( !mSearchWidgetToolButton->isActive() )
159  {
160  widget->clearWidget();
161  }
162  }
163 
164  if ( mSearchWidgets.count() >= 2 )
165  {
166  mSearchWidgets.at( 1 )->widget()->setVisible( flags & QgsSearchWidgetWrapper::Between ||
168  }
169 }
170 
171 void QgsAttributeFormWidget::updateWidgets()
172 {
173  switch ( mMode )
174  {
175  case DefaultMode:
176  case MultiEditMode:
177  mStack->setCurrentWidget( mEditPage );
178  break;
179 
180  case SearchMode:
181  case AggregateSearchMode:
182  {
183  mStack->setCurrentWidget( mSearchPage );
184  break;
185  }
186  }
187 
188 }
189 
191 {
192  return mSearchWidgetToolButton->isVisible();
193 }
194 
195 void QgsAttributeFormWidget::setSearchWidgetToolButtonVisible( bool searchWidgetToolButtonVisible )
196 {
197  mSearchWidgetToolButton->setVisible( searchWidgetToolButtonVisible );
198 }
199 
201 {
202  return mSearchPage;
203 }
204 
205 QStackedWidget *QgsAttributeFormWidget::stack() const
206 {
207  return mStack;
208 }
209 
211 {
212  return mEditPage;
213 }
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 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