/
qgscomposerlabel.cpp
364 lines (282 loc) · 11.1 KB
/
qgscomposerlabel.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/***************************************************************************
qgscomposerlabel.cpp
-------------------
begin : January 2005
copyright : (C) 2005 by Radim Blazek
email : blazek@itc.it
***************************************************************************/
/***************************************************************************
* *
* 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 "qgscomposerlabel.h"
#include "qgsproject.h"
#include <QGraphicsScene>
#include <QAbstractGraphicsShapeItem>
#include <QPolygonF>
#include <QFontDialog>
#include <QPainter>
#include <iostream>
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
int x, int y, QString text, int fontSize )
: QWidget(composition), QAbstractGraphicsShapeItem(0), mBox(false)
{
setupUi(this);
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
#endif
mComposition = composition;
mId = id;
//mText = text;
mText = "Quantum GIS";
// Font and pen
mFont.setPointSize ( fontSize );
// Could make this user variable in the future
mPen.setWidthF (0.5);
QAbstractGraphicsShapeItem::setPos(x, y);
mSelected = false;
setOptions();
// Add to canvas
mComposition->canvas()->addItem(this);
QAbstractGraphicsShapeItem::setZValue(100);
QAbstractGraphicsShapeItem::show();
QAbstractGraphicsShapeItem::update();
writeSettings();
}
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
: QAbstractGraphicsShapeItem(0)
{
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
#endif
setupUi(this);
mComposition = composition;
mId = id;
mSelected = false;
readSettings();
setOptions();
// Add to canvas
mComposition->canvas()->addItem(this);
QAbstractGraphicsShapeItem::setZValue(100);
QAbstractGraphicsShapeItem::show();
QAbstractGraphicsShapeItem::update();
}
QgsComposerLabel::~QgsComposerLabel()
{
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
#endif
//make ourselves disappear so there aren't any interesting effects when we get destroyed
QGraphicsItem::hide();
}
#define FONT_WORKAROUND_SCALE 10
// This partially resolves a problem with the bounding rectangle for the text
// being too short for most font sizes.
#define WIDTH_EXTENSION 1.0
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::paint" << std::endl;
#endif
float size = 25.4 * mComposition->scale() * mFont.pointSizeF() / 72;
//mBoxBuffer = size / 10 * mComposition->scale();
mBoxBuffer = 0;
QFont font ( mFont );
font.setStyleStrategy ( QFont::StyleStrategy(QFont::PreferOutline | QFont::PreferAntialias) );
font.setPointSizeF ( size );
QFontMetricsF metrics ( font );
QSizeF textSize = metrics.size(0, mText);
qreal w = textSize.width() + WIDTH_EXTENSION;
qreal h = textSize.height();
QRectF r (0, -h, w, h); //used as the rectangle to draw the selection boxes on the corners of if there is no box
QRectF boxRect;
if ( mBox ) {
boxRect.setRect(-mBoxBuffer, -(h+mBoxBuffer), w + (mBoxBuffer * 2)+1, h + (mBoxBuffer * 2));
QBrush brush ( QColor(255,255,255) );
painter->setBrush ( brush );
painter->setPen(QPen(QColor(0, 0, 0), .2));
painter->drawRect ( boxRect );
}
font.setPointSizeF ( size * FONT_WORKAROUND_SCALE );
painter->setFont ( font );
painter->setPen ( mPen );
if ( plotStyle() == QgsComposition::Postscript )
{
// This metrics.ascent() is empirical
size = metrics.ascent() * 72.0 / mComposition->resolution() * FONT_WORKAROUND_SCALE;
font.setPointSizeF ( size );
painter->setFont ( font );
//std::cout << "label using PS render size" << std::endl;
}
//Hack to work around the Qt font bug
painter->save();
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);
QRectF scaledBox(r.x() * FONT_WORKAROUND_SCALE,
r.y() * FONT_WORKAROUND_SCALE,
(r.width() + WIDTH_EXTENSION) * FONT_WORKAROUND_SCALE,
r.height() * FONT_WORKAROUND_SCALE);
painter->drawText(scaledBox, Qt::AlignLeft|Qt::AlignVCenter, mText);
painter->restore(); //undo our scaling of painter - End of the font workaround
// Show selected / Highlight
if ( mSelected && plotStyle() == QgsComposition::Preview ) {
QRectF hr;
if ( mBox ) {
hr = boxRect;
} else {
hr = r;
}
hr.setWidth(hr.width() + WIDTH_EXTENSION);
painter->setPen( mComposition->selectionPen() );
painter->setBrush( mComposition->selectionBrush() );
double s = mComposition->selectionBoxSize();
painter->drawRect (QRectF(hr.x(), hr.y(), s, s ));
painter->drawRect (QRectF(hr.x()+hr.width()-s, hr.y(), s, s ));
painter->drawRect (QRectF(hr.x()+hr.width()-s, hr.y()+hr.height()-s, s, s ));
painter->drawRect (QRectF(hr.x(), hr.y()+hr.height()-s, s, s ));
}
}
void QgsComposerLabel::on_mFontButton_clicked()
{
bool result;
QRectF r = boundingRect();
mFont = QFontDialog::getFont(&result, mFont, this );
if ( result ) {
QAbstractGraphicsShapeItem::prepareGeometryChange();
QAbstractGraphicsShapeItem::update();
}
writeSettings();
}
void QgsComposerLabel::on_mBoxCheckBox_clicked()
{
QRectF r = boundingRect();
mBox = mBoxCheckBox->isChecked();
QAbstractGraphicsShapeItem::prepareGeometryChange();
QAbstractGraphicsShapeItem::update();
writeSettings();
}
QRectF QgsComposerLabel::boundingRect ( void ) const
{
// Recalculate sizes according to current font size
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
QFont font ( mFont );
font.setPointSizeF ( size );
QFontMetricsF metrics ( font );
QSizeF textSize = metrics.size(0, mText);
double w = textSize.width() + WIDTH_EXTENSION;
double h = textSize.height();
/*
int buf = 0;
if ( mBox ) {
buf = (int) ( size / 10 * mComposition->scale() + 2 ); // 2 is for line width
}
QRectF r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
*/
QRectF r;
if(mBox){
//what happens if we haven't called paint() first?
r.setRect(-mBoxBuffer, -(h+mBoxBuffer), w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
}
else{
r.setRect(0, -h, w, h);
}
return r;
}
QPolygonF QgsComposerLabel::areaPoints() const
{
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::areaPoints" << std::endl;
#endif
QRectF r = boundingRect();
QPolygonF pa;
pa << QPointF( r.x(), r.y() );
pa << QPointF( r.x()+r.width(), r.y() );
pa << QPointF( r.x()+r.width(), r.y()+r.height() );
pa << QPointF( r.x(), r.y()+r.height() );
return pa ;
}
void QgsComposerLabel::setOptions ( void )
{
mTextEdit->setText ( mText );
mBoxCheckBox->setChecked ( mBox );
}
void QgsComposerLabel::on_mTextEdit_textChanged()
{
QRectF r = boundingRect();
#if QT_VERSION < 0x040300
mText = mTextEdit->text();
#else
mText = mTextEdit->toPlainText();
#endif
QAbstractGraphicsShapeItem::prepareGeometryChange();
QAbstractGraphicsShapeItem::update();
writeSettings();
}
void QgsComposerLabel::setSelected ( bool s )
{
#ifdef QGISDEBUG
std::cout << "QgsComposerLabel::setSelected" << std::endl;
#endif
mSelected = s;
QAbstractGraphicsShapeItem::update(); // show highlight
}
bool QgsComposerLabel::selected( void )
{
return mSelected;
}
QWidget *QgsComposerLabel::options ( void )
{
setOptions ();
return ( dynamic_cast <QWidget *> (this) );
}
bool QgsComposerLabel::writeSettings ( void )
{
QString path;
path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId );
QgsProject::instance()->writeEntry( "Compositions", path+"text", mText );
QgsProject::instance()->writeEntry( "Compositions", path+"x", mComposition->toMM((int)QAbstractGraphicsShapeItem::x()) );
QgsProject::instance()->writeEntry( "Compositions", path+"y", mComposition->toMM((int)QAbstractGraphicsShapeItem::y()) );
QgsProject::instance()->writeEntry( "Compositions", path+"font/size", mFont.pointSize() );
QgsProject::instance()->writeEntry( "Compositions", path+"font/family", mFont.family() );
QgsProject::instance()->writeEntry( "Compositions", path+"font/weight", mFont.weight() );
QgsProject::instance()->writeEntry( "Compositions", path+"font/underline", mFont.underline() );
QgsProject::instance()->writeEntry( "Compositions", path+"font/strikeout", mFont.strikeOut() );
QgsProject::instance()->writeEntry( "Compositions", path+"box", mBox );
return true;
}
bool QgsComposerLabel::readSettings ( void )
{
std::cout << "QgsComposerLabel::readSettings mId = " << mId << std::endl;
bool ok;
QString path;
path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId );
mText = QgsProject::instance()->readEntry("Compositions", path+"text", "???", &ok);
int x = mComposition->fromMM( QgsProject::instance()->readDoubleEntry( "Compositions", path+"x", 0, &ok) );
int y = mComposition->fromMM(QgsProject::instance()->readDoubleEntry( "Compositions", path+"y", 0, &ok) );
QAbstractGraphicsShapeItem::setPos(x,y);
mFont.setFamily ( QgsProject::instance()->readEntry("Compositions", path+"font/family", "", &ok) );
mFont.setPointSize ( QgsProject::instance()->readNumEntry("Compositions", path+"font/size", 10, &ok) );
mFont.setWeight( QgsProject::instance()->readNumEntry("Compositions", path+"font/weight", (int)QFont::Normal, &ok) );
mFont.setUnderline( QgsProject::instance()->readBoolEntry("Compositions", path+"font/underline", false, &ok) );
mFont.setStrikeOut( QgsProject::instance()->readBoolEntry("Compositions", path+"font/strikeout", false, &ok) );
mBox = QgsProject::instance()->readBoolEntry("Compositions", path+"box", false, &ok);
QAbstractGraphicsShapeItem::update();
return true;
}
bool QgsComposerLabel::removeSettings ( void )
{
QString path;
path.sprintf("/composition_%d/label_%d", mComposition->id(), mId );
return QgsProject::instance()->removeEntry ( "Compositions", path );
}
bool QgsComposerLabel::writeXML( QDomNode & node, QDomDocument & document, bool temp )
{
return true;
}
bool QgsComposerLabel::readXML( QDomNode & node )
{
return true;
}