Skip to content

Commit 9e2c4a0

Browse files
committedJun 16, 2013
Fix for #8003, font not written to table when using change label properties tool
The following have been updated/added to match layer's new labeling gui: - Comboboxes for font family and style - Buttons for underline, strikeout, bold and italic
1 parent 192e130 commit 9e2c4a0

File tree

4 files changed

+335
-71
lines changed

4 files changed

+335
-71
lines changed
 

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 123 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
#include "qgslabelpropertydialog.h"
1919
#include <qgsdatadefined.h>
20+
#include <qgsfontutils.h>
21+
#include <qgslogger.h>
2022
#include "qgsmaplayerregistry.h"
2123
#include "qgsmaprenderer.h"
2224
#include "qgsvectorlayer.h"
25+
2326
#include <QColorDialog>
24-
#include <QFontDialog>
27+
#include <QFontDatabase>
2528

26-
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QString& labelText, QgsMapRenderer* renderer, QWidget * parent, Qt::WindowFlags f ):
27-
QDialog( parent, f ), mMapRenderer( renderer ), mCurLabelField( -1 )
29+
QgsLabelPropertyDialog::QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QgsMapRenderer* renderer, QWidget * parent, Qt::WindowFlags f ):
30+
QDialog( parent, f ), mMapRenderer( renderer ), mLabelFont( labelFont ), mCurLabelField( -1 )
2831
{
2932
setupUi( this );
3033
fillHaliComboBox();
@@ -99,10 +102,11 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
99102
}
100103

101104
//get attributes of the feature and fill data defined values
102-
mLabelFont = layerSettings.textFont;
103105

104-
//set all the gui elements to the default values
105-
mFontSizeSpinBox->setValue( layerSettings.textFont.pointSizeF() );
106+
// font is set directly from QgsLabelPosition
107+
updateFont( mLabelFont, false );
108+
109+
//set all the gui elements to the default layer-level values
106110
mBufferColorButton->setColor( layerSettings.textColor );
107111
mLabelDistanceSpinBox->setValue( layerSettings.dist );
108112
mBufferSizeSpinBox->setValue( layerSettings.bufferSize );
@@ -115,14 +119,16 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
115119

116120
disableGuiElements();
117121

118-
bool fontEditing = false;
119-
120122
mDataDefinedProperties = layerSettings.dataDefinedProperties;
121123
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* >::const_iterator propIt = mDataDefinedProperties.constBegin();
122124

123125
for ( ; propIt != mDataDefinedProperties.constEnd(); ++propIt )
124126
{
125127
QgsDataDefined* dd = propIt.value();
128+
if ( !dd )
129+
{
130+
continue;
131+
}
126132
QString ddField = dd->field();
127133
if ( !dd->isActive() || dd->useExpression() || ddField.isEmpty() )
128134
{
@@ -135,6 +141,8 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
135141
continue;
136142
}
137143

144+
QgsDebugMsg( QString( "ddField: %1" ).arg( ddField ) );
145+
138146
bool ok = false;
139147
switch ( propIt.key() )
140148
{
@@ -169,18 +177,6 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
169177
}
170178
break;
171179
}
172-
case QgsPalLayerSettings::Size:
173-
{
174-
mFontSizeSpinBox->setEnabled( true );
175-
double fontSize = mCurLabelFeat.attribute( ddIndx ).toDouble( &ok );
176-
if ( ok )
177-
{
178-
mLabelFont.setPointSizeF( fontSize );
179-
mFontSizeSpinBox->setValue( fontSize );
180-
fontEditing = true;
181-
}
182-
break;
183-
}
184180
case QgsPalLayerSettings::BufferSize:
185181
{
186182
mBufferSizeSpinBox->setEnabled( true );
@@ -249,31 +245,31 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
249245
}
250246

