Skip to content

Commit f9dcc37

Browse files
committed
use QgsFieldExpressionWidget in data defined properties
1 parent dc905ac commit f9dcc37

File tree

4 files changed

+43
-152
lines changed

4 files changed

+43
-152
lines changed

src/gui/symbology-ng/qgsdatadefinedsymboldialog.cpp

Lines changed: 24 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "qgsdatadefinedsymboldialog.h"
22
#include "qgsexpressionbuilderdialog.h"
3+
#include "qgsfieldexpressionwidget.h"
34
#include "qgsvectorlayer.h"
45
#include "qgslogger.h"
6+
57
#include <QCheckBox>
6-
#include <QComboBox>
7-
#include <QPushButton>
8+
89

910
QgsDataDefinedSymbolDialog::QgsDataDefinedSymbolDialog( const QList< DataDefinedSymbolEntry >& entries, const QgsVectorLayer* vl, QWidget * parent, Qt::WindowFlags f )
1011
: QDialog( parent, f )
@@ -18,56 +19,33 @@ QgsDataDefinedSymbolDialog::QgsDataDefinedSymbolDialog( const QList< DataDefined
1819
attributeFields = mVectorLayer->pendingFields();
1920
}
2021

21-
mTableWidget->setRowCount( entries.size() );
22-
2322
int i = 0;
2423
QList< DataDefinedSymbolEntry >::const_iterator entryIt = entries.constBegin();
2524
for ( ; entryIt != entries.constEnd(); ++entryIt )
2625
{
26+
QTreeWidgetItem* item = new QTreeWidgetItem( mTreeWidget );
27+
2728
//check box
28-
QCheckBox* cb = new QCheckBox( this );
29+
QCheckBox* cb = new QCheckBox( entryIt->title, this );
2930
cb->setChecked( !entryIt->initialValue.isEmpty() );
30-
mTableWidget->setCellWidget( i, 0, cb );
31-
mTableWidget->setColumnWidth( 0, cb->width() );
32-
33-
34-
//property name
35-
QTableWidgetItem* propertyItem = new QTableWidgetItem( entryIt->title );
36-
propertyItem->setData( Qt::UserRole, entryIt->property );
37-
mTableWidget->setItem( i, 1, propertyItem );
38-
39-
//attribute list
40-
QString expressionString = entryIt->initialValue;
41-
QComboBox* attributeComboBox = new QComboBox( this );
42-
attributeComboBox->addItem( QString() );
43-
for ( int j = 0; j < attributeFields.count(); ++j )
44-
{
45-
attributeComboBox->addItem( attributeFields.at( j ).name() );
46-
}
47-
48-
int attrComboIndex = comboIndexForExpressionString( expressionString, attributeComboBox );
49-
if ( attrComboIndex >= 0 )
50-
{
51-
attributeComboBox->setCurrentIndex( attrComboIndex );
52-
}
53-
else
54-
{
55-
attributeComboBox->setItemText( 0, expressionString );
56-
}
31+
item->setData( 0, Qt::UserRole, entryIt->property );
32+
mTreeWidget->setItemWidget( item, 0, cb );
5733

58-
mTableWidget->setCellWidget( i, 2, attributeComboBox );
59-
60-
//expression button
61-
QPushButton* expressionButton = new QPushButton( "...", this );
62-
QObject::connect( expressionButton, SIGNAL( clicked() ), this, SLOT( expressionButtonClicked() ) );
63-
mTableWidget->setCellWidget( i, 3, expressionButton );
34+
// expression
35+
QgsFieldExpressionWidget* few = new QgsFieldExpressionWidget( this );
36+
few->setLayer( const_cast<QgsVectorLayer*>( vl ) );
37+
few->setField( entryIt->initialValue );
38+
mTreeWidget->setItemWidget( item, 1, few );
6439

6540
//help text
66-
QTableWidgetItem* helpItem = new QTableWidgetItem( entryIt->helpText );
67-
mTableWidget->setItem( i, 4, helpItem );
41+
item->setText( 2, entryIt->helpText );
6842

43+
mTreeWidget->addTopLevelItem( item );
6944
++i;
7045
}
46+
47+
for ( int c = 0; c != mTreeWidget->columnCount() - 1; c++ )
48+
mTreeWidget->resizeColumnToContents( c );
7149
}
7250

