1
1
#include " qgsdatadefinedsymboldialog.h"
2
2
#include " qgsexpressionbuilderdialog.h"
3
+ #include " qgsfieldexpressionwidget.h"
3
4
#include " qgsvectorlayer.h"
4
5
#include " qgslogger.h"
6
+
5
7
#include < QCheckBox>
6
- #include < QComboBox>
7
- #include < QPushButton>
8
+
8
9
9
10
QgsDataDefinedSymbolDialog::QgsDataDefinedSymbolDialog ( const QList< DataDefinedSymbolEntry >& entries, const QgsVectorLayer* vl, QWidget * parent, Qt::WindowFlags f )
10
11
: QDialog( parent, f )
@@ -18,56 +19,33 @@ QgsDataDefinedSymbolDialog::QgsDataDefinedSymbolDialog( const QList< DataDefined
18
19
attributeFields = mVectorLayer ->pendingFields ();
19
20
}
20
21
21
- mTableWidget ->setRowCount ( entries.size () );
22
-
23
22
int i = 0 ;
24
23
QList< DataDefinedSymbolEntry >::const_iterator entryIt = entries.constBegin ();
25
24
for ( ; entryIt != entries.constEnd (); ++entryIt )
26
25
{
26
+ QTreeWidgetItem* item = new QTreeWidgetItem ( mTreeWidget );
27
+
27
28
// check box
28
- QCheckBox* cb = new QCheckBox ( this );
29
+ QCheckBox* cb = new QCheckBox ( entryIt-> title , this );
29
30
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 );
57
33
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 );
64
39
65
40
// help text
66
- QTableWidgetItem* helpItem = new QTableWidgetItem ( entryIt->helpText );
67
- mTableWidget ->setItem ( i, 4 , helpItem );
41
+ item->setText ( 2 , entryIt->helpText );
68
42
43
+ mTreeWidget ->addTopLevelItem ( item );
69
44
++i;
70
45
}
46
+
47
+ for ( int c = 0 ; c != mTreeWidget ->columnCount () - 1 ; c++ )
48
+ mTreeWidget ->resizeColumnToContents ( c );
71
49
}
72
50
73
51
QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog ()
@@ -78,95 +56,30 @@ QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog()
78
56
QMap< QString, QString > QgsDataDefinedSymbolDialog::dataDefinedProperties () const
79
57
{
80
58
QMap< QString, QString > propertyMap;
81
- int rowCount = mTableWidget -> rowCount ();
59
+ int rowCount = mTreeWidget -> topLevelItemCount ();
82
60
for ( int i = 0 ; i < rowCount; ++i )
83
61
{
62
+ QTreeWidgetItem* item = mTreeWidget ->topLevelItem ( i );
84
63
// property
85
- QString propertyKey = mTableWidget -> item ( i, 1 ) ->data ( Qt::UserRole ).toString ();
64
+ QString propertyKey = item->data ( 0 , Qt::UserRole ).toString ();
86
65
// checked?
87
66
bool checked = false ;
88
- QCheckBox* cb = qobject_cast<QCheckBox*>( mTableWidget -> cellWidget ( i , 0 ) );
67
+ QCheckBox* cb = qobject_cast<QCheckBox*>( mTreeWidget -> itemWidget ( item , 0 ) );
89
68
if ( cb )
90
69
{
91
70
checked = cb->isChecked ();
92
71
}
93
72
QString expressionString;
94
73
if ( checked )
95
74
{
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 ();
102
77
}
103
78
propertyMap.insert ( propertyKey, expressionString );
104
79
}
105
80
return propertyMap;
106
81
}
107
82
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
-
170
83
QString QgsDataDefinedSymbolDialog::doubleHelpText ()
171
84
{
172
85
return tr ( " double" );
0 commit comments