251247
//font related properties
252-
case QgsPalLayerSettings::Bold:
253-
mLabelFont.setBold( mCurLabelFeat.attribute( ddIndx ).toBool() );
254-
fontEditing = true;
248+
case QgsPalLayerSettings::Family:
249+
mFontFamilyCmbBx->setEnabled( true );
255250
break;
256-
case QgsPalLayerSettings::Italic:
257-
mLabelFont.setItalic( mCurLabelFeat.attribute( ddIndx ).toBool() );
258-
fontEditing = true;
251+
case QgsPalLayerSettings::FontStyle:
252+
mFontStyleCmbBx->setEnabled( true );
259253
break;
260254
case QgsPalLayerSettings::Underline:
261-
mLabelFont.setUnderline( mCurLabelFeat.attribute( ddIndx ).toBool() );
262-
fontEditing = true;
255+
mFontUnderlineBtn->setEnabled( true );
263256
break;
264257
case QgsPalLayerSettings::Strikeout:
265-
mLabelFont.setStrikeOut( mCurLabelFeat.attribute( ddIndx ).toBool() );
266-
fontEditing = true;
258+
mFontStrikethroughBtn->setEnabled( true );
267259
break;
268-
case QgsPalLayerSettings::Family:
269-
mLabelFont.setFamily( mCurLabelFeat.attribute( ddIndx ).toString() );
270-
fontEditing = true;
260+
case QgsPalLayerSettings::Bold:
261+
mFontBoldBtn->setEnabled( true );
262+
break;
263+
case QgsPalLayerSettings::Italic:
264+
mFontItalicBtn->setEnabled( true );
265+
break;
266+
case QgsPalLayerSettings::Size:
267+
mFontSizeSpinBox->setEnabled( true );
271268
break;
272269
default:
273270
break;
274271
}
275272
}
276-
mFontPushButton->setEnabled( fontEditing );
277273
blockElementSignals( false );
278274
}
279275

@@ -283,9 +279,14 @@ void QgsLabelPropertyDialog::disableGuiElements()
283279
mAlwaysShowChkbx->setEnabled( false );
284280
mMinScaleSpinBox->setEnabled( false );
285281
mMaxScaleSpinBox->setEnabled( false );
282+
mFontFamilyCmbBx->setEnabled( false );
283+
mFontStyleCmbBx->setEnabled( false );
284+
mFontUnderlineBtn->setEnabled( false );
285+
mFontStrikethroughBtn->setEnabled( false );
286+
mFontBoldBtn->setEnabled( false );
287+
mFontItalicBtn->setEnabled( false );
286288
mFontSizeSpinBox->setEnabled( false );
287289
mBufferSizeSpinBox->setEnabled( false );
288-
mFontPushButton->setEnabled( false );
289290
mFontColorButton->setEnabled( false );
290291
mBufferColorButton->setEnabled( false );
291292
mLabelDistanceSpinBox->setEnabled( false );
@@ -302,9 +303,14 @@ void QgsLabelPropertyDialog::blockElementSignals( bool block )
302303
mAlwaysShowChkbx->blockSignals( block );
303304
mMinScaleSpinBox->blockSignals( block );
304305
mMaxScaleSpinBox->blockSignals( block );
306+
mFontFamilyCmbBx->blockSignals( block );
307+
mFontStyleCmbBx->blockSignals( block );
308+
mFontUnderlineBtn->blockSignals( block );
309+
mFontStrikethroughBtn->blockSignals( block );
310+
mFontBoldBtn->blockSignals( block );
311+
mFontItalicBtn->blockSignals( block );
305312
mFontSizeSpinBox->blockSignals( block );
306313
mBufferSizeSpinBox->blockSignals( block );
307-
mFontPushButton->blockSignals( block );
308314
mFontColorButton->blockSignals( block );
309315
mBufferColorButton->blockSignals( block );
310316
mLabelDistanceSpinBox->blockSignals( block );
@@ -315,6 +321,45 @@ void QgsLabelPropertyDialog::blockElementSignals( bool block )
315321
mRotationSpinBox->blockSignals( block );
316322
}
317323

