Skip to content
Permalink
Browse files

Make constraint reporting less obtrusive

  • Loading branch information
m-kuhn committed Oct 26, 2017
1 parent d40447e commit 3eefe0dba9dba3f0d8a15f045b2abe38ecff9e24
@@ -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 );
@@ -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;
}
}
@@ -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( "<li>%1: <i>%2</i></li>" ).arg( f[i], d[i] );

QString msg = QStringLiteral( "<b>%1</b><ul>%2</ul>" ).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<QgsEditorWidgetWrapper *>( sender() );
Q_ASSERT( eww );

QLabel *buddy = mBuddyMap.value( eww->widget() );

if ( buddy )
{
QString tooltip = QStringLiteral( "<b>" ) + tr( "Constraints: " ) + QStringLiteral( "</b>" ) + description +
QStringLiteral( "<br /><b>" ) + tr( "Raw expression: " ) + QStringLiteral( "</b>" ) + constraint +
QStringLiteral( "<br /><b>" ) + tr( "Result: " ) + QStringLiteral( "</b>" ) + 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<font color=\"red\">✘</font>" ).arg( text ) );
break;

case QgsEditorWidgetWrapper::ConstraintResultFailSoft:
buddy->setText( trUtf8( "%1<font color=\"orange\">✘</font>" ).arg( text ) );
break;

case QgsEditorWidgetWrapper::ConstraintResultPass:
buddy->setText( trUtf8( "%1<font color=\"green\">✔</font>" ).arg( text ) );
break;
}
}
formEditorWidget->setConstraintStatus( constraint, description, err, result );
}

QList<QgsEditorWidgetWrapper *> 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<QLabel *> labels = findChildren<QLabel *>();

Q_FOREACH ( QLabel *label, labels )
{
if ( label->buddy() )
mBuddyMap.insert( label->buddy(), label );
}
}


@@ -339,18 +339,13 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
bool currentFormFeature( QgsFeature &feature );
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );
void clearInvalidConstraintsMessage();
void displayInvalidConstraintMessage( const QStringList &invalidFields,
const QStringList &description );

QgsVectorLayer *mLayer = nullptr;
QgsFeature mFeature;
QgsMessageBar *mMessageBar = nullptr;
bool mOwnsMessageBar;
QgsMessageBarItem *mMultiEditUnsavedMessageBarItem = nullptr;
QgsMessageBarItem *mMultiEditMessageBarItem = nullptr;
QLabel *mInvalidConstraintMessage = nullptr;
QWidget *mTopMessageWidget = nullptr;
QList<QgsWidgetWrapper *> mWidgets;
QgsAttributeEditorContext mContext;
QDialogButtonBox *mButtonBox = nullptr;
@@ -415,8 +410,6 @@ class GUI_EXPORT QgsAttributeForm : public QWidget

Mode mMode;

//! Backlinks widgets to buddies.
QMap<QWidget *, QLabel *> mBuddyMap;
QMap<QWidget *, QSvgWidget *> mIconMap;

friend class TestQgsDualView;
@@ -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( "<font color=\"#FF9800\">✘</font>" ) );
mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
break;

case QgsEditorWidgetWrapper::ConstraintResultFailSoft:
mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">✘</font>" ) );
mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
break;

case QgsEditorWidgetWrapper::ConstraintResultPass:
mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259b24\">✔</font>" ) );
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;
}

@@ -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();
};

0 comments on commit 3eefe0d

Please sign in to comment.
You can’t perform that action at this time.