valuerelation.diff

Jürgen Fischer, 2011-04-10 01:38 PM

Download (18 KB)

View differences:

src/app/qgsattributetypedialog.h (working copy)
78 78
    void setCheckedState( QString checked, QString unchecked );
79 79

  
80 80
    /**
81
     * Setter to value relation to be displayed and edited in this dialog
82
     * @param valueRelation value relation data which is to be displayed
83
     */
84
    void setValueRelation( QgsVectorLayer::ValueRelationData valueRelationData );
85

  
86
    /**
81 87
     * Getter for checked state after editing
82 88
     * @return string representing the checked
83 89
     */
......
95 101
     */
96 102
    QgsVectorLayer::RangeData rangeData();
97 103

  
104
    /**
105
     * Getter for value relation data
106
     */
107
    QgsVectorLayer::ValueRelationData valueRelationData();
108

  
98 109
  private slots:
99 110
    /**
100 111
     * Slot to handle change of index in combobox to select correct page
......
124 135
     */
125 136
    void vCellChanged( int row, int column );
126 137

  
138
    /**
139
     * update columns list
140
     */
141
    void updateLayerColumns( int idx );
142

  
127 143
  private:
128 144

  
129 145
    QString defaultWindowTitle();
......
147 163
    int mIndex;
148 164

  
149 165
    QgsVectorLayer::RangeData mRangeData;
166
    QgsVectorLayer::ValueRelationData mValueRelationData;
150 167
    QgsVectorLayer::EditType mEditType;
151 168
};
152 169

  
src/app/qgsvectorlayerproperties.h (working copy)
173 173
    QMap<int, QgsVectorLayer::EditType> mEditTypeMap;
174 174
    QMap<int, QMap<QString, QVariant> > mValueMaps;
175 175
    QMap<int, QgsVectorLayer::RangeData> mRanges;
176
    QMap<int, QgsVectorLayer::ValueRelationData> mValueRelationData;
176 177
    QMap<int, QPair<QString, QString> > mCheckedStates;
177 178

  
178 179
    QFont mDiagramFont;
src/app/qgsvectorlayerproperties.cpp (working copy)
245 245

  
246 246
  attributeTypeDialog.setValueMap( mValueMaps.value( index, layer->valueMap( index ) ) );
247 247
  attributeTypeDialog.setRange( mRanges.value( index, layer->range( index ) ) );
248
  attributeTypeDialog.setValueRelation( mValueRelationData.value( index, layer->valueRelation( index ) ) );
248 249

  
249 250
  QPair<QString, QString> checkStates = mCheckedStates.value( index, layer->checkedState( index ) );
250 251
  attributeTypeDialog.setCheckedState( checkStates.first, checkStates.second );
......
272 273
    case QgsVectorLayer::CheckBox:
273 274
      mCheckedStates.insert( index, attributeTypeDialog.checkedState() );
274 275
      break;
276
    case QgsVectorLayer::ValueRelation:
277
      mValueRelationData.insert( index, attributeTypeDialog.valueRelationData() );
278
      break;
275 279
    case QgsVectorLayer::LineEdit:
276 280
    case QgsVectorLayer::TextEdit:
277 281
    case QgsVectorLayer::UniqueValues:
......
554 558
  editTypeMap.insert( QgsVectorLayer::CheckBox, tr( "Checkbox" ) );
555 559
  editTypeMap.insert( QgsVectorLayer::TextEdit, tr( "Text edit" ) );
556 560
  editTypeMap.insert( QgsVectorLayer::Calendar, tr( "Calendar" ) );
561
  editTypeMap.insert( QgsVectorLayer::ValueRelation, tr( "Value relation" ) );
557 562
}
558 563

  
559 564
QString QgsVectorLayerProperties::editTypeButtonText( QgsVectorLayer::EditType type )
......
635 640
        }
636 641
        break;
637 642

  
643
      case QgsVectorLayer::ValueRelation:
644
        if ( mValueRelationData.contains( idx ) )
645
        {
646
          layer->valueRelation( idx ) = mValueRelationData[idx];
647
        }
648
        break;
649

  
638 650
      case QgsVectorLayer::LineEdit:
