QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  connect( mModeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]
44  {
45  switch ( static_cast< Qgis::VectorTemporalMode>( mModeComboBox->currentData().toInt() ) )
46  {
47  case Qgis::VectorTemporalMode::FixedTemporalRange:
48  case Qgis::VectorTemporalMode::FeatureDateTimeInstantFromField:
49  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromFields:
50  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndDurationFromFields:
51  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromExpressions:
52  mLimitsComboBox->show();
53  mLimitsLabel->show();
54  break;
55  case Qgis::VectorTemporalMode::RedrawLayerOnly:
56  mLimitsComboBox->hide();
57  mLimitsLabel->hide();
58  break;
59  }
60  } );
61 
62  mLimitsComboBox->addItem( tr( "Include Start, Exclude End (default)" ), static_cast< int >( Qgis::VectorTemporalLimitMode::IncludeBeginExcludeEnd ) );
63  mLimitsComboBox->addItem( tr( "Include Start, Include End" ), static_cast< int >( Qgis::VectorTemporalLimitMode::IncludeBeginIncludeEnd ) );
64 
65  mStartTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
66  mEndTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
67 
68  mSingleFieldComboBox->setLayer( layer );
69  mStartFieldComboBox->setLayer( layer );
70  mEndFieldComboBox->setLayer( layer );
71  mDurationStartFieldComboBox->setLayer( layer );
72  mDurationFieldComboBox->setLayer( layer );
73  mSingleFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
74  mStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
75  mStartFieldComboBox->setAllowEmptyFieldName( true );
76  mEndFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
77  mEndFieldComboBox->setAllowEmptyFieldName( true );
78  mDurationStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
79  mDurationFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
80 
81  mFixedDurationSpinBox->setMinimum( 0 );
82  mFixedDurationSpinBox->setClearValue( 0 );
83 
84  for ( const QgsUnitTypes::TemporalUnit u :
85  {
96  } )
97  {
100  mDurationUnitsComboBox->addItem( title, u );
101  mFixedDurationUnitsComboBox->addItem( title, u );
102  }
103 
104  mFixedDurationUnitsComboBox->setEnabled( !mAccumulateCheckBox->isChecked() );
105  mFixedDurationSpinBox->setEnabled( !mAccumulateCheckBox->isChecked() );
106  connect( mAccumulateCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
107  {
108  mFixedDurationUnitsComboBox->setEnabled( !checked );
109  mFixedDurationSpinBox->setEnabled( !checked );
110  } );
111 
112  mStartExpressionWidget->setAllowEmptyFieldName( true );
113  mEndExpressionWidget->setAllowEmptyFieldName( true );
114  mStartExpressionWidget->setLayer( layer );
115  mEndExpressionWidget->setLayer( layer );
116  mStartExpressionWidget->registerExpressionContextGenerator( this );
117  mEndExpressionWidget->registerExpressionContextGenerator( this );
118 
119  syncToLayer();
120 }
121 
123 {
124  QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
125 
126  properties->setIsActive( mTemporalGroupBox->isChecked() );
127  properties->setMode( static_cast< Qgis::VectorTemporalMode >( mModeComboBox->currentData().toInt() ) );
128  properties->setLimitMode( static_cast< Qgis::VectorTemporalLimitMode >( mLimitsComboBox->currentData().toInt() ) );
129 
130  const QgsDateTimeRange normalRange = QgsDateTimeRange( mStartTemporalDateTimeEdit->dateTime(),
131  mEndTemporalDateTimeEdit->dateTime() );
132 
133  properties->setFixedTemporalRange( normalRange );
134 
135  switch ( properties->mode() )
136  {
137  case Qgis::VectorTemporalMode::FeatureDateTimeInstantFromField:
138  case Qgis::VectorTemporalMode::FixedTemporalRange:
139  case Qgis::VectorTemporalMode::RedrawLayerOnly:
140  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromExpressions:
141  properties->setStartField( mSingleFieldComboBox->currentField() );
142  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
143  break;
144 
145  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndEndFromFields:
146  properties->setStartField( mStartFieldComboBox->currentField() );
147  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
148  break;
149 
150  case Qgis::VectorTemporalMode::FeatureDateTimeStartAndDurationFromFields:
151  properties->setStartField( mDurationStartFieldComboBox->currentField() );
152  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mDurationUnitsComboBox->currentData().toInt() ) );
153  break;
154  }
155 
156  properties->setEndField( mEndFieldComboBox->currentField() );
157  properties->setDurationField( mDurationFieldComboBox->currentField() );
158  properties->setFixedDuration( mFixedDurationSpinBox->value() );
159  properties->setAccumulateFeatures( mAccumulateCheckBox->isChecked() );
160  properties->setStartExpression( mStartExpressionWidget->currentField() );
161  properties->setEndExpression( mEndExpressionWidget->currentField() );
162 }
163 
165 {
166  QgsExpressionContext context;
168  return context;
169 }
170 
172 {
173  const QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
174  mTemporalGroupBox->setChecked( properties->isActive() );
175 
176  mModeComboBox->setCurrentIndex( mModeComboBox->findData( static_cast< int >( properties->mode() ) ) );
177  mStackedWidget->setCurrentIndex( static_cast< int >( properties->mode() ) );
178 
179  mLimitsComboBox->setCurrentIndex( mLimitsComboBox->findData( static_cast< int >( properties->limitMode() ) ) );
180 
181  mStartTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().begin() );
182  mEndTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().end() );
183 
184  mFixedDurationSpinBox->setValue( properties->fixedDuration() );
185 
186  mSingleFieldComboBox->setField( properties->startField() );
187  mStartFieldComboBox->setField( properties->startField() );
188  mDurationStartFieldComboBox->setField( properties->startField() );
189  mEndFieldComboBox->setField( properties->endField() );
190  mDurationFieldComboBox->setField( properties->durationField() );
191  mDurationUnitsComboBox->setCurrentIndex( mDurationUnitsComboBox->findData( properties->durationUnits() ) );
192  mFixedDurationUnitsComboBox->setCurrentIndex( mDurationUnitsComboBox->findData( properties->durationUnits() ) );
193 
194  mAccumulateCheckBox->setChecked( properties->accumulateFeatures() );
195 
196  mStartExpressionWidget->setField( properties->startExpression() );
197  mEndExpressionWidget->setField( properties->endExpression() );
198 }
QgsFieldProxyModel::Numeric
@ Numeric
All numeric fields.
Definition: qgsfieldproxymodel.h:45
QgsUnitTypes::TemporalDecades
@ TemporalDecades
Decades.
Definition: qgsunittypes.h:159
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsVectorLayerTemporalProperties::setStartField
void setStartField(const QString &field)
Sets the name of the start datetime field, which contains the start time for the feature's time spans...
Definition: qgsvectorlayertemporalproperties.cpp:400
qgsvectordataprovidertemporalcapabilities.h
qgsexpressioncontextutils.h
QgsExpressionContext::appendScopes
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Definition: qgsexpressioncontext.cpp:499
QgsVectorLayerTemporalProperties::endField
QString endField() const
Returns the name of the end datetime field, which contains the end time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:405
QgsStringUtils::capitalize
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Definition: qgsstringutils.cpp:24
QgsVectorLayerTemporalProperties::fixedTemporalRange
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
Definition: qgsvectorlayertemporalproperties.cpp:254
QgsVectorLayerTemporalPropertiesWidget::QgsVectorLayerTemporalPropertiesWidget
QgsVectorLayerTemporalPropertiesWidget(QWidget *parent=nullptr, QgsVectorLayer *layer=nullptr)
Constructor for QgsVectorLayerTemporalPropertiesWidget.
Definition: qgsvectorlayertemporalpropertieswidget.cpp:28
QgsVectorLayerTemporalProperties::setEndField
void setEndField(const QString &field)
Sets the name of the end datetime field, which contains the end time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:410
QgsVectorLayerTemporalProperties::durationUnits
QgsUnitTypes::TemporalUnit durationUnits() const
Returns the units of the event's duration.
Definition: qgsvectorlayertemporalproperties.cpp:425
QgsFieldProxyModel::DateTime
@ DateTime
Datetime fieldss.
Definition: qgsfieldproxymodel.h:49
QgsVectorLayerTemporalProperties::setStartExpression
void setStartExpression(const QString &expression)
Sets the expression to use for the start time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:360
QgsFieldProxyModel::Date
@ Date
Date or datetime fields.
Definition: qgsfieldproxymodel.h:46
Qgis::VectorTemporalLimitMode::IncludeBeginIncludeEnd
@ IncludeBeginIncludeEnd
Mode to include both limits of the filtering timeframe.
qgsstringutils.h
qgsgui.h
QgsUnitTypes::TemporalYears
@ TemporalYears
Years.
Definition: qgsunittypes.h:158
QgsUnitTypes::TemporalDays
@ TemporalDays
Days.
Definition: qgsunittypes.h:155
qgsvectorlayertemporalpropertieswidget.h
QgsVectorLayerTemporalProperties::setLimitMode
void setLimitMode(Qgis::VectorTemporalLimitMode mode)
Sets the temporal limit mode (to include or exclude begin/end limits).
Definition: qgsvectorlayertemporalproperties.cpp:237
Qgis::VectorTemporalMode
VectorTemporalMode
Vector layer temporal feature modes.
Definition: qgis.h:1166
QgsGui::higFlags
static QgsGui::HigFlags higFlags()
Returns the platform's HIG flags.
Definition: qgsgui.cpp:199
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:377
QgsGui::HigDialogTitleIsTitleCase
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:235
Qgis::Capitalization::TitleCase
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:149
QgsVectorLayerTemporalProperties::setDurationField
void setDurationField(const QString &field)
Sets the name of the duration field, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:420
QgsVectorLayerTemporalProperties::startExpression
QString startExpression() const
Returns the expression for the start time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:355
QgsUnitTypes::TemporalSeconds
@ TemporalSeconds
Seconds.
Definition: qgsunittypes.h:152
QgsUnitTypes::TemporalMonths
@ TemporalMonths
Months.
Definition: qgsunittypes.h:157
QgsVectorLayerTemporalProperties::durationField
QString durationField() const
Returns the name of the duration field, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:415
qgsvectorlayertemporalproperties.h
QgsVectorLayerTemporalProperties::setAccumulateFeatures
void setAccumulateFeatures(bool accumulate)
Sets whether features will be accumulated over time (i.e.
Definition: qgsvectorlayertemporalproperties.cpp:380
QgsVectorLayer::temporalProperties
QgsMapLayerTemporalProperties * temporalProperties() override
Returns the layer's temporal properties.
Definition: qgsvectorlayer.cpp:686
QgsTemporalProperty::isActive
bool isActive() const
Returns true if the temporal property is active.
Definition: qgstemporalproperty.cpp:36
QgsVectorLayerTemporalPropertiesWidget::syncToLayer
void syncToLayer()
Updates the widget state to match the current layer state.
Definition: qgsvectorlayertemporalpropertieswidget.cpp:171
QgsVectorLayerTemporalProperties::setFixedDuration
void setFixedDuration(double duration)
Sets the fixed event duration, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:390
QgsUnitTypes::toString
static Q_INVOKABLE QString toString(QgsUnitTypes::DistanceUnit unit)
Returns a translated string representing a distance unit.
Definition: qgsunittypes.cpp:199
QgsVectorLayerTemporalProperties::endExpression
QString endExpression() const
Returns the expression for the end time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:365
QgsVectorLayerTemporalPropertiesWidget::saveTemporalProperties
void saveTemporalProperties()
Save widget temporal properties inputs.
Definition: qgsvectorlayertemporalpropertieswidget.cpp:122
QgsVectorLayerTemporalProperties
Implementation of map layer temporal properties for vector layers.
Definition: qgsvectorlayertemporalproperties.h:70
QgsVectorLayerTemporalProperties::startField
QString startField() const
Returns the name of the start datetime field, which contains the start time for the feature's time sp...
Definition: qgsvectorlayertemporalproperties.cpp:395
Qgis::VectorTemporalLimitMode::IncludeBeginExcludeEnd
@ IncludeBeginExcludeEnd
Default mode: include the Begin limit, but exclude the End limit.
QgsVectorLayerTemporalProperties::fixedDuration
double fixedDuration() const
Returns the fixed duration length, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:385
QgsTemporalProperty::setIsActive
void setIsActive(bool active)
Sets whether the temporal property is active.
Definition: qgstemporalproperty.cpp:27
QgsVectorLayerTemporalProperties::limitMode
Qgis::VectorTemporalLimitMode limitMode() const
Returns the temporal limit mode (to include or exclude begin/end limits).
Definition: qgsvectorlayertemporalproperties.cpp:232
QgsUnitTypes::TemporalHours
@ TemporalHours
Hours.
Definition: qgsunittypes.h:154
qgsvectorlayer.h
qgsprojecttimesettings.h
QgsVectorLayerTemporalProperties::setDurationUnits
void setDurationUnits(QgsUnitTypes::TemporalUnit units)
Sets the units of the event's duration.
Definition: qgsvectorlayertemporalproperties.cpp:430
QgsUnitTypes::TemporalMinutes
@ TemporalMinutes
Minutes.
Definition: qgsunittypes.h:153
QgsUnitTypes::TemporalMilliseconds
@ TemporalMilliseconds
Milliseconds.
Definition: qgsunittypes.h:151
QgsUnitTypes::TemporalCenturies
@ TemporalCenturies
Centuries.
Definition: qgsunittypes.h:160
QgsVectorLayerTemporalPropertiesWidget::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsvectorlayertemporalpropertieswidget.cpp:164
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsUnitTypes::TemporalWeeks
@ TemporalWeeks
Weeks.
Definition: qgsunittypes.h:156
QgsVectorLayerTemporalProperties::setEndExpression
void setEndExpression(const QString &endExpression)
Sets the expression to use for the end time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:370
QgsVectorLayerTemporalProperties::setMode
void setMode(Qgis::VectorTemporalMode mode)
Sets the temporal properties mode.
Definition: qgsvectorlayertemporalproperties.cpp:225
QgsVectorLayerTemporalProperties::mode
Qgis::VectorTemporalMode mode() const
Returns the temporal properties mode.
Definition: qgsvectorlayertemporalproperties.cpp:220
QgsVectorLayerTemporalProperties::setFixedTemporalRange
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
Definition: qgsvectorlayertemporalproperties.cpp:249
Qgis::VectorTemporalLimitMode
VectorTemporalLimitMode
Mode for the handling of the limits of the filtering timeframe for vector features.
Definition: qgis.h:1182
QgsVectorLayerTemporalProperties::accumulateFeatures
bool accumulateFeatures() const
Returns true if features will be accumulated over time (i.e.
Definition: qgsvectorlayertemporalproperties.cpp:375
qgsproject.h