QGIS API Documentation  3.0.2-Girona (307d082)
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  , mMode( DefaultMode )
25  , mForm( form )
26  , mWidget( widget )
27 {
28  mEditPage = new QWidget();
29  QHBoxLayout *l = new QHBoxLayout();
30  l->setMargin( 0 );
31  l->setContentsMargins( 0, 0, 0, 0 );
32  mEditPage->setLayout( l );
33 
34  l = new QHBoxLayout();
35  l->setMargin( 0 );
36  l->setContentsMargins( 0, 0, 0, 0 );
37  mSearchFrame = new QWidget();
38  mSearchFrame->setLayout( l );
39 
40  mSearchPage = new QWidget();
41  l = new QHBoxLayout();
42  l->setMargin( 0 );
43  l->setContentsMargins( 0, 0, 0, 0 );
44  mSearchPage->setLayout( l );
45  l->addWidget( mSearchFrame, 1 );
46  mSearchWidgetToolButton = new QgsSearchWidgetToolButton();
47  mSearchWidgetToolButton->setObjectName( QStringLiteral( "SearchWidgetToolButton" ) );
48  connect( mSearchWidgetToolButton, &QgsSearchWidgetToolButton::activeFlagsChanged,
49  this, &QgsAttributeFormWidget::searchWidgetFlagsChanged );
50  l->addWidget( mSearchWidgetToolButton, 0 );
51 
52 
53  mStack = new QStackedWidget;
54  mStack->addWidget( mEditPage );
55  mStack->addWidget( mSearchPage );
56 
57  l = new QHBoxLayout();
58  l->setMargin( 0 );
59  l->setContentsMargins( 0, 0, 0, 0 );
60  setLayout( l );
61  l->addWidget( mStack );
62 
63  if ( !mWidget || !mForm )
64  return;
65 
66  mEditPage->layout()->addWidget( mWidget->widget() );
67 
68  updateWidgets();
69 }
70 
72 {
73  mMode = mode;
74  updateWidgets();
75 }
76 
78 {
79  return mForm;
80 }
81 
83 {
84  return mSearchFrame;
85 }
86 
88 {
89  mSearchWidgets.clear();
90  mSearchWidgets << wrapper;
91  mSearchFrame->layout()->addWidget( wrapper->widget() );
92  mSearchWidgetToolButton->setAvailableFlags( wrapper->supportedFlags() );
93  mSearchWidgetToolButton->setActiveFlags( QgsSearchWidgetWrapper::FilterFlags() );
94  mSearchWidgetToolButton->setDefaultFlags( wrapper->defaultFlags() );
95  connect( wrapper, &QgsSearchWidgetWrapper::valueChanged, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setActive );
96  connect( wrapper, &QgsSearchWidgetWrapper::valueCleared, mSearchWidgetToolButton, &QgsSearchWidgetToolButton::setInactive );
97 }
98 
100 {
101  mSearchWidgets << wrapper;
102 
103  mSearchFrame->layout()->addWidget( wrapper->widget() );
104  wrapper->widget()->hide();
105 }
106 
108 {
109  return mSearchWidgets;
110 }
111 
113 {
114  if ( mSearchWidgets.isEmpty() )
115  return QString();
116 
117  if ( !mSearchWidgetToolButton->isActive() )
118  return QString();
119 
120  if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::Between )
121  {
122  // special case: Between search
123  QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::GreaterThanOrEqualTo );
124  QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::LessThanOrEqualTo );
125  return QStringLiteral( "%1 AND %2" ).arg( filter1, filter2 );
126  }
127  else if ( mSearchWidgetToolButton->activeFlags() & QgsSearchWidgetWrapper::IsNotBetween )
128  {
129  // special case: Is Not Between search
130  QString filter1 = mSearchWidgets.at( 0 )->createExpression( QgsSearchWidgetWrapper::LessThan );
131  QString filter2 = mSearchWidgets.at( 1 )->createExpression( QgsSearchWidgetWrapper::GreaterThan );
132  return QStringLiteral( "%1 OR %2" ).arg( filter1, filter2 );
133  }
134 
135  return mSearchWidgets.at( 0 )->createExpression( mSearchWidgetToolButton->activeFlags() );
136 }
137 
139 {
140  mSearchWidgetToolButton->setInactive();
141  Q_FOREACH ( QgsSearchWidgetWrapper *widget, mSearchWidgets )
142  {
143  widget->clearWidget();
144  }
145 }
146 
148 {
149  return mWidget->layer();
150 }
151 
152 void QgsAttributeFormWidget::searchWidgetFlagsChanged( QgsSearchWidgetWrapper::FilterFlags flags )
153 {
154  Q_FOREACH ( QgsSearchWidgetWrapper *widget, mSearchWidgets )
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 
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 }
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 setEnabled(bool enabled) override
Toggles whether the search widget is enabled or disabled.
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.
virtual void clearWidget()
Clears the widget&#39;s current value and resets it back to the default state.
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:119
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
Access the QgsVectorLayer, you are working on.
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...