• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MerginMaps / input / 6569946352

19 Oct 2023 04:38AM UTC coverage: 62.291% (-0.07%) from 62.358%
6569946352

push

github

web-flow
Merge pull request #2860 from MerginMaps/iss_2058_photorelation

fix photo in relation preview

7688 of 12342 relevant lines covered (62.29%)

105.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.47
/app/relationfeaturesmodel.cpp
1
/***************************************************************************
2
 *                                                                         *
3
 *   This program is free software; you can redistribute it and/or modify  *
4
 *   it under the terms of the GNU General Public License as published by  *
5
 *   the Free Software Foundation; either version 2 of the License, or     *
6
 *   (at your option) any later version.                                   *
7
 *                                                                         *
8
 ***************************************************************************/
9

10
#include "relationfeaturesmodel.h"
11
#include "qgsvectorlayer.h"
12
#include "inpututils.h"
13

14
#include "qgsproject.h"
15

16
RelationFeaturesModel::RelationFeaturesModel( QObject *parent )
2✔
17
  : FeaturesModel( parent )
2✔
18
{
19
}
2✔
20

21
QVariant RelationFeaturesModel::data( const QModelIndex &index, int role ) const
1✔
22
{
23
  int row = index.row();
1✔
24
  if ( row < 0 || row >= FeaturesModel::rowCount() )
1✔
25
    return QVariant();
×
26

27
  if ( !index.isValid() )
1✔
28
    return QVariant();
×
29

30
  if ( role == PhotoPath )
1✔
31
  {
32
    const FeatureLayerPair pair = FeaturesModel::data( index, FeaturesModel::FeaturePair ).value<FeatureLayerPair>();
×
33
    return relationPhotoPath( pair );
×
34
  }
×
35
  else
36
    return FeaturesModel::data( index, role );
1✔
37
}
38

39
QHash<int, QByteArray> RelationFeaturesModel::roleNames() const
×
40
{
41
  QHash<int, QByteArray> roles = FeaturesModel::roleNames();
×
42
  roles[PhotoPath] = QStringLiteral( "PhotoPath" ).toLatin1();
×
43

44
  return roles;
×
45
}
×
46

47
void RelationFeaturesModel::setup()
3✔
48
{
49
  if ( !mRelation.isValid() || !mParentFeatureLayerPair.isValid() )
3✔
50
    return;
2✔
51

52
  setIsTextType( photoFieldIndex( mRelation.referencingLayer() ) == -1 );
1✔
53

54
  QObject::connect( mRelation.referencingLayer(), &QgsVectorLayer::afterCommitChanges, this, &RelationFeaturesModel::populate );
1✔
55

56
  FeaturesModel::setLayer( mRelation.referencingLayer() );
1✔
57
  populate();
1✔
58
}
59

60
void RelationFeaturesModel::setupFeatureRequest( QgsFeatureRequest &request )
7✔
61
{
62
  FeaturesModel::setupFeatureRequest( request );
7✔
63

64
  QgsFeatureRequest e = mRelation.getRelatedFeaturesRequest( mParentFeatureLayerPair.feature() );
7✔
65
  request.combineFilterExpression( e.filterExpression()->operator QString() );
7✔
66
}
7✔
67

68
void RelationFeaturesModel::setParentFeatureLayerPair( FeatureLayerPair pair )
2✔
69
{
70
  if ( mParentFeatureLayerPair == pair )
2✔
71
    return;
×
72

73
  mParentFeatureLayerPair = pair;
2✔
74
  emit parentFeatureLayerPairChanged( mParentFeatureLayerPair );
2✔
75

76
  if ( !InputUtils::isFeatureIdValid( pair.feature().id() ) )
2✔
77
  {
78
    //
79
    // Clear the model in case parent feature has invalid id (e.g. is new) and do not populate it
80
    //
81

82
    beginResetModel();
1✔
83
    reset();
1✔
84
    endResetModel();
1✔
85
  }
86
  else
87
  {
88
    setup();
1✔
89
  }
90
}
91

92
void RelationFeaturesModel::setRelation( QgsRelation relation )
2✔
93
{
94
  if ( mRelation.id() != relation.id() )
2✔
95
  {
96
    mRelation = relation;
2✔
97
    emit relationChanged( mRelation );
2✔
98

99
    setup();
2✔
100
  }
101
}
2✔
102

103
FeatureLayerPair RelationFeaturesModel::parentFeatureLayerPair() const
×
104
{
105
  return mParentFeatureLayerPair;
×
106
}
107

108
QgsRelation RelationFeaturesModel::relation() const
×
109
{
110
  return mRelation;
×
111
}
112

113
QVariant RelationFeaturesModel::relationPhotoPath( const FeatureLayerPair &featurePair ) const
×
114
{
115
  int fieldIndex = photoFieldIndex( featurePair.layer() );
×
116
  QgsEditorWidgetSetup setup = featurePair.layer()->editorWidgetSetup( fieldIndex );
×
117
  QVariantMap config = setup.config();
×
118

119
  const QgsFeature feature = featurePair.feature();
×
120
  QString path = feature.attribute( fieldIndex ).toString();
×
121

122
  QString finalPath = InputUtils::resolvePath( path, homePath(), config, featurePair, QgsProject::instance() );
×
123

124
  return QVariant( finalPath );
×
125
}
×
126

127
int RelationFeaturesModel::photoFieldIndex( QgsVectorLayer *layer ) const
1✔
128
{
129
  if ( !layer )
1✔
130
  {
131
    return -1;
×
132
  }
133

134
  QgsFields fields = layer->fields();
1✔
135
  for ( int i = 0; i < fields.size(); i++ )
5✔
136
  {
137
    // Lets try by widget type
138
    QgsEditorWidgetSetup setup = layer->editorWidgetSetup( i );
4✔
139
    if ( setup.type() == QStringLiteral( "ExternalResource" ) )
4✔
140
    {
141
      return i;
×
142
    }
143
  }
4✔
144
  return -1;
1✔
145
}
1✔
146

147
bool RelationFeaturesModel::isTextType() const
×
148
{
149
  return mIsTextType;
×
150
}
151

152
void RelationFeaturesModel::setIsTextType( bool isTextType )
1✔
153
{
154
  if ( isTextType != mIsTextType )
1✔
155
  {
156
    mIsTextType = isTextType;
×
157
    emit isTextTypeChanged();
×
158
  }
159
}
1✔
160

161
QString RelationFeaturesModel::homePath() const
×
162
{
163
  return mHomePath;
×
164
}
165

166
void RelationFeaturesModel::setHomePath( const QString &homePath )
×
167
{
168
  if ( homePath != mHomePath )
×
169
  {
170
    mHomePath = homePath;
×
171
    emit homePathChanged();
×
172
  }
173
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc