Skip to content
Permalink
Browse files

[FEATURE][composer] Option for 'wrap text on' for attribute tables

Fix #8006

Sponsored by City of Uster
  • Loading branch information
nyalldawson committed Aug 18, 2015
1 parent 759f842 commit b78564899a62d4df0252a080689a4ed5be77b371
@@ -248,6 +248,22 @@ class QgsComposerAttributeTableV2 : QgsComposerTableV2
*/
//QList<QPair<int, bool> > sortAttributes() const;

/** Sets a string to wrap the contents of the table cells by. Occurances of this string will
* be replaced by a line break.
* @param wrapString string to replace with line break
* @note added in QGIS 2.12
* @see wrapString
*/
void setWrapString( const QString& wrapString );

/** Returns the string used to wrap the contents of the table cells by. Occurances of this string will
* be replaced by a line break.
* @returns string which will be replaced with line break
* @note added in QGIS 2.12
* @see setWrapString
*/
QString wrapString() const;

/** Queries the attribute table's vector layer for attributes to show in the table.
* @param contents table content
* @returns true if attributes were successfully fetched
@@ -518,6 +518,7 @@ void QgsComposerAttributeTableWidget::updateGuiElements()
mEmptyMessageLineEdit->setEnabled( mComposerTable->emptyTableBehaviour() == QgsComposerTableV2::ShowMessage );
mEmptyMessageLabel->setEnabled( mComposerTable->emptyTableBehaviour() == QgsComposerTableV2::ShowMessage );
mDrawEmptyCheckBox->setChecked( mComposerTable->showEmptyRows() );
mWrapStringLineEdit->setText( mComposerTable->wrapString() );

mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mComposerTable->resizeMode() ) );
mAddFramePushButton->setEnabled( mComposerTable->resizeMode() == QgsComposerMultiFrame::UseExistingFrames );
@@ -634,6 +635,7 @@ void QgsComposerAttributeTableWidget::blockAllSignals( bool b )
mEmptyFrameCheckBox->blockSignals( b );
mHideEmptyBgCheckBox->blockSignals( b );
mDrawEmptyCheckBox->blockSignals( b );
mWrapStringLineEdit->blockSignals( b );
}

void QgsComposerAttributeTableWidget::setMaximumNumberOfFeatures( int n )
@@ -852,6 +854,25 @@ void QgsComposerAttributeTableWidget::on_mHeaderModeComboBox_currentIndexChanged
}
}

void QgsComposerAttributeTableWidget::on_mWrapStringLineEdit_editingFinished()
{
if ( !mComposerTable )
{
return;
}

QgsComposition* composition = mComposerTable->composition();
if ( composition )
{
composition->beginMultiFrameCommand( mComposerTable, tr( "Table wrap string changed" ) );
}
mComposerTable->setWrapString( mWrapStringLineEdit->text() );
if ( composition )
{
composition->endMultiFrameCommand();
}
}

void QgsComposerAttributeTableWidget::changeLayer( QgsMapLayer *layer )
{
if ( !mComposerTable )
@@ -66,6 +66,7 @@ class QgsComposerAttributeTableWidget: public QgsComposerItemBaseWidget, private
void on_mFeatureFilterButton_clicked();
void on_mHeaderHAlignmentComboBox_currentIndexChanged( int index );
void on_mHeaderModeComboBox_currentIndexChanged( int index );
void on_mWrapStringLineEdit_editingFinished();
void changeLayer( QgsMapLayer* layer );
void on_mAddFramePushButton_clicked();
void on_mResizeModeComboBox_currentIndexChanged( int index );
@@ -545,7 +545,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
int idx = layer->fieldNameIndex(( *columnIt )->attribute() );
if ( idx != -1 )
{
currentRow << f.attributes()[idx];
currentRow << replaceWrapChar( f.attributes()[idx] );
}
else
{
@@ -579,6 +579,17 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
return true;
}

QVariant QgsComposerAttributeTableV2::replaceWrapChar( const QVariant &variant ) const
{
//avoid converting variants to string if not required (try to maintain original type for sorting)
if ( mWrapString.isEmpty() || !variant.toString().contains( mWrapString ) )
return variant;

QString replaced = variant.toString();
replaced.replace( mWrapString, "\n" );
return replaced;
}

QgsVectorLayer *QgsComposerAttributeTableV2::sourceLayer()
{
switch ( mSource )
@@ -645,6 +656,18 @@ QList<QPair<int, bool> > QgsComposerAttributeTableV2::sortAttributes() const
return attributesBySortRank;
}

void QgsComposerAttributeTableV2::setWrapString( const QString &wrapString )
{
if ( wrapString == mWrapString )
{
return;
}

mWrapString = wrapString;
refreshAttributes();
emit changed();
}

bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{
QDomElement composerTableElem = doc.createElement( "ComposerAttributeTableV2" );
@@ -656,6 +679,7 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
composerTableElem.setAttribute( "maxFeatures", mMaximumNumberOfFeatures );
composerTableElem.setAttribute( "filterFeatures", mFilterFeatures ? "true" : "false" );
composerTableElem.setAttribute( "featureFilter", mFeatureFilter );
composerTableElem.setAttribute( "wrapString", mWrapString );

if ( mComposerMap )
{
@@ -711,6 +735,7 @@ bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QD
mFilterFeatures = itemElem.attribute( "filterFeatures", "false" ) == "true" ? true : false;
mFeatureFilter = itemElem.attribute( "featureFilter", "" );
mMaximumNumberOfFeatures = itemElem.attribute( "maxFeatures", "5" ).toInt();
mWrapString = itemElem.attribute( "wrapString" );

//composer map
int composerMapId = itemElem.attribute( "composerMap", "-1" ).toInt();
@@ -270,6 +270,22 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
*/
QList<QPair<int, bool> > sortAttributes() const;

/** Sets a string to wrap the contents of the table cells by. Occurances of this string will
* be replaced by a line break.
* @param wrapString string to replace with line break
* @note added in QGIS 2.12
* @see wrapString
*/
void setWrapString( const QString& wrapString );

/** Returns the string used to wrap the contents of the table cells by. Occurances of this string will
* be replaced by a line break.
* @returns string which will be replaced with line break
* @note added in QGIS 2.12
* @see setWrapString
*/
QString wrapString() const { return mWrapString; }

/** Queries the attribute table's vector layer for attributes to show in the table.
* @param contents table content
* @returns true if attributes were successfully fetched
@@ -308,6 +324,8 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
/** Feature filter expression*/
QString mFeatureFilter;

QString mWrapString;

/** Returns a list of attribute indices corresponding to displayed fields in the table.
* @note kept for compatibility with 2.0 api only
*/
@@ -319,6 +337,11 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
*/
void restoreFieldAliasMap( const QMap<int, QString>& map );

/** Replaces occurences of the wrap character with line breaks.
* @param text input text
*/
QVariant replaceWrapChar( const QVariant &variant ) const;

private slots:
/** Checks if this vector layer will be removed (and sets mVectorLayer to 0 if yes) */
void removeLayer( QString layerId );
@@ -54,9 +54,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-433</y>
<width>392</width>
<height>1192</height>
<height>1225</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
@@ -258,39 +258,6 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<item row="2" column="0">
<widget class="QLabel" name="mMarginLabel">
<property name="text">
<string>Cell margins</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="buddy">
<cstring>mMarginSpinBox</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QgsDoubleSpinBox" name="mMarginSpinBox">
<property name="suffix">
<string> mm</string>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Display header</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="mHeaderModeComboBox">
<item>
@@ -317,26 +284,6 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="mEmptyModeComboBox"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mEmptyMessageLabel">
<property name="text">
<string>Message to display</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="mEmptyMessageLineEdit"/>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Background color</string>
</property>
</widget>
</item>
<item row="7" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
@@ -373,13 +320,83 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mMarginLabel">
<property name="text">
<string>Cell margins</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="buddy">
<cstring>mMarginSpinBox</cstring>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Display header</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QgsDoubleSpinBox" name="mMarginSpinBox">
<property name="suffix">
<string> mm</string>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="mEmptyMessageLineEdit"/>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="mDrawEmptyCheckBox">
<property name="text">
<string>Show empty rows</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Background color</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mEmptyMessageLabel">
<property name="text">
<string>Message to display</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="mEmptyModeComboBox"/>
</item>
<item row="8" column="1">
<widget class="QLineEdit" name="mWrapStringLineEdit">
<property name="frame">
<bool>true</bool>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_11">
<property name="text">
<string>Wrap text on</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -735,16 +752,6 @@
<header>qgscolorbuttonv2.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBoxBasic</class>
<extends>QGroupBox</extends>
@@ -756,6 +763,16 @@
<extends>QComboBox</extends>
<header>qgsmaplayercombobox.h</header>
</customwidget>
<customwidget>
<class>QgsDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
@@ -781,6 +798,7 @@
<tabstop>mEmptyModeComboBox</tabstop>
<tabstop>mEmptyMessageLineEdit</tabstop>
<tabstop>mBackgroundColorButton</tabstop>
<tabstop>mWrapStringLineEdit</tabstop>
<tabstop>mShowGridGroupCheckBox</tabstop>
<tabstop>mGridStrokeWidthSpinBox</tabstop>
<tabstop>mGridColorButton</tabstop>

0 comments on commit b785648

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