/
qgsdatadefinedsymboldialog.cpp
130 lines (116 loc) · 3.89 KB
/
qgsdatadefinedsymboldialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "qgsdatadefinedsymboldialog.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsvectorlayer.h"
#include <QCheckBox>
#include <QComboBox>
#include <QPushButton>
QgsDataDefinedSymbolDialog::QgsDataDefinedSymbolDialog( const QMap< QString, QPair< QString, QString > >& properties, const QgsVectorLayer* vl,
QWidget* parent, Qt::WindowFlags f ): QDialog( parent, f ), mVectorLayer( vl )
{
setupUi( this );
QgsFields attributeFields;
if ( mVectorLayer )
{
attributeFields = mVectorLayer->pendingFields();
}
mTableWidget->setRowCount( properties.size() );
int i = 0;
QMap< QString, QPair< QString, QString > >::const_iterator it = properties.constBegin();
for ( ; it != properties.constEnd(); ++it )
{
//check box
mTableWidget->setCellWidget( i, 0, new QCheckBox( this ) );
//property name
QTableWidgetItem* propertyItem = new QTableWidgetItem( it.value().first );
propertyItem->setData( Qt::UserRole, it.key() );
mTableWidget->setItem( i, 1, propertyItem );
//attribute list
QComboBox* attributeComboBox = new QComboBox( this );
attributeComboBox->addItem( it.value().second );
for ( int j = 0; j < attributeFields.count(); ++j )
{
attributeComboBox->addItem( attributeFields.at( j ).name() );
}
mTableWidget->setCellWidget( i, 2, attributeComboBox );
//expression button
QPushButton* expressionButton = new QPushButton( "...", this );
QObject::connect( expressionButton, SIGNAL( clicked() ), this, SLOT( expressionButtonClicked() ) );
mTableWidget->setCellWidget( i, 3, expressionButton );
++i;
}
//connect( this, SIGNAL( cellClicked( int, int ) ), this, SLOT( handleCellClick(int,int) ) );
}
QgsDataDefinedSymbolDialog::~QgsDataDefinedSymbolDialog()
{
}
QMap< QString, QString > QgsDataDefinedSymbolDialog::dataDefinedProperties() const
{
QMap< QString, QString > propertyMap;
int rowCount = mTableWidget->rowCount();
for ( int i = 0; i < rowCount; ++i )
{
//property
QString propertyKey = mTableWidget->item( i, 1 )->data( Qt::UserRole ).toString();
//checked?
bool checked = false;
QCheckBox* cb = qobject_cast<QCheckBox*>( mTableWidget->cellWidget( i, 0 ) );
if ( cb )
{
checked = cb->isChecked();
}
QString expressionString;
if ( checked )
{
QComboBox* comboBox = qobject_cast<QComboBox*>( mTableWidget->cellWidget( i, 2 ) );
expressionString = comboBox->currentText();
if ( comboBox->currentIndex() > 0 )
{
expressionString.prepend( "\"" ).append( "\"" );
}
}
propertyMap.insert( propertyKey, expressionString );
}
return propertyMap;
}
void QgsDataDefinedSymbolDialog::expressionButtonClicked()
{
qWarning( "Expression button clicked" );
//find out row
QObject* senderObj = sender();
int row = 0;
for ( ; row < mTableWidget->rowCount(); ++row )
{
if ( senderObj == mTableWidget->cellWidget( row, 3 ) )
{
break;
}
}
QComboBox* attributeCombo = qobject_cast<QComboBox*>( mTableWidget->cellWidget( row, 2 ) );
if ( !attributeCombo )
{
return;
}
QString previousText = attributeCombo->itemText( attributeCombo->currentIndex() );
if ( attributeCombo->currentIndex() > 0 )
{
previousText.prepend( "\"" ).append( "\"" );
}
QgsExpressionBuilderDialog d( const_cast<QgsVectorLayer*>( mVectorLayer ), previousText );
if ( d.exec() == QDialog::Accepted )
{
QString expressionString = d.expressionText();
QString attributeString = expressionString.trimmed();
attributeString.remove( 0, 1 ).chop( 1 );
int comboIndex = attributeCombo->findText( attributeString );
if ( attributeString.size() < 1 || comboIndex == -1 )
{
attributeCombo->setItemText( 0, d.expressionText() );
attributeCombo->setCurrentIndex( 0 );
}
else
{
attributeCombo->setItemText( 0, QString() );
attributeCombo->setCurrentIndex( comboIndex );
}
}
}