324+
void QgsLabelPropertyDialog::updateFont( const QFont& font, bool block )
325+
{
326+
// update background reference font
327+
if ( font != mLabelFont )
328+
{
329+
mLabelFont = font;
330+
}
331+
332+
if ( block )
333+
blockElementSignals( true );
334+
mFontFamilyCmbBx->setCurrentFont( mLabelFont );
335+
populateFontStyleComboBox();
336+
mFontUnderlineBtn->setChecked( mLabelFont.underline() );
337+
mFontStrikethroughBtn->setChecked( mLabelFont.strikeOut() );
338+
mFontBoldBtn->setChecked( mLabelFont.bold() );
339+
mFontItalicBtn->setChecked( mLabelFont.italic() );
340+
mFontSizeSpinBox->setValue( mLabelFont.pointSizeF() );
341+
if ( block )
342+
blockElementSignals( false );
343+
}
344+
345+
void QgsLabelPropertyDialog::populateFontStyleComboBox()
346+
{
347+
mFontStyleCmbBx->clear();
348+
foreach ( const QString &style, mFontDB.styles( mLabelFont.family() ) )
349+
{
350+
mFontStyleCmbBx->addItem( style );
351+
}
352+
353+
int curIndx = 0;
354+
int stylIndx = mFontStyleCmbBx->findText( mFontDB.styleString( mLabelFont ) );
355+
if ( stylIndx > -1 )
356+
{
357+
curIndx = stylIndx;
358+
}
359+
360+
mFontStyleCmbBx->setCurrentIndex( curIndx );
361+
}
362+
318363
void QgsLabelPropertyDialog::fillHaliComboBox()
319364
{
320365
mHaliComboBox->addItem( "Left" );
@@ -365,6 +410,48 @@ void QgsLabelPropertyDialog::on_mYCoordSpinBox_valueChanged( double d )
365410
insertChangedValue( QgsPalLayerSettings::PositionY, d );
366411
}
367412

413+
void QgsLabelPropertyDialog::on_mFontFamilyCmbBx_currentFontChanged( const QFont& f )
414+
{
415+
mLabelFont.setFamily( f.family() );
416+
updateFont( mLabelFont );
417+
insertChangedValue( QgsPalLayerSettings::Family, f.family() );
418+
}
419+
420+
void QgsLabelPropertyDialog::on_mFontStyleCmbBx_currentIndexChanged( const QString & text )
421+
{
422+
QgsFontUtils::updateFontViaStyle( mLabelFont, text );
423+
updateFont( mLabelFont );
424+
insertChangedValue( QgsPalLayerSettings::FontStyle, text );
425+
}
426+
427+
void QgsLabelPropertyDialog::on_mFontUnderlineBtn_toggled( bool ckd )
428+
{
429+
mLabelFont.setUnderline( ckd );
430+
updateFont( mLabelFont );
431+
insertChangedValue( QgsPalLayerSettings::Underline, ckd );
432+
}
433+
434+
void QgsLabelPropertyDialog::on_mFontStrikethroughBtn_toggled( bool ckd )
435+
{
436+
mLabelFont.setStrikeOut( ckd );
437+
updateFont( mLabelFont );
438+
insertChangedValue( QgsPalLayerSettings::Strikeout, ckd );
439+
}
440+
441+
void QgsLabelPropertyDialog::on_mFontBoldBtn_toggled( bool ckd )
442+
{
443+
mLabelFont.setBold( ckd );
444+
updateFont( mLabelFont );
445+
insertChangedValue( QgsPalLayerSettings::Bold, ckd );
446+
}
447+
448+
void QgsLabelPropertyDialog::on_mFontItalicBtn_toggled( bool ckd )
449+
{
450+
mLabelFont.setItalic( ckd );
451+
updateFont( mLabelFont );
452+
insertChangedValue( QgsPalLayerSettings::Italic, ckd );
453+
}
454+
368455
void QgsLabelPropertyDialog::on_mFontSizeSpinBox_valueChanged( double d )
369456
{
370457
insertChangedValue( QgsPalLayerSettings::Size, d );
@@ -380,25 +467,6 @@ void QgsLabelPropertyDialog::on_mRotationSpinBox_valueChanged( double d )
380467
insertChangedValue( QgsPalLayerSettings::Rotation, d );
381468
}
382469

