Skip to content

Commit

Permalink
[FIX #7543] Dual View Table/Form: Preview column not saved in project
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Apr 8, 2013
1 parent 9c9dd2b commit 57c2307
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 36 deletions.
28 changes: 28 additions & 0 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
{
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
Expand Down
21 changes: 21 additions & 0 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;

Expand Down
98 changes: 63 additions & 35 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 )
Expand All @@ -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
{
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
11 changes: 11 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
*
Expand Down
10 changes: 9 additions & 1 deletion src/gui/attributetable/qgsfeaturelistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/gui/attributetable/qgsfeaturelistview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57c2307

Please sign in to comment.