639 651
      case QgsVectorLayer::UniqueValues:
640 652
      case QgsVectorLayer::UniqueValuesEditable:
src/app/qgsattributetypedialog.cpp (working copy)
19 19
#include "qgsattributetypedialog.h"
20 20
#include "qgsattributetypeloaddialog.h"
21 21
#include "qgsvectordataprovider.h"
22
#include "qgsmaplayerregistry.h"
22 23

  
23 24
#include "qgslogger.h"
24 25

  
......
42 43
  connect( loadFromLayerButton, SIGNAL( clicked() ), this, SLOT( loadFromLayerButtonPushed() ) );
43 44
  connect( loadFromCSVButton, SIGNAL( clicked() ), this, SLOT( loadFromCSVButtonPushed() ) );
44 45
  connect( tableWidget, SIGNAL( cellChanged( int, int ) ), this, SLOT( vCellChanged( int, int ) ) );
46

  
47
  valueRelationLayer->clear();
48
  foreach( QgsMapLayer *l, QgsMapLayerRegistry::instance()->mapLayers() )
49
  {
50
    QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
51
    if ( vl )
52
      valueRelationLayer->addItem( vl->name(), vl->id() );
53
  }
54

  
55
  connect( valueRelationLayer, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateLayerColumns( int ) ) );
56
  valueRelationLayer->setCurrentIndex( -1 );
45 57
}
46 58

  
47 59
QgsAttributeTypeDialog::~QgsAttributeTypeDialog()
......
59 71
  return mRangeData;
60 72
}
61 73

  
74
QgsVectorLayer::ValueRelationData QgsAttributeTypeDialog::valueRelationData()
75
{
76
  return mValueRelationData;
77
}
78

  
62 79
QMap<QString, QVariant> &QgsAttributeTypeDialog::valueMap()
63 80
{
64 81
  return mValueMap;
......
259 276
    case QgsVectorLayer::Calendar:
260 277
      setPage( 11 );
261 278
      break;
279

  
280
    case QgsVectorLayer::ValueRelation:
281
      setPage( 12 );
282
      break;
262 283
  }
263 284
}
264 285

  
......
272 293
  mRangeData = range;
273 294
}
274 295

  
296
void QgsAttributeTypeDialog::setValueRelation( QgsVectorLayer::ValueRelationData valueRelation )
297
{
298
  mValueRelationData = valueRelation;
299
}
300

  
275 301
void QgsAttributeTypeDialog::setIndex( int index, QgsVectorLayer::EditType editType )
276 302
{
277 303
  mIndex = index;
......
397 423
      editableUniqueValues->setChecked( editType == QgsVectorLayer::UniqueValuesEditable );
398 424
      break;
399 425

  
426
    case QgsVectorLayer::ValueRelation:
427
      valueRelationLayer->setCurrentIndex( valueRelationLayer->findData( mValueRelationData.mLayer ) );
428
      valueRelationKeyColumn->setCurrentIndex( valueRelationKeyColumn->findText( mValueRelationData.mKey ) );
429
      valueRelationValueColumn->setCurrentIndex( valueRelationValueColumn->findText( mValueRelationData.mValue ) );
430
      valueRelationAllowNull->setChecked( mValueRelationData.mAllowNull );
431
      break;
432

  
400 433
    case QgsVectorLayer::LineEdit:
401 434
    case QgsVectorLayer::UniqueValues:
402 435
    case QgsVectorLayer::Classification:
......
559 592
    case 11:
560 593
      mEditType = QgsVectorLayer::Calendar;
561 594
      break;
595
    case 12:
596
      mEditType = QgsVectorLayer::ValueRelation;
597
      mValueRelationData.mLayer = valueRelationLayer->itemData( valueRelationLayer->currentIndex() ).toString();
598
      mValueRelationData.mKey = valueRelationKeyColumn->currentText();
599
      mValueRelationData.mValue = valueRelationValueColumn->currentText();
600
      mValueRelationData.mAllowNull = valueRelationAllowNull->isChecked();
601
      break;
562 602
  }
563 603

  
564 604
  QDialog::accept();
......
568 608
{
569 609
  return tr( "Attribute Edit Dialog" );
570 610
}
611

  
612
void QgsAttributeTypeDialog::updateLayerColumns( int idx )
613
{
614
  valueRelationKeyColumn->clear();
615
  valueRelationValueColumn->clear();
616

  
617
  QString id = valueRelationLayer->itemData( idx ).toString();
618

  
619
  QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
620
  if ( !vl )
621
    return;
622

  
623
  foreach( const QgsField &f, vl->pendingFields() )
624
  {
625
    valueRelationKeyColumn->addItem( f.name() );
626
    valueRelationValueColumn->addItem( f.name() );
627
  }
628

  
629
  valueRelationKeyColumn->setCurrentIndex( valueRelationKeyColumn->findText( mValueRelationData.mKey ) );
630
  valueRelationValueColumn->setCurrentIndex( valueRelationValueColumn->findText( mValueRelationData.mValue ) );
631
}
src/gui/qgsattributeeditor.cpp (working copy)
24 24
#include <qgssymbol.h>
25 25
#include <qgslonglongvalidator.h>
26 26
#include <qgsfieldvalidator.h>
27
#include <qgsmaplayerregistry.h>
27 28

  
28 29
#include <QPushButton>
29 30
#include <QLineEdit>
......
174 175
    }
175 176
    break;
176 177

  
178
    case QgsVectorLayer::ValueRelation:
179
    {
180
      QSettings settings;
181
      QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
182

  
183
      const QgsVectorLayer::ValueRelationData &data = vl->valueRelation( idx );
184

  
185
      QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( data.mLayer ) );
186
      QMap< QString, QString > map;
187
      if ( layer )
188
      {
189
        int ki = layer->fieldNameIndex( data.mKey );
190
        int vi = layer->fieldNameIndex( data.mValue );
191

  
192
        if ( data.mAllowNull )
193
          map.insert( nullValue, tr( "(no selection)" ) );
194

  
195
        if ( ki >= 0 && vi >= 0 )
196
        {
197
          layer->select( QgsAttributeList() << ki << vi, QgsRectangle(), false );
198
          QgsFeature f;
199
          while ( layer->nextFeature( f ) )
200
          {
201
            map.insert( f.attributeMap()[ ki ].toString(), f.attributeMap()[ vi ].toString() );
202
          }
203
        }
204
      }
205

  
206
      QComboBox *cb = comboBox( editor, parent );
207
      if ( cb )
208
      {
209
        for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
210
        {
211
          cb->addItem( it.value(), it.key() );
212
        }
213

  
214
        myWidget = cb;
215
      }
216
    }
217
    break;
218

  
177 219
    case QgsVectorLayer::Classification:
178 220
    {
179 221
      QMap<QString, QString> classes;
......
482 524
  {
483 525
    if ( editType == QgsVectorLayer::UniqueValues ||
484 526
         editType == QgsVectorLayer::ValueMap ||
485
         editType == QgsVectorLayer::Classification )
527
         editType == QgsVectorLayer::Classification ||
528
	 editType == QgsVectorLayer::ValueRelation )
486 529
    {
487 530
      text = cb->itemData( cb->currentIndex() ).toString();
488 531
      if ( text == nullValue )
......
621 664
    case QgsVectorLayer::UniqueValues:
622 665
    case QgsVectorLayer::Enumeration:
623 666
    case QgsVectorLayer::ValueMap:
667
    case QgsVectorLayer::ValueRelation:
624 668
    {
625 669
      QVariant v = value;
626 670
      QComboBox *cb = qobject_cast<QComboBox *>( editor );
src/core/qgsvectorlayer.cpp (working copy)
3013 3013
      {
3014 3014
        mCheckedStates[ name ] = QPair<QString, QString>( editTypeElement.attribute( "checked" ), editTypeElement.attribute( "unchecked" ) );
3015 3015
      }
3016
      else if ( editType == ValueRelation )
3017
      {
3018
        QString id = editTypeElement.attribute( "layer" );
3019
        QString key = editTypeElement.attribute( "key" );
3020
        QString value = editTypeElement.attribute( "value" );
3021
        bool allowNull = editTypeElement.attribute( "allowNull" ) == "true";
3022
        mValueRelations[ name ] = ValueRelationData( id, key, value, allowNull );
3023
      }
3016 3024
    }
3017 3025
  }
3018 3026

  
......
3208 3216
          }
