Skip to content

Commit cffc257

Browse files
author
mhugent
committedNov 17, 2010
Forgot to add two files in last commit
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14698 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+393
-0
lines changed

2 files changed

+393
-0
lines changed
 

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
/***************************************************************************
2+
qgslabelpropertydialog.cpp
3+
--------------------------
4+
begin : 2010-11-12
5+
copyright : (C) 2010 by Marco Hugentobler
6+
email : marco dot hugentobler at sourcepole dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgslabelpropertydialog.h"
19+
#include "qgsmaplayerregistry.h"
20+
#include "qgsmaprenderer.h"
21+
#include <QColorDialog>
22+
#include <QFontDialog>
23+
24+
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, QgsMapRenderer* renderer, QWidget * parent, Qt::WindowFlags f ):
25+
QDialog( parent, f ), mMapRenderer( renderer ), mCurrentLabelField( -1 )
26+
{
27+
setupUi( this );
28+
fillHaliComboBox();
29+
fillValiComboBox();
30+
init( layerId, featureId );
31+
}
32+
33+
QgsLabelPropertyDialog::~QgsLabelPropertyDialog()
34+
{
35+
}
36+
37+
void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
38+
{
39+
if( !mMapRenderer )
40+
{
41+
return;
42+
}
43+
44+
//get feature attributes
45+
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
46+
if( !vlayer )
47+
{
48+
return;
49+
}
50+
51+
QgsFeature f;
52+
if( !vlayer->featureAtId( featureId, f, false, true ) )
53+
{
54+
return;
55+
}
56+
const QgsAttributeMap& attributeValues = f.attributeMap();
57+
QgsAttributeMap::const_iterator attIt = attributeValues.constBegin();
58+
59+
//get layerproperties. Problem: only for pallabeling...
60+
QgsPalLabeling* lbl = dynamic_cast<QgsPalLabeling*>( mMapRenderer->labelingEngine() );
61+
if( !lbl )
62+
{
63+
return;
64+
}
65+
66+
blockElementSignals( true );
67+
68+
//get label field and fill line edit
69+
QString labelFieldName = vlayer->customProperty( "labeling/fieldName" ).toString();
70+
if( !labelFieldName.isEmpty() )
71+
{
72+
mCurrentLabelField = vlayer->fieldNameIndex( labelFieldName );
73+
mLabelTextLineEdit->setText( attributeValues[mCurrentLabelField].toString() );
74+
const QgsFieldMap& layerFields = vlayer->pendingFields();
75+
switch( layerFields[mCurrentLabelField].type() )
76+
{
77+
case QVariant::Double:
78+
mLabelTextLineEdit->setValidator( new QDoubleValidator( this ) );
79+
break;
80+
case QVariant::Int:
81+
case QVariant::UInt:
82+
case QVariant::LongLong:
83+
mLabelTextLineEdit->setValidator( new QIntValidator( this ) );
84+
break;
85+
}
86+
}
87+
88+
//get attributes of the feature and fill data defined values
89+
QgsPalLayerSettings& layerSettings = lbl->layer( layerId );
90+
mLabelFont = layerSettings.textFont;
91+
92+
//set all the gui elements to the default values
93+
mFontSizeSpinBox->setValue( layerSettings.textFont.pointSizeF() );
94+
mBufferColorButton->setColor( layerSettings.textColor );
95+
mLabelDistanceSpinBox->setValue( layerSettings.dist );
96+
mBufferSizeSpinBox->setValue( layerSettings.bufferSize );
97+
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText("Left") );
98+
mValiComboBox->setCurrentIndex( mValiComboBox->findText("Bottom") );
99+
100+
disableGuiElements();
101+
102+
mDataDefinedProperties = layerSettings.dataDefinedProperties;
103+
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator propIt = mDataDefinedProperties.constBegin();
104+
105+
for(; propIt != mDataDefinedProperties.constEnd(); ++propIt )
106+
{
107+
switch( propIt.key() )
108+
{
109+
case QgsPalLayerSettings::Size:
110+
mFontSizeSpinBox->setEnabled( true );
111+
mLabelFont.setPointSizeF( attributeValues[propIt.value()].toDouble() );
112+
mFontSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
113+
break;
114+
case QgsPalLayerSettings::BufferSize:
115+
mBufferSizeSpinBox->setEnabled( true );
116+
mBufferSizeSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
117+
break;
118+
case QgsPalLayerSettings::PositionX:
119+
mXCoordSpinBox->setEnabled( true );
120+
mXCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
121+
break;
122+
case QgsPalLayerSettings::PositionY:
123+
mYCoordSpinBox->setEnabled( true );
124+
mYCoordSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
125+
break;
126+
case QgsPalLayerSettings::LabelDistance:
127+
mLabelDistanceSpinBox->setEnabled( true );
128+
mLabelDistanceSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
129+
break;
130+
case QgsPalLayerSettings::Hali:
131+
mHaliComboBox->setEnabled( true );
132+
mHaliComboBox->setCurrentIndex( mHaliComboBox->findText( attributeValues[propIt.value()].toString() ) );
133+
break;
134+
case QgsPalLayerSettings::Vali:
135+
mValiComboBox->setEnabled( true );
136+
mValiComboBox->setCurrentIndex( mValiComboBox->findText( attributeValues[propIt.value()].toString() ) );
137+
break;
138+
case QgsPalLayerSettings::BufferColor:
139+
mBufferColorButton->setEnabled( true );
140+
mBufferColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) );
141+
break;
142+
case QgsPalLayerSettings::Color:
143+
mFontColorButton->setEnabled( true );
144+
mFontColorButton->setColor( QColor( attributeValues[propIt.value()].toString() ) );
145+
break;
146+
case QgsPalLayerSettings::Rotation:
147+
mRotationSpinBox->setEnabled( true );
148+
mRotationSpinBox->setValue( attributeValues[propIt.value()].toDouble() );
149+
break;
150+
151+
//font related properties
152+
case QgsPalLayerSettings::Bold:
153+
mLabelFont.setBold( attributeValues[propIt.value()].toBool() );
154+
break;
155+
case QgsPalLayerSettings::Italic:
156+
mLabelFont.setItalic( attributeValues[propIt.value()].toBool() );
157+
break;
158+
case QgsPalLayerSettings::Underline:
159+
mLabelFont.setUnderline( attributeValues[propIt.value()].toBool() );
160+
break;
161+
case QgsPalLayerSettings::Strikeout:
162+
mLabelFont.setStrikeOut( attributeValues[propIt.value()].toBool() );
163+
break;
164+
case QgsPalLayerSettings::Family:
165+
mLabelFont.setFamily( attributeValues[propIt.value()].toString() );
166+
break;
167+
}
168+
}
169+
mFontPushButton->setEnabled( labelFontEditingPossible() );
170+
blockElementSignals( false );
171+
}
172+
173+
void QgsLabelPropertyDialog::disableGuiElements()
174+
{
175+
mFontSizeSpinBox->setEnabled( false );
176+
mBufferSizeSpinBox->setEnabled( false );
177+
mFontPushButton->setEnabled( false );
178+
mFontColorButton->setEnabled( false );
179+
mBufferColorButton->setEnabled( false );
180+
mLabelDistanceSpinBox->setEnabled( false );
181+
mXCoordSpinBox->setEnabled( false );
182+
mYCoordSpinBox->setEnabled( false );
183+
mHaliComboBox->setEnabled( false );
184+
mValiComboBox->setEnabled( false );
185+
mRotationSpinBox->setEnabled( false );
186+
}
187+
188+
void QgsLabelPropertyDialog::blockElementSignals( bool block )
189+
{
190+
mFontSizeSpinBox->blockSignals( block );
191+
mBufferSizeSpinBox->blockSignals( block );
192+
mFontPushButton->blockSignals( block );
193+
mBufferColorButton->blockSignals( block );
194+
mLabelDistanceSpinBox->blockSignals( block );
195+
mXCoordSpinBox->blockSignals( block );
196+
mYCoordSpinBox->blockSignals( block );
197+
mHaliComboBox->blockSignals( block );
198+
mValiComboBox->blockSignals( block );
199+
mRotationSpinBox->blockSignals( block );
200+
}
201+
202+
void QgsLabelPropertyDialog::fillHaliComboBox()
203+
{
204+
mHaliComboBox->addItem( "Left" );
205+
mHaliComboBox->addItem( "Center" );
206+
mHaliComboBox->addItem( "Right" );
207+
}
208+
209+
void QgsLabelPropertyDialog::fillValiComboBox()
210+
{
211+
mValiComboBox->addItem( "Bottom" );
212+
mValiComboBox->addItem( "Base" );
213+
mValiComboBox->addItem( "Half" );
214+
mValiComboBox->addItem( "Top" );
215+
}
216+
217+
void QgsLabelPropertyDialog::on_mLabelDistanceSpinBox_valueChanged( double d )
218+
{
219+
220+
}
221+
222+
void QgsLabelPropertyDialog::on_mXCoordSpinBox_valueChanged( double d )
223+
{
224+
insertChangedValue( QgsPalLayerSettings::PositionX, d );
225+
}
226+
227+
void QgsLabelPropertyDialog::on_mYCoordSpinBox_valueChanged( double d )
228+
{
229+
insertChangedValue( QgsPalLayerSettings::PositionY, d );
230+
}
231+
232+
void QgsLabelPropertyDialog::on_mFontSizeSpinBox_valueChanged( double d )
233+
{
234+
insertChangedValue( QgsPalLayerSettings::Size, d );
235+
}
236+
237+
void QgsLabelPropertyDialog::on_mBufferSizeSpinBox_valueChanged( double d )
238+
{
239+
insertChangedValue( QgsPalLayerSettings::PositionX, d );
240+
}
241+
242+
void QgsLabelPropertyDialog::on_mRotationSpinBox_valueChanged( double d )
243+
{
244+
insertChangedValue( QgsPalLayerSettings::Rotation, d );
245+
}
246+
247+
void QgsLabelPropertyDialog::on_mFontPushButton_clicked()
248+
{
249+
bool ok;
250+
mLabelFont = QFontDialog::getFont( &ok, mLabelFont, 0, tr("Label font") );
251+
if( ok )
252+
{
253+
insertChangedValue( QgsPalLayerSettings::Size, mLabelFont.pointSizeF() );
254+
insertChangedValue( QgsPalLayerSettings::Bold, mLabelFont.bold() );
255+
insertChangedValue( QgsPalLayerSettings::Italic, mLabelFont.italic() );
256+
insertChangedValue( QgsPalLayerSettings::Underline, mLabelFont.underline() );
257+
}
258+
}
259+
260+
void QgsLabelPropertyDialog::on_mFontColorButton_clicked()
261+
{
262+
QColor c = QColorDialog::getColor ( mFontColorButton->color(), 0, tr("Font color"), QColorDialog::ShowAlphaChannel );
263+
if( c.isValid() )
264+
{
265+
mFontColorButton->setColor( c );
266+
insertChangedValue( QgsPalLayerSettings::Color, c.name() );
267+
}
268+
}
269+
270+
void QgsLabelPropertyDialog::on_mBufferColorButton_clicked()
271+
{
272+
QColor c = QColorDialog::getColor ( mBufferColorButton->color(), 0, tr("Buffer color"), QColorDialog::ShowAlphaChannel );
273+
if( c.isValid() )
274+
{
275+
mFontColorButton->setColor( c );
276+
insertChangedValue( QgsPalLayerSettings::BufferColor, c.name() );
277+
}
278+
}
279+
280+
void QgsLabelPropertyDialog::on_mHaliComboBox_currentIndexChanged( const QString& text )
281+
{
282+
insertChangedValue( QgsPalLayerSettings::Hali, text );
283+
}
284+
285+
void QgsLabelPropertyDialog::on_mValiComboBox_currentIndexChanged( const QString& text )
286+
{
287+
insertChangedValue( QgsPalLayerSettings::Vali, text );
288+
}
289+
290+
void QgsLabelPropertyDialog::on_mLabelTextLineEdit_textChanged ( const QString& text )
291+
{
292+
if( mCurrentLabelField != -1 )
293+
{
294+
mChangedProperties.insert( mCurrentLabelField, text );
295+
}
296+
}
297+
298+
void QgsLabelPropertyDialog::insertChangedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant value )
299+
{
300+
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator ddIt = mDataDefinedProperties.find( p );
301+
if( ddIt != mDataDefinedProperties.constEnd() )
302+
{
303+
mChangedProperties.insert( ddIt.value(), value );
304+
}
305+
}
306+
307+
bool QgsLabelPropertyDialog::labelFontEditingPossible() const
308+
{
309+
return( mDataDefinedProperties.contains( QgsPalLayerSettings:: Size ) || mDataDefinedProperties.contains( QgsPalLayerSettings::Bold )
310+
|| mDataDefinedProperties.contains( QgsPalLayerSettings::Italic ) || mDataDefinedProperties.contains( QgsPalLayerSettings::Underline )
311+
|| mDataDefinedProperties.contains( QgsPalLayerSettings::Strikeout )
312+
|| mDataDefinedProperties.contains( QgsPalLayerSettings::Family ) );
313+
}
314+

‎src/app/qgslabelpropertydialog.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/***************************************************************************
2+
qgslabelpropertydialog.h
3+
------------------------
4+
begin : 2010-11-12
5+
copyright : (C) 2010 by Marco Hugentobler
6+
email : marco dot hugentobler at sourcepole dot ch
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSLABELPROPERTYDIALOG_H
19+
#define QGSLABELPROPERTYDIALOG_H
20+
21+
#include "ui_qgslabelpropertydialogbase.h"
22+
#include "qgsfeature.h"
23+
#include "qgspallabeling.h"
24+
#include <QDialog>
25+
26+
class QgsMapRenderer;
27+
28+
/**A dialog to enter data defined label attributes*/
29+
class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialogBase
30+
{
31+
Q_OBJECT
32+
public:
33+
QgsLabelPropertyDialog( const QString& layerId, int featureId, QgsMapRenderer* renderer, QWidget * parent = 0, Qt::WindowFlags f = 0 );
34+
~QgsLabelPropertyDialog();
35+
36+
/**Returns properties changed by the user*/
37+
const QgsAttributeMap& changedProperties() const { return mChangedProperties; }
38+
39+
private slots:
40+
void on_mLabelDistanceSpinBox_valueChanged( double d );
41+
void on_mXCoordSpinBox_valueChanged( double d );
42+
void on_mYCoordSpinBox_valueChanged( double d );
43+
void on_mFontSizeSpinBox_valueChanged( double d );
44+
void on_mBufferSizeSpinBox_valueChanged( double d );
45+
void on_mRotationSpinBox_valueChanged( double d );
46+
void on_mFontPushButton_clicked();
47+
void on_mFontColorButton_clicked();
48+
void on_mBufferColorButton_clicked();
49+
void on_mHaliComboBox_currentIndexChanged( const QString& text );
50+
void on_mValiComboBox_currentIndexChanged( const QString& text );
51+
void on_mLabelTextLineEdit_textChanged ( const QString& text );
52+
53+
private:
54+
/**Sets activation / values to the gui elements depending on the label settings and feature values*/
55+
void init( const QString& layerId, int featureId );
56+
void disableGuiElements();
57+
/**Block / unblock all input element signals*/
58+
void blockElementSignals( bool block );
59+
60+
void fillHaliComboBox();
61+
void fillValiComboBox();
62+
63+
/**Insert changed value into mChangedProperties*/
64+
void insertChangedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant value );
65+
66+
/**Returns true if any font related setting is contained in mDataDefinedProperties*/
67+
bool labelFontEditingPossible() const;
68+
69+
QgsMapRenderer* mMapRenderer;
70+
71+
QgsAttributeMap mChangedProperties;
72+
QMap< QgsPalLayerSettings::DataDefinedProperties, int > mDataDefinedProperties;
73+
QFont mLabelFont;
74+
75+
/**Label field for the current layer (or -1 if none)*/
76+
int mCurrentLabelField;
77+
};
78+
79+
#endif // QGSLAYERPROPERTYDIALOG_H

0 commit comments

Comments
 (0)
Please sign in to comment.