Skip to content
Permalink
Browse files

[FIX #7543] Dual View Table/Form: Preview column not saved in project

  • Loading branch information
m-kuhn committed Apr 8, 2013
1 parent 9c9dd2b commit 57c23078c7981391a9ce058690fdc832dad9dde5
@@ -1511,6 +1511,18 @@ bool QgsVectorLayer::readXml( const QDomNode& layer_node )
updateFields();
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( checkJoinLayerRemove( QString ) ) );

QDomNode prevExpNode = layer_node.namedItem( "previewExpression" );

if( prevExpNode.isNull() )
{
mDisplayExpression = "";
}
else
{
QDomElement prevExpElem = prevExpNode.toElement();
mDisplayExpression = prevExpElem.text();
}

QString errorMsg;
if ( !readSymbology( layer_node, errorMsg ) )
{
@@ -1658,6 +1670,12 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
layer_node.appendChild( provider );
}

// save preview expression
QDomElement prevExpElem = document.createElement( "previewExpression" );
QDomText prevExpText = document.createTextNode( mDisplayExpression );
prevExpElem.appendChild( prevExpText );
layer_node.appendChild( prevExpElem );

//save joins
mJoinBuffer->writeXml( layer_node, document );

@@ -2790,6 +2808,16 @@ const QString QgsVectorLayer::displayField() const
return mDisplayField;
}

void QgsVectorLayer::setDisplayExpression( const QString displayExpression )
{
mDisplayExpression = displayExpression;
}

const QString QgsVectorLayer::displayExpression()
{
return mDisplayExpression;
}

bool QgsVectorLayer::isEditable() const
{
return ( mEditBuffer && mDataProvider );
@@ -257,6 +257,24 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Returns the primary display field name used in the identify results dialog */
const QString displayField() const;

/** Set the preview expression, used to create a human readable preview string.
* Used e.g. in the attribute table feature list. Uses @link {QgsExpression}.
*
* @param previewExpression The expression which will be used to preview features
* for this layer
* @note added in 2.0
*/
void setDisplayExpression( const QString displayExpression );

/**
* Get the preview expression, used to create a human readable preview string.
* Uses @link {QgsExpression}.
*
* @return The expression which will be used to preview features for this layer
* @note added in 2.0
*/
const QString displayExpression();

/** Returns the data provider */
QgsVectorDataProvider* dataProvider();

@@ -973,6 +991,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** index of the primary label field */
QString mDisplayField;

/** the preview expression used to generate a human readable preview string for features */
QString mDisplayExpression;

/** Data provider key */
QString mProviderKey;

@@ -46,6 +46,7 @@ QgsDualView::QgsDualView( QWidget* parent )
// Connect layer list preview signals
connect( mActionExpressionPreview, SIGNAL( triggered() ), SLOT( previewExpressionBuilder() ) );
connect( mPreviewActionMapper, SIGNAL( mapped( QObject* ) ), SLOT( previewColumnChanged( QObject* ) ) );
connect( mFeatureList, SIGNAL( displayExpressionChanged(QString) ), this, SLOT( previewExpressionChanged(QString) ) );
}

QgsDualView::~QgsDualView()
@@ -76,6 +77,54 @@ void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, QgsDista

