~ubuntu-branches/ubuntu/utopic/qgis/utopic

« back to all changes in this revision

Viewing changes to src/qgslabeldialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                         qgslabeldialog.cpp  -  render vector labels
 
3
                             -------------------
 
4
    begin                : August 2004
 
5
    copyright            : (C) 2004 by Radim Blazek & Tim Sutton
 
6
    email                : blazek@itc.it
 
7
 ***************************************************************************/
 
8
/***************************************************************************
 
9
 *                                                                         *
 
10
 *   This program is free software; you can redistribute it and/or modify  *
 
11
 *   it under the terms of the GNU General Public License as published by  *
 
12
 *   the Free Software Foundation; either version 2 of the License, or     *
 
13
 *   (at your option) any later version.                                   *
 
14
 *                                                                         *
 
15
 ***************************************************************************/
 
16
/* $Id: qgslabeldialog.cpp,v 1.9.2.1 2004/12/04 20:49:25 gsherman Exp $ */
 
17
#include <iostream>
 
18
 
 
19
//#include <qobject.h>
 
20
#include <qstring.h>
 
21
#include <qstringlist.h>
 
22
#include <qpoint.h>
 
23
#include "qpixmap.h"
 
24
#include <qwidget.h>
 
25
#include <qlayout.h>
 
26
#include <qtoolbutton.h>
 
27
#include <qlabel.h>
 
28
#include <qtable.h>
 
29
#include <qlineedit.h>
 
30
#include <qcombobox.h>
 
31
#include <qfontdatabase.h>
 
32
#include <qradiobutton.h> 
 
33
#include <qfont.h>
 
34
#include <qpushbutton.h>
 
35
#include <qspinbox.h>
 
36
#include <qcolor.h>
 
37
#include <qcolordialog.h>
 
38
#include <qfontdatabase.h>
 
39
#include <qfontdialog.h>
 
40
#include <qcheckbox.h>
 
41
 
 
42
#include "qgsfield.h"
 
43
#include "qgspatterndialog.h"
 
44
#include "qgslinestyledialog.h"
 
45
 
 
46
#include "qgslabelattributes.h"
 
47
#include "qgslabel.h"
 
48
#include "qgslabeldialog.h"
 
49
 
 
50
 
 
51
 
 
52
#define PIXMAP_WIDTH 200
 
53
#define PIXMAP_HEIGHT 20
 
54
 
 
55
QgsLabelDialog::QgsLabelDialog ( QgsLabel *label,  QWidget *parent ) : QgsLabelDialogBase (parent)
 
56
{
 
57
    #ifdef QGISDEBUG
 
58
//    std::cerr << "QgsLabelDialog::QgsLabelDialog()" << std::endl;
 
59
    #endif
 
60
 
 
61
    mLabel = label;
 
62
    init();
 
63
}
 
64
 
 
65
void QgsLabelDialog::init ( void )
 
66
{
 
67
#ifdef QGISDEBUG
 
68
  std::cerr << "QgsLabelDialog::init" << std::endl;
 
69
#endif
 
70
  QgsLabelAttributes * myLabelAttributes = mLabel->layerAttributes();
 
71
  //populate a string list with all the field names which will be used to set up the 
 
72
  //data bound combos
 
73
  std::vector<QgsField> myFieldsVector = mLabel->fields();
 
74
  QStringList myFieldStringList;
 
75
  myFieldStringList.append ( "" );
 
76
  for (unsigned int i = 0; i < myFieldsVector.size(); i++ ) 
 
77
  {
 
78
    myFieldStringList.append ( myFieldsVector[i].name() );
 
79
  }
 
80
  //
 
81
  //now set all the combos that need field lists using the string list
 
82
  //
 
83
  cboLabelField->clear();
 
84
  cboLabelField->insertStringList(myFieldStringList);
 
85
  cboLabelField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Text),myFieldStringList));
 
86
 
 
87
 
 
88
  cboFontField->clear();
 
89
  cboFontField->insertStringList(myFieldStringList); 
 
90
  cboFontField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Family),myFieldStringList));
 
91
 
 
92
  cboBoldField->clear();
 
93
  cboBoldField->insertStringList(myFieldStringList); 
 
94
  cboBoldField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Bold),myFieldStringList));
 
95
 
 
96
 
 
97
  cboItalicField->clear();
 
98
  cboItalicField->insertStringList(myFieldStringList); 
 
99
  cboItalicField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Italic),myFieldStringList));
 
100
 
 
101
  cboUnderlineField->clear();
 
102
  cboUnderlineField->insertStringList(myFieldStringList); 
 
103
  cboUnderlineField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Underline),myFieldStringList));
 
104
 
 
105
  cboFontSizeField->clear();
 
106
  cboFontSizeField->insertStringList(myFieldStringList); 
 
107
  cboFontSizeField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Size),myFieldStringList));
 
108
 
 
109
  cboFontTransparencyField->clear();
 
110
  cboFontTransparencyField->insertStringList(myFieldStringList); 
 
111
  //cboFontTransparencyField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::FontTransparency),myFieldStringList));
 
112
 
 
113
  //cboBufferColorField->clear();
 
114
  //cboBufferColorField->insertStringList(myFieldStringList); 
 
115
 
 
116
  cboBufferSizeField->clear();
 
117
  cboBufferSizeField->insertStringList(myFieldStringList); 
 
118
  cboBufferSizeField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::BufferSize),myFieldStringList));
 
119
 
 
120
  cboBufferTransparencyField->clear();
 
121
  cboBufferTransparencyField->insertStringList(myFieldStringList); 
 
122
  //cboBufferTransparencyField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::BufferTransparency),myFieldStringList));
 
123
 
 
124
  cboXCoordinateField->clear();
 
125
  cboXCoordinateField->insertStringList(myFieldStringList); 
 
126
  cboXCoordinateField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::XCoordinate),myFieldStringList));
 
127
 
 
128
  cboYCoordinateField->clear();
 
129
  cboYCoordinateField->insertStringList(myFieldStringList); 
 
130
  cboYCoordinateField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::YCoordinate),myFieldStringList));
 
131
 
 
132
  cboXOffsetField->clear();
 
133
  cboXOffsetField->insertStringList(myFieldStringList); 
 
134
  cboXOffsetField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::XOffset),myFieldStringList));
 
135
 
 
136
  cboYOffsetField->clear();
 
137
  cboYOffsetField->insertStringList(myFieldStringList); 
 
138
  cboYOffsetField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::YOffset),myFieldStringList));
 
139
 
 
140
  cboAlignmentField->clear();
 
141
  cboAlignmentField->insertStringList(myFieldStringList); 
 
142
  cboAlignmentField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Alignment),myFieldStringList));
 
143
 
 
144
  cboAngleField->clear();
 
145
  cboAngleField->insertStringList(myFieldStringList); 
 
146
  cboAngleField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Angle),myFieldStringList));
 
147
 
 
148
 
 
149
  //
 
150
  //set the non-databound fields up now
 
151
  //
 
152
  leDefaultLabel->setText(myLabelAttributes->text());
 
153
  mFont.setFamily(myLabelAttributes->family());
 
154
  if (myLabelAttributes->sizeIsSet())
 
155
  {
 
156
    mFont.setPointSize(static_cast<int>(myLabelAttributes->size()));
 
157
 
 
158
    int myTypeInt = myLabelAttributes->sizeType();
 
159
    if (myTypeInt == QgsLabelAttributes::PointUnits)
 
160
    {
 
161
      radioFontSizeUnitsPoints->setChecked(true);
 
162
    }
 
163
    else //assume map units is checked
 
164
    { 
 
165
      radioFontSizeUnitsMap->setChecked(true);
 
166
    }
 
167
  }
 
168
  else //defaults for when no size has been set
 
169
  {
 
170
    mFont.setPointSize(static_cast<int>(myLabelAttributes->size()));
 
171
    radioFontSizeUnitsPoints->setChecked(true);
 
172
  }
 
173
 
 
174
  if (myLabelAttributes->boldIsSet())
 
175
  {
 
176
     mFont.setBold(myLabelAttributes->bold());
 
177
  }
 
178
  else
 
179
  {
 
180
     mFont.setBold(false);
 
181
  }
 
182
  if (myLabelAttributes->italicIsSet())
 