3209 3217
          break;
3210 3218

  
3219
        case ValueRelation:
3220
          if ( mValueRelations.contains( it.key() ) )
3221
          {
3222
            const ValueRelationData &data = mValueRelations[ it.key() ];
3223
            editTypeElement.setAttribute( "layer", data.mLayer );
3224
            editTypeElement.setAttribute( "key", data.mKey );
3225
            editTypeElement.setAttribute( "value", data.mValue );
3226
            editTypeElement.setAttribute( "allowNull", data.mAllowNull ? "true" : "false" );
3227
          }
3228
          break;
3229

  
3211 3230
        case LineEdit:
3212 3231
        case UniqueValues:
3213 3232
        case UniqueValuesEditable:
......
5256 5275
    mDiagramLayerSettings = new QgsDiagramLayerSettings();
5257 5276
  *mDiagramLayerSettings = s;
5258 5277
}
5278

  
5279
QgsVectorLayer::ValueRelationData &QgsVectorLayer::valueRelation( int idx )
5280
{
5281
  const QgsFieldMap &fields = pendingFields();
5282

  
5283
  // FIXME: throw an exception!?
5284
  if ( fields.contains( idx ) )
5285
  {
5286
    QgsDebugMsg( QString( "field %1 not found" ).arg( idx ) );
5287
  }
5288

  
5289
  if ( !mValueRelations.contains( fields[idx].name() ) )
5290
  {
5291
    mValueRelations[ fields[idx].name()] = ValueRelationData();
5292
  }
5293

  
5294
  return mValueRelations[ fields[idx].name()];
5295
}
src/core/qgsvectorlayer.h (working copy)
98 98
      Classification,
99 99
      EditRange,
100 100
      SliderRange,
101
      CheckBox,    /* added in 1.4 */
101
      CheckBox,      /* added in 1.4 */
102 102
      FileName,
103 103
      Enumeration,
104
      Immutable,   /* The attribute value should not be changed in the attribute form*/
105
      Hidden,      /* The attribute value should not be shown in the attribute form @added in 1.4 */
106
      TextEdit,    /* multiline edit @added in 1.4*/
107
      Calendar,    /* calendar widget @added in 1.5 */
108
      DialRange,   /* dial range @added in 1.5 */
104
      Immutable,     /* The attribute value should not be changed in the attribute form*/
105
      Hidden,        /* The attribute value should not be shown in the attribute form @added in 1.4 */
106
      TextEdit,      /* multiline edit @added in 1.4*/
107
      Calendar,      /* calendar widget @added in 1.5 */
108
      DialRange,     /* dial range @added in 1.5 */
109
      ValueRelation, /* value map from an table @added in 1.8 */
109 110
    };
110 111

  
111 112
    struct RangeData
......
119 120
      QVariant mStep;
120 121
    };
121 122

  
123
    struct ValueRelationData
124
    {
125
      ValueRelationData() {}
126
      ValueRelationData( QString layer, QString key, QString value, bool allowNull )
127
          : mLayer( layer ), mKey( key ), mValue( value ), mAllowNull( allowNull ) {}
128

  
129
      QString mLayer;
130
      QString mKey;
131
      QString mValue;
132
      bool mAllowNull;
133
    };
134

  
122 135
    /** Constructor */
123 136
    QgsVectorLayer( QString path = QString::null, QString baseName = QString::null,
124 137
                    QString providerLib = QString::null, bool loadDefaultStyleFlag = true );
......
587 600
    /**access range */
588 601
    RangeData &range( int idx );
589 602

  
603
    /**access relations
604
     * @note added in 1.8
605
     **/
606
    ValueRelationData &valueRelation( int idx );
607

  
590 608
    /**Adds a new overlay to this class. QgsVectorLayer takes ownership of the object
591 609
    @note this method was added in version 1.1
592 610
    */
......
928 946
    QMap< QString, QMap<QString, QVariant> > mValueMaps;
929 947
    QMap< QString, RangeData > mRanges;
930 948
    QMap< QString, QPair<QString, QString> > mCheckedStates;
949
    QMap< QString, ValueRelationData > mValueRelations;
931 950

  
932 951
    QString mEditForm, mEditFormInit;
933 952
    //annotation form for this layer
src/ui/qgsattributetypeedit.ui (working copy)
76 76
       <string>Calendar</string>
77 77
      </property>
78 78
     </item>
79
     <item>
80
      <property name="text">
81
       <string>Value relation</string>
82
      </property>
83
     </item>
79 84
    </widget>
80 85
   </item>
81 86
   <item>
......
87 92
      </sizepolicy>
88 93
     </property>
89 94
     <property name="currentIndex">
90
      <number>0</number>
95
      <number>12</number>
91 96
     </property>
92 97
     <widget class="QWidget" name="lineEditPage">
93 98
      <layout class="QVBoxLayout" name="verticalLayout_1">
......
591 596
       </item>
592 597
      </layout>
593 598
     </widget>
599
     <widget class="QWidget" name="page">
600
      <layout class="QGridLayout" name="gridLayout_3">
601
       <item row="1" column="0">
602
        <widget class="QLabel" name="label_5">
603
         <property name="text">
604
          <string>Layer</string>
605
         </property>
606
         <property name="buddy">
607
          <cstring>valueRelationLayer</cstring>
608
         </property>
609
        </widget>
610
       </item>
611
       <item row="1" column="1">
612
        <widget class="QComboBox" name="valueRelationLayer"/>
613
       </item>
614
       <item row="2" column="0">
615
        <widget class="QLabel" name="label_6">
616
         <property name="text">
617
          <string>Key column</string>
618
         </property>
619
         <property name="buddy">
620
          <cstring>valueRelationKeyColumn</cstring>
621
         </property>
622
        </widget>
623
       </item>
624
       <item row="2" column="1">
625
        <widget class="QComboBox" name="valueRelationKeyColumn"/>
626
       </item>
627
       <item row="3" column="0">
628
        <widget class="QLabel" name="label_7">
629
         <property name="text">
630
          <string>Value column</string>
631
         </property>
632
         <property name="buddy">
633
          <cstring>valueRelationValueColumn</cstring>
634
         </property>
635
        </widget>
636
       </item>
637
       <item row="3" column="1">
638
        <widget class="QComboBox" name="valueRelationValueColumn"/>
639
       </item>
640
       <item row="0" column="0" colspan="2">
641
        <widget class="QLabel" name="label_8">
642
         <property name="text">
643
          <string>Select layer, key column and value column</string>
644
         </property>
645
        </widget>
646
       </item>
647
       <item row="5" column="0" colspan="2">
648
        <spacer name="verticalSpacer_10">
649
         <property name="orientation">
650
          <enum>Qt::Vertical</enum>
651
         </property>
652
         <property name="sizeHint" stdset="0">
653
          <size>
654
           <width>20</width>
655
           <height>40</height>
656
          </size>
657
         </property>
658
        </spacer>
659
       </item>
660
       <item row="4" column="0" colspan="2">
661
        <widget class="QCheckBox" name="valueRelationAllowNull">
662
         <property name="layoutDirection">
663
          <enum>Qt::RightToLeft</enum>
664
         </property>
665
         <property name="text">
666
          <string>Allow null value</string>
667
         </property>
668
        </widget>
669
       </item>
670
      </layout>
671
     </widget>
594 672
    </widget>
595 673
   </item>
596 674
   <item>
......
614 692
   <slot>accept()</slot>
615 693
   <hints>
616 694
    <hint type="sourcelabel">
617
     <x>248</x>
618
     <y>254</y>
695
     <x>257</x>
696
     <y>418</y>
619 697
    </hint>
620 698
    <hint type="destinationlabel">
621 699
     <x>157</x>
......
630 708
   <slot>reject()</slot>
631 709
   <hints>
632 710
    <hint type="sourcelabel">
633
     <x>316</x>
634
     <y>260</y>
711
     <x>325</x>
712
     <y>418</y>
635 713
    </hint>
636 714
    <hint type="destinationlabel">
637 715
     <x>286</x>