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