QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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->setMargin( 0 );
30  l->setContentsMargins( 0, 0, 0, 0 );
31  mEditPage->setLayout( l );
32 
33  l = new QHBoxLayout();
34  l->setMargin( 0 );
35  l->setContentsMargins( 0, 0, 0, 0 );
36  mSearchFrame = new QWidget();
37  mSearchFrame->setLayout( l );
38 
39  mSearchPage = new QWidget();
40  l = new QHBoxLayout();
41  l->setMargin( 0 );
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,
48  this, &QgsAttributeFormWidget::searchWidgetFlagsChanged );
49  l->addWidget( mSearchWidgetToolButton, 0 );
50 
51 
52  mStack = new QStackedWidget;
53  mStack->addWidget( mEditPage );
54  mStack->addWidget( mSearchPage );
55 
56  l = new QHBoxLayout();
57  l->setMargin( 0 );
58  l->setContentsMargins( 0, 0, 0, 0 );
59  setLayout( l );
60  l->addWidget( mStack );
61 
62  if ( !mWidget || !mForm )
63  return;
64 
65  mEditPage->layout()->addWidget( mWidget->widget() );
66 
67  updateWidgets();
68 }
69 
71 {
72  mMode = mode;
73  updateWidgets();
74 }
75 
77 {
78  return mForm;
79 }
80 
82 {
83  return mSearchFrame;
84 }
85 
87 {
88  mSearchWidgets.clear();
89  mSearchWidgets << wrapper;
90  mSearchFrame->layout()->addWidget( wrapper->widget() );
91  mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
92  mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
93  mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
94  connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
95  connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
96 }
97 
99 {
100  mSearchWidgets << wrapper;
101 
102  mSearchFrame->layout()->addWidget( wrapper->widget() );
103  wrapper->widget()->hide();
104 }
105 
107 {
108  return mSearchWidgets;
109 }
110 
112 {
113  if ( mSearchWidgets.isEmpty() )
114  return QString();
115 
116  if ( !mSearchWidgetToolButton->isActive() )
117  return QString();
118 
119  if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
120  {
121  // special case: Between search
122  QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
123  QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
124  return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
125  }
126  else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
127  {
128  // special case: Is Not Between search
129  QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
130  QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
131  return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
132  }
133 
134  return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
135 }
136 
138 {
139  mSearchWidgetToolButton->setInactive();
140  const auto constMSearchWidgets = mSearchWidgets;
141  for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
142  {
143  widget->clearWidget();
144  }
145 }
146 
148 {
149  return mWidget->layer();
150 }
151 
152 void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
153 {
154  const auto constMSearchWidgets = mSearchWidgets;
155  for ( QgsSearchWidgetWrapper *widget : constMSearchWidgets )
156  {
157  widget->setEnabled( !( flags & QgsSearchWidgetWrapper::IsNull )
158  && !( flags & QgsSearchWidgetWrapper::IsNotNull ) );
159  if ( !mSearchWidgetToolButton->isActive() )
160  {
161  widget->clearWidget();
162  }
163  }
164 
165  if ( mSearchWidgets.count() >= 2 )
166  {
167  mSearchWidgets.at( 1 )->widget()->setVisible( flags & QgsSearchWidgetWrapper::Between ||
169  }
170 }
171 
172 void QgsAttributeFormWidget::updateWidgets()
173 {
174  switch ( mMode )
175  {
176  case DefaultMode:
177  case MultiEditMode:
178  mStack->setCurrentWidget( mEditPage );
179  break;
180 
181  case SearchMode:
182  case AggregateSearchMode:
183  {
184  mStack->setCurrentWidget( mSearchPage );
185  break;
186  }
187  }
188 
189 }
190 
192 {
193  return mSearchWidgetToolButton->isVisible();
194 }
195 
197 {
198  mSearchWidgetToolButton->setVisible( searchWidgetToolButtonVisible );
199 }
200 
202 {
203  return mSearchPage;
204 }
205 
206 QStackedWidget *QgsAttributeFormWidget::stack() const
207 {
208  return mStack;
209 }
210 
212 {
213  return mEditPage;
214 }
Shows a search widget on a filter form.
void setActive()
Sets the search widget as active by selecting the first available search type.
void setDefaultFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the default filter flags to show in the widget.
void resetSearch()
Resets the search/filter value of the widget.
void setActiveFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the current active filter flags for the widget.
Supports searching for non-null values.
Supports searching for values outside of a set range.
A tool button widget which is displayed next to search widgets in forms, and allows for controlling h...
virtual QString currentFilterExpression() const
Creates an expression matching the current search filter value and search properties represented in t...
void setMode(Mode mode)
Sets the current mode for the widget.
QStackedWidget * stack() const
Returns a pointer to the stacked widget managing edit and search page.
Default mode, only the editor widget is shown.
Supports searching for null values.
QList< QgsSearchWidgetWrapper *> searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
QWidget * searchPage() const
Returns a pointer to the search page widget.
void setSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Sets the search widget wrapper for the widget used when the form is in search mode.
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsAttributeFormWidget(QgsWidgetWrapper *widget, QgsAttributeForm *form)
A new form widget for the wrapper widget on form.
void valueChanged()
Emitted when a user changes the value of the search widget.
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
Mode mode() const
Returns the current mode for the widget.
bool searchWidgetToolButtonVisible() const
The visibility of the search widget tool button, that allows (de)activating this search widgte or def...
void setInactive()
Sets the search widget as inactive, ie do not search the corresponding field.
void setSearchWidgetToolButtonVisible(bool searchWidgetToolButtonVisible)
The visibility of the search widget tool button, that allows (de)activating this search widgte or def...
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
void setAvailableFlags(QgsSearchWidgetWrapper::FilterFlags flags)
Sets the available filter flags to show in the widget.
Multi edit mode, both the editor widget and a QgsMultiEditToolButton is shown.
bool isActive() const
Returns true if the widget is set to be included in the search.
void addAdditionalSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Adds an additional search widget wrapper.
void valueCleared()
Emitted when a user changes the value of the search widget back to an empty, default state...
QgsSearchWidgetWrapper::FilterFlags activeFlags() const
Returns the active filter flags shown in the widget.
QWidget * widget()
Access the widget managed by this wrapper.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.
Embedded in a search form, show additional aggregate function toolbutton.
Represents a vector layer which manages a vector based data sets.
Manages an editor widget Widget and wrapper share the same parent.
QgsAttributeForm * form() const
The form on which this widget is shown.
Supports searches between two values.
void activeFlagsChanged(QgsSearchWidgetWrapper::FilterFlags flags)
Emitted when the active flags selected in the widget is changed.
QWidget * searchWidgetFrame()
Returns the widget which should be used as a parent during construction of the search widget wrapper...