Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add search widget wrapper for check box widget
- Loading branch information
1 parent
ca5c7e2
commit 195b4de
Showing
10 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
python/gui/editorwidgets/qgscheckboxsearchwidgetwrapper.sip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Wraps a checkbox search widget. | ||
*/ | ||
class QgsCheckboxSearchWidgetWrapper : QgsSearchWidgetWrapper | ||
{ | ||
%TypeHeaderCode | ||
#include <qgscheckboxsearchwidgetwrapper.h> | ||
%End | ||
public: | ||
|
||
explicit QgsCheckboxSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent /TransferThis/ = nullptr ); | ||
|
||
// QgsSearchWidgetWrapper interface | ||
public: | ||
bool applyDirectly(); | ||
QString expression(); | ||
bool valid() const; | ||
QVariant value() const; | ||
FilterFlags supportedFlags() const; | ||
FilterFlags defaultFlags() const; | ||
virtual QString createExpression( FilterFlags flags ) const; | ||
|
||
public slots: | ||
|
||
virtual void clearWidget(); | ||
virtual void setEnabled( bool enabled ); | ||
|
||
protected: | ||
QWidget* createWidget( QWidget* parent ); | ||
void initWidget( QWidget* editor ); | ||
|
||
protected slots: | ||
void setExpression( QString exp ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/gui/editorwidgets/qgscheckboxsearchwidgetwrapper.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/*************************************************************************** | ||
qgscheckboxsearchwidgetwrapper.cpp | ||
--------------------------------- | ||
Date : 2016-05-23 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgscheckboxsearchwidgetwrapper.h" | ||
|
||
#include "qgsfield.h" | ||
#include "qgscheckboxwidgetfactory.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
#include <QSettings> | ||
#include <QCheckBox> | ||
|
||
QgsCheckboxSearchWidgetWrapper::QgsCheckboxSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) | ||
: QgsSearchWidgetWrapper( vl, fieldIdx, parent ) | ||
, mCheckBox( nullptr ) | ||
, mLayer( nullptr ) | ||
{ | ||
} | ||
|
||
bool QgsCheckboxSearchWidgetWrapper::applyDirectly() | ||
{ | ||
return true; | ||
} | ||
|
||
QString QgsCheckboxSearchWidgetWrapper::expression() | ||
{ | ||
return mExpression; | ||
} | ||
|
||
QVariant QgsCheckboxSearchWidgetWrapper::value() const | ||
{ | ||
QVariant v; | ||
|
||
if ( mCheckBox ) | ||
v = mCheckBox->isChecked() ? config( "CheckedState" ) : config( "UncheckedState" ); | ||
|
||
return v; | ||
} | ||
|
||
QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::supportedFlags() const | ||
{ | ||
return EqualTo | IsNull; | ||
} | ||
|
||
QgsSearchWidgetWrapper::FilterFlags QgsCheckboxSearchWidgetWrapper::defaultFlags() const | ||
{ | ||
return EqualTo; | ||
} | ||
|
||
QString QgsCheckboxSearchWidgetWrapper::createExpression( QgsSearchWidgetWrapper::FilterFlags flags ) const | ||
{ | ||
QVariant::Type fldType = layer()->fields().at( mFieldIdx ).type(); | ||
QString fieldName = QgsExpression::quotedColumnRef( layer()->fields().at( mFieldIdx ).name() ); | ||
|
||
//clear any unsupported flags | ||
flags &= supportedFlags(); | ||
if ( flags & IsNull ) | ||
return fieldName + " IS NULL"; | ||
|
||
QVariant v = value(); | ||
if ( !v.isValid() ) | ||
return QString(); | ||
|
||
switch ( fldType ) | ||
{ | ||
case QVariant::Int: | ||
case QVariant::UInt: | ||
case QVariant::Double: | ||
case QVariant::LongLong: | ||
case QVariant::ULongLong: | ||
{ | ||
if ( flags & EqualTo ) | ||
return fieldName + '=' + v.toString(); | ||
else if ( flags & NotEqualTo ) | ||
return fieldName + "<>" + v.toString(); | ||
break; | ||
} | ||
|
||
default: | ||
{ | ||
if ( flags & EqualTo ) | ||
return fieldName + "='" + v.toString() + '\''; | ||
else if ( flags & NotEqualTo ) | ||
return fieldName + "<>'" + v.toString() + '\''; | ||
break; | ||
} | ||
} | ||
|
||
return QString(); | ||
} | ||
|
||
void QgsCheckboxSearchWidgetWrapper::clearWidget() | ||
{ | ||
if ( mCheckBox ) | ||
{ | ||
whileBlocking( mCheckBox )->setCheckState( Qt::PartiallyChecked ); | ||
} | ||
} | ||
|
||
void QgsCheckboxSearchWidgetWrapper::setEnabled( bool enabled ) | ||
{ | ||
if ( mCheckBox ) | ||
{ | ||
mCheckBox->setEnabled( enabled ); | ||
} | ||
} | ||
|
||
bool QgsCheckboxSearchWidgetWrapper::valid() const | ||
{ | ||
return true; | ||
} | ||
|
||
void QgsCheckboxSearchWidgetWrapper::setExpression( QString exp ) | ||
{ | ||
QString fieldName = layer()->fields().at( mFieldIdx ).name(); | ||
|
||
QString str = QString( "%1 = '%3'" ) | ||
.arg( QgsExpression::quotedColumnRef( fieldName ), | ||
exp.replace( '\'', "''" ) | ||
); | ||
mExpression = str; | ||
} | ||
|
||
void QgsCheckboxSearchWidgetWrapper::stateChanged( int ) | ||
{ | ||
if ( mCheckBox ) | ||
{ | ||
mCheckBox->setTristate( false ); | ||
QString exp = value().toString(); | ||
setExpression( exp ); | ||
emit valueChanged(); | ||
emit expressionChanged( mExpression ); | ||
} | ||
} | ||
|
||
QWidget* QgsCheckboxSearchWidgetWrapper::createWidget( QWidget* parent ) | ||
{ | ||
QCheckBox* c = new QCheckBox( parent ); | ||
c->setChecked( Qt::PartiallyChecked ); | ||
return c; | ||
} | ||
|
||
void QgsCheckboxSearchWidgetWrapper::initWidget( QWidget* editor ) | ||
{ | ||
mCheckBox = qobject_cast<QCheckBox*>( editor ); | ||
|
||
if ( mCheckBox ) | ||
{ | ||
mCheckBox->setChecked( Qt::PartiallyChecked ); | ||
connect( mCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( stateChanged( int ) ) ); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*************************************************************************** | ||
qgscheckboxsearchwidgetwrapper.h | ||
------------------------------- | ||
Date : 2016-05-23 | ||
Copyright : (C) 2016 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSCHECKBOXSEARCHWIDGETWRAPPER_H | ||
#define QGSCHECKBOXSEARCHWIDGETWRAPPER_H | ||
|
||
#include "qgssearchwidgetwrapper.h" | ||
#include "qgscheckboxwidgetwrapper.h" | ||
|
||
#include <QComboBox> | ||
#include <QListWidget> | ||
#include <QLineEdit> | ||
|
||
class QgsCheckboxWidgetFactory; | ||
|
||
/** | ||
* Wraps a checkbox search widget. | ||
*/ | ||
class GUI_EXPORT QgsCheckboxSearchWidgetWrapper : public QgsSearchWidgetWrapper | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit QgsCheckboxSearchWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* parent = nullptr ); | ||
bool applyDirectly() override; | ||
QString expression() override; | ||
bool valid() const override; | ||
QVariant value() const; | ||
FilterFlags supportedFlags() const override; | ||
FilterFlags defaultFlags() const override; | ||
virtual QString createExpression( FilterFlags flags ) const override; | ||
|
||
public slots: | ||
|
||
virtual void clearWidget() override; | ||
virtual void setEnabled( bool enabled ) override; | ||
|
||
protected: | ||
QWidget* createWidget( QWidget* parent ) override; | ||
void initWidget( QWidget* editor ) override; | ||
|
||
protected slots: | ||
void setExpression( QString exp ) override; | ||
|
||
private slots: | ||
void stateChanged( int state ); | ||
|
||
private: | ||
QCheckBox* mCheckBox; | ||
QgsVectorLayer* mLayer; | ||
|
||
friend class QgsCheckboxWidgetFactory; | ||
}; | ||
|
||
#endif // QGSCHECKBOXSEARCHWIDGETWRAPPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters