|
| 1 | +/*************************************************************************** |
| 2 | + qgsexpressionlineedit.cpp |
| 3 | + ------------------------ |
| 4 | + Date : 18.08.2016 |
| 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 "qgsexpressionlineedit.h" |
| 17 | +#include "qgsfilterlineedit.h" |
| 18 | +#include "qgsexpressioncontext.h" |
| 19 | +#include "qgsapplication.h" |
| 20 | +#include "qgsexpressionbuilderdialog.h" |
| 21 | +#include "qgsexpressioncontextgenerator.h" |
| 22 | +#include "qgscodeeditorsql.h" |
| 23 | +#include <QHBoxLayout> |
| 24 | +#include <QVBoxLayout> |
| 25 | +#include <QToolButton> |
| 26 | + |
| 27 | + |
| 28 | +QgsExpressionLineEdit::QgsExpressionLineEdit( QWidget *parent ) |
| 29 | + : QWidget( parent ) |
| 30 | + , mLineEdit( nullptr ) |
| 31 | + , mCodeEditor( nullptr ) |
| 32 | + , mExpressionDialogTitle( tr( "Expression dialog" ) ) |
| 33 | + , mDa( nullptr ) |
| 34 | + , mExpressionContextGenerator( nullptr ) |
| 35 | + , mLayer( nullptr ) |
| 36 | +{ |
| 37 | + mButton = new QToolButton(); |
| 38 | + mButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); |
| 39 | + mButton->setIcon( QgsApplication::getThemeIcon( "/mIconExpression.svg" ) ); |
| 40 | + connect( mButton, SIGNAL( clicked() ), this, SLOT( editExpression() ) ); |
| 41 | + |
| 42 | + //sets up layout |
| 43 | + setMultiLine( false ); |
| 44 | + |
| 45 | + mExpressionContext = QgsExpressionContext(); |
| 46 | + mExpressionContext << QgsExpressionContextUtils::globalScope() |
| 47 | + << QgsExpressionContextUtils::projectScope(); |
| 48 | +} |
| 49 | + |
| 50 | +void QgsExpressionLineEdit::setExpressionDialogTitle( const QString& title ) |
| 51 | +{ |
| 52 | + mExpressionDialogTitle = title; |
| 53 | +} |
| 54 | + |
| 55 | +void QgsExpressionLineEdit::setMultiLine( bool multiLine ) |
| 56 | +{ |
| 57 | + QString exp = expression(); |
| 58 | + |
| 59 | + if ( multiLine && !mCodeEditor ) |
| 60 | + { |
| 61 | + mCodeEditor = new QgsCodeEditorSQL(); |
| 62 | + mCodeEditor->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); |
| 63 | + delete mLineEdit; |
| 64 | + mLineEdit = nullptr; |
| 65 | + |
| 66 | + QHBoxLayout* newLayout = new QHBoxLayout(); |
| 67 | + newLayout->setContentsMargins( 0, 0, 0, 0 ); |
| 68 | + newLayout->addWidget( mCodeEditor ); |
| 69 | + |
| 70 | + QVBoxLayout* vLayout = new QVBoxLayout(); |
| 71 | + vLayout->addWidget( mButton ); |
| 72 | + vLayout->addStretch(); |
| 73 | + newLayout->addLayout( vLayout ); |
| 74 | + |
| 75 | + delete layout(); |
| 76 | + setLayout( newLayout ); |
| 77 | + |
| 78 | + setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); |
| 79 | + |
| 80 | + setFocusProxy( mCodeEditor ); |
| 81 | + connect( mCodeEditor, SIGNAL( textChanged() ), this, SLOT( expressionEdited() ) ); |
| 82 | + |
| 83 | + setExpression( exp ); |
| 84 | + } |
| 85 | + else if ( !multiLine && !mLineEdit ) |
| 86 | + { |
| 87 | + delete mCodeEditor; |
| 88 | + mCodeEditor = nullptr; |
| 89 | + mLineEdit = new QgsFilterLineEdit(); |
| 90 | + mLineEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); |
| 91 | + |
| 92 | + QHBoxLayout* newLayout = new QHBoxLayout(); |
| 93 | + newLayout->setContentsMargins( 0, 0, 0, 0 ); |
| 94 | + newLayout->addWidget( mLineEdit ); |
| 95 | + newLayout->addWidget( mButton ); |
| 96 | + |
| 97 | + delete layout(); |
| 98 | + setLayout( newLayout ); |
| 99 | + |
| 100 | + setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum ); |
| 101 | + |
| 102 | + setFocusProxy( mLineEdit ); |
| 103 | + connect( mLineEdit, SIGNAL( textChanged( QString ) ), this, SLOT( expressionEdited( QString ) ) ); |
| 104 | + |
| 105 | + setExpression( exp ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void QgsExpressionLineEdit::setGeomCalculator( const QgsDistanceArea &da ) |
| 110 | +{ |
| 111 | + mDa.reset( new QgsDistanceArea( da ) ); |
| 112 | +} |
| 113 | + |
| 114 | +void QgsExpressionLineEdit::setLayer( QgsVectorLayer* layer ) |
| 115 | +{ |
| 116 | + mLayer = layer; |
| 117 | +} |
| 118 | + |
| 119 | +QString QgsExpressionLineEdit::expression() const |
| 120 | +{ |
| 121 | + if ( mLineEdit ) |
| 122 | + return mLineEdit->text(); |
| 123 | + else if ( mCodeEditor ) |
| 124 | + return mCodeEditor->text(); |
| 125 | + |
| 126 | + return QString(); |
| 127 | +} |
| 128 | + |
| 129 | +bool QgsExpressionLineEdit::isValidExpression( QString *expressionError ) const |
| 130 | +{ |
| 131 | + QString temp; |
| 132 | + return QgsExpression::isValid( expression(), &mExpressionContext, expressionError ? *expressionError : temp ); |
| 133 | +} |
| 134 | + |
| 135 | +void QgsExpressionLineEdit::registerExpressionContextGenerator( const QgsExpressionContextGenerator* generator ) |
| 136 | +{ |
| 137 | + mExpressionContextGenerator = generator; |
| 138 | +} |
| 139 | + |
| 140 | +void QgsExpressionLineEdit::setExpression( const QString& newExpression ) |
| 141 | +{ |
| 142 | + if ( mLineEdit ) |
| 143 | + mLineEdit->setText( newExpression ); |
| 144 | + else if ( mCodeEditor ) |
| 145 | + mCodeEditor->setText( newExpression ); |
| 146 | +} |
| 147 | + |
| 148 | +void QgsExpressionLineEdit::editExpression() |
| 149 | +{ |
| 150 | + QString currentExpression = expression(); |
| 151 | + |
| 152 | + QgsExpressionContext context = mExpressionContextGenerator ? mExpressionContextGenerator->createExpressionContext() : mExpressionContext; |
| 153 | + |
| 154 | + QgsExpressionBuilderDialog dlg( mLayer, currentExpression, this, "generic", context ); |
| 155 | + if ( !mDa.isNull() ) |
| 156 | + { |
| 157 | + dlg.setGeomCalculator( *mDa ); |
| 158 | + } |
| 159 | + dlg.setWindowTitle( mExpressionDialogTitle ); |
| 160 | + |
| 161 | + if ( dlg.exec() ) |
| 162 | + { |
| 163 | + QString newExpression = dlg.expressionText(); |
| 164 | + setExpression( newExpression ); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +void QgsExpressionLineEdit::expressionEdited() |
| 169 | +{ |
| 170 | + emit expressionChanged( expression() ); |
| 171 | +} |
| 172 | + |
| 173 | +void QgsExpressionLineEdit::expressionEdited( const QString& expression ) |
| 174 | +{ |
| 175 | + updateLineEditStyle( expression ); |
| 176 | + emit expressionChanged( expression ); |
| 177 | +} |
| 178 | + |
| 179 | +void QgsExpressionLineEdit::changeEvent( QEvent* event ) |
| 180 | +{ |
| 181 | + if ( event->type() == QEvent::EnabledChange ) |
| 182 | + { |
| 183 | + updateLineEditStyle( expression() ); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +void QgsExpressionLineEdit::updateLineEditStyle( const QString& expression ) |
| 188 | +{ |
| 189 | + if ( !mLineEdit ) |
| 190 | + return; |
| 191 | + |
| 192 | + QPalette palette; |
| 193 | + if ( !isEnabled() ) |
| 194 | + { |
| 195 | + palette.setColor( QPalette::Text, Qt::gray ); |
| 196 | + } |
| 197 | + else |
| 198 | + { |
| 199 | + bool isValid = true; |
| 200 | + if ( !expression.isEmpty() ) |
| 201 | + { |
| 202 | + isValid = isExpressionValid( expression ); |
| 203 | + } |
| 204 | + if ( !isValid ) |
| 205 | + { |
| 206 | + palette.setColor( QPalette::Text, Qt::red ); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + palette.setColor( QPalette::Text, Qt::black ); |
| 211 | + } |
| 212 | + } |
| 213 | + mLineEdit->setPalette( palette ); |
| 214 | +} |
| 215 | + |
| 216 | +bool QgsExpressionLineEdit::isExpressionValid( const QString& expressionStr ) |
| 217 | +{ |
| 218 | + QgsExpression expression( expressionStr ); |
| 219 | + expression.prepare( &mExpressionContext ); |
| 220 | + return !expression.hasParserError(); |
| 221 | +} |
0 commit comments