QGIS API Documentation  3.21.0-Master (5b68dc587e)
qgsvectorlayertemporalpropertieswidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayertemporalpropertieswidget.cpp
3  ------------------------------
4  begin : May 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsgui.h"
20 #include "qgsproject.h"
21 #include "qgsprojecttimesettings.h"
23 #include "qgsvectorlayer.h"
25 #include "qgsstringutils.h"
27 
29  : QWidget( parent )
30  , mLayer( layer )
31 {
32  Q_ASSERT( mLayer );
33  setupUi( this );
34 
35  mModeComboBox->addItem( tr( "Fixed Time Range" ), static_cast< int >( Qgis::VectorTemporalMode::FixedTemporalRange ) );
36  mModeComboBox->addItem( tr( "Single Field with Date/Time" ), static_cast< int >( Qgis::VectorTemporalMode::FeatureDateTimeInstantFromField ) );
37  mModeComboBox->addItem( tr( "Separate Fields for Start and End Date/Time" ), static_cast< int >( Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromFields ) );
38  mModeComboBox->addItem( tr( "Separate Fields for Start and Event Duration" ), static_cast< int >( Qgis::VectorTemporalMode::FeatureDateTimeStartAndDurationFromFields ) );
39  mModeComboBox->addItem( tr( "Start and End Date/Time from Expressions" ), static_cast< int >( Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromExpressions ) );
40  mModeComboBox->addItem( tr( "Redraw Layer Only" ), static_cast< int >( Qgis::VectorTemporalMode::RedrawLayerOnly ) );
41 
42  connect( mModeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), mStackedWidget, &QStackedWidget::setCurrentIndex );
43 
44  mLimitsComboBox->addItem( tr( "Include Start, Exclude End (default)" ), static_cast< int >( Qgis::VectorTemporalLimitMode::IncludeBeginExcludeEnd ) );
45  mLimitsComboBox->addItem( tr( "Include Start, Include End" ), static_cast< int >( Qgis::VectorTemporalLimitMode::IncludeBeginIncludeEnd ) );
46 
47  mStartTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
48  mEndTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
49 
50  mSingleFieldComboBox->setLayer( layer );
51  mStartFieldComboBox->setLayer( layer );
52  mEndFieldComboBox->setLayer( layer );
53  mDurationStartFieldComboBox->setLayer( layer );
54  mDurationFieldComboBox->setLayer( layer );
55  mSingleFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
56  mStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
57  mStartFieldComboBox->setAllowEmptyFieldName( true );
58  mEndFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
59  mEndFieldComboBox->setAllowEmptyFieldName( true );
60  mDurationStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
61  mDurationFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
62 
63  mFixedDurationSpinBox->setMinimum( 0 );
64  mFixedDurationSpinBox->setClearValue( 0 );
65 
66  for ( const QgsUnitTypes::TemporalUnit u :
67  {
78  } )
79  {
82  mDurationUnitsComboBox->addItem( title, u );
83  mFixedDurationUnitsComboBox->addItem( title, u );
84  }
85 
86  mFixedDurationUnitsComboBox->setEnabled( !mAccumulateCheckBox->isChecked() );
87  mFixedDurationSpinBox->setEnabled( !mAccumulateCheckBox->isChecked() );
88  connect( mAccumulateCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
89  {
90  mFixedDurationUnitsComboBox->setEnabled( !checked );
91  mFixedDurationSpinBox->setEnabled( !checked );
92  } );
93 
94  mStartExpressionWidget->setAllowEmptyFieldName( true );
95  mEndExpressionWidget->setAllowEmptyFieldName( true );
96  mStartExpressionWidget->setLayer( layer );
97  mEndExpressionWidget->setLayer( layer );
98  mStartExpressionWidget->registerExpressionContextGenerator( this );
99  mEndExpressionWidget->registerExpressionContextGenerator( this );
100 
101  syncToLayer();
102 }
103 
105 {
106  QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
107 
108  properties->setIsActive( mTemporalGroupBox->isChecked() );
109  properties->setMode( static_cast< Qgis::VectorTemporalMode >( mModeComboBox->currentData().toInt() ) );
110  properties->setLimitMode( static_cast< Qgis::VectorTemporalLimitMode >( mLimitsComboBox->currentData().toInt() ) );
111 
112  const QgsDateTimeRange normalRange = QgsDateTimeRange( mStartTemporalDateTimeEdit->dateTime(),
113  mEndTemporalDateTimeEdit->dateTime() );
114 
115  properties->setFixedTemporalRange( normalRange );
116 
117  switch ( properties->mode() )
118  {
119  case Qgis::VectorTemporalMode::FeatureDateTimeInstantFromField:
120  case Qgis::VectorTemporalMode::FixedTemporalRange:
121  case Qgis::VectorTemporalMode::RedrawLayerOnly:
122  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromExpressions:
123  properties->setStartField( mSingleFieldComboBox->currentField() );
124  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
125  break;
126 
127  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromFields:
128  properties->setStartField( mStartFieldComboBox->currentField() );
129  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
130  break;
131 
132  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndDurationFromFields:
133  properties->setStartField( mDurationStartFieldComboBox->currentField() );
134  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mDurationUnitsComboBox->currentData().toInt() ) );
135  break;
136  }
137 
138  properties->setEndField( mEndFieldComboBox->currentField() );
139  properties->setDurationField( mDurationFieldComboBox->currentField() );
140  properties->setFixedDuration( mFixedDurationSpinBox->value() );
141  properties->setAccumulateFeatures( mAccumulateCheckBox->isChecked() );
142  properties->setStartExpression( mStartExpressionWidget->currentField() );
143  properties->setEndExpression( mEndExpressionWidget->currentField() );
144 }
145 
147 {
148  QgsExpressionContext context;
150  return context;
151 }
152 
154 {
155  const QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
156  mTemporalGroupBox->setChecked( properties->isActive() );
157 
158  mModeComboBox->setCurrentIndex( mModeComboBox->findData( static_cast< int >( properties->mode() ) ) );
159  mStackedWidget->setCurrentIndex( static_cast< int >( properties->mode() ) );
160 
161  mLimitsComboBox->setCurrentIndex( mLimitsComboBox->findData( static_cast< int >( properties->limitMode() ) ) );
162 
163  mStartTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().begin() );
164  mEndTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().end() );
165 
166  mFixedDurationSpinBox->setValue( properties->fixedDuration() );
167 
168  mSingleFieldComboBox->setField( properties->startField() );
169  mStartFieldComboBox->setField( properties->startField() );
170  mDurationStartFieldComboBox->setField( properties->startField() );
171  mEndFieldComboBox->setField( properties->endField() );
172  mDurationFieldComboBox->setField( properties->durationField() );
173  mDurationUnitsComboBox->setCurrentIndex( mDurationUnitsComboBox->findData( properties->durationUnits() ) );
174  mFixedDurationUnitsComboBox->setCurrentIndex( mDurationUnitsComboBox->findData( properties->durationUnits() ) );
175 
176  mAccumulateCheckBox->setChecked( properties->accumulateFeatures() );
177 
178  mStartExpressionWidget->setField( properties->startExpression() );
179  mEndExpressionWidget->setField( properties->endExpression() );
180 }
VectorTemporalMode
Vector layer temporal feature modes.
Definition: qgis.h:827
VectorTemporalLimitMode
Mode for the handling of the limits of the filtering timeframe for vector features.
Definition: qgis.h:843
@ IncludeBeginExcludeEnd
Default mode: include the Begin limit, but exclude the End limit.
@ IncludeBeginIncludeEnd
Mode to include both limits of the filtering timeframe.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
@ DateTime
Datetime fieldss.
@ Date
Date or datetime fields.
@ Numeric
All numeric fields.
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:220
static QgsGui::HigFlags higFlags()
Returns the platform's HIG flags.
Definition: qgsgui.cpp:187
static QString capitalize(const QString &string, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
bool isActive() const
Returns true if the temporal property is active.
void setIsActive(bool active)
Sets whether the temporal property is active.
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:150
@ TemporalMonths
Months.
Definition: qgsunittypes.h:157
@ TemporalWeeks
Weeks.
Definition: qgsunittypes.h:156
@ TemporalMilliseconds
Milliseconds.
Definition: qgsunittypes.h:151
@ TemporalDays
Days.
Definition: qgsunittypes.h:155
@ TemporalDecades
Decades.
Definition: qgsunittypes.h:159
@ TemporalCenturies
Centuries.
Definition: qgsunittypes.h:160
@ TemporalSeconds
Seconds.
Definition: qgsunittypes.h:152
@ TemporalMinutes
Minutes.
Definition: qgsunittypes.h:153
@ TemporalYears
Years.
Definition: qgsunittypes.h:158
@ TemporalHours
Hours.
Definition: qgsunittypes.h:154
void syncToLayer()
Updates the widget state to match the current layer state.
QgsVectorLayerTemporalPropertiesWidget(QWidget *parent=nullptr, QgsVectorLayer *layer=nullptr)
Constructor for QgsVectorLayerTemporalPropertiesWidget.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void saveTemporalProperties()
Save widget temporal properties inputs.
Implementation of map layer temporal properties for vector layers.
QString endExpression() const
Returns the expression for the end time for the feature's time spans.
void setDurationField(const QString &field)
Sets the name of the duration field, which contains the duration of the event.
void setMode(Qgis::VectorTemporalMode mode)
Sets the temporal properties mode.
void setStartExpression(const QString &expression)
Sets the expression to use for the start time for the feature's time spans.
Qgis::VectorTemporalLimitMode limitMode() const
Returns the temporal limit mode (to include or exclude begin/end limits).
void setLimitMode(Qgis::VectorTemporalLimitMode mode)
Sets the temporal limit mode (to include or exclude begin/end limits).
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
double fixedDuration() const
Returns the fixed duration length, which contains the duration of the event.
bool accumulateFeatures() const
Returns true if features will be accumulated over time (i.e.
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
QgsUnitTypes::TemporalUnit durationUnits() const
Returns the units of the event's duration.
void setEndExpression(const QString &endExpression)
Sets the expression to use for the end time for the feature's time spans.
QString durationField() const
Returns the name of the duration field, which contains the duration of the event.
QString endField() const
Returns the name of the end datetime field, which contains the end time for the feature's time spans.
void setDurationUnits(QgsUnitTypes::TemporalUnit units)
Sets the units of the event's duration.
void setAccumulateFeatures(bool accumulate)
Sets whether features will be accumulated over time (i.e.
void setFixedDuration(double duration)
Sets the fixed event duration, which contains the duration of the event.
void setEndField(const QString &field)
Sets the name of the end datetime field, which contains the end time for the feature's time spans.
Qgis::VectorTemporalMode mode() const
Returns the temporal properties mode.
QString startField() const
Returns the name of the start datetime field, which contains the start time for the feature's time sp...
void setStartField(const QString &field)
Sets the name of the start datetime field, which contains the start time for the feature's time spans...
QString startExpression() const
Returns the expression for the start time for the feature's time spans.
Represents a vector layer which manages a vector based data sets.
QgsMapLayerTemporalProperties * temporalProperties() override
Returns temporal properties associated with the vector layer.