Skip to content

Commit 3eefe0d

Browse files
committedOct 26, 2017
Make constraint reporting less obtrusive
1 parent d40447e commit 3eefe0d

File tree

6 files changed

+44
-99
lines changed

6 files changed

+44
-99
lines changed
 

‎python/gui/qgsattributeformeditorwidget.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class QgsAttributeFormEditorWidget : QWidget
9696
:rtype: str
9797
%End
9898

99+
void setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
100+
%Docstring
101+
Set the constraint status for this widget.
102+
%End
103+
99104
public slots:
100105

101106
void setIsMixed( bool mixed );

‎src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ void QgsEditorWidgetWrapper::updateConstraintWidgetStatus( ConstraintResult cons
110110
break;
111111

112112
case ConstraintResultFailHard:
113-
widget()->setStyleSheet( QStringLiteral( "background-color: #dd7777;" ) );
113+
widget()->setStyleSheet( QStringLiteral( "background-color: #FFE0B2;" ) );
114114
break;
115115

116116
case ConstraintResultFailSoft:
117-
widget()->setStyleSheet( QStringLiteral( "background-color: #ffd85d;" ) );
117+
widget()->setStyleSheet( QStringLiteral( "background-color: #FFECB3;" ) );
118118
break;
119119
}
120120
}

‎src/gui/qgsattributeform.cpp

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,6 @@ void QgsAttributeForm::setMode( QgsAttributeForm::Mode mode )
201201
case QgsAttributeForm::SearchMode:
202202
mSearchButtonBox->setVisible( true );
203203
hideButtonBox();
204-
if ( mContext.formMode() != QgsAttributeEditorContext::Embed )
205-
{
206-
delete mInvalidConstraintMessage;
207-
mInvalidConstraintMessage = nullptr;
208-
}
209-
else
210-
{
211-
mTopMessageWidget->hide();
212-
}
213204
break;
214205
}
215206

@@ -807,29 +798,6 @@ bool QgsAttributeForm::currentFormFeature( QgsFeature &feature )
807798
return rc;
808799
}
809800

810-
void QgsAttributeForm::clearInvalidConstraintsMessage()
811-
{
812-
mTopMessageWidget->hide();
813-
mInvalidConstraintMessage->clear();
814-
}
815-
816-
void QgsAttributeForm::displayInvalidConstraintMessage( const QStringList &f,
817-
const QStringList &d )
818-
{
819-
clearInvalidConstraintsMessage();
820-
821-
// show only the third first errors (to avoid a too long label)
822-
int max = 3;
823-
int size = f.size() > max ? max : f.size();
824-
QString descriptions;
825-
for ( int i = 0; i < size; i++ )
826-
descriptions += QStringLiteral( "<li>%1: <i>%2</i></li>" ).arg( f[i], d[i] );
827-
828-
QString msg = QStringLiteral( "<b>%1</b><ul>%2</ul>" ).arg( tr( "Invalid fields" ), descriptions );
829-
830-
mInvalidConstraintMessage->setText( msg );
831-
mTopMessageWidget->show();
832-
}
833801

