src/app/qgslabeldialog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgslabeldialog.cpp  -  render vector labels
00003                              -------------------
00004     begin                : August 2004
00005     copyright            : (C) 2004 by Radim Blazek & Tim Sutton
00006     email                : blazek@itc.it
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 /* $Id: qgslabeldialog.cpp 9189 2008-08-27 12:57:47Z jef $ */
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   //populate a string list with all the field names which will be used to set up the
00059   //data bound combos
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   //now set all the combos that need field lists using the string list
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   //cboFontTransparencyField->setCurrentIndex(itemNoForField(mLabel->labelField(QgsLabel::FontTransparency),myFieldStringList));
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   //cboBufferTransparencyField->setCurrentIndex(itemNoForField(mLabel->labelField(QgsLabel::BufferTransparency),myFieldStringList));
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   //set the non-databound fields up now
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 //assume map units is checked
00152     {
00153       radioFontSizeUnitsMap->setChecked( true );
00154     }
00155   }
00156   else //defaults for when no size has been set
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 //defaults for when no offset is defined
00196   {
00197     spinXOffset->setValue( 0 );
00198     spinYOffset->setValue( 0 );
00199   }
00200   spinAngle->setValue( static_cast<int>( myLabelAttributes->angle() ) );
00201 
00202   //the values here may seem a bit counterintuitive - basically everything
00203   //is the reverse of the way you think it should be...
00204   //TODO investigate in QgsLabel why this needs to be the case
00205   //TODO add support for corners (e.g. bottom right) to xml serialiser
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   //note that it could be that buffer properties are set, but the bufer is disabled
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 //defaults for when no offset is defined
00233   {
00234     spinBufferSize->setValue( 1 );
00235   }
00236   //set the state of the multiline enabled checkbox
00237   chkUseMultiline->setChecked( myLabelAttributes->multilineEnabled() );
00238   //set the state of the buffer enabled checkbox
00239   chkUseBuffer->setChecked( myLabelAttributes->bufferEnabled() );
00240 
00241   //NOTE: do we need this line too? TS
00242   spinBufferSize->setValue( static_cast<int>( myLabelAttributes->bufferSize() ) );
00243   //TODO - transparency attributes for buffers
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     // font is set to the font the user selected
00258   }
00259   else
00260   {
00261     // the user cancelled the dialog; font is set to the initial
00262     // value, in this case Helvetica [Cronyx], 10
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   //if no matches assume first item in list is blank and return that
00297   return 0;
00298 }
00299 
00300 QgsLabelDialog::~QgsLabelDialog()
00301 {
00302   QgsDebugMsg( "entering." );
00303 }
00304 
00305 void QgsLabelDialog::apply()
00306 {
00307   QgsDebugMsg( "entering." );
00308 
00309   //set the label props that are NOT bound to a field in the attributes tbl
00310   //All of these are set in the layerAttributes member of the layer
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 //assume map units is checked
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   //the values here may seem a bit counterintuitive - basically everything
00341   //is the reverse of the way you think it should be...
00342   //TODO investigate in QgsLabel why this needs to be the case
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   //TODO - transparency attributes for buffers
00367 
00368   //set the label props that are data bound to a field in the attributes tbl
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   //mLabel->setLabelField( QgsLabel::BufferTransparency,  cboBufferTransparencyField->currentText() );
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 }

Generated on Tue Oct 28 16:51:26 2008 for Quantum GIS API Documentation by  doxygen 1.5.1