1
/***************************************************************************
2
qgslabeldialog.cpp - render vector labels
5
copyright : (C) 2004 by Radim Blazek & Tim Sutton
7
***************************************************************************/
8
/***************************************************************************
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. *
15
***************************************************************************/
16
/* $Id: qgslabeldialog.cpp,v 1.9.2.1 2004/12/04 20:49:25 gsherman Exp $ */
19
//#include <qobject.h>
21
#include <qstringlist.h>
26
#include <qtoolbutton.h>
29
#include <qlineedit.h>
30
#include <qcombobox.h>
31
#include <qfontdatabase.h>
32
#include <qradiobutton.h>
34
#include <qpushbutton.h>
37
#include <qcolordialog.h>
38
#include <qfontdatabase.h>
39
#include <qfontdialog.h>
40
#include <qcheckbox.h>
43
#include "qgspatterndialog.h"
44
#include "qgslinestyledialog.h"
46
#include "qgslabelattributes.h"
48
#include "qgslabeldialog.h"
52
#define PIXMAP_WIDTH 200
53
#define PIXMAP_HEIGHT 20
55
QgsLabelDialog::QgsLabelDialog ( QgsLabel *label, QWidget *parent ) : QgsLabelDialogBase (parent)
58
// std::cerr << "QgsLabelDialog::QgsLabelDialog()" << std::endl;
65
void QgsLabelDialog::init ( void )
68
std::cerr << "QgsLabelDialog::init" << std::endl;
70
QgsLabelAttributes * myLabelAttributes = mLabel->layerAttributes();
71
//populate a string list with all the field names which will be used to set up the
73
std::vector<QgsField> myFieldsVector = mLabel->fields();
74
QStringList myFieldStringList;
75
myFieldStringList.append ( "" );
76
for (unsigned int i = 0; i < myFieldsVector.size(); i++ )
78
myFieldStringList.append ( myFieldsVector[i].name() );
81
//now set all the combos that need field lists using the string list
83
cboLabelField->clear();
84
cboLabelField->insertStringList(myFieldStringList);
85
cboLabelField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Text),myFieldStringList));
88
cboFontField->clear();
89
cboFontField->insertStringList(myFieldStringList);
90
cboFontField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Family),myFieldStringList));
92
cboBoldField->clear();
93
cboBoldField->insertStringList(myFieldStringList);
94
cboBoldField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Bold),myFieldStringList));
97
cboItalicField->clear();
98
cboItalicField->insertStringList(myFieldStringList);
99
cboItalicField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Italic),myFieldStringList));
101
cboUnderlineField->clear();
102
cboUnderlineField->insertStringList(myFieldStringList);
103
cboUnderlineField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Underline),myFieldStringList));
105
cboFontSizeField->clear();
106
cboFontSizeField->insertStringList(myFieldStringList);
107
cboFontSizeField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Size),myFieldStringList));
109
cboFontTransparencyField->clear();
110
cboFontTransparencyField->insertStringList(myFieldStringList);
111
//cboFontTransparencyField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::FontTransparency),myFieldStringList));
113
//cboBufferColorField->clear();
114
//cboBufferColorField->insertStringList(myFieldStringList);
116
cboBufferSizeField->clear();
117
cboBufferSizeField->insertStringList(myFieldStringList);
118
cboBufferSizeField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::BufferSize),myFieldStringList));
120
cboBufferTransparencyField->clear();
121
cboBufferTransparencyField->insertStringList(myFieldStringList);
122
//cboBufferTransparencyField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::BufferTransparency),myFieldStringList));
124
cboXCoordinateField->clear();
125
cboXCoordinateField->insertStringList(myFieldStringList);
126
cboXCoordinateField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::XCoordinate),myFieldStringList));
128
cboYCoordinateField->clear();
129
cboYCoordinateField->insertStringList(myFieldStringList);
130
cboYCoordinateField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::YCoordinate),myFieldStringList));
132
cboXOffsetField->clear();
133
cboXOffsetField->insertStringList(myFieldStringList);
134
cboXOffsetField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::XOffset),myFieldStringList));
136
cboYOffsetField->clear();
137
cboYOffsetField->insertStringList(myFieldStringList);
138
cboYOffsetField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::YOffset),myFieldStringList));
140
cboAlignmentField->clear();
141
cboAlignmentField->insertStringList(myFieldStringList);
142
cboAlignmentField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Alignment),myFieldStringList));
144
cboAngleField->clear();
145
cboAngleField->insertStringList(myFieldStringList);
146
cboAngleField->setCurrentItem(itemNoForField(mLabel->labelField(QgsLabel::Angle),myFieldStringList));
150
//set the non-databound fields up now
152
leDefaultLabel->setText(myLabelAttributes->text());
153
mFont.setFamily(myLabelAttributes->family());
154
if (myLabelAttributes->sizeIsSet())
156
mFont.setPointSize(static_cast<int>(myLabelAttributes->size()));
158
int myTypeInt = myLabelAttributes->sizeType();
159
if (myTypeInt == QgsLabelAttributes::PointUnits)
161
radioFontSizeUnitsPoints->setChecked(true);
163
else //assume map units is checked
165
radioFontSizeUnitsMap->setChecked(true);
168
else //defaults for when no size has been set
170
mFont.setPointSize(static_cast<int>(myLabelAttributes->size()));
171
radioFontSizeUnitsPoints->setChecked(true);
174
if (myLabelAttributes->boldIsSet())
176
mFont.setBold(myLabelAttributes->bold());
180
mFont.setBold(false);
182
if (myLabelAttributes->italicIsSet())
184
mFont.setBold(myLabelAttributes->italic());
188
mFont.setItalic(false);
190
mFont.setUnderline(myLabelAttributes->underline());
191
mFontColor= myLabelAttributes->color();
193
if (myLabelAttributes->offsetIsSet())
195
int myTypeInt = myLabelAttributes->offsetType();
196
if ( myTypeInt == QgsLabelAttributes::PointUnits )
198
radioOffsetUnitsPoints->setChecked(true);
202
radioOffsetUnitsMap->setChecked(true);
204
spinXOffset->setValue(static_cast<int>(myLabelAttributes->xOffset()));
205
spinYOffset->setValue(static_cast<int>(myLabelAttributes->yOffset()));
207
else //defaults for when no offset is defined
209
spinXOffset->setValue(0);
210
spinYOffset->setValue(0);
212
spinAngle->setValue(static_cast<int>(myLabelAttributes->angle()));
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
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) ;
229
mBufferColor = myLabelAttributes->bufferColor();
230
//note that it could be that buffer properties are set, but the bufer is disabled
231
if (myLabelAttributes->bufferSizeIsSet() )
233
int myTypeInt = myLabelAttributes->bufferSizeType();
234
if ( myTypeInt == QgsLabelAttributes::PointUnits )
236
radioBufferUnitsPoints->setChecked(true);
240
radioBufferUnitsMap->setChecked(true);
242
spinBufferSize->setValue(static_cast<int>(myLabelAttributes->bufferSize()));
244
else //defaults for when no offset is defined
246
spinBufferSize->setValue(0);
248
//set the state of the buffer enabled checkbox
249
chkUseBuffer->setChecked(myLabelAttributes->bufferEnabled());
251
//NOTE: do we need this line too? TS
252
spinBufferSize->setValue(static_cast<int>(myLabelAttributes->bufferSize()));
253
//TODO - transparency attributes for buffers
259
void QgsLabelDialog::changeFont ( void )
262
std::cerr << "QgsLabelDialog::changeFont()" << std::endl;
265
mFont = QFontDialog::getFont(&resultFlag, mFont, this );
268
// font is set to the font the user selected
271
// the user cancelled the dialog; font is set to the initial
272
// value, in this case Helvetica [Cronyx], 10
274
lblSample->setFont(mFont);
277
void QgsLabelDialog::changeFontColor(void)
280
std::cerr << "QgsLabelDialog::changeFontColor()" << std::endl;
282
mFontColor = QColorDialog::getColor ( mFontColor );
283
lblSample->setPaletteForegroundColor(mFontColor);
286
void QgsLabelDialog::changeBufferColor(void)
289
std::cerr << "QgsLabelDialog::changeBufferColor()" << std::endl;
291
mBufferColor = QColorDialog::getColor ( mBufferColor );
292
lblSample->setPaletteBackgroundColor(mBufferColor);
296
int QgsLabelDialog::itemNoForField(QString theFieldName, QStringList theFieldList)
299
for ( QStringList::Iterator it = theFieldList.begin(); it != theFieldList.end(); ++it )
301
if (theFieldName == *it) return myItemInt;
304
//if no matches assume first item in list is blank and return that
308
QgsLabelDialog::~QgsLabelDialog()
311
std::cerr << "QgsLabelDialog::~QgsLabelDialog()" << std::endl;
315
void QgsLabelDialog::apply()
318
std::cerr << "QgsLabelDialog::apply()" << std::endl;
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());
327
if ( radioFontSizeUnitsPoints->isChecked())
329
myTypeInt = QgsLabelAttributes::PointUnits;
331
else //assume map units is checked
333
myTypeInt = QgsLabelAttributes::MapUnits;
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);
341
if ( radioOffsetUnitsPoints->isChecked() )
343
myTypeInt = QgsLabelAttributes::PointUnits;
347
myTypeInt = QgsLabelAttributes::MapUnits;
349
myLabelAttributes->setOffset(spinXOffset->value() ,spinYOffset->value(), myTypeInt);
350
myLabelAttributes->setAngle(spinAngle->value());
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);
365
myLabelAttributes->setBufferEnabled(chkUseBuffer->isChecked());
366
myLabelAttributes->setBufferColor(mBufferColor);
368
if ( radioBufferUnitsPoints->isChecked() )
370
myTypeInt = QgsLabelAttributes::PointUnits;
374
myTypeInt = QgsLabelAttributes::MapUnits;
376
myLabelAttributes->setBufferSize(spinBufferSize->value(), myTypeInt);
377
//TODO - transparency attributes for buffers
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() );