183
  {
 
184
     mFont.setBold(myLabelAttributes->italic());
 
185
  }
 
186
  else
 
187
  {
 
188
     mFont.setItalic(false);
 
189
  }
 
190
  mFont.setUnderline(myLabelAttributes->underline());
 
191
  mFontColor= myLabelAttributes->color(); 
 
192
  
 
193
  if (myLabelAttributes->offsetIsSet())
 
194
  {
 
195
    int myTypeInt = myLabelAttributes->offsetType();
 
196
    if ( myTypeInt == QgsLabelAttributes::PointUnits )
 
197
    {
 
198
      radioOffsetUnitsPoints->setChecked(true);
 
199
    } 
 
200
    else 
 
201
    { 
 
202
      radioOffsetUnitsMap->setChecked(true);
 
203
    }
 
204
    spinXOffset->setValue(static_cast<int>(myLabelAttributes->xOffset()));
 
205
    spinYOffset->setValue(static_cast<int>(myLabelAttributes->yOffset()));
 
206
  }
 
207
  else //defaults for when no offset is defined
 
208
  {
 
209
    spinXOffset->setValue(0);
 
210
    spinYOffset->setValue(0);
 
211
  }
 
212
  spinAngle->setValue(static_cast<int>(myLabelAttributes->angle())); 
 
213
 
 
214
  //the values here may seem a bit counterintuitive - basically everything 
 
215
  //is the reverse of the way you think it should be...
 
216
  //TODO investigate in QgsLabel why this needs to be the case
 
217
  //TODO add support for corners (e.g. bottom right) to xml serialiser
 
218
  
 
219
  if ( myLabelAttributes->alignment() == (Qt::AlignRight | Qt::AlignBottom  )) radioAboveLeft->setChecked(true)   ;
 
220
  if ( myLabelAttributes->alignment() == (Qt::AlignRight | Qt::AlignTop     )) radioBelowLeft->setChecked(true)   ; 
 
221
  if ( myLabelAttributes->alignment() == (Qt::AlignLeft  | Qt::AlignBottom  )) radioAboveRight->setChecked(true)  ;
 
222
  if ( myLabelAttributes->alignment() == (Qt::AlignLeft  | Qt::AlignTop     )) radioBelowRight->setChecked(true)  ;
 
223
  if ( myLabelAttributes->alignment() == (Qt::AlignRight | Qt::AlignVCenter )) radioLeft->setChecked(true)        ;
 
224
  if ( myLabelAttributes->alignment() == (Qt::AlignLeft  | Qt::AlignVCenter )) radioRight->setChecked(true)       ;
 
225
  if ( myLabelAttributes->alignment() == (Qt::AlignBottom| Qt::AlignHCenter )) radioAbove->setChecked(true)       ; 
 
226
  if ( myLabelAttributes->alignment() == (Qt::AlignTop   | Qt::AlignHCenter )) radioBelow->setChecked(true)       ; 
 
227
  if ( myLabelAttributes->alignment() == Qt::AlignCenter                   ) radioOver->setChecked(true)        ;  
 
228
 
 
229
  mBufferColor = myLabelAttributes->bufferColor(); 
 
230
  //note that it could be that buffer properties are set, but the bufer is disabled
 
231
  if (myLabelAttributes->bufferSizeIsSet() )
 
232
  {
 
233
    int myTypeInt = myLabelAttributes->bufferSizeType();
 
234
    if ( myTypeInt == QgsLabelAttributes::PointUnits )
 
235
    {
 
236
      radioBufferUnitsPoints->setChecked(true);
 
237
    } 
 
238
    else 
 
239
    { 
 
240
      radioBufferUnitsMap->setChecked(true);
 
241
    }
 
242
    spinBufferSize->setValue(static_cast<int>(myLabelAttributes->bufferSize()));
 
243
  }
 
244
  else //defaults for when no offset is defined
 
245
  {
 
246
    spinBufferSize->setValue(0);
 
247
  }
 
248
  //set the state of the buffer enabled checkbox
 
249
  chkUseBuffer->setChecked(myLabelAttributes->bufferEnabled());   
 
250
  
 
251
  //NOTE: do we need this line too? TS
 
252
  spinBufferSize->setValue(static_cast<int>(myLabelAttributes->bufferSize()));
 
253
  //TODO - transparency attributes for buffers
 
254
 
 
255
}
 
256
 
 
257
 
 
258
 
 
259
void QgsLabelDialog::changeFont ( void )
 
260
{
 
261
    #ifdef QGISDEBUG
 
262
    std::cerr << "QgsLabelDialog::changeFont()" << std::endl;
 
263
    #endif
 
264
    bool resultFlag;
 
265
    mFont = QFontDialog::getFont(&resultFlag, mFont, this );
 
266
    if ( resultFlag ) 
 
267
    {
 
268
        // font is set to the font the user selected
 
269
    } else 
 
270
    {
 
271
        // the user cancelled the dialog; font is set to the initial
 
272
        // value, in this case Helvetica [Cronyx], 10
 
273
    }
 
274
    lblSample->setFont(mFont);
 
275
}
 
276
 
 
277
void QgsLabelDialog::changeFontColor(void)
 
278
{
 
279
    #ifdef QGISDEBUG
 
280
    std::cerr << "QgsLabelDialog::changeFontColor()" << std::endl;
 
281
    #endif
 
282
    mFontColor = QColorDialog::getColor ( mFontColor );
 
283
    lblSample->setPaletteForegroundColor(mFontColor);
 
284
}
 
285
 
 
286
void QgsLabelDialog::changeBufferColor(void)
 
287
{
 
288
    #ifdef QGISDEBUG
 
289
    std::cerr << "QgsLabelDialog::changeBufferColor()" << std::endl;
 
290
    #endif
 
291
    mBufferColor = QColorDialog::getColor ( mBufferColor );
 
292
    lblSample->setPaletteBackgroundColor(mBufferColor);
 
293
}
 
294
 
 
295
 
 
296
int QgsLabelDialog::itemNoForField(QString theFieldName, QStringList theFieldList)
 
297
{
 
298
    int myItemInt=0;
 
299
    for ( QStringList::Iterator it = theFieldList.begin(); it != theFieldList.end(); ++it ) 
 
300
    {
 
301
        if (theFieldName == *it) return myItemInt;
 
302
        ++myItemInt;
 
303
    }
 
304
    //if no matches assume first item in list is blank and return that
 
305
    return 0;
 
306
}
 
307
 
 
308
QgsLabelDialog::~QgsLabelDialog()
 
309
{
 
310
    #ifdef QGISDEBUG
 
311
    std::cerr << "QgsLabelDialog::~QgsLabelDialog()" << std::endl;
 
312
    #endif
 
313
}
 
314
 
 
315
void QgsLabelDialog::apply()
 
316
{
 
317
    #ifdef QGISDEBUG
 
318
    std::cerr << "QgsLabelDialog::apply()" << std::endl;
 
319
    #endif
 
320
 
 
321
    //set the label props that are NOT bound to a field in the attributes tbl
 
322
    //All of these are set in the layerAttributes member of the layer
 
323
    QgsLabelAttributes * myLabelAttributes = mLabel->layerAttributes();
 
324
    myLabelAttributes->setText(leDefaultLabel->text()); 
 
325
    myLabelAttributes->setFamily(mFont.family());
 
326
    int myTypeInt = 0;
 
327
    if ( radioFontSizeUnitsPoints->isChecked())
 
328
    {
 
329
       myTypeInt = QgsLabelAttributes::PointUnits; 
 
330
    } 
 
331
    else //assume map units is checked
 
332
    { 
 
333
       myTypeInt = QgsLabelAttributes::MapUnits;
 
334
    }
 
335
    myLabelAttributes->setSize(mFont.pointSize() , myTypeInt);
 
336
    myLabelAttributes->setBold(mFont.bold());
 
337
    myLabelAttributes->setItalic(mFont.italic());
 
338
    myLabelAttributes->setUnderline(mFont.underline());
 
339
    myLabelAttributes->setColor(mFontColor); 
 
340
    myTypeInt = 0;
 
341
    if ( radioOffsetUnitsPoints->isChecked() )
 
342
    {
 
343
       myTypeInt = QgsLabelAttributes::PointUnits; 
 
344
    } 
 
345
    else 
 
346
    { 
 
347
       myTypeInt = QgsLabelAttributes::MapUnits;
 
348
    }
 
349
    myLabelAttributes->setOffset(spinXOffset->value() ,spinYOffset->value(), myTypeInt);
 
350
    myLabelAttributes->setAngle(spinAngle->value()); 
 
351
 
 
352
    //the values here may seem a bit counterintuitive - basically everything 
 
353
    //is the reverse of the way you think it should be...
 
354
    //TODO investigate in QgsLabel why this needs to be the case
 
355
    if (radioAboveLeft->isChecked())   myLabelAttributes->setAlignment(Qt::AlignRight | Qt::AlignBottom);
 
356
    if (radioBelowLeft->isChecked())   myLabelAttributes->setAlignment(Qt::AlignRight | Qt::AlignTop);
 
357
    if (radioAboveRight->isChecked())  myLabelAttributes->setAlignment(Qt::AlignLeft  | Qt::AlignBottom);
 
358
    if (radioBelowRight->isChecked())  myLabelAttributes->setAlignment(Qt::AlignLeft  | Qt::AlignTop);
 
359
    if (radioLeft->isChecked())        myLabelAttributes->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
 
360
    if (radioRight->isChecked())       myLabelAttributes->setAlignment(Qt::AlignLeft  | Qt::AlignVCenter);
 
361
    if (radioAbove->isChecked())       myLabelAttributes->setAlignment(Qt::AlignBottom| Qt::AlignHCenter); 
 
362
    if (radioBelow->isChecked())       myLabelAttributes->setAlignment(Qt::AlignTop   | Qt::AlignHCenter); 
 
363
    if (radioOver->isChecked())        myLabelAttributes->setAlignment(Qt::AlignCenter);
 
364
      
 
365
    myLabelAttributes->setBufferEnabled(chkUseBuffer->isChecked());
 
366
    myLabelAttributes->setBufferColor(mBufferColor); 
 
367
    myTypeInt = 0;
 
368
    if ( radioBufferUnitsPoints->isChecked() )
 
369
    {
 
370
       myTypeInt = QgsLabelAttributes::PointUnits; 
 
371
    } 
 
372
    else 
 
373
    { 
 
374
       myTypeInt = QgsLabelAttributes::MapUnits;
 
375
    }
 
376
    myLabelAttributes->setBufferSize(spinBufferSize->value(), myTypeInt);
 
377
    //TODO - transparency attributes for buffers
 
378
    
 
379
    //set the label props that are data bound to a field in the attributes tbl
 
380
    mLabel->setLabelField( QgsLabel::Text,  cboLabelField->currentText() );
 
381
    mLabel->setLabelField( QgsLabel::Family, cboFontField->currentText() );
 
382
    mLabel->setLabelField( QgsLabel::Bold,  cboBoldField->currentText() );
 
383
    mLabel->setLabelField( QgsLabel::Italic,  cboItalicField->currentText() );
 
384
    mLabel->setLabelField( QgsLabel::Underline,  cboUnderlineField->currentText() );
 
385
    mLabel->setLabelField( QgsLabel::Size,  cboFontSizeField->currentText() );
 
386
    mLabel->setLabelField( QgsLabel::BufferSize,  cboBufferSizeField->currentText() );
 
387
    //mLabel->setLabelField( QgsLabel::BufferTransparency,  cboBufferTransparencyField->currentText() );
 
388
    mLabel->setLabelField( QgsLabel::XCoordinate,  cboXCoordinateField->currentText() );
 
389
    mLabel->setLabelField( QgsLabel::YCoordinate,  cboYCoordinateField->currentText() );
 
390
    mLabel->setLabelField( QgsLabel::XOffset,  cboXOffsetField->currentText() );
 
391
    mLabel->setLabelField( QgsLabel::YOffset,  cboYOffsetField->currentText() );
 
392
    mLabel->setLabelField( QgsLabel::Alignment,  cboAlignmentField->currentText() );
 
393
    mLabel->setLabelField( QgsLabel::Angle,  cboAngleField->currentText() );
 
394
 
 
395
}
 
396