void QgsDualView::columnBoxInit()
{
// load fields
QList<QgsField> fields = mLayerCache->layer()->pendingFields().toList();

// default expression: saved value
QString displayExpression = mLayerCache->layer()->displayExpression();

// if no display expression is saved: use display field instead
if ( displayExpression == "" )
{
displayExpression = mLayerCache->layer()->displayField();
}

// if neither diaplay expression nor display field is saved...
if ( displayExpression == "" )
{
QgsAttributeList pkAttrs = mLayerCache->layer()->pendingPkAttributesList();

if ( pkAttrs.size() > 0 )
{
// ... If there are primary key(s) defined
QStringList pkFields;

foreach ( int attr, pkAttrs )
{
pkFields.append( "\"" + fields[attr].name() + "\"" );
}

displayExpression = pkFields.join( "||', '||" );
}
else if ( fields.size() > 0 )
{
// ... concat all fields
QStringList fieldNames;
foreach ( QgsField field, fields )
{
fieldNames.append( "\"" + field.name() + "\"" );
}

displayExpression = fieldNames.join( "||', '||" );
}
else
{
// ... there isn't really much to display
displayExpression = "[Please define preview text]";
}
}

// now initialise the menu
QList< QAction* > previewActions = mFeatureListPreviewButton->actions();
foreach ( QAction* a, previewActions )
{
@@ -89,8 +138,6 @@ void QgsDualView::columnBoxInit()
mFeatureListPreviewButton->addAction( mActionExpressionPreview );
mFeatureListPreviewButton->addAction( mActionPreviewColumnsMenu );

QList<QgsField> fields = mLayerCache->layer()->pendingFields().toList();

foreach ( const QgsField field, fields )
{
if ( mLayerCache->layer()->editType( mLayerCache->layer()->fieldNameIndex( field.name() ) ) != QgsVectorLayer::Hidden )
@@ -104,45 +151,18 @@ void QgsDualView::columnBoxInit()
connect( previewAction, SIGNAL( triggered() ), mPreviewActionMapper, SLOT( map() ) );
mPreviewColumnsMenu->addAction( previewAction );

if ( text == mLayerCache->layer()->displayField() )
if ( text == displayExpression )
{
mFeatureListPreviewButton->setDefaultAction( previewAction );
}
}
}

// Most likely no displayField is defined
// Join primary key fields
// If there is no single field found as preview
if ( !mFeatureListPreviewButton->defaultAction() )
{
mFeatureList->setDisplayExpression( displayExpression );
mFeatureListPreviewButton->setDefaultAction( mActionExpressionPreview );
QgsAttributeList pkAttrs = mLayerCache->layer()->pendingPkAttributesList();
// If there is a primary key defined
if ( pkAttrs.size() > 0 )
{
QStringList pkFields;

foreach ( int attr, pkAttrs )
{
pkFields.append( "\"" + fields[attr].name() + "\"" );
}

mFeatureList->setDisplayExpression( pkFields.join( "||', '||" ) );
}
else if ( fields.size() > 0 )
{
QStringList fieldNames;
foreach ( QgsField field, fields )
{
fieldNames.append( "\"" + field.name() + "\"" );
}

mFeatureList->setDisplayExpression( fieldNames.join( "||', '||" ) );
}
else
{
mFeatureList->setDisplayExpression( "[Please define preview text]" );
}
}
else
{
@@ -193,6 +213,7 @@ void QgsDualView::initModels( QgsMapCanvas* mapCanvas )
mFilterModel = new QgsAttributeTableFilterModel( mapCanvas, mMasterModel, mMasterModel );

connect( mFilterModel, SIGNAL( filterInvalidated() ), this, SIGNAL( filterChanged() ) );
connect( mFeatureList, SIGNAL( displayExpressionChanged(QString) ), this, SIGNAL( displayExpressionChanged(QString) ) );

mFeatureListModel = new QgsFeatureListModel( mFilterModel, mFilterModel );
}
@@ -275,9 +296,11 @@ void QgsDualView::previewColumnChanged( QObject* action )
.arg( mFeatureList->parserErrorString() )
);
}

mFeatureListPreviewButton->setDefaultAction( previewAction );
mFeatureListPreviewButton->setPopupMode( QToolButton::InstantPopup );
else
{
mFeatureListPreviewButton->setDefaultAction( previewAction );
mFeatureListPreviewButton->setPopupMode( QToolButton::InstantPopup );
}
}

Q_ASSERT( previewAction );
@@ -328,6 +351,11 @@ void QgsDualView::viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex )
menu->addAction( tr( "Open form" ), a, SLOT( featureForm() ) );
}

void QgsDualView::previewExpressionChanged( const QString expression )
{
mLayerCache->layer()->setDisplayExpression( expression );
}

void QgsDualView::setFilteredFeatures( QgsFeatureIds filteredFeatures )
{
mFilterModel->setFilteredFeatures( filteredFeatures );
@@ -98,6 +98,15 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
void setCurrentEditSelection( const QgsFeatureIds& fids );

signals:
/**
* Is emitted, whenever the display expression is successfully changed
* @param The expression that was applied
*/
void displayExpressionChanged( const QString expression );

/**
* Is emitted, whenever the filter changes
*/
void filterChanged();

private slots:
@@ -114,6 +123,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex );

void previewExpressionChanged( const QString expression );

/**
* Will be called periodically, when loading layers from slow data providers.
*
@@ -73,7 +73,15 @@ void QgsFeatureListView::setModel( QgsFeatureListModel* featureListModel )

bool QgsFeatureListView::setDisplayExpression( const QString expression )
{
return mModel->setDisplayExpression( expression );
if ( mModel->setDisplayExpression( expression ) )
{
emit displayExpressionChanged( expression );
return true;
}
else
{
return false;
}
}

const QString& QgsFeatureListView::displayExpression() const
@@ -111,6 +111,12 @@ class GUI_EXPORT QgsFeatureListView : public QListView
*/
void currentEditSelectionChanged( QgsFeature &feat );

/**
* Is emitted, whenever the display expression is successfully changed
* @param The expression that was applied
*/
void displayExpressionChanged( const QString expression );

public slots:
/**
* Set the feature(s) to be edited

0 comments on commit 57c2307

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