834802
void QgsAttributeForm::registerContainerInformation( QgsAttributeForm::ContainerInformation *info )
835803
{
@@ -928,35 +896,9 @@ void QgsAttributeForm::onConstraintStatusChanged( const QString &constraint,
928896
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( sender() );
929897
Q_ASSERT( eww );
930898

931-
QLabel *buddy = mBuddyMap.value( eww->widget() );
932-
933-
if ( buddy )
934-
{
935-
QString tooltip = QStringLiteral( "<b>" ) + tr( "Constraints: " ) + QStringLiteral( "</b>" ) + description +
936-
QStringLiteral( "<br /><b>" ) + tr( "Raw expression: " ) + QStringLiteral( "</b>" ) + constraint +
937-
QStringLiteral( "<br /><b>" ) + tr( "Result: " ) + QStringLiteral( "</b>" ) + err;
938-
buddy->setToolTip( tooltip );
939-
940-
if ( !buddy->property( "originalText" ).isValid() )
941-
buddy->setProperty( "originalText", buddy->text() );
942-
943-
QString text = buddy->property( "originalText" ).toString();
899+
QgsAttributeFormEditorWidget *formEditorWidget = mFormEditorWidgets.value( eww->fieldIdx() );
944900

945-
switch ( result )
946-
{
947-
case QgsEditorWidgetWrapper::ConstraintResultFailHard:
948-
buddy->setText( trUtf8( "%1<font color=\"red\">✘</font>" ).arg( text ) );
949-
break;
950-
951-
case QgsEditorWidgetWrapper::ConstraintResultFailSoft:
952-
buddy->setText( trUtf8( "%1<font color=\"orange\">✘</font>" ).arg( text ) );
953-
break;
954-
955-
case QgsEditorWidgetWrapper::ConstraintResultPass:
956-
buddy->setText( trUtf8( "%1<font color=\"green\">✔</font>" ).arg( text ) );
957-
break;
958-
}
959-
}
901+
formEditorWidget->setConstraintStatus( constraint, description, err, result );
960902
}
961903

962904
QList<QgsEditorWidgetWrapper *> QgsAttributeForm::constraintDependencies( QgsEditorWidgetWrapper *w )
@@ -1030,17 +972,11 @@ void QgsAttributeForm::synchronizeEnabledState()
1030972
}
1031973
}
1032974

1033-
// push a message and disable the OK button if constraints are invalid
1034-
clearInvalidConstraintsMessage();
1035-
1036975
if ( mMode != SearchMode )
1037976
{
1038977
QStringList invalidFields, descriptions;
1039978
bool validConstraint = currentFormValidConstraints( invalidFields, descriptions );
1040979

1041-
if ( ! invalidFields.isEmpty() )
1042-
displayInvalidConstraintMessage( invalidFields, descriptions );
1043-
1044980
isEditable = isEditable & validConstraint;
1045981
}
1046982

@@ -1088,19 +1024,6 @@ void QgsAttributeForm::init()
10881024
mMessageBar->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed );
10891025
vl->addWidget( mMessageBar );
10901026

1091-
mTopMessageWidget = new QWidget();
1092-
mTopMessageWidget->hide();
1093-
mTopMessageWidget->setLayout( new QHBoxLayout() );
1094-
1095-
QSvgWidget *warningIcon = new QSvgWidget( QgsApplication::iconPath( QStringLiteral( "/mIconWarning.svg" ) ) );
1096-
warningIcon->setFixedSize( 48, 48 );
1097-
mTopMessageWidget->layout()->addWidget( warningIcon );
1098-
mInvalidConstraintMessage = new QLabel( this );
1099-
mTopMessageWidget->layout()->addWidget( mInvalidConstraintMessage );
1100-
mTopMessageWidget->hide();
1101-
1102-
vl->addWidget( mTopMessageWidget );
1103-
11041027
setLayout( vl );
11051028

11061029
// Get a layout
@@ -1813,16 +1736,6 @@ void QgsAttributeForm::afterWidgetInit()
18131736
connect( eww, &QgsEditorWidgetWrapper::constraintStatusChanged, this, &QgsAttributeForm::onConstraintStatusChanged );
18141737
}
18151738
}
1816-
1817-
// Update buddy widget list
1818-
mBuddyMap.clear();
1819-
QList<QLabel *> labels = findChildren<QLabel *>();
1820-
1821-
Q_FOREACH ( QLabel *label, labels )
1822-
{
1823-
if ( label->buddy() )
1824-
mBuddyMap.insert( label->buddy(), label );
1825-
}
18261739
}
18271740

18281741

‎src/gui/qgsattributeform.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,18 +339,13 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
339339
bool currentFormFeature( QgsFeature &feature );
340340
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
341341
QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
342-
void clearInvalidConstraintsMessage();
343-
void displayInvalidConstraintMessage( const QStringList &invalidFields,
344-
const QStringList &description );
345342

