00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "qgslabeldialog.h"
00018 #include "qgsfield.h"
00019 #include "qgslabel.h"
00020 #include "qgslabelattributes.h"
00021
00022 #include <QColorDialog>
00023 #include <QFontDialog>
00024 #include <QTabWidget>
00025 #include "qgslogger.h"
00026
00027
00028 const int PIXMAP_WIDTH = 200;
00029 const int PIXMAP_HEIGHT = 20;
00030
00031 QgsLabelDialog::QgsLabelDialog( QgsLabel *label, QWidget *parent )
00032 : QWidget( parent ),
00033 mLabel( label ),
00034 mFontColor( Qt::black ),
00035 mBufferColor( Qt::black ),
00036 mFont( "Helvetica" )
00037 {
00038 setupUi( this );
00039 QgsDebugMsg( "entering." );
00040
00041 Q_ASSERT( label );
00042
00043 init();
00044
00045 connect( btnDefaultFont, SIGNAL( clicked() ),
00046 this, SLOT( changeFont() ) );
00047 connect( pbnDefaultBufferColor_2, SIGNAL( clicked() ),
00048 this, SLOT( changeBufferColor() ) );
00049 connect( pbnDefaultFontColor, SIGNAL( clicked() ),
00050 this, SLOT( changeFontColor() ) );
00051 }
00052
00053
00054 void QgsLabelDialog::init( )
00055 {
00056 QgsDebugMsg( "entering." );
00057 QgsLabelAttributes * myLabelAttributes = mLabel->layerAttributes();
00058
00059
00060 QgsFieldMap& myFieldsMap = mLabel->fields();
00061 QStringList myFieldStringList;
00062 myFieldStringList.append( "" );
00063 for ( QgsFieldMap::iterator it = myFieldsMap.begin(); it != myFieldsMap.end(); ++it )
00064 {
00065 myFieldStringList.append( it->name() );
00066 }
00067
00068
00069
00070 cboLabelField->clear();
00071 cboLabelField->addItems( myFieldStringList );
00072 cboLabelField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Text ), myFieldStringList ) );
00073
00074
00075 cboFontField->clear();
00076 cboFontField->addItems( myFieldStringList );
00077 cboFontField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Family ), myFieldStringList ) );
00078
00079 cboBoldField->clear();
00080 cboBoldField->addItems( myFieldStringList );
00081 cboBoldField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Bold ), myFieldStringList ) );
00082
00083
00084 cboItalicField->clear();
00085 cboItalicField->addItems( myFieldStringList );
00086 cboItalicField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Italic ), myFieldStringList ) );
00087
00088 cboUnderlineField->clear();
00089 cboUnderlineField->addItems( myFieldStringList );
00090 cboUnderlineField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Underline ), myFieldStringList ) );
00091
00092 cboFontSizeField->clear();
00093 cboFontSizeField->addItems( myFieldStringList );
00094 cboFontSizeField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Size ), myFieldStringList ) );
00095
00096 cboFontSizeTypeField->clear();
00097 cboFontSizeTypeField->addItems( myFieldStringList );
00098 cboFontSizeTypeField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::SizeType ), myFieldStringList ) );
00099
00100 cboFontTransparencyField->clear();
00101 cboFontTransparencyField->addItems( myFieldStringList );
00102
00103
00104 cboBufferSizeField->clear();
00105 cboBufferSizeField->addItems( myFieldStringList );
00106 cboBufferSizeField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::BufferSize ), myFieldStringList ) );
00107
00108 cboBufferTransparencyField->clear();
00109 cboBufferTransparencyField->addItems( myFieldStringList );
00110
00111
00112 cboXCoordinateField->clear();
00113 cboXCoordinateField->addItems( myFieldStringList );
00114 cboXCoordinateField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::XCoordinate ), myFieldStringList ) );
00115
00116 cboYCoordinateField->clear();
00117 cboYCoordinateField->addItems( myFieldStringList );
00118 cboYCoordinateField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::YCoordinate ), myFieldStringList ) );
00119
00120 cboXOffsetField->clear();
00121 cboXOffsetField->addItems( myFieldStringList );
00122 cboXOffsetField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::XOffset ), myFieldStringList ) );
00123
00124 cboYOffsetField->clear();
00125 cboYOffsetField->addItems( myFieldStringList );
00126 cboYOffsetField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::YOffset ), myFieldStringList ) );
00127
00128 cboAlignmentField->clear();
00129 cboAlignmentField->addItems( myFieldStringList );
00130 cboAlignmentField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Alignment ), myFieldStringList ) );
00131
00132 cboAngleField->clear();
00133 cboAngleField->addItems( myFieldStringList );
00134 cboAngleField->setCurrentIndex( itemNoForField( mLabel->labelField( QgsLabel::Angle ), myFieldStringList ) );
00135
00136
00137
00138
00139
00140 leDefaultLabel->setText( myLabelAttributes->text() );
00141 mFont.setFamily( myLabelAttributes->family() );
00142 if ( myLabelAttributes->sizeIsSet() )
00143 {
00144 mFont.setPointSize( static_cast<int>( myLabelAttributes->size() ) );
00145
00146 int myTypeInt = myLabelAttributes->sizeType();
00147 if ( myTypeInt == QgsLabelAttributes::PointUnits )
00148 {
00149 radioFontSizeUnitsPoints->setChecked( true );
00150 }
00151 else
00152 {
00153 radioFontSizeUnitsMap->setChecked( true );
00154 }
00155 }
00156 else
00157 {
00158 mFont.setPointSize( static_cast<int>( myLabelAttributes->size() ) );
00159 radioFontSizeUnitsPoints->setChecked( true );
00160 }
00161
00162 if ( myLabelAttributes->boldIsSet() )
00163 {
00164 mFont.setBold( myLabelAttributes->bold() );
00165 }
00166 else
00167 {
00168 mFont.setBold( false );
00169 }
00170 if ( myLabelAttributes->italicIsSet() )
00171 {
00172 mFont.setItalic( myLabelAttributes->italic() );
00173 }
00174 else
00175 {
00176 mFont.setItalic( false );
00177 }
00178 mFont.setUnderline( myLabelAttributes->underline() );
00179 mFontColor = myLabelAttributes->color();
00180
00181 if ( myLabelAttributes->offsetIsSet() )
00182 {
00183 int myTypeInt = myLabelAttributes->offsetType();
00184 if ( myTypeInt == QgsLabelAttributes::PointUnits )
00185 {
00186 radioOffsetUnitsPoints->setChecked( true );
00187 }
00188 else
00189 {
00190 radioOffsetUnitsMap->setChecked( true );
00191 }
00192 spinXOffset->setValue( static_cast<int>( myLabelAttributes->xOffset() ) );
00193 spinYOffset->setValue( static_cast<int>( myLabelAttributes->yOffset() ) );
00194 }
00195 else
00196 {
00197 spinXOffset->setValue( 0 );
00198 spinYOffset->setValue( 0 );
00199 }
00200 spinAngle->setValue( static_cast<int>( myLabelAttributes->angle() ) );
00201
00202
00203
00204
00205
00206
00207 if ( myLabelAttributes->alignment() == ( Qt::AlignRight | Qt::AlignBottom ) ) radioAboveLeft->setChecked( true ) ;
00208 if ( myLabelAttributes->alignment() == ( Qt::AlignRight | Qt::AlignTop ) ) radioBelowLeft->setChecked( true ) ;
00209 if ( myLabelAttributes->alignment() == ( Qt::AlignLeft | Qt::AlignBottom ) ) radioAboveRight->setChecked( true ) ;
00210 if ( myLabelAttributes->alignment() == ( Qt::AlignLeft | Qt::AlignTop ) ) radioBelowRight->setChecked( true ) ;
00211 if ( myLabelAttributes->alignment() == ( Qt::AlignRight | Qt::AlignVCenter ) ) radioLeft->setChecked( true ) ;
00212 if ( myLabelAttributes->alignment() == ( Qt::AlignLeft | Qt::AlignVCenter ) ) radioRight->setChecked( true ) ;
00213 if ( myLabelAttributes->alignment() == ( Qt::AlignBottom | Qt::AlignHCenter ) ) radioAbove->setChecked( true ) ;
00214 if ( myLabelAttributes->alignment() == ( Qt::AlignTop | Qt::AlignHCenter ) ) radioBelow->setChecked( true ) ;
00215 if ( myLabelAttributes->alignment() == Qt::AlignCenter ) radioOver->setChecked( true ) ;
00216
00217 mBufferColor = myLabelAttributes->bufferColor();
00218
00219 if ( myLabelAttributes->bufferSizeIsSet() )
00220 {
00221 int myTypeInt = myLabelAttributes->bufferSizeType();
00222 if ( myTypeInt == QgsLabelAttributes::PointUnits )
00223 {
00224 radioBufferUnitsPoints->setChecked( true );
00225 }
00226 else
00227 {
00228 radioBufferUnitsMap->setChecked( true );
00229 }
00230 spinBufferSize->setValue( static_cast<int>( myLabelAttributes->bufferSize() ) );
00231 }
00232 else
00233 {
00234 spinBufferSize->setValue( 1 );
00235 }
00236
00237 chkUseMultiline->setChecked( myLabelAttributes->multilineEnabled() );
00238
00239 chkUseBuffer->setChecked( myLabelAttributes->bufferEnabled() );
00240
00241
00242 spinBufferSize->setValue( static_cast<int>( myLabelAttributes->bufferSize() ) );
00243
00244
00245 }
00246
00247
00248
00249 void QgsLabelDialog::changeFont( void )
00250 {
00251 QgsDebugMsg( "entering." );
00252
00253 bool resultFlag;
00254 mFont = QFontDialog::getFont( &resultFlag, mFont, this );
00255 if ( resultFlag )
00256 {
00257
00258 }
00259 else
00260 {
00261
00262
00263 }
00264 lblSample->setFont( mFont );
00265 }
00266
00267 void QgsLabelDialog::changeFontColor( void )
00268 {
00269 QgsDebugMsg( "entering." );
00270
00271 mFontColor = QColorDialog::getColor( mFontColor );
00272 QPalette palette = lblSample->palette();
00273 palette.setColor( lblSample->foregroundRole(), mFontColor );
00274 lblSample->setPalette( palette );
00275 }
00276
00277 void QgsLabelDialog::changeBufferColor( void )
00278 {
00279 QgsDebugMsg( "entering." );
00280
00281 mBufferColor = QColorDialog::getColor( mBufferColor );
00282 QPalette palette = lblSample->palette();
00283 palette.setColor( lblSample->backgroundRole(), mBufferColor );
00284 lblSample->setPalette( palette );
00285 }
00286
00287
00288 int QgsLabelDialog::itemNoForField( QString theFieldName, QStringList theFieldList )
00289 {
00290 int myItemInt = 0;
00291 for ( QStringList::Iterator it = theFieldList.begin(); it != theFieldList.end(); ++it )
00292 {
00293 if ( theFieldName == *it ) return myItemInt;
00294 ++myItemInt;
00295 }
00296
00297 return 0;
00298 }
00299
00300 QgsLabelDialog::~QgsLabelDialog()
00301 {
00302 QgsDebugMsg( "entering." );
00303 }
00304
00305 void QgsLabelDialog::apply()
00306 {
00307 QgsDebugMsg( "entering." );
00308
00309
00310
00311 QgsLabelAttributes * myLabelAttributes = mLabel->layerAttributes();
00312 myLabelAttributes->setText( leDefaultLabel->text() );
00313 myLabelAttributes->setFamily( mFont.family() );
00314 int myTypeInt = 0;
00315 if ( radioFontSizeUnitsPoints->isChecked() )
00316 {
00317 myTypeInt = QgsLabelAttributes::PointUnits;
00318 }
00319 else
00320 {
00321 myTypeInt = QgsLabelAttributes::MapUnits;
00322 }
00323 myLabelAttributes->setSize( mFont.pointSize(), myTypeInt );
00324 myLabelAttributes->setBold( mFont.bold() );
00325 myLabelAttributes->setItalic( mFont.italic() );
00326 myLabelAttributes->setUnderline( mFont.underline() );
00327 myLabelAttributes->setColor( mFontColor );
00328 myTypeInt = 0;
00329 if ( radioOffsetUnitsPoints->isChecked() )
00330 {
00331 myTypeInt = QgsLabelAttributes::PointUnits;
00332 }
00333 else
00334 {
00335 myTypeInt = QgsLabelAttributes::MapUnits;
00336 }
00337 myLabelAttributes->setOffset( spinXOffset->value(), spinYOffset->value(), myTypeInt );
00338 myLabelAttributes->setAngle( spinAngle->value() );
00339
00340
00341
00342
00343 if ( radioAboveLeft->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignRight | Qt::AlignBottom );
00344 if ( radioBelowLeft->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignRight | Qt::AlignTop );
00345 if ( radioAboveRight->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignLeft | Qt::AlignBottom );
00346 if ( radioBelowRight->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignLeft | Qt::AlignTop );
00347 if ( radioLeft->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00348 if ( radioRight->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignLeft | Qt::AlignVCenter );
00349 if ( radioAbove->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignBottom | Qt::AlignHCenter );
00350 if ( radioBelow->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignTop | Qt::AlignHCenter );
00351 if ( radioOver->isChecked() ) myLabelAttributes->setAlignment( Qt::AlignCenter );
00352
00353 myLabelAttributes->setMultilineEnabled( chkUseMultiline->isChecked() );
00354 myLabelAttributes->setBufferEnabled( chkUseBuffer->isChecked() );
00355 myLabelAttributes->setBufferColor( mBufferColor );
00356 myTypeInt = 0;
00357 if ( radioBufferUnitsPoints->isChecked() )
00358 {
00359 myTypeInt = QgsLabelAttributes::PointUnits;
00360 }
00361 else
00362 {
00363 myTypeInt = QgsLabelAttributes::MapUnits;
00364 }
00365 myLabelAttributes->setBufferSize( spinBufferSize->value(), myTypeInt );
00366
00367
00368
00369 mLabel->setLabelField( QgsLabel::Text, fieldIndexFromName( cboLabelField->currentText() ) );
00370 mLabel->setLabelField( QgsLabel::Family, fieldIndexFromName( cboFontField->currentText() ) );
00371 mLabel->setLabelField( QgsLabel::Bold, fieldIndexFromName( cboBoldField->currentText() ) );
00372 mLabel->setLabelField( QgsLabel::Italic, fieldIndexFromName( cboItalicField->currentText() ) );
00373 mLabel->setLabelField( QgsLabel::Underline, fieldIndexFromName( cboUnderlineField->currentText() ) );
00374 mLabel->setLabelField( QgsLabel::Size, fieldIndexFromName( cboFontSizeField->currentText() ) );
00375 mLabel->setLabelField( QgsLabel::SizeType, fieldIndexFromName( cboFontSizeTypeField->currentText() ) );
00376 mLabel->setLabelField( QgsLabel::BufferSize, fieldIndexFromName( cboBufferSizeField->currentText() ) );
00377
00378 mLabel->setLabelField( QgsLabel::XCoordinate, fieldIndexFromName( cboXCoordinateField->currentText() ) );
00379 mLabel->setLabelField( QgsLabel::YCoordinate, fieldIndexFromName( cboYCoordinateField->currentText() ) );
00380 mLabel->setLabelField( QgsLabel::XOffset, fieldIndexFromName( cboXOffsetField->currentText() ) );
00381 mLabel->setLabelField( QgsLabel::YOffset, fieldIndexFromName( cboYOffsetField->currentText() ) );
00382 mLabel->setLabelField( QgsLabel::Alignment, fieldIndexFromName( cboAlignmentField->currentText() ) );
00383 mLabel->setLabelField( QgsLabel::Angle, fieldIndexFromName( cboAngleField->currentText() ) );
00384
00385 }
00386
00387 int QgsLabelDialog::fieldIndexFromName( QString name )
00388 {
00389 const QgsFieldMap& fields = mLabel->fields();
00390 for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
00391 {
00392 if ( it->name() == name )
00393 return it.key();
00394 }
00395 return -1;
00396 }