1
/***************************************************************************
5
copyright : (C) 2005 by Radim Blazek
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
17
#include "qgscomposerlabel.h"
19
#include "qgsproject.h"
20
#include <QFontDialog>
24
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
25
int x, int y, QString text, int fontSize )
26
: QWidget(composition), Q3CanvasPolygonalItem(0), mBox(false)
30
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
32
mComposition = composition;
36
mText = "Quantum GIS";
39
mFont.setPointSize ( fontSize );
40
// Could make this user variable in the future
43
Q3CanvasPolygonalItem::setX(x);
44
Q3CanvasPolygonalItem::setY(y);
51
setCanvas(mComposition->canvas());
52
Q3CanvasPolygonalItem::setZ(100);
54
Q3CanvasPolygonalItem::show();
55
Q3CanvasPolygonalItem::update(); // ?
60
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
61
: Q3CanvasPolygonalItem(0)
63
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
67
mComposition = composition;
76
setCanvas(mComposition->canvas());
77
Q3CanvasPolygonalItem::setZ(100);
79
Q3CanvasPolygonalItem::show();
80
Q3CanvasPolygonalItem::update(); // ?
84
QgsComposerLabel::~QgsComposerLabel()
86
std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
90
void QgsComposerLabel::drawShape ( QPainter & painter )
92
std::cout << "QgsComposerLabel::drawShape" << std::endl;
96
void QgsComposerLabel::draw ( QPainter & painter )
98
std::cout << "QgsComposerLabel::render" << std::endl;
100
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
101
mBoxBuffer = (int) ( size / 10 * mComposition->scale() );
103
QFont font ( mFont );
104
font.setPointSizeFloat ( size );
105
QFontMetrics metrics ( font );
107
// Not sure about Style Strategy, QFont::PreferMatch ?
108
//font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
110
painter.setPen ( mPen );
111
painter.setFont ( font );
113
int x = (int) Q3CanvasPolygonalItem::x();
114
int y = (int) Q3CanvasPolygonalItem::y();
116
int w = metrics.width ( mText );
117
int h = metrics.height() ;
119
QRect r ( (int)(x - w/2), (int) (y - h/2), w, h );
123
// I don't know why, but the box seems to be too short -> add 1 * mBoxBuffer to width
124
boxRect.setRect ( (int)(r.x()-1.5*mBoxBuffer), r.y()-mBoxBuffer, (int)(r.width()+3*mBoxBuffer), r.height()+2*mBoxBuffer );
125
QBrush brush ( QColor(255,255,255) );
126
painter.setBrush ( brush );
127
painter.drawRect ( boxRect );
129
painter.setPen ( mPen );
131
// The width is not sufficient in postscript
133
tr.setWidth ( r.width() );
135
if ( plotStyle() == QgsComposition::Postscript )
137
// This metrics.ascent() is empirical
138
size = metrics.ascent() * 72.0 / mComposition->resolution();
139
font.setPointSizeF ( size );
140
painter.setFont ( font );
142
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
144
// Show selected / Highlight
145
if ( mSelected && plotStyle() == QgsComposition::Preview ) {
152
painter.setPen( mComposition->selectionPen() );
153
painter.setBrush( mComposition->selectionBrush() );
154
int s = mComposition->selectionBoxSize();
156
painter.drawRect ( hr.x(), hr.y(), s, s );
157
painter.drawRect ( hr.x()+hr.width()-s, hr.y(), s, s );
158
painter.drawRect ( hr.x()+hr.width()-s, hr.y()+hr.height()-s, s, s );
159
painter.drawRect ( hr.x(), hr.y()+hr.height()-s, s, s );
163
void QgsComposerLabel::on_mFontButton_clicked()
167
QRect r = boundingRect();
169
mFont = QFontDialog::getFont(&result, mFont, this );
172
Q3CanvasPolygonalItem::invalidate();
173
Q3CanvasPolygonalItem::canvas()->setChanged(r);
174
Q3CanvasPolygonalItem::update();
175
Q3CanvasPolygonalItem::canvas()->update();
180
void QgsComposerLabel::on_mBoxCheckBox_clicked()
182
QRect r = boundingRect();
184
mBox = mBoxCheckBox->isChecked();
186
Q3CanvasPolygonalItem::invalidate();
187
Q3CanvasPolygonalItem::canvas()->setChanged(r);
188
Q3CanvasPolygonalItem::update();
189
Q3CanvasPolygonalItem::canvas()->update();
194
QRect QgsComposerLabel::boundingRect ( void ) const
196
// Recalculate sizes according to current font size
198
float size = 25.4 * mComposition->scale() * mFont.pointSize() / 72;
200
QFont font ( mFont );
201
font.setPointSizeFloat ( size );
203
QFontMetrics metrics ( font );
205
int x = (int) Q3CanvasPolygonalItem::x();
206
int y = (int) Q3CanvasPolygonalItem::y();
207
int w = metrics.width ( mText );
208
int h = metrics.height() ;
213
buf = (int) ( size / 10 * mComposition->scale() + 2 ); // 2 is for line width
216
QRect r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
221
Q3PointArray QgsComposerLabel::areaPoints() const
223
std::cout << "QgsComposerLabel::areaPoints" << std::endl;
224
QRect r = boundingRect();
227
pa[0] = QPoint( r.x(), r.y() );
228
pa[1] = QPoint( r.x()+r.width(), r.y() );
229
pa[2] = QPoint( r.x()+r.width(), r.y()+r.height() );
230
pa[3] = QPoint( r.x(), r.y()+r.height() );
235
void QgsComposerLabel::setOptions ( void )
237
mTextLineEdit->setText ( mText );
238
mBoxCheckBox->setChecked ( mBox );
242
void QgsComposerLabel::on_mTextLineEdit_returnPressed()
244
QRect r = boundingRect();
245
mText = mTextLineEdit->text();
246
Q3CanvasPolygonalItem::invalidate();
247
Q3CanvasPolygonalItem::canvas()->setChanged(r);
248
Q3CanvasPolygonalItem::update();
249
Q3CanvasPolygonalItem::canvas()->update();
253
void QgsComposerLabel::setSelected ( bool s )
255
std::cout << "QgsComposerLabel::setSelected" << std::endl;
257
Q3CanvasPolygonalItem::update(); // show highlight
259
std::cout << "mSelected = " << mSelected << std::endl;
262
bool QgsComposerLabel::selected( void )
267
QWidget *QgsComposerLabel::options ( void )
270
return ( dynamic_cast <QWidget *> (this) );
273
bool QgsComposerLabel::writeSettings ( void )
276
path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId );
278
QgsProject::instance()->writeEntry( "Compositions", path+"text", mText );
280
QgsProject::instance()->writeEntry( "Compositions", path+"x", mComposition->toMM((int)Q3CanvasPolygonalItem::x()) );
281
QgsProject::instance()->writeEntry( "Compositions", path+"y", mComposition->toMM((int)Q3CanvasPolygonalItem::y()) );
283
QgsProject::instance()->writeEntry( "Compositions", path+"font/size", mFont.pointSize() );
284
QgsProject::instance()->writeEntry( "Compositions", path+"font/family", mFont.family() );
285
QgsProject::instance()->writeEntry( "Compositions", path+"font/weight", mFont.weight() );
286
QgsProject::instance()->writeEntry( "Compositions", path+"font/underline", mFont.underline() );
287
QgsProject::instance()->writeEntry( "Compositions", path+"font/strikeout", mFont.strikeOut() );
289
QgsProject::instance()->writeEntry( "Compositions", path+"box", mBox );
294
bool QgsComposerLabel::readSettings ( void )
296
std::cout << "QgsComposerLabel::readSettings mId = " << mId << std::endl;
300
path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId );
302
mText = QgsProject::instance()->readEntry("Compositions", path+"text", "???", &ok);
304
int x = mComposition->fromMM( QgsProject::instance()->readDoubleEntry( "Compositions", path+"x", 0, &ok) );
305
Q3CanvasPolygonalItem::setX( x );
306
int y = mComposition->fromMM(QgsProject::instance()->readDoubleEntry( "Compositions", path+"y", 0, &ok) );
307
Q3CanvasPolygonalItem::setY( y );
309
mFont.setFamily ( QgsProject::instance()->readEntry("Compositions", path+"font/family", "", &ok) );
310
mFont.setPointSize ( QgsProject::instance()->readNumEntry("Compositions", path+"font/size", 10, &ok) );
311
mFont.setWeight( QgsProject::instance()->readNumEntry("Compositions", path+"font/weight", (int)QFont::Normal, &ok) );
312
mFont.setUnderline( QgsProject::instance()->readBoolEntry("Compositions", path+"font/underline", false, &ok) );
313
mFont.setStrikeOut( QgsProject::instance()->readBoolEntry("Compositions", path+"font/strikeout", false, &ok) );
315
mBox = QgsProject::instance()->readBoolEntry("Compositions", path+"box", false, &ok);
317
Q3CanvasPolygonalItem::update();
322
bool QgsComposerLabel::removeSettings ( void )
325
path.sprintf("/composition_%d/label_%d", mComposition->id(), mId );
326
return QgsProject::instance()->removeEntry ( "Compositions", path );
329
bool QgsComposerLabel::writeXML( QDomNode & node, QDomDocument & document, bool temp )
334
bool QgsComposerLabel::readXML( QDomNode & node )