From 3eefe0dba9dba3f0d8a15f045b2abe38ecff9e24 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Thu, 26 Oct 2017 18:58:42 +0200 Subject: [PATCH] Make constraint reporting less obtrusive --- python/gui/qgsattributeformeditorwidget.sip | 5 + .../core/qgseditorwidgetwrapper.cpp | 4 +- src/gui/qgsattributeform.cpp | 91 +------------------ src/gui/qgsattributeform.h | 7 -- src/gui/qgsattributeformeditorwidget.cpp | 27 ++++++ src/gui/qgsattributeformeditorwidget.h | 9 +- 6 files changed, 44 insertions(+), 99 deletions(-) diff --git a/python/gui/qgsattributeformeditorwidget.sip b/python/gui/qgsattributeformeditorwidget.sip index fc299ccb3ecd..86840dcfec33 100644 --- a/python/gui/qgsattributeformeditorwidget.sip +++ b/python/gui/qgsattributeformeditorwidget.sip @@ -96,6 +96,11 @@ class QgsAttributeFormEditorWidget : QWidget :rtype: str %End + void setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result ); +%Docstring + Set the constraint status for this widget. +%End + public slots: void setIsMixed( bool mixed ); diff --git a/src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp b/src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp index 399abed8ee84..7745631f6d2f 100644 --- a/src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp +++ b/src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp @@ -110,11 +110,11 @@ void QgsEditorWidgetWrapper::updateConstraintWidgetStatus( ConstraintResult cons break; case ConstraintResultFailHard: - widget()->setStyleSheet( QStringLiteral( "background-color: #dd7777;" ) ); + widget()->setStyleSheet( QStringLiteral( "background-color: #FFE0B2;" ) ); break; case ConstraintResultFailSoft: - widget()->setStyleSheet( QStringLiteral( "background-color: #ffd85d;" ) ); + widget()->setStyleSheet( QStringLiteral( "background-color: #FFECB3;" ) ); break; } } diff --git a/src/gui/qgsattributeform.cpp b/src/gui/qgsattributeform.cpp index 48866ef06471..ac5be9a6231e 100644 --- a/src/gui/qgsattributeform.cpp +++ b/src/gui/qgsattributeform.cpp @@ -201,15 +201,6 @@ void QgsAttributeForm::setMode( QgsAttributeForm::Mode mode ) case QgsAttributeForm::SearchMode: mSearchButtonBox->setVisible( true ); hideButtonBox(); - if ( mContext.formMode() != QgsAttributeEditorContext::Embed ) - { - delete mInvalidConstraintMessage; - mInvalidConstraintMessage = nullptr; - } - else - { - mTopMessageWidget->hide(); - } break; } @@ -807,29 +798,6 @@ bool QgsAttributeForm::currentFormFeature( QgsFeature &feature ) return rc; } -void QgsAttributeForm::clearInvalidConstraintsMessage() -{ - mTopMessageWidget->hide(); - mInvalidConstraintMessage->clear(); -} - -void QgsAttributeForm::displayInvalidConstraintMessage( const QStringList &f, - const QStringList &d ) -{ - clearInvalidConstraintsMessage(); - - // show only the third first errors (to avoid a too long label) - int max = 3; - int size = f.size() > max ? max : f.size(); - QString descriptions; - for ( int i = 0; i < size; i++ ) - descriptions += QStringLiteral( "
  • %1: %2
  • " ).arg( f[i], d[i] ); - - QString msg = QStringLiteral( "%1" ).arg( tr( "Invalid fields" ), descriptions ); - - mInvalidConstraintMessage->setText( msg ); - mTopMessageWidget->show(); -} void QgsAttributeForm::registerContainerInformation( QgsAttributeForm::ContainerInformation *info ) { @@ -928,35 +896,9 @@ void QgsAttributeForm::onConstraintStatusChanged( const QString &constraint, QgsEditorWidgetWrapper *eww = qobject_cast( sender() ); Q_ASSERT( eww ); - QLabel *buddy = mBuddyMap.value( eww->widget() ); - - if ( buddy ) - { - QString tooltip = QStringLiteral( "" ) + tr( "Constraints: " ) + QStringLiteral( "" ) + description + - QStringLiteral( "
    " ) + tr( "Raw expression: " ) + QStringLiteral( "" ) + constraint + - QStringLiteral( "
    " ) + tr( "Result: " ) + QStringLiteral( "" ) + err; - buddy->setToolTip( tooltip ); - - if ( !buddy->property( "originalText" ).isValid() ) - buddy->setProperty( "originalText", buddy->text() ); - - QString text = buddy->property( "originalText" ).toString(); + QgsAttributeFormEditorWidget *formEditorWidget = mFormEditorWidgets.value( eww->fieldIdx() ); - switch ( result ) - { - case QgsEditorWidgetWrapper::ConstraintResultFailHard: - buddy->setText( trUtf8( "%1" ).arg( text ) ); - break; - - case QgsEditorWidgetWrapper::ConstraintResultFailSoft: - buddy->setText( trUtf8( "%1" ).arg( text ) ); - break; - - case QgsEditorWidgetWrapper::ConstraintResultPass: - buddy->setText( trUtf8( "%1" ).arg( text ) ); - break; - } - } + formEditorWidget->setConstraintStatus( constraint, description, err, result ); } QList QgsAttributeForm::constraintDependencies( QgsEditorWidgetWrapper *w ) @@ -1030,17 +972,11 @@ void QgsAttributeForm::synchronizeEnabledState() } } - // push a message and disable the OK button if constraints are invalid - clearInvalidConstraintsMessage(); - if ( mMode != SearchMode ) { QStringList invalidFields, descriptions; bool validConstraint = currentFormValidConstraints( invalidFields, descriptions ); - if ( ! invalidFields.isEmpty() ) - displayInvalidConstraintMessage( invalidFields, descriptions ); - isEditable = isEditable & validConstraint; } @@ -1088,19 +1024,6 @@ void QgsAttributeForm::init() mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ); vl->addWidget( mMessageBar ); - mTopMessageWidget = new QWidget(); - mTopMessageWidget->hide(); - mTopMessageWidget->setLayout( new QHBoxLayout() ); - - QSvgWidget *warningIcon = new QSvgWidget( QgsApplication::iconPath( QStringLiteral( "/mIconWarning.svg" ) ) ); - warningIcon->setFixedSize( 48, 48 ); - mTopMessageWidget->layout()->addWidget( warningIcon ); - mInvalidConstraintMessage = new QLabel( this ); - mTopMessageWidget->layout()->addWidget( mInvalidConstraintMessage ); - mTopMessageWidget->hide(); - - vl->addWidget( mTopMessageWidget ); - setLayout( vl ); // Get a layout @@ -1813,16 +1736,6 @@ void QgsAttributeForm::afterWidgetInit() connect( eww, &QgsEditorWidgetWrapper::constraintStatusChanged, this, &QgsAttributeForm::onConstraintStatusChanged ); } } - - // Update buddy widget list - mBuddyMap.clear(); - QList labels = findChildren(); - - Q_FOREACH ( QLabel *label, labels ) - { - if ( label->buddy() ) - mBuddyMap.insert( label->buddy(), label ); - } } diff --git a/src/gui/qgsattributeform.h b/src/gui/qgsattributeform.h index 355cbc289258..6af74288353d 100644 --- a/src/gui/qgsattributeform.h +++ b/src/gui/qgsattributeform.h @@ -339,9 +339,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget bool currentFormFeature( QgsFeature &feature ); bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ); QList constraintDependencies( QgsEditorWidgetWrapper *w ); - void clearInvalidConstraintsMessage(); - void displayInvalidConstraintMessage( const QStringList &invalidFields, - const QStringList &description ); QgsVectorLayer *mLayer = nullptr; QgsFeature mFeature; @@ -349,8 +346,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget bool mOwnsMessageBar; QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr; QgsMessageBarItem *mMultiEditMessageBarItem = nullptr; - QLabel *mInvalidConstraintMessage = nullptr; - QWidget *mTopMessageWidget = nullptr; QList mWidgets; QgsAttributeEditorContext mContext; QDialogButtonBox *mButtonBox = nullptr; @@ -415,8 +410,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget Mode mMode; - //! Backlinks widgets to buddies. - QMap mBuddyMap; QMap mIconMap; friend class TestQgsDualView; diff --git a/src/gui/qgsattributeformeditorwidget.cpp b/src/gui/qgsattributeformeditorwidget.cpp index 0480c298c92c..68b23e434ba6 100644 --- a/src/gui/qgsattributeformeditorwidget.cpp +++ b/src/gui/qgsattributeformeditorwidget.cpp @@ -37,6 +37,9 @@ QgsAttributeFormEditorWidget::QgsAttributeFormEditorWidget( QgsEditorWidgetWrapp , mIsMixed( false ) , mIsChanged( false ) { + mConstraintResultLabel = new QLabel(); + mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() ); + mEditPage = new QWidget(); QHBoxLayout *l = new QHBoxLayout(); l->setMargin( 0 ); @@ -134,6 +137,27 @@ QList< QgsSearchWidgetWrapper * > QgsAttributeFormEditorWidget::searchWidgetWrap return mSearchWidgets; } +void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result ) +{ + switch ( result ) + { + case QgsEditorWidgetWrapper::ConstraintResultFailHard: + mConstraintResultLabel->setText( QStringLiteral( "" ) ); + mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "%1: %2" ).arg( constraint, err ) : description ); + break; + + case QgsEditorWidgetWrapper::ConstraintResultFailSoft: + mConstraintResultLabel->setText( QStringLiteral( "" ) ); + mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "%1: %2" ).arg( constraint, err ) : description ); + break; + + case QgsEditorWidgetWrapper::ConstraintResultPass: + mConstraintResultLabel->setText( QStringLiteral( "" ) ); + mConstraintResultLabel->setToolTip( QString() ); + break; + } +} + void QgsAttributeFormEditorWidget::setMode( QgsAttributeFormEditorWidget::Mode mode ) { mMode = mode; @@ -316,6 +340,9 @@ void QgsAttributeFormEditorWidget::updateWidgets() case MultiEditMode: { mStack->setCurrentWidget( mEditPage ); + + mEditPage->layout()->addWidget( mConstraintResultLabel ); + break; } diff --git a/src/gui/qgsattributeformeditorwidget.h b/src/gui/qgsattributeformeditorwidget.h index 36e2f580c099..9aa204536cf0 100644 --- a/src/gui/qgsattributeformeditorwidget.h +++ b/src/gui/qgsattributeformeditorwidget.h @@ -23,6 +23,7 @@ #include "qgsattributeeditorcontext.h" #include "qgssearchwidgetwrapper.h" #include "qgis_gui.h" +#include "qgseditorwidgetwrapper.h" class QgsAttributeForm; class QgsEditorWidgetWrapper; @@ -31,6 +32,7 @@ class QgsSearchWidgetToolButton; class QgsVectorLayer; class QStackedWidget; class QgsAttributeEditorContext; +class QLabel; /** * \ingroup gui @@ -116,6 +118,11 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget */ QString currentFilterExpression() const; + /** + * Set the constraint status for this widget. + */ + void setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result ); + public slots: /** @@ -202,6 +209,7 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget QgsEditorWidgetWrapper *mWidget = nullptr; QList< QgsSearchWidgetWrapper * > mSearchWidgets; QgsAttributeForm *mForm = nullptr; + QLabel *mConstraintResultLabel; Mode mMode; QgsMultiEditToolButton *mMultiEditButton = nullptr; @@ -211,7 +219,6 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget bool mIsMixed; bool mIsChanged; - QgsVectorLayer *layer(); void updateWidgets(); };