346343
QgsVectorLayer *mLayer = nullptr;
347344
QgsFeature mFeature;
348345
QgsMessageBar *mMessageBar = nullptr;
349346
bool mOwnsMessageBar;
350347
QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
351348
QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
352-
QLabel *mInvalidConstraintMessage = nullptr;
353-
QWidget *mTopMessageWidget = nullptr;
354349
QList<QgsWidgetWrapper *> mWidgets;
355350
QgsAttributeEditorContext mContext;
356351
QDialogButtonBox *mButtonBox = nullptr;
@@ -415,8 +410,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
415410

416411
Mode mMode;
417412

418-
//! Backlinks widgets to buddies.
419-
QMap<QWidget *, QLabel *> mBuddyMap;
420413
QMap<QWidget *, QSvgWidget *> mIconMap;
421414

422415
friend class TestQgsDualView;

‎src/gui/qgsattributeformeditorwidget.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ QgsAttributeFormEditorWidget::QgsAttributeFormEditorWidget( QgsEditorWidgetWrapp
3737
, mIsMixed( false )
3838
, mIsChanged( false )
3939
{
40+
mConstraintResultLabel = new QLabel();
41+
mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() );
42+
4043
mEditPage = new QWidget();
4144
QHBoxLayout *l = new QHBoxLayout();
4245
l->setMargin( 0 );
@@ -134,6 +137,27 @@ QList< QgsSearchWidgetWrapper * > QgsAttributeFormEditorWidget::searchWidgetWrap
134137
return mSearchWidgets;
135138
}
136139

140+
void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result )
141+
{
142+
switch ( result )
143+
{
144+
case QgsEditorWidgetWrapper::ConstraintResultFailHard:
145+
mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FF9800\">✘</font>" ) );
146+
mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
147+
break;
148+
149+
case QgsEditorWidgetWrapper::ConstraintResultFailSoft:
150+
mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">✘</font>" ) );
151+
mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
152+
break;
153+
154+
case QgsEditorWidgetWrapper::ConstraintResultPass:
155+
mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259b24\">✔</font>" ) );
156+
mConstraintResultLabel->setToolTip( QString() );
157+
break;
158+
}
159+
}
160+
137161
void QgsAttributeFormEditorWidget::setMode( QgsAttributeFormEditorWidget::Mode mode )
138162
{
139163
mMode = mode;
@@ -316,6 +340,9 @@ void QgsAttributeFormEditorWidget::updateWidgets()
316340
case MultiEditMode:
317341
{
318342
mStack->setCurrentWidget( mEditPage );
343+
344+
mEditPage->layout()->addWidget( mConstraintResultLabel );
345+
319346
break;
320347
}
321348

‎src/gui/qgsattributeformeditorwidget.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsattributeeditorcontext.h"
2424
#include "qgssearchwidgetwrapper.h"
2525
#include "qgis_gui.h"
26+
#include "qgseditorwidgetwrapper.h"
2627

2728
class QgsAttributeForm;
2829
class QgsEditorWidgetWrapper;
@@ -31,6 +32,7 @@ class QgsSearchWidgetToolButton;
3132
class QgsVectorLayer;
3233
class QStackedWidget;
3334
class QgsAttributeEditorContext;
35+
class QLabel;
3436

3537
/**
3638
* \ingroup gui
@@ -116,6 +118,11 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget
116118
*/
117119
QString currentFilterExpression() const;
118120

121+
/**
122+
* Set the constraint status for this widget.
123+
*/
124+
void setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result );
125+
119126
public slots:
120127

121128
/**
@@ -202,6 +209,7 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget
202209
QgsEditorWidgetWrapper *mWidget = nullptr;
203210
QList< QgsSearchWidgetWrapper * > mSearchWidgets;
204211
QgsAttributeForm *mForm = nullptr;
212+
QLabel *mConstraintResultLabel;
205213
Mode mMode;
206214

207215
QgsMultiEditToolButton *mMultiEditButton = nullptr;
@@ -211,7 +219,6 @@ class GUI_EXPORT QgsAttributeFormEditorWidget : public QWidget
211219
bool mIsMixed;
212220
bool mIsChanged;
213221

214-
215222
QgsVectorLayer *layer();
216223
void updateWidgets();
217224
};

0 commit comments

Comments
 (0)
Please sign in to comment.