383-
void QgsLabelPropertyDialog::on_mFontPushButton_clicked()
384-
{
385-
bool ok;
386-
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
387-
// Native Mac dialog works only for Qt Carbon
388-
mLabelFont = QFontDialog::getFont( &ok, mLabelFont, 0, tr( "Label font" ), QFontDialog::DontUseNativeDialog );
389-
#else
390-
mLabelFont = QFontDialog::getFont( &ok, mLabelFont, 0, tr( "Label font" ) );
391-
#endif
392-
if ( ok )
393-
{
394-
insertChangedValue( QgsPalLayerSettings::Size, mLabelFont.pointSizeF() );
395-
insertChangedValue( QgsPalLayerSettings::Bold, mLabelFont.bold() );
396-
insertChangedValue( QgsPalLayerSettings::Italic, mLabelFont.italic() );
397-
insertChangedValue( QgsPalLayerSettings::Underline, mLabelFont.underline() );
398-
insertChangedValue( QgsPalLayerSettings::Strikeout, mLabelFont.strikeOut() );
399-
}
400-
}
401-
402470
void QgsLabelPropertyDialog::on_mFontColorButton_colorChanged( const QColor &color )
403471
{
404472
insertChangedValue( QgsPalLayerSettings::Color, color.name() );

‎src/app/qgslabelpropertydialog.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
3030
{
3131
Q_OBJECT
3232
public:
33-
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QString& labelText, QgsMapRenderer* renderer, QWidget * parent = 0, Qt::WindowFlags f = 0 );
33+
QgsLabelPropertyDialog( const QString& layerId, int featureId, const QFont& labelFont, const QString& labelText, QgsMapRenderer* renderer, QWidget * parent = 0, Qt::WindowFlags f = 0 );
3434
~QgsLabelPropertyDialog();
3535

3636
/**Returns properties changed by the user*/
@@ -44,10 +44,21 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
4444
void on_mLabelDistanceSpinBox_valueChanged( double d );
4545
void on_mXCoordSpinBox_valueChanged( double d );
4646
void on_mYCoordSpinBox_valueChanged( double d );
47+
/** @note added in 1.9 */
48+
void on_mFontFamilyCmbBx_currentFontChanged( const QFont& f );
49+
/** @note added in 1.9 */
50+
void on_mFontStyleCmbBx_currentIndexChanged( const QString & text );
51+
/** @note added in 1.9 */
52+
void on_mFontUnderlineBtn_toggled( bool ckd );
53+
/** @note added in 1.9 */
54+
void on_mFontStrikethroughBtn_toggled( bool ckd );
55+
/** @note added in 1.9 */
56+
void on_mFontBoldBtn_toggled( bool ckd );
57+
/** @note added in 1.9 */
58+
void on_mFontItalicBtn_toggled( bool ckd );
4759
void on_mFontSizeSpinBox_valueChanged( double d );
4860
void on_mBufferSizeSpinBox_valueChanged( double d );
4961
void on_mRotationSpinBox_valueChanged( double d );
50-
void on_mFontPushButton_clicked();
5162
void on_mFontColorButton_colorChanged( const QColor &color );
5263
void on_mBufferColorButton_colorChanged( const QColor &color );
5364
void on_mHaliComboBox_currentIndexChanged( const QString& text );
@@ -61,6 +72,14 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
6172
/**Block / unblock all input element signals*/
6273
void blockElementSignals( bool block );
6374

75+
/** Updates font when family or style is updated
76+
* @note added in 1.9 */
77+
void updateFont( const QFont& font, bool block = true );
78+
79+
/** Updates combobox with named styles of font
80+
* @note added in 1.9 */
81+
void populateFontStyleComboBox();
82+
6483
void fillHaliComboBox();
6584
void fillValiComboBox();
6685

@@ -73,6 +92,9 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog
7392
QMap< QgsPalLayerSettings::DataDefinedProperties, QgsDataDefined* > mDataDefinedProperties;
7493
QFont mLabelFont;
7594

95+
/** @note added in 1.9 */
96+
QFontDatabase mFontDB;
97+
7698
/**Label field for the current layer (or -1 if none)*/
7799
int mCurLabelField;
78100

‎src/app/qgsmaptoolchangelabelproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void QgsMapToolChangeLabelProperties::canvasReleaseEvent( QMouseEvent *e )
6161
labeltext = mCurrentLabelPos.labelText;
6262
}
6363

