QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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" ), QgsVectorLayerTemporalProperties::ModeFixedTemporalRange );
36  mModeComboBox->addItem( tr( "Single Field with Date/Time" ), QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField );
37  mModeComboBox->addItem( tr( "Separate Fields for Start and End Date/Time" ), QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields );
38  mModeComboBox->addItem( tr( "Separate Fields for Start and Event Duration" ), QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndDurationFromFields );
39  mModeComboBox->addItem( tr( "Start and End Date/Time from Expressions" ), QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromExpressions );
40  mModeComboBox->addItem( tr( "Redraw Layer Only" ), QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly );
41 
42  connect( mModeComboBox, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), mStackedWidget, &QStackedWidget::setCurrentIndex );
43 
44  mStartTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
45  mEndTemporalDateTimeEdit->setDisplayFormat( "yyyy-MM-dd HH:mm:ss" );
46 
47  mSingleFieldComboBox->setLayer( layer );
48  mStartFieldComboBox->setLayer( layer );
49  mEndFieldComboBox->setLayer( layer );
50  mDurationStartFieldComboBox->setLayer( layer );
51  mDurationFieldComboBox->setLayer( layer );
52  mSingleFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
53  mStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
54  mStartFieldComboBox->setAllowEmptyFieldName( true );
55  mEndFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
56  mEndFieldComboBox->setAllowEmptyFieldName( true );
57  mDurationStartFieldComboBox->setFilters( QgsFieldProxyModel::DateTime | QgsFieldProxyModel::Date );
58  mDurationFieldComboBox->setFilters( QgsFieldProxyModel::Numeric );
59 
60  mFixedDurationSpinBox->setMinimum( 0 );
61  mFixedDurationSpinBox->setClearValue( 0 );
62 
64  {
75  } )
76  {
79  mDurationUnitsComboBox->addItem( title, u );
80  mFixedDurationUnitsComboBox->addItem( title, u );
81  }
82 
83  mFixedDurationUnitsComboBox->setEnabled( !mAccumulateCheckBox->isChecked() );
84  mFixedDurationSpinBox->setEnabled( !mAccumulateCheckBox->isChecked() );
85  connect( mAccumulateCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
86  {
87  mFixedDurationUnitsComboBox->setEnabled( !checked );
88  mFixedDurationSpinBox->setEnabled( !checked );
89  } );
90 
91  mStartExpressionWidget->setAllowEmptyFieldName( true );
92  mEndExpressionWidget->setAllowEmptyFieldName( true );
93  mStartExpressionWidget->setLayer( layer );
94  mEndExpressionWidget->setLayer( layer );
95  mStartExpressionWidget->registerExpressionContextGenerator( this );
96  mEndExpressionWidget->registerExpressionContextGenerator( this );
97 
98  syncToLayer();
99 }
100 
102 {
103  QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
104 
105  properties->setIsActive( mTemporalGroupBox->isChecked() );
106  properties->setMode( static_cast< QgsVectorLayerTemporalProperties::TemporalMode >( mModeComboBox->currentData().toInt() ) );
107 
108  QgsDateTimeRange normalRange = QgsDateTimeRange( mStartTemporalDateTimeEdit->dateTime(),
109  mEndTemporalDateTimeEdit->dateTime() );
110 
111  properties->setFixedTemporalRange( normalRange );
112 
113  switch ( properties->mode() )
114  {
119  properties->setStartField( mSingleFieldComboBox->currentField() );
120  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
121  break;
122 
124  properties->setStartField( mStartFieldComboBox->currentField() );
125  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mFixedDurationUnitsComboBox->currentData().toInt() ) );
126  break;
127 
129  properties->setStartField( mDurationStartFieldComboBox->currentField() );
130  properties->setDurationUnits( static_cast< QgsUnitTypes::TemporalUnit >( mDurationUnitsComboBox->currentData().toInt() ) );
131  break;
132  }
133 
134  properties->setEndField( mEndFieldComboBox->currentField() );
135  properties->setDurationField( mDurationFieldComboBox->currentField() );
136  properties->setFixedDuration( mFixedDurationSpinBox->value() );
137  properties->setAccumulateFeatures( mAccumulateCheckBox->isChecked() );
138  properties->setStartExpression( mStartExpressionWidget->currentField() );
139  properties->setEndExpression( mEndExpressionWidget->currentField() );
140 }
141 
143 {
144  QgsExpressionContext context;
146  return context;
147 }
148 
150 {
151  const QgsVectorLayerTemporalProperties *properties = qobject_cast< QgsVectorLayerTemporalProperties * >( mLayer->temporalProperties() );
152  mTemporalGroupBox->setChecked( properties->isActive() );
153 
154  mModeComboBox->setCurrentIndex( mModeComboBox->findData( properties->mode() ) );
155  mStackedWidget->setCurrentIndex( static_cast< int >( properties->mode() ) );
156 
157  mStartTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().begin() );
158  mEndTemporalDateTimeEdit->setDateTime( properties->fixedTemporalRange().end() );
159 
160  mFixedDurationSpinBox->setValue( properties->fixedDuration() );
161 
162  if ( !properties->startField().isEmpty() )
163  {
164  mSingleFieldComboBox->setField( properties->startField() );
165  mStartFieldComboBox->setField( properties->startField() );
166  mDurationStartFieldComboBox->setField( properties->startField() );
167  }
168  if ( !properties->endField().isEmpty() )
169  {
170  mEndFieldComboBox->setField( properties->endField() );
171  }
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 }
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:369
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:369
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:495
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:374
QgsVectorLayerTemporalProperties::fixedTemporalRange
const QgsDateTimeRange & fixedTemporalRange() const
Returns the fixed temporal range for the layer.
Definition: qgsvectorlayertemporalproperties.cpp:225
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:379
QgsVectorLayerTemporalProperties::durationUnits
QgsUnitTypes::TemporalUnit durationUnits() const
Returns the units of the event's duration.
Definition: qgsvectorlayertemporalproperties.cpp:394
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:329
QgsFieldProxyModel::Date
@ Date
Date or datetime fields.
Definition: qgsfieldproxymodel.h:46
QgsVectorLayerTemporalProperties::ModeFixedTemporalRange
@ ModeFixedTemporalRange
Mode when temporal properties have fixed start and end datetimes.
Definition: qgsvectorlayertemporalproperties.h:91
qgsstringutils.h
qgsgui.h
QgsUnitTypes::TemporalYears
@ TemporalYears
Years.
Definition: qgsunittypes.h:158
QgsUnitTypes::TemporalDays
@ TemporalDays
Days.
Definition: qgsunittypes.h:155
qgsvectorlayertemporalpropertieswidget.h
QgsGui::higFlags
static QgsGui::HigFlags higFlags()
Returns the platform's HIG flags.
Definition: qgsgui.cpp:152
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:306
QgsGui::HigDialogTitleIsTitleCase
@ HigDialogTitleIsTitleCase
Dialog titles should be title case.
Definition: qgsgui.h:177
QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromExpressions
@ ModeFeatureDateTimeStartAndEndFromExpressions
Mode when features use expressions for start and end times.
Definition: qgsvectorlayertemporalproperties.h:95
QgsUnitTypes::TemporalUnit
TemporalUnit
Temporal units.
Definition: qgsunittypes.h:149
QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndEndFromFields
@ ModeFeatureDateTimeStartAndEndFromFields
Mode when features have separate fields for start and end times.
Definition: qgsvectorlayertemporalproperties.h:93
QgsVectorLayerTemporalProperties::setDurationField
void setDurationField(const QString &field)
Sets the name of the duration field, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:389
QgsVectorLayerTemporalProperties::startExpression
QString startExpression() const
Returns the expression for the start time for the feature's time spans.
Definition: qgsvectorlayertemporalproperties.cpp:324
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:384
qgsvectorlayertemporalproperties.h
QgsVectorLayerTemporalProperties::setAccumulateFeatures
void setAccumulateFeatures(bool accumulate)
Sets whether features will be accumulated over time (i.e.
Definition: qgsvectorlayertemporalproperties.cpp:349
QgsVectorLayerTemporalProperties::ModeFeatureDateTimeInstantFromField
@ ModeFeatureDateTimeInstantFromField
Mode when features have a datetime instant taken from a single field.
Definition: qgsvectorlayertemporalproperties.h:92
QgsVectorLayer::temporalProperties
QgsMapLayerTemporalProperties * temporalProperties() override
Returns temporal properties associated with the vector layer.
Definition: qgsvectorlayer.cpp:637
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:149
QgsVectorLayerTemporalProperties::setFixedDuration
void setFixedDuration(double duration)
Sets the fixed event duration, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:359
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:334
QgsVectorLayerTemporalPropertiesWidget::saveTemporalProperties
void saveTemporalProperties()
Save widget temporal properties inputs.
Definition: qgsvectorlayertemporalpropertieswidget.cpp:101
QgsVectorLayerTemporalProperties
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:364
QgsVectorLayerTemporalProperties::fixedDuration
double fixedDuration() const
Returns the fixed duration length, which contains the duration of the event.
Definition: qgsvectorlayertemporalproperties.cpp:354
QgsStringUtils::capitalize
static QString capitalize(const QString &string, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Definition: qgsstringutils.cpp:25
QgsTemporalProperty::setIsActive
void setIsActive(bool active)
Sets whether the temporal property is active.
Definition: qgstemporalproperty.cpp:27
QgsStringUtils::TitleCase
@ TitleCase
Simple title case conversion - does not fully grammatically parse the text and uses simple rules only...
Definition: qgsstringutils.h:194
QgsUnitTypes::TemporalHours
@ TemporalHours
Hours.
Definition: qgsunittypes.h:154
qgsvectorlayer.h
qgsprojecttimesettings.h
QgsVectorLayerTemporalProperties::mode
TemporalMode mode() const
Returns the temporal properties mode.
Definition: qgsvectorlayertemporalproperties.cpp:203
QgsVectorLayerTemporalProperties::setDurationUnits
void setDurationUnits(QgsUnitTypes::TemporalUnit units)
Sets the units of the event's duration.
Definition: qgsvectorlayertemporalproperties.cpp:399
QgsVectorLayerTemporalProperties::ModeFeatureDateTimeStartAndDurationFromFields
@ ModeFeatureDateTimeStartAndDurationFromFields
Mode when features have a field for start time and a field for event duration.
Definition: qgsvectorlayertemporalproperties.h:94
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:142
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsUnitTypes::TemporalWeeks
@ TemporalWeeks
Weeks.
Definition: qgsunittypes.h:156
QgsVectorLayerTemporalProperties::setMode
void setMode(TemporalMode mode)
Sets the temporal properties mode.
Definition: qgsvectorlayertemporalproperties.cpp:208
QgsVectorLayerTemporalProperties::ModeRedrawLayerOnly
@ ModeRedrawLayerOnly
Redraw the layer when temporal range changes, but don't apply any filtering. Useful when symbology or...
Definition: qgsvectorlayertemporalproperties.h:96
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:339
QgsVectorLayerTemporalProperties::TemporalMode
TemporalMode
Mode of the vector temporal properties.
Definition: qgsvectorlayertemporalproperties.h:89
QgsVectorLayerTemporalProperties::setFixedTemporalRange
void setFixedTemporalRange(const QgsDateTimeRange &range)
Sets a temporal range to apply to the whole layer.
Definition: qgsvectorlayertemporalproperties.cpp:220
QgsVectorLayerTemporalProperties::accumulateFeatures
bool accumulateFeatures() const
Returns true if features will be accumulated over time (i.e.
Definition: qgsvectorlayertemporalproperties.cpp:344
qgsproject.h