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
910QgsDataDefinedSymbolDialog::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
7351QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog ()
@@ -78,95 +56,30 @@ QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog()
7856QMap< 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-
17083QString QgsDataDefinedSymbolDialog::doubleHelpText ()
17184{
17285 return tr ( " double" );
0 commit comments