|
| 1 | +/*************************************************************************** |
| 2 | + qgsjoindialog.cpp |
| 3 | + -------------------- |
| 4 | + begin : July 10, 2010 |
| 5 | + copyright : (C) 2010 by Marco Hugentobler |
| 6 | + email : marco dot hugentobler at sourcepole dot ch |
| 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 | + |
| 18 | +#include "qgsjoindialog.h" |
| 19 | +#include "qgsmaplayer.h" |
| 20 | +#include "qgsmaplayerregistry.h" |
| 21 | +#include "qgsvectordataprovider.h" |
| 22 | +#include "qgsvectorlayer.h" |
| 23 | +#include "qgsmaplayercombobox.h" |
| 24 | +#include "qgsfieldcombobox.h" |
| 25 | + |
| 26 | +#include <QStandardItemModel> |
| 27 | + |
| 28 | +QgsJoinDialog::QgsJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ) |
| 29 | + : QDialog( parent, f ) |
| 30 | + , mLayer( layer ) |
| 31 | +{ |
| 32 | + setupUi( this ); |
| 33 | + |
| 34 | + if ( !mLayer ) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + mTargetFieldComboBox->setLayer( mLayer ); |
| 40 | + mJoinLayerComboBox->setExceptedLayerList( QList<QgsMapLayer*>() << mLayer ); |
| 41 | + connect( mJoinLayerComboBox, SIGNAL( layerChanged( QgsMapLayer* ) ), mJoinFieldComboBox, SLOT( setLayer( QgsMapLayer* ) ) ); |
| 42 | + connect( mJoinLayerComboBox, SIGNAL( layerChanged( QgsMapLayer* ) ), this, SLOT( joinedLayerChanged( QgsMapLayer* ) ) ); |
| 43 | + connect( mJoinFieldComboBox, SIGNAL( fieldChanged( QString ) ), this, SLOT( allowAttrIndexCreation() ) ); |
| 44 | + |
| 45 | + mCacheInMemoryCheckBox->setChecked( true ); |
| 46 | +} |
| 47 | + |
| 48 | +QgsJoinDialog::~QgsJoinDialog() |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +void QgsJoinDialog::setJoinInfo( const QgsVectorJoinInfo& joinInfo ) |
| 53 | +{ |
| 54 | + mJoinLayerComboBox->setLayer( QgsMapLayerRegistry::instance()->mapLayer( joinInfo.joinLayerId ) ); |
| 55 | + mJoinFieldComboBox->setField( joinInfo.joinFieldName ); |
| 56 | + mTargetFieldComboBox->setField( joinInfo.targetFieldName ); |
| 57 | + mCacheInMemoryCheckBox->setChecked( joinInfo.memoryCache ); |
| 58 | + if ( joinInfo.prefix.isNull() ) |
| 59 | + { |
| 60 | + mUseCustomPrefix->setChecked( false ); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + mUseCustomPrefix->setChecked( true ); |
| 65 | + mCustomPrefix->setText( joinInfo.prefix ); |
| 66 | + } |
| 67 | + |
| 68 | + QStringList* lst = joinInfo.joinFieldNamesSubset(); |
| 69 | + mUseJoinFieldsSubset->setChecked( lst->count() > 0 ); |
| 70 | + QAbstractItemModel* model = mJoinFieldsSubsetView->model(); |
| 71 | + if ( model ) |
| 72 | + { |
| 73 | + for ( int i = 0; i < model->rowCount(); ++i ) |
| 74 | + { |
| 75 | + QModelIndex index = model->index( i, 0 ); |
| 76 | + if ( lst->contains( model->data( index, Qt::DisplayRole ).toString() ) ) |
| 77 | + { |
| 78 | + model->setData( index, Qt::Checked, Qt::CheckStateRole ); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + model->setData( index, Qt::Unchecked, Qt::CheckStateRole ); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +QgsVectorJoinInfo QgsJoinDialog::joinInfo() const |
| 89 | +{ |
| 90 | + QgsVectorJoinInfo info; |
| 91 | + info.joinLayerId = mJoinLayerComboBox->currentLayer()->id(); |
| 92 | + info.joinFieldName = mJoinFieldComboBox->currentField(); |
| 93 | + info.targetFieldName = mTargetFieldComboBox->currentField(); |
| 94 | + info.memoryCache = mCacheInMemoryCheckBox->isChecked(); |
| 95 | + |
| 96 | + if ( mUseCustomPrefix->isChecked() ) |
| 97 | + info.prefix = mCustomPrefix->text(); |
| 98 | + else |
| 99 | + info.prefix = QString::null; |
| 100 | + |
| 101 | + if ( mUseJoinFieldsSubset->isChecked() ) |
| 102 | + { |
| 103 | + QStringList lst; |
| 104 | + QAbstractItemModel* model = mJoinFieldsSubsetView->model(); |
| 105 | + if ( model ) |
| 106 | + { |
| 107 | + for ( int i = 0; i < model->rowCount(); ++i ) |
| 108 | + { |
| 109 | + QModelIndex index = model->index( i, 0 ); |
| 110 | + if ( model->data( index, Qt::CheckStateRole ).toInt() == Qt::Checked ) |
| 111 | + lst << model->data( index ).toString(); |
| 112 | + } |
| 113 | + } |
| 114 | + info.setJoinFieldNamesSubset( new QStringList( lst ) ); |
| 115 | + } |
| 116 | + |
| 117 | + return info; |
| 118 | +} |
| 119 | + |
| 120 | +bool QgsJoinDialog::createAttributeIndex() const |
| 121 | +{ |
| 122 | + return mCreateIndexCheckBox->isChecked(); |
| 123 | +} |
| 124 | + |
| 125 | +void QgsJoinDialog::joinedLayerChanged( QgsMapLayer* layer ) |
| 126 | +{ |
| 127 | + |
| 128 | + mJoinFieldComboBox->clear(); |
| 129 | + |
| 130 | + QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>( layer ); |
| 131 | + if ( !vLayer ) |
| 132 | + { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + mUseJoinFieldsSubset->setChecked( false ); |
| 137 | + QStandardItemModel* subsetModel = new QStandardItemModel( this ); |
| 138 | + const QgsFields& layerFields = vLayer->pendingFields(); |
| 139 | + for ( int idx = 0; idx < layerFields.count(); ++idx ) |
| 140 | + { |
| 141 | + QStandardItem* subsetItem = new QStandardItem( layerFields[idx].name() ); |
| 142 | + subsetItem->setCheckable( true ); |
| 143 | + //subsetItem->setFlags( subsetItem->flags() | Qt::ItemIsUserCheckable ); |
| 144 | + subsetModel->appendRow( subsetItem ); |
| 145 | + } |
| 146 | + mJoinFieldsSubsetView->setModel( subsetModel ); |
| 147 | + |
| 148 | + QgsVectorDataProvider* dp = vLayer->dataProvider(); |
| 149 | + bool canCreateAttrIndex = dp && ( dp->capabilities() & QgsVectorDataProvider::CreateAttributeIndex ); |
| 150 | + if ( canCreateAttrIndex ) |
| 151 | + { |
| 152 | + mCreateIndexCheckBox->setEnabled( true ); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + mCreateIndexCheckBox->setEnabled( false ); |
| 157 | + mCreateIndexCheckBox->setChecked( false ); |
| 158 | + } |
| 159 | + |
| 160 | + if ( !mUseCustomPrefix->isChecked() ) |
| 161 | + { |
| 162 | + mCustomPrefix->setText( layer->name() + "_" ); |
| 163 | + } |
| 164 | +} |
0 commit comments