/
labelinggui.cpp
156 lines (133 loc) · 4.93 KB
/
labelinggui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
labelinggui.cpp
Smart labeling for vector layers
-------------------
begin : June 2009
copyright : (C) Martin Dobias
email : wonder.sk at gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "labelinggui.h"
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>
#include <qgsmaplayerregistry.h>
#include "pallabeling.h"
#include "engineconfigdialog.h"
#include <QColorDialog>
#include <QFontDialog>
#include <iostream>
#include <QApplication>
LabelingGui::LabelingGui( PalLabeling* lbl, QString layerId, QWidget* parent )
: QDialog( parent ), mLBL( lbl ), mLayerId( layerId )
{
setupUi( this );
connect(btnTextColor, SIGNAL(clicked()), this, SLOT(changeTextColor()) );
connect(btnChangeFont, SIGNAL(clicked()), this, SLOT(changeTextFont()) );
connect(btnEngineSettings, SIGNAL(clicked()), this, SLOT(showEngineConfigDialog()) );
populatePlacementMethods();
populateFieldNames();
LayerSettings lyr = lbl->layer(layerId);
if (!lyr.layerId.isEmpty())
{
// load the labeling settings
cboPlacement->setCurrentIndex( cboPlacement->findData( QVariant( (int)lyr.placement ) ) );
cboFieldName->setCurrentIndex( cboFieldName->findText(lyr.fieldName) );
chkEnableLabeling->setChecked( lyr.enabled );
sliderPriority->setValue( lyr.priority );
chkNoObstacle->setChecked( !lyr.obstacle );
}
else
{
// set enabled by default
chkEnableLabeling->setChecked( true );
}
btnTextColor->setColor( lyr.textColor );
updateFontPreview( lyr.textFont );
}
LabelingGui::~LabelingGui()
{
}
QgsVectorLayer* LabelingGui::layer()
{
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer(mLayerId);
if (layer == NULL || layer->type() != QgsMapLayer::VectorLayer)
return NULL;
return static_cast<QgsVectorLayer*>(layer);
}
LayerSettings LabelingGui::layerSettings()
{
LayerSettings lyr;
lyr.layerId = mLayerId;
lyr.fieldName = cboFieldName->currentText();
lyr.placement = (LayerSettings::Placement) cboPlacement->itemData(cboPlacement->currentIndex()).toInt();
lyr.textColor = btnTextColor->color();
lyr.textFont = lblFontPreview->font();
lyr.enabled = chkEnableLabeling->isChecked();
lyr.priority = sliderPriority->value();
lyr.obstacle = !chkNoObstacle->isChecked();
return lyr;
}
void LabelingGui::populatePlacementMethods()
{
switch (layer()->geometryType())
{
case QGis::Point:
cboPlacement->addItem(tr("Around the point"), QVariant(LayerSettings::AroundPoint));
break;
case QGis::Line:
cboPlacement->addItem(tr("On the line"), QVariant(LayerSettings::OnLine));
cboPlacement->addItem(tr("Around the line"), QVariant(LayerSettings::AroundLine));
break;
case QGis::Polygon:
cboPlacement->addItem(tr("Horizontal"), QVariant(LayerSettings::Horizontal));
cboPlacement->addItem(tr("Free"), QVariant(LayerSettings::Free));
cboPlacement->addItem(tr("Around the centroid"), QVariant(LayerSettings::AroundPoint));
cboPlacement->addItem(tr("On the perimeter"), QVariant(LayerSettings::OnLine));
cboPlacement->addItem(tr("Around the perimeter"), QVariant(LayerSettings::AroundLine));
break;
}
}
void LabelingGui::populateFieldNames()
{
QgsFieldMap fields = layer()->dataProvider()->fields();
for (QgsFieldMap::iterator it = fields.begin(); it != fields.end(); it++)
{
cboFieldName->addItem(it->name());
}
}
void LabelingGui::changeTextColor()
{
QColor color = QColorDialog::getColor( btnTextColor->color(), this);
if (!color.isValid())
return;
btnTextColor->setColor(color);
updateFontPreview( lblFontPreview->font() );
}
void LabelingGui::changeTextFont()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, lblFontPreview->font(), this);
if (ok)
updateFontPreview( font );
}
void LabelingGui::updateFontPreview(QFont font)
{
lblFontName->setText( QString("%1, %2").arg(font.family()).arg(font.pointSize()) );
lblFontPreview->setFont(font);
QPalette palette = lblFontPreview->palette();
QBrush brush(btnTextColor->color());
palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
lblFontPreview->setPalette(palette);
}
void LabelingGui::showEngineConfigDialog()
{
EngineConfigDialog dlg(mLBL, this);
dlg.exec();
}