~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/plugins/labeling/labelinggui.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
  labelinggui.cpp
 
3
  Smart labeling for vector layers
 
4
  -------------------
 
5
         begin                : June 2009
 
6
         copyright            : (C) Martin Dobias
 
7
         email                : wonder.sk at gmail.com
 
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 "labelinggui.h"
 
19
 
 
20
#include <qgsvectorlayer.h>
 
21
#include <qgsvectordataprovider.h>
 
22
#include <qgsmaplayerregistry.h>
 
23
 
 
24
#include "pallabeling.h"
 
25
#include "engineconfigdialog.h"
 
26
 
 
27
#include <QColorDialog>
 
28
#include <QFontDialog>
 
29
 
 
30
#include <iostream>
 
31
#include <QApplication>
 
32
 
 
33
 
 
34
 
 
35
LabelingGui::LabelingGui( PalLabeling* lbl, QgsVectorLayer* layer, QWidget* parent )
 
36
    : QDialog( parent ), mLBL( lbl ), mLayer( layer )
 
37
{
 
38
  setupUi( this );
 
39
 
 
40
  connect( btnTextColor, SIGNAL( clicked() ), this, SLOT( changeTextColor() ) );
 
41
  connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
 
42
  connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
 
43
  connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
 
44
  connect( spinBufferSize, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
 
45
  connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );
 
46
 
 
47
  // set placement methods page based on geometry type
 
48
  switch ( layer->geometryType() )
 
49
  {
 
50
    case QGis::Point:
 
51
      stackedPlacement->setCurrentWidget( pagePoint );
 
52
      break;
 
53
    case QGis::Line:
 
54
      stackedPlacement->setCurrentWidget( pageLine );
 
55
      break;
 
56
    case QGis::Polygon:
 
57
      stackedPlacement->setCurrentWidget( pagePolygon );
 
58
      break;
 
59
    default:
 
60
      Q_ASSERT( 0 && "NOOOO!" );
 
61
  }
 
62
 
 
63
  chkMergeLines->setEnabled( layer->geometryType() == QGis::Line );
 
64
 
 
65
  populateFieldNames();
 
66
 
 
67
  // load labeling settings from layer
 
68
  LayerSettings lyr;
 
69
  lyr.readFromLayer( layer );
 
70
 
 
71
  // placement
 
72
  switch ( lyr.placement )
 
73
  {
 
74
    case LayerSettings::AroundPoint:
 
75
      radAroundPoint->setChecked( true );
 
76
      radAroundCentroid->setChecked( true );
 
77
      spinDistPoint->setValue( lyr.dist );
 
78
      //spinAngle->setValue(lyr.angle);
 
79
      break;
 
80
    case LayerSettings::OverPoint:
 
81
      radOverPoint->setChecked( true );
 
82
      radOverCentroid->setChecked( true );
 
83
      break;
 
84
    case LayerSettings::Line:
 
85
      radLineParallel->setChecked( true );
 
86
      radPolygonPerimeter->setChecked( true );
 
87
      break;
 
88
    case LayerSettings::Curved:
 
89
      radLineCurved->setChecked( true );
 
90
      break;
 
91
    case LayerSettings::Horizontal:
 
92
      radPolygonHorizontal->setChecked( true );
 
93
      radLineHorizontal->setChecked( true );
 
94
      break;
 
95
    case LayerSettings::Free:
 
96
      radPolygonFree->setChecked( true );
 
97
      break;
 
98
    default:
 
99
      Q_ASSERT( 0 && "NOOO!" );
 
100
  }
 
101
 
 
102
  if ( lyr.placement == LayerSettings::Line || lyr.placement == LayerSettings::Curved )
 
103
  {
 
104
    spinDistLine->setValue( lyr.dist );
 
105
    chkLineAbove->setChecked( lyr.placementFlags & LayerSettings::AboveLine );
 
106
    chkLineBelow->setChecked( lyr.placementFlags & LayerSettings::BelowLine );
 
107
    chkLineOn->setChecked( lyr.placementFlags & LayerSettings::OnLine );
 
108
    if ( lyr.placementFlags & LayerSettings::MapOrientation )
 
109
      radOrientationMap->setChecked( true );
 
110
    else
 
111
      radOrientationLine->setChecked( true );
 
112
  }
 
113
 
 
114
  cboFieldName->setCurrentIndex( cboFieldName->findText( lyr.fieldName ) );
 
115
  chkEnableLabeling->setChecked( lyr.enabled );
 
116
  sliderPriority->setValue( lyr.priority );
 
117
  chkNoObstacle->setChecked( !lyr.obstacle );
 
118
  chkLabelPerFeaturePart->setChecked( lyr.labelPerPart );
 
119
  chkMergeLines->setChecked( lyr.mergeLines );
 
120
 
 
121
  bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
 
122
  chkScaleBasedVisibility->setChecked( scaleBased );
 
123
  if ( scaleBased )
 
124
  {
 
125
    spinScaleMin->setValue( lyr.scaleMin );
 
126
    spinScaleMax->setValue( lyr.scaleMax );
 
127
  }
 
128
 
 
129
  bool buffer = ( lyr.bufferSize != 0 );
 
130
  chkBuffer->setChecked( buffer );
 
131
  if ( buffer )
 
132
    spinBufferSize->setValue( lyr.bufferSize );
 
133
 
 
134
  btnTextColor->setColor( lyr.textColor );
 
135
  btnBufferColor->setColor( lyr.bufferColor );
 
136
  updateFont( lyr.textFont );
 
137
  updateUi();
 
138
 
 
139
  updateOptions();
 
140
 
 
141
  connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
 
142
  connect( chkScaleBasedVisibility, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
 
143
 
 
144
  // setup connection to changes in the placement
 
145
  QRadioButton* placementRadios[] =
 
146
  {
 
147
    radAroundPoint, radOverPoint, // point
 
148
    radLineParallel, radLineCurved, radLineHorizontal, // line
 
149
    radAroundCentroid, radPolygonHorizontal, radPolygonFree, radPolygonPerimeter // polygon
 
150
  };
 
151
  for ( unsigned int i = 0; i < sizeof( placementRadios ) / sizeof( QRadioButton* ); i++ )
 
152
    connect( placementRadios[i], SIGNAL( toggled( bool ) ), this, SLOT( updateOptions() ) );
 
153
}
 
154
 
 
155
LabelingGui::~LabelingGui()
 
156
{
 
157
}
 
158
 
 
159
LayerSettings LabelingGui::layerSettings()
 
160
{
 
161
  LayerSettings lyr;
 
162
  lyr.fieldName = cboFieldName->currentText();
 
163
 
 
164
  lyr.dist = 0;
 
165
  lyr.placementFlags = 0;
 
166
 
 
167
  if (( stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked() )
 
168
      || ( stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked() ) )
 
169
  {
 
170
    lyr.placement = LayerSettings::AroundPoint;
 
171
    lyr.dist = spinDistPoint->value();
 
172
    //lyr.angle = spinAngle->value();
 
173
  }
 
174
  else if (( stackedPlacement->currentWidget() == pagePoint && radOverPoint->isChecked() )
 
175
           || ( stackedPlacement->currentWidget() == pagePolygon && radOverCentroid->isChecked() ) )
 
176
  {
 
177
    lyr.placement = LayerSettings::OverPoint;
 
178
  }
 
179
  else if (( stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked() )
 
180
           || ( stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked() )
 
181
           || ( stackedPlacement->currentWidget() == pageLine && radLineCurved->isChecked() ) )
 
182
  {
 
183
    bool curved = ( stackedPlacement->currentWidget() == pageLine && radLineCurved->isChecked() );
 
184
    lyr.placement = ( curved ? LayerSettings::Curved : LayerSettings::Line );
 
185
    lyr.dist = spinDistLine->value();
 
186
    if ( chkLineAbove->isChecked() )
 
187
      lyr.placementFlags |= LayerSettings::AboveLine;
 
188
    if ( chkLineBelow->isChecked() )
 
189
      lyr.placementFlags |= LayerSettings::BelowLine;
 
190
    if ( chkLineOn->isChecked() )
 
191
      lyr.placementFlags |= LayerSettings::OnLine;
 
192
 
 
193
    if ( radOrientationMap->isChecked() )
 
194
      lyr.placementFlags |= LayerSettings::MapOrientation;
 
195
  }
 
196
  else if (( stackedPlacement->currentWidget() == pageLine && radLineHorizontal->isChecked() )
 
197
           || ( stackedPlacement->currentWidget() == pagePolygon && radPolygonHorizontal->isChecked() ) )
 
198
  {
 
199
    lyr.placement = LayerSettings::Horizontal;
 
200
  }
 
201
  else if ( radPolygonFree->isChecked() )
 
202
  {
 
203
    lyr.placement = LayerSettings::Free;
 
204
  }
 
205
  else
 
206
    Q_ASSERT( 0 && "NOOO!" );
 
207
 
 
208
 
 
209
  lyr.textColor = btnTextColor->color();
 
210
  lyr.textFont = lblFontPreview->font();
 
211
  lyr.enabled = chkEnableLabeling->isChecked();
 
212
  lyr.priority = sliderPriority->value();
 
213
  lyr.obstacle = !chkNoObstacle->isChecked();
 
214
  lyr.labelPerPart = chkLabelPerFeaturePart->isChecked();
 
215
  lyr.mergeLines = chkMergeLines->isChecked();
 
216
  if ( chkScaleBasedVisibility->isChecked() )
 
217
  {
 
218
    lyr.scaleMin = spinScaleMin->value();
 
219
    lyr.scaleMax = spinScaleMax->value();
 
220
  }
 
221
  else
 
222
  {
 
223
    lyr.scaleMin = lyr.scaleMax = 0;
 
224
  }
 
225
  if ( chkBuffer->isChecked() )
 
226
  {
 
227
    lyr.bufferSize = spinBufferSize->value();
 
228
    lyr.bufferColor = btnBufferColor->color();
 
229
  }
 
230
  else
 
231
  {
 
232
    lyr.bufferSize = 0;
 
233
  }
 
234
 
 
235
  return lyr;
 
236
}
 
237
 
 
238
 
 
239
void LabelingGui::populateFieldNames()
 
240
{
 
241
  QgsFieldMap fields = mLayer->dataProvider()->fields();
 
242
  for ( QgsFieldMap::iterator it = fields.begin(); it != fields.end(); it++ )
 
243
  {
 
244
    cboFieldName->addItem( it->name() );
 
245
  }
 
246
}
 
247
 
 
248
void LabelingGui::changeTextColor()
 
249
{
 
250
  QColor color = QColorDialog::getColor( btnTextColor->color(), this );
 
251
  if ( !color.isValid() )
 
252
    return;
 
253
 
 
254
  btnTextColor->setColor( color );
 
255
  updatePreview();
 
256
}
 
257
 
 
258
void LabelingGui::changeTextFont()
 
259
{
 
260
  bool ok;
 
261
  QFont font = QFontDialog::getFont( &ok, lblFontPreview->font(), this );
 
262
  if ( ok )
 
263
    updateFont( font );
 
264
}
 
265
 
 
266
void LabelingGui::updateFont( QFont font )
 
267
{
 
268
  lblFontName->setText( QString( "%1, %2" ).arg( font.family() ).arg( font.pointSize() ) );
 
269
  lblFontPreview->setFont( font );
 
270
 
 
271
  updatePreview();
 
272
}
 
273
 
 
274
void LabelingGui::updatePreview()
 
275
{
 
276
  lblFontPreview->setTextColor( btnTextColor->color() );
 
277
  if ( chkBuffer->isChecked() )
 
278
    lblFontPreview->setBuffer( spinBufferSize->value(), btnBufferColor->color() );
 
279
  else
 
280
    lblFontPreview->setBuffer( 0, Qt::white );
 
281
}
 
282
 
 
283
void LabelingGui::showEngineConfigDialog()
 
284
{
 
285
  EngineConfigDialog dlg( mLBL, this );
 
286
  dlg.exec();
 
287
}
 
288
 
 
289
void LabelingGui::updateUi()
 
290
{
 
291
  // enable/disable scale-based, buffer
 
292
  bool buf = chkBuffer->isChecked();
 
293
  spinBufferSize->setEnabled( buf );
 
294
  btnBufferColor->setEnabled( buf );
 
295
 
 
296
  bool scale = chkScaleBasedVisibility->isChecked();
 
297
  spinScaleMin->setEnabled( scale );
 
298
  spinScaleMax->setEnabled( scale );
 
299
}
 
300
 
 
301
void LabelingGui::changeBufferColor()
 
302
{
 
303
  QColor color = QColorDialog::getColor( btnBufferColor->color(), this );
 
304
  if ( !color.isValid() )
 
305
    return;
 
306
 
 
307
  btnBufferColor->setColor( color );
 
308
  updatePreview();
 
309
}
 
310
 
 
311
void LabelingGui::updateOptions()
 
312
{
 
313
  if (( stackedPlacement->currentWidget() == pagePoint && radAroundPoint->isChecked() )
 
314
      || ( stackedPlacement->currentWidget() == pagePolygon && radAroundCentroid->isChecked() ) )
 
315
  {
 
316
    stackedOptions->setCurrentWidget( pageOptionsPoint );
 
317
  }
 
318
  else if (( stackedPlacement->currentWidget() == pageLine && radLineParallel->isChecked() )
 
319
           || ( stackedPlacement->currentWidget() == pagePolygon && radPolygonPerimeter->isChecked() )
 
320
           || ( stackedPlacement->currentWidget() == pageLine && radLineCurved->isChecked() ) )
 
321
  {
 
322
    stackedOptions->setCurrentWidget( pageOptionsLine );
 
323
  }
 
324
  else
 
325
  {
 
326
    stackedOptions->setCurrentWidget( pageOptionsEmpty );
 
327
  }
 
328
}