7351
QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog()
@@ -78,95 +56,30 @@ QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog()
7856
QMap< QString, QString > QgsDataDefinedSymbolDialog::dataDefinedProperties() const
7957
{
8058
QMap< QString, QString > propertyMap;
81-
int rowCount = mTableWidget->rowCount();
59+
int rowCount = mTreeWidget->topLevelItemCount();
8260
for ( int i = 0; i < rowCount; ++i )
8361
{
62+
QTreeWidgetItem* item = mTreeWidget->topLevelItem( i );
8463
//property
85-
QString propertyKey = mTableWidget->item( i, 1 )->data( Qt::UserRole ).toString();
64+
QString propertyKey = item->data( 0, Qt::UserRole ).toString();
8665
//checked?
8766
bool checked = false;
88-
QCheckBox* cb = qobject_cast<QCheckBox*>( mTableWidget->cellWidget( i, 0 ) );
67+
QCheckBox* cb = qobject_cast<QCheckBox*>( mTreeWidget->itemWidget( item, 0 ) );
8968
if ( cb )
9069
{
9170
checked = cb->isChecked();
9271
}
9372
QString expressionString;
9473
if ( checked )
9574
{
96-
QComboBox* comboBox = qobject_cast<QComboBox*>( mTableWidget->cellWidget( i, 2 ) );
97-
expressionString = comboBox->currentText();
98-
if ( comboBox->currentIndex() > 0 )
99-
{
100-
expressionString.prepend( "\"" ).append( "\"" );
101-
}
75+
QgsFieldExpressionWidget* few = qobject_cast<QgsFieldExpressionWidget*>( mTreeWidget->itemWidget( item, 1 ) );
76+
expressionString = few->currentField();
10277
}
10378
propertyMap.insert( propertyKey, expressionString );
10479
}
10580
return propertyMap;
10681
}
10782

108-
void QgsDataDefinedSymbolDialog::expressionButtonClicked()
109-
{
110-
QgsDebugMsg( "Expression button clicked" );
111-
112-
//find out row
113-
QObject* senderObj = sender();
114-
int row = 0;
115-
for ( ; row < mTableWidget->rowCount(); ++row )
116-
{
117-
if ( senderObj == mTableWidget->cellWidget( row, 3 ) )
118-
{
119-
break;
120-
}
121-
}
122-
123-
QComboBox* attributeCombo = qobject_cast<QComboBox*>( mTableWidget->cellWidget( row, 2 ) );
124-
if ( !attributeCombo )
125-
{
126-
return;
127-
}
128-
129-
QString previousText = attributeCombo->itemText( attributeCombo->currentIndex() );
130-
if ( attributeCombo->currentIndex() > 0 )
131-
{
132-
previousText.prepend( "\"" ).append( "\"" );
133-
}
134-
135-
QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer*>( mVectorLayer ), previousText );
136-
if ( d.exec() == QDialog::Accepted )
137-
{
138-
QString expressionString = d.expressionText();
139-
int comboIndex = comboIndexForExpressionString( d.expressionText(), attributeCombo );
140-
141-
if ( comboIndex == -1 )
142-
{
143-
attributeCombo->setItemText( 0, d.expressionText() );
144-
attributeCombo->setCurrentIndex( 0 );
145-
Q_ASSERT( d.expressionText() == attributeCombo->currentText() );
146-
}
147-
else
148-
{
149-
if ( comboIndex != 0 )
150-
{
151-
attributeCombo->setItemText( 0, QString() );
152-
}
153-
attributeCombo->setCurrentIndex( comboIndex );
154-
}
155-
}
156-
}
157-
158-
int QgsDataDefinedSymbolDialog::comboIndexForExpressionString( const QString& expr, const QComboBox* cb )
159-
{
160-
QString attributeString = expr.trimmed();
161-
int comboIndex = cb->findText( attributeString );
162-
if ( comboIndex == -1 && attributeString.startsWith( '"' ) && attributeString.endsWith( '"' ) )
163-
{
164-
attributeString.remove( 0, 1 ).chop( 1 );
165-
comboIndex = cb->findText( attributeString );
166-
}
167-
return comboIndex;
168-
}
169-
17083
QString QgsDataDefinedSymbolDialog::doubleHelpText()
17184
{
17285
return tr( "double" );

src/gui/symbology-ng/qgsdatadefinedsymboldialog.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,8 @@ class GUI_EXPORT QgsDataDefinedSymbolDialog: public QDialog, private Ui::QgsData
4040
static QString gradientSpreadHelpText();
4141
static QString boolHelpText();
4242

43-
private slots:
44-
void expressionButtonClicked();
45-
4643
private:
4744
const QgsVectorLayer* mVectorLayer;
48-
49-
/**Tries to fiend a combo box field for an expression string (considering whitespaces, brackets around attribute names)
50-
@return index or -1 in case not found*/
51-
int comboIndexForExpressionString( const QString& expr, const QComboBox* cb );
5245
};
5346