64-
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, labeltext, mCanvas->mapRenderer() );
64+
QgsLabelPropertyDialog d( mCurrentLabelPos.layerID, mCurrentLabelPos.featureId, mCurrentLabelPos.labelFont, labeltext, mCanvas->mapRenderer() );
6565
if ( d.exec() == QDialog::Accepted )
6666
{
6767
const QgsAttributeMap& changes = d.changedProperties();

‎src/ui/qgslabelpropertydialogbase.ui

Lines changed: 187 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>415</width>
10-
<height>611</height>
10+
<height>695</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -34,14 +34,34 @@
3434
<string>Font</string>
3535
</property>
3636
<layout class="QGridLayout" name="gridLayout">
37-
<item row="0" column="2">
38-
<widget class="QPushButton" name="mFontPushButton">
37+
<item row="1" column="1" colspan="2">
38+
<widget class="QComboBox" name="mFontStyleCmbBx">
39+
<property name="toolTip">
40+
<string>Available typeface styles</string>
41+
</property>
42+
</widget>
43+
</item>
44+
<item row="3" column="2">
45+
<widget class="QgsColorButton" name="mFontColorButton">
46+
<property name="maximumSize">
47+
<size>
48+
<width>100</width>
49+
<height>16777215</height>
50+
</size>
51+
</property>
3952
<property name="text">
40-
<string>Font</string>
53+
<string/>
4154
</property>
4255
</widget>
4356
</item>
44-
<item row="0" column="0">
57+
<item row="0" column="0" colspan="3">
58+
<widget class="QFontComboBox" name="mFontFamilyCmbBx">
59+
<property name="editable">
60+
<bool>false</bool>
61+
</property>
62+
</widget>
63+
</item>
64+
<item row="3" column="0" colspan="2">
4565
<layout class="QHBoxLayout" name="horizontalLayout">
4666
<item>
4767
<widget class="QLabel" name="mFontSizeLabel">
@@ -68,19 +88,173 @@
6888
</item>
6989
</layout>
7090
</item>
71-
<item row="0" column="1">
72-
<widget class="QgsColorButton" name="mFontColorButton">
73-
<property name="maximumSize">
74-
<size>
75-
<width>100</width>
76-
<height>16777215</height>
77-
</size>
91+
<item row="1" column="0">
92+
<widget class="QLabel" name="label_3">
93+
<property name="sizePolicy">
94+
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
95+
<horstretch>0</horstretch>
96+
<verstretch>0</verstretch>
97+
</sizepolicy>
7898
</property>
7999
<property name="text">
80-
<string/>
100+
<string>Style</string>
81101
</property>
82102
</widget>
83103
</item>
104+
<item row="2" column="1" colspan="2">
105+
<layout class="QHBoxLayout" name="horizontalLayout_4">
106+
<item>
107+
<widget class="QToolButton" name="mFontUnderlineBtn">
108+
<property name="enabled">
109+
<bool>true</bool>
110+
</property>
111+
<property name="minimumSize">
112+
<size>
113+
<width>24</width>
114+
<height>24</height>
115+
</size>
116+
</property>
117+
<property name="maximumSize">
118+
<size>
119+
<width>24</width>
120+
<height>24</height>
121+
</size>
122+
</property>
123+
<property name="font">
124+
<font>
125+
<pointsize>13</pointsize>
126+
<underline>true</underline>
127+
</font>
128+
</property>
129+
<property name="toolTip">
130+
<string>Underlined text</string>
131+
</property>
132+
<property name="text">
133+
<string>U</string>
134+
</property>
135+
<property name="checkable">
136+
<bool>true</bool>
137+
</property>
138+
</widget>
139+
</item>
140+
<item>
141+
<widget class="QToolButton" name="mFontStrikethroughBtn">
142+
<property name="enabled">
143+
<bool>true</bool>
144+
</property>
145+
<property name="minimumSize">
146+
<size>
147+
<width>24</width>
148+
<height>24</height>
149+
</size>
150+
</property>
151+
<property name="maximumSize">
152+
<size>
153+
<width>24</width>
154+
<height>24</height>
155+
</size>
156+
</property>
157+
<property name="font">
158+
<font>
159+
<pointsize>13</pointsize>
160+
<strikeout>true</strikeout>
161+
</font>
162+
</property>
163+
<property name="toolTip">
164+
<string>Strikeout text</string>
165+
</property>
166+
<property name="text">
167+
<string>S</string>
168+
</property>
169+
<property name="checkable">
170+
<bool>true</bool>
171+
</property>
172+
</widget>
173+
</item>
174+
<item>
175+
<spacer name="horizontalSpacer">
176+
<property name="orientation">
177+
<enum>Qt::Horizontal</enum>
178+
</property>
179+
<property name="sizeHint" stdset="0">
180+
<size>
181+
<width>0</width>
182+
<height>20</height>
183+
</size>
184+
</property>
185+
</spacer>
186+
</item>
187+
<item>
188+
<widget class="QToolButton" name="mFontBoldBtn">
189+
<property name="enabled">
190+
<bool>false</bool>
191+
</property>
192+
<property name="minimumSize">
193+
<size>
194+
<width>24</width>
195+
<height>24</height>
196+
</size>
197+
</property>
198+
<property name="maximumSize">
199+
<size>
200+
<width>24</width>
201+
<height>24</height>
202+
</size>
203+
</property>
204+
<property name="font">
205+
<font>
206+
<pointsize>13</pointsize>
207+
</font>
208+
</property>
209+
<property name="toolTip">
210+
<string>Bold text
211+
(data defined only, overrides Style)</string>
212+
</property>
213+
<property name="text">
214+
<string>B</string>
215+
</property>
216+
<property name="checkable">
217+
<bool>true</bool>
218+
</property>
219+
</widget>
220+
</item>
221+
<item>
222+
<widget class="QToolButton" name="mFontItalicBtn">
223+
<property name="enabled">
224+
<bool>false</bool>
225+
</property>
226+
<property name="minimumSize">
227+
<size>
228+
<width>24</width>
229+
<height>24</height>
230+
</size>
231+
</property>
232+
<property name="maximumSize">
233+
<size>
234+
<width>24</width>
235+
<height>24</height>
236+
</size>
237+
</property>
238+
<property name="font">
239+
<font>
240+
<pointsize>13</pointsize>
241+
<italic>true</italic>
242+
</font>
243+
</property>
244+
<property name="toolTip">
245+
<string>Italic text
246+
(data defined only, overrides Style)</string>
247+
</property>
248+
<property name="text">
249+
<string>I</string>
250+
</property>
251+
<property name="checkable">
252+
<bool>true</bool>
253+
</property>
254+
</widget>
255+
</item>
256+
</layout>
257+
</item>
84258
</layout>
85259
</widget>
86260
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.