|
| 1 | +/*************************************************************************** |
| 2 | + qgscheckboxsearchwidgetwrapper.cpp |
| 3 | + --------------------------------- |
| 4 | + Date : 2016-05-23 |
| 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 "qgscheckboxsearchwidgetwrapper.h" |
| 17 | + |
| 18 | +#include "qgsfield.h" |
| 19 | +#include "qgscheckboxwidgetfactory.h" |
| 20 | +#include "qgsvectorlayer.h" |
| 21 | + |
| 22 | +#include <QSettings> |
| 23 | +#include <QCheckBox> |
| 24 | + |
| 25 | +QgsCheckboxSearchWidgetWrapper::QgsCheckboxSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) |
| 26 | + : QgsSearchWidgetWrapper( vl, fieldIdx, parent ) |
| 27 | + , mCheckBox( nullptr ) |
| 28 | + , mLayer( nullptr ) |
| 29 | +{ |
| 30 | +} |
| 31 | + |
| 32 | +bool QgsCheckboxSearchWidgetWrapper::applyDirectly() |
| 33 | +{ |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +QString QgsCheckboxSearchWidgetWrapper::expression() |
| 38 | +{ |
| 39 | + return mExpression; |
| 40 | +} |
| 41 | + |
| 42 | +QVariant QgsCheckboxSearchWidgetWrapper::value() const |
| 43 | +{ |
| 44 | + QVariant v; |
| 45 | + |
| 46 | + if ( mCheckBox ) |
| 47 | + v = mCheckBox->isChecked() ? config( "CheckedState" ) : config( "UncheckedState" ); |
| 48 | + |
| 49 | + return v; |
| 50 | +} |
| 51 | + |
| 52 | +QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::supportedFlags() const |
| 53 | +{ |
| 54 | + return EqualTo | IsNull; |
| 55 | +} |
| 56 | + |
| 57 | +QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::defaultFlags() const |
| 58 | +{ |
| 59 | + return EqualTo; |
| 60 | +} |
| 61 | + |
| 62 | +QString QgsCheckboxSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const |
| 63 | +{ |
| 64 | + QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type(); |
| 65 | + QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() ); |
| 66 | + |
| 67 | + //clear any unsupported flags |
| 68 | + flags &= supportedFlags(); |
| 69 | + if ( flags & IsNull ) |
| 70 | + return fieldName + " IS NULL"; |
| 71 | + |
| 72 | + QVariant v = value(); |
| 73 | + if ( !v.isValid() ) |
| 74 | + return QString(); |
| 75 | + |
| 76 | + switch ( fldType ) |
| 77 | + { |
| 78 | + case QVariant::Int: |
| 79 | + case QVariant::UInt: |
| 80 | + case QVariant::Double: |
| 81 | + case QVariant::LongLong: |
| 82 | + case QVariant::ULongLong: |
| 83 | + { |
| 84 | + if ( flags & EqualTo ) |
| 85 | + return fieldName + '=' + v.toString(); |
| 86 | + else if ( flags & NotEqualTo ) |
| 87 | + return fieldName + "<>" + v.toString(); |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + default: |
| 92 | + { |
| 93 | + if ( flags & EqualTo ) |
| 94 | + return fieldName + "='" + v.toString() + '\''; |
| 95 | + else if ( flags & NotEqualTo ) |
| 96 | + return fieldName + "<>'" + v.toString() + '\''; |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return QString(); |
| 102 | +} |
| 103 | + |
| 104 | +void QgsCheckboxSearchWidgetWrapper::clearWidget() |
| 105 | +{ |
| 106 | + if ( mCheckBox ) |
| 107 | + { |
| 108 | + whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void QgsCheckboxSearchWidgetWrapper::setEnabled( bool enabled ) |
| 113 | +{ |
| 114 | + if ( mCheckBox ) |
| 115 | + { |
| 116 | + mCheckBox->setEnabled( enabled ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +bool QgsCheckboxSearchWidgetWrapper::valid() const |
| 121 | +{ |
| 122 | + return true; |
| 123 | +} |
| 124 | + |
| 125 | +void QgsCheckboxSearchWidgetWrapper::setExpression( QString exp ) |
| 126 | +{ |
| 127 | + QString fieldName = layer()->fields().at( mFieldIdx ).name(); |
| 128 | + |
| 129 | + QString str = QString( "%1 = '%3'" ) |
| 130 | + .arg( QgsExpression::quotedColumnRef( fieldName ), |
| 131 | + exp.replace( '\'', "''" ) |
| 132 | + ); |
| 133 | + mExpression = str; |
| 134 | +} |
| 135 | + |
| 136 | +void QgsCheckboxSearchWidgetWrapper::stateChanged( int ) |
| 137 | +{ |
| 138 | + if ( mCheckBox ) |
| 139 | + { |
| 140 | + mCheckBox->setTristate( false ); |
| 141 | + QString exp = value().toString(); |
| 142 | + setExpression( exp ); |
| 143 | + emit valueChanged(); |
| 144 | + emit expressionChanged( mExpression ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +QWidget* QgsCheckboxSearchWidgetWrapper::createWidget( QWidget* parent ) |
| 149 | +{ |
| 150 | + QCheckBox* c = new QCheckBox( parent ); |
| 151 | + c->setChecked( Qt::PartiallyChecked ); |
| 152 | + return c; |
| 153 | +} |
| 154 | + |
| 155 | +void QgsCheckboxSearchWidgetWrapper::initWidget( QWidget* editor ) |
| 156 | +{ |
| 157 | + mCheckBox = qobject_cast<QCheckBox*>( editor ); |
| 158 | + |
| 159 | + if ( mCheckBox ) |
| 160 | + { |
| 161 | + mCheckBox->setChecked( Qt::PartiallyChecked ); |
| 162 | + connect( mCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( stateChanged( int ) ) ); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
0 commit comments