5447
#endif // QGSDATADEFINEDSYMBOLLAYERDIALOG_H

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ void QgsSimpleMarkerSymbolLayerV2Widget::on_mDataDefinedPropertiesButton_clicked
513513

514514
QList< QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry > dataDefinedProperties;
515515
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "name", tr( "Name" ), mLayer->dataDefinedPropertyString( "name" ),
516-
"'square'|'rectangle'|'diamond'|'pentagon'\n|'triangle'|'equilateral_triangle'|'star'\n|'regular_star'|'arrow'|'filled_arrowhead'|'circle'\n|'cross'|'x'|'cross2'|'line'|'arrowhead'" );
516+
"'square'|'rectangle'|'diamond'|'pentagon'|'triangle'|'equilateral_triangle'|'star'|'regular_star'|'arrow'|'filled_arrowhead'|'circle'|'cross'|'x'|'cross2'|'line'|'arrowhead'" );
517517
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "color", tr( "Fill color" ), mLayer->dataDefinedPropertyString( "color" ),
518518
QgsDataDefinedSymbolDialog::colorHelpText() );
519519
dataDefinedProperties << QgsDataDefinedSymbolDialog::DataDefinedSymbolEntry( "color_border", tr( "Border color" ), mLayer->dataDefinedPropertyString( "color_border" ),

src/ui/qgsdatadefinedsymboldialogbase.ui

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>409</width>
10-
<height>299</height>
9+
<width>439</width>
10+
<height>320</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -37,60 +37,45 @@
3737
</widget>
3838
</item>
3939
<item row="0" column="0">
40-
<widget class="QTableWidget" name="mTableWidget">
40+
<widget class="QTreeWidget" name="mTreeWidget">
4141
<property name="frameShape">
4242
<enum>QFrame::NoFrame</enum>
4343
</property>
44+
<property name="alternatingRowColors">
45+
<bool>true</bool>
46+
</property>
4447
<property name="selectionMode">
4548
<enum>QAbstractItemView::NoSelection</enum>
4649
</property>
47-
<property name="showGrid">
48-
<bool>false</bool>
49-
</property>
50-
<property name="gridStyle">
51-
<enum>Qt::NoPen</enum>
52-
</property>
53-
<attribute name="horizontalHeaderVisible">
50+
<property name="wordWrap">
5451
<bool>true</bool>
52+
</property>
53+
<attribute name="headerMinimumSectionSize">
54+
<number>10</number>
5555
</attribute>
56-
<attribute name="horizontalHeaderHighlightSections">
57-
<bool>false</bool>
58-
</attribute>
59-
<attribute name="verticalHeaderVisible">
60-
<bool>false</bool>
61-
</attribute>
62-
<attribute name="verticalHeaderHighlightSections">
63-
<bool>false</bool>
64-
</attribute>
65-
<row>
66-
<property name="text">
67-
<string>New Row</string>
68-
</property>
69-
</row>
70-
<column>
71-
<property name="text">
72-
<string/>
73-
</property>
74-
</column>
7556
<column>
7657
<property name="text">
7758
<string>Property</string>
7859
</property>
79-
</column>
80-
<column>
81-
<property name="text">
82-
<string>Field</string>
60+
<property name="textAlignment">
61+
<set>AlignHCenter|AlignVCenter|AlignCenter</set>
8362
</property>
8463
</column>
8564
<column>
8665
<property name="text">
8766
<string>Expression</string>
8867
</property>
68+
<property name="textAlignment">
69+
<set>AlignHCenter|AlignVCenter|AlignCenter</set>
70+
</property>
8971
</column>
9072
<column>
9173
<property name="text">
9274
<string>Help</string>
9375
</property>
76+
<property name="textAlignment">
77+
<set>AlignHCenter|AlignVCenter|AlignCenter</set>
78+
</property>
9479
</column>
9580
</widget>
9681
</item>

0 commit comments

Comments
 (0)