|
| 1 | +/*************************************************************************** |
| 2 | + qgsrelationreferencesearchwidgetwrapper.cpp |
| 3 | + ------------------------------------------ |
| 4 | + Date : 2016-05-25 |
| 5 | + Copyright : (C) 2016 Nyall Dawson |
| 6 | + Email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsrelationreferencesearchwidgetwrapper.h" |
| 17 | + |
| 18 | +#include "qgsfield.h" |
| 19 | +#include "qgsmaplayerregistry.h" |
| 20 | +#include "qgsvaluerelationwidgetfactory.h" |
| 21 | +#include "qgsvectorlayer.h" |
| 22 | +#include "qgsproject.h" |
| 23 | + |
| 24 | +#include <QSettings> |
| 25 | +#include <QStringListModel> |
| 26 | + |
| 27 | +QgsRelationReferenceSearchWidgetWrapper::QgsRelationReferenceSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QgsMapCanvas* canvas, QWidget* parent ) |
| 28 | + : QgsSearchWidgetWrapper( vl, fieldIdx, parent ) |
| 29 | + , mWidget( nullptr ) |
| 30 | + , mLayer( nullptr ) |
| 31 | + , mCanvas( canvas ) |
| 32 | +{ |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +bool QgsRelationReferenceSearchWidgetWrapper::applyDirectly() |
| 37 | +{ |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +QString QgsRelationReferenceSearchWidgetWrapper::expression() |
| 42 | +{ |
| 43 | + return mExpression; |
| 44 | +} |
| 45 | + |
| 46 | +QVariant QgsRelationReferenceSearchWidgetWrapper::value() const |
| 47 | +{ |
| 48 | + if ( mWidget ) |
| 49 | + { |
| 50 | + return mWidget->foreignKey(); |
| 51 | + } |
| 52 | + return QVariant(); |
| 53 | +} |
| 54 | + |
| 55 | +QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::supportedFlags() const |
| 56 | +{ |
| 57 | + return EqualTo | NotEqualTo | IsNull | IsNotNull; |
| 58 | +} |
| 59 | + |
| 60 | +QgsSearchWidgetWrapper::FilterFlags QgsRelationReferenceSearchWidgetWrapper::defaultFlags() const |
| 61 | +{ |
| 62 | + return EqualTo; |
| 63 | +} |
| 64 | + |
| 65 | +QString QgsRelationReferenceSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const |
| 66 | +{ |
| 67 | + QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() ); |
| 68 | + |
| 69 | + //clear any unsupported flags |
| 70 | + flags &= supportedFlags(); |
| 71 | + if ( flags & IsNull ) |
| 72 | + return fieldName + " IS NULL"; |
| 73 | + if ( flags & IsNotNull ) |
| 74 | + return fieldName + " IS NOT NULL"; |
| 75 | + |
| 76 | + QVariant v = value(); |
| 77 | + if ( !v.isValid() ) |
| 78 | + return QString(); |
| 79 | + |
| 80 | + switch ( v.type() ) |
| 81 | + { |
| 82 | + case QVariant::Int: |
| 83 | + case QVariant::UInt: |
| 84 | + case QVariant::Double: |
| 85 | + case QVariant::LongLong: |
| 86 | + case QVariant::ULongLong: |
| 87 | + { |
| 88 | + if ( flags & EqualTo ) |
| 89 | + return fieldName + '=' + v.toString(); |
| 90 | + else if ( flags & NotEqualTo ) |
| 91 | + return fieldName + "<>" + v.toString(); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + default: |
| 96 | + { |
| 97 | + if ( flags & EqualTo ) |
| 98 | + return fieldName + "='" + v.toString() + '\''; |
| 99 | + else if ( flags & NotEqualTo ) |
| 100 | + return fieldName + "<>'" + v.toString() + '\''; |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return QString(); |
| 106 | +} |
| 107 | + |
| 108 | +void QgsRelationReferenceSearchWidgetWrapper::clearWidget() |
| 109 | +{ |
| 110 | + if ( mWidget ) |
| 111 | + { |
| 112 | + mWidget->showIndeterminateState(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void QgsRelationReferenceSearchWidgetWrapper::setEnabled( bool enabled ) |
| 117 | +{ |
| 118 | + if ( mWidget ) |
| 119 | + { |
| 120 | + mWidget->setEnabled( enabled ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +bool QgsRelationReferenceSearchWidgetWrapper::valid() const |
| 125 | +{ |
| 126 | + return true; |
| 127 | +} |
| 128 | + |
| 129 | +void QgsRelationReferenceSearchWidgetWrapper::onValueChanged( QVariant value ) |
| 130 | +{ |
| 131 | + if ( !value.isValid() ) |
| 132 | + { |
| 133 | + clearExpression(); |
| 134 | + emit valueCleared(); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + QSettings settings; |
| 139 | + setExpression( value.isNull() ? settings.value( "qgis/nullValue", "NULL" ).toString() : value.toString() ); |
| 140 | + emit valueChanged(); |
| 141 | + } |
| 142 | + emit expressionChanged( mExpression ); |
| 143 | +} |
| 144 | + |
| 145 | +void QgsRelationReferenceSearchWidgetWrapper::setExpression( QString exp ) |
| 146 | +{ |
| 147 | + QSettings settings; |
| 148 | + QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString(); |
| 149 | + QString fieldName = layer()->fields().at( mFieldIdx ).name(); |
| 150 | + |
| 151 | + QString str; |
| 152 | + if ( exp == nullValue ) |
| 153 | + { |
| 154 | + str = QString( "%1 IS NULL" ).arg( QgsExpression::quotedColumnRef( fieldName ) ); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + str = QString( "%1 = '%3'" ) |
| 159 | + .arg( QgsExpression::quotedColumnRef( fieldName ), |
| 160 | + exp.replace( '\'', "''" ) |
| 161 | + ); |
| 162 | + } |
| 163 | + mExpression = str; |
| 164 | +} |
| 165 | + |
| 166 | +QWidget* QgsRelationReferenceSearchWidgetWrapper::createWidget( QWidget* parent ) |
| 167 | +{ |
| 168 | + return new QgsRelationReferenceWidget( parent ); |
| 169 | +} |
| 170 | + |
| 171 | +void QgsRelationReferenceSearchWidgetWrapper::initWidget( QWidget* editor ) |
| 172 | +{ |
| 173 | + mWidget = qobject_cast<QgsRelationReferenceWidget*>( editor ); |
| 174 | + if ( !mWidget ) |
| 175 | + return; |
| 176 | + |
| 177 | + mWidget->setEditorContext( context(), mCanvas, nullptr ); |
| 178 | + |
| 179 | + mWidget->setEmbedForm( false ); |
| 180 | + mWidget->setReadOnlySelector( false ); |
| 181 | + mWidget->setAllowMapIdentification( config( "MapIdentification", false ).toBool() ); |
| 182 | + mWidget->setOrderByValue( config( "OrderByValue", false ).toBool() ); |
| 183 | + mWidget->setAllowAddFeatures( false ); |
| 184 | + mWidget->setOpenFormButtonVisible( false ); |
| 185 | + |
| 186 | + QgsRelation relation = QgsProject::instance()->relationManager()->relation( config( "Relation" ).toString() ); |
| 187 | + mWidget->setRelation( relation, false ); |
| 188 | + |
| 189 | + mWidget->showIndeterminateState(); |
| 190 | + connect( mWidget, SIGNAL( foreignKeyChanged( QVariant ) ), this, SLOT( onValueChanged( QVariant ) ) ); |
| 191 | +} |
| 192 | + |
| 193 | + |
0 commit comments