QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
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:190
qgssearchwidgettoolbutton.h
qgsattributeform.h
QgsWidgetWrapper
Manages an editor widget Widget and wrapper share the same parent.
Definition: qgswidgetwrapper.h:52
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:195
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:38
QgsSearchWidgetWrapper
Shows a search widget on a filter form.
Definition: qgssearchwidgetwrapper.h:85
QgsAttributeFormWidget::layer
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
Definition: qgsattributeformwidget.cpp:146
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:97
QgsWidgetWrapper::widget
QWidget * widget()
Access the widget managed by this wrapper.
Definition: qgswidgetwrapper.cpp:47
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:45
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:92
qgsattributeformwidget.h
QgsAttributeFormWidget::form
QgsAttributeForm * form() const
The form on which this widget is shown.
Definition: qgsattributeformwidget.cpp:75
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:80
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:136
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:391
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:205
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:110
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:69
QgsAttributeForm
Definition: qgsattributeform.h:44
QgsAttributeFormWidget::searchWidgetWrappers
QList< QgsSearchWidgetWrapper * > searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
Definition: qgsattributeformwidget.cpp:105
QgsAttributeFormWidget::searchPage
QWidget * searchPage() const
Returns a pointer to the search page widget.
Definition: qgsattributeformwidget.cpp:200
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:85
QgsAttributeFormWidget::editPage
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
Definition: qgsattributeformwidget.cpp:210