QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
196 void QgsAttributeFormWidget::setSearchWidgetToolButtonVisible( bool searchWidgetToolButtonVisible )
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 }
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:191
qgssearchwidgettoolbutton.h
qgsattributeform.h
QgsWidgetWrapper
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:196
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
Definition: qgssearchwidgettoolbutton.h:38
QgsSearchWidgetWrapper
Definition: qgssearchwidgetwrapper.h:85
QgsAttributeFormWidget::layer
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
Definition: qgsattributeformwidget.cpp:147
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:98
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: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:91
qgsattributeformwidget.h
QgsAttributeFormWidget::form
QgsAttributeForm * form() const
The form on which this widget is shown.
Definition: qgsattributeformwidget.cpp:76
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:81
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:137
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
Definition: qgsvectorlayer.h:385
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:206
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:111
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:70
QgsAttributeForm
Definition: qgsattributeform.h:44
QgsAttributeFormWidget::searchWidgetWrappers
QList< QgsSearchWidgetWrapper * > searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
Definition: qgsattributeformwidget.cpp:106
QgsAttributeFormWidget::searchPage
QWidget * searchPage() const
Returns a pointer to the search page widget.
Definition: qgsattributeformwidget.cpp:201
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:86
QgsAttributeFormWidget::editPage
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
Definition: qgsattributeformwidget.cpp:211