Skip to content

Commit 51a75e2

Browse files
author
timlinux
committedJul 16, 2007
Applied patch from #740 with many thanks to Steven Bell
- this patch replaces the q3canvas in the map composer with a qgraphicsview based implementation. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7072 c8812cc2-4d05-0410-92ff-de0c093fc19c

15 files changed

+1621
-1524
lines changed
 

‎src/app/composer/qgscomposer.cpp

Lines changed: 368 additions & 308 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposerlabel.cpp

Lines changed: 102 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
* *
1616
***************************************************************************/
1717
#include "qgscomposerlabel.h"
18-
1918
#include "qgsproject.h"
19+
20+
#include <QGraphicsScene>
21+
#include <QAbstractGraphicsShapeItem>
22+
#include <QPolygonF>
2023
#include <QFontDialog>
2124
#include <QPainter>
25+
2226
#include <iostream>
2327

2428
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
2529
int x, int y, QString text, int fontSize )
26-
: QWidget(composition), Q3CanvasPolygonalItem(0), mBox(false)
30+
: QWidget(composition), QAbstractGraphicsShapeItem(0), mBox(false)
2731
{
2832
setupUi(this);
2933

@@ -37,28 +41,29 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
3741

3842
// Font and pen
3943
mFont.setPointSize ( fontSize );
44+
4045
// Could make this user variable in the future
4146
mPen.setWidthF (0.5);
4247

43-
Q3CanvasPolygonalItem::setX(x);
44-
Q3CanvasPolygonalItem::setY(y);
48+
QAbstractGraphicsShapeItem::setPos(x, y);
4549

4650
mSelected = false;
4751

4852
setOptions();
4953

5054
// Add to canvas
51-
setCanvas(mComposition->canvas());
52-
Q3CanvasPolygonalItem::setZ(100);
53-
setActive(true);
54-
Q3CanvasPolygonalItem::show();
55-
Q3CanvasPolygonalItem::update(); // ?
55+
mComposition->canvas()->addItem(this);
56+
57+
QAbstractGraphicsShapeItem::setZValue(100);
58+
//setActive(true); //no equivalent
59+
QAbstractGraphicsShapeItem::show();
60+
QAbstractGraphicsShapeItem::update();
5661

5762
writeSettings();
5863
}
5964

6065
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
61-
: Q3CanvasPolygonalItem(0)
66+
: QAbstractGraphicsShapeItem(0)
6267
{
6368
std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
6469

@@ -73,161 +78,169 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
7378
setOptions();
7479

7580
// Add to canvas
76-
setCanvas(mComposition->canvas());
77-
Q3CanvasPolygonalItem::setZ(100);
78-
setActive(true);
79-
Q3CanvasPolygonalItem::show();
80-
Q3CanvasPolygonalItem::update(); // ?
81+
mComposition->canvas()->addItem(this);
82+
QAbstractGraphicsShapeItem::setZValue(100);
83+
//setActive(true);//no equivalent
84+
QAbstractGraphicsShapeItem::show();
85+
QAbstractGraphicsShapeItem::update();
8186

8287
}
8388

8489
QgsComposerLabel::~QgsComposerLabel()
8590
{
8691
std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
87-
Q3CanvasItem::hide();
92+
QGraphicsItem::hide();
8893
}
89-
90-
void QgsComposerLabel::drawShape ( QPainter & painter )
94+
/*
95+
void QgsComposerLabel::drawShape ( QPainter & painter, const QStyleOptionGraphicsItem* item, QWidget* pWidget)
9196
{
9297
std::cout << "QgsComposerLabel::drawShape" << std::endl;
93-
draw ( painter );
98+
paint ( painter, item, pWidget );
9499
}
100+
*/
95101

96-
void QgsComposerLabel::draw ( QPainter & painter )
102+
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
97103
{
98-
std::cout << "QgsComposerLabel::render" << std::endl;
104+
std::cout << "QgsComposerLabel::paint" << std::endl;
99105

100106
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
101107
mBoxBuffer = (int) ( size / 10 * mComposition->scale() );
108+
mBoxBuffer = 1;
109+
102110

103111
QFont font ( mFont );
104112
font.setPointSizeFloat ( size );
105-
QFontMetrics metrics ( font );
113+
QFontMetricsF metrics ( font );
106114

107115
// Not sure about Style Strategy, QFont::PreferMatch ?
108116
//font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
109117

110-
painter.setPen ( mPen );
111-
painter.setFont ( font );
112-
113-
int x = (int) Q3CanvasPolygonalItem::x();
114-
int y = (int) Q3CanvasPolygonalItem::y();
118+
painter->setPen ( mPen );
119+
painter->setFont ( font );
115120

116-
int w = metrics.width ( mText );
117-
int h = metrics.height() ;
121+
double w = metrics.width ( mText );
122+
double h = metrics.height() - metrics.descent();
118123

119-
QRect r ( (int)(x - w/2), (int) (y - h/2), w, h );
124+
QRectF r (0, -h, w, h); //used as the rectangle to draw the selection boxes on the corners of if there is no box
120125

121-
QRect boxRect;
126+
QRectF boxRect;
122127
if ( mBox ) {
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 );
128+
//I don't know why the top coordinate is -h rather than -(h+mBoxBuffer), but it seems to work better.
129+
boxRect.setRect(-mBoxBuffer, -h, w + (2 * mBoxBuffer), h + (2 * mBoxBuffer));
130+
QBrush brush ( QColor(255,255,255) );
131+
painter->setBrush ( brush );
132+
painter->setPen(QPen(QColor(0, 0, 0), .2));
133+
painter->drawRect ( boxRect );
128134
}
129-
painter.setPen ( mPen );
135+
painter->setPen ( mPen );
130136

131137
// The width is not sufficient in postscript
132-
QRect tr = r;
138+
QRectF tr = r;
133139
tr.setWidth ( r.width() );
134140

135141
if ( plotStyle() == QgsComposition::Postscript )
136142
{
137143
// This metrics.ascent() is empirical
138144
size = metrics.ascent() * 72.0 / mComposition->resolution();
139145
font.setPointSizeF ( size );
140-
painter.setFont ( font );
146+
painter->setFont ( font );
147+
std::cout << "label using PS render size" << std::endl;
141148
}
142-
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
149+
painter->drawText(0, 0, mText);
143150

144151
// Show selected / Highlight
145152
if ( mSelected && plotStyle() == QgsComposition::Preview ) {
146-
QRect hr;
147-
if ( mBox ) {
148-
hr = boxRect;
149-
} else {
150-
hr = r;
151-
}
152-
painter.setPen( mComposition->selectionPen() );
153-
painter.setBrush( mComposition->selectionBrush() );
154-
int s = mComposition->selectionBoxSize();
153+
QRectF hr;
154+
if ( mBox ) {
155+
hr = boxRect;
156+
} else {
157+
hr = r;
158+
}
159+
painter->setPen( mComposition->selectionPen() );
160+
painter->setBrush( mComposition->selectionBrush() );
161+
162+
double s = mComposition->selectionBoxSize();
155163

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 );
164+
painter->drawRect (QRectF(hr.x(), hr.y(), s, s ));
165+
painter->drawRect (QRectF(hr.x()+hr.width()-s, hr.y(), s, s ));
166+
painter->drawRect (QRectF(hr.x()+hr.width()-s, hr.y()+hr.height()-s, s, s ));
167+
painter->drawRect (QRectF(hr.x(), hr.y()+hr.height()-s, s, s ));
160168
}
161169
}
162170

163171
void QgsComposerLabel::on_mFontButton_clicked()
164172
{
165173
bool result;
166174

167-
QRect r = boundingRect();
175+
QRectF r = boundingRect();
168176

169177
mFont = QFontDialog::getFont(&result, mFont, this );
170178

171179
if ( result ) {
172-
Q3CanvasPolygonalItem::invalidate();
173-
Q3CanvasPolygonalItem::canvas()->setChanged(r);
174-
Q3CanvasPolygonalItem::update();
175-
Q3CanvasPolygonalItem::canvas()->update();
180+
QAbstractGraphicsShapeItem::prepareGeometryChange();
181+
QAbstractGraphicsShapeItem::update();
176182
}
177183
writeSettings();
178184
}
179185

180186
void QgsComposerLabel::on_mBoxCheckBox_clicked()
181187
{
182-
QRect r = boundingRect();
188+
QRectF r = boundingRect();
183189

184190
mBox = mBoxCheckBox->isChecked();
185191

186-
Q3CanvasPolygonalItem::invalidate();
187-
Q3CanvasPolygonalItem::canvas()->setChanged(r);
188-
Q3CanvasPolygonalItem::update();
189-
Q3CanvasPolygonalItem::canvas()->update();
190-
192+
QAbstractGraphicsShapeItem::prepareGeometryChange();
193+
QAbstractGraphicsShapeItem::update();
191194
writeSettings();
192195
}
193196

194-
QRect QgsComposerLabel::boundingRect ( void ) const
197+
QRectF QgsComposerLabel::boundingRect ( void ) const
195198
{
196199
// Recalculate sizes according to current font size
197200

198-
float size = 25.4 * mComposition->scale() * mFont.pointSize() / 72;
199-
201+
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
202+
200203
QFont font ( mFont );
201204
font.setPointSizeFloat ( size );
202-
203205
QFontMetrics metrics ( font );
204-
205-
int x = (int) Q3CanvasPolygonalItem::x();
206-
int y = (int) Q3CanvasPolygonalItem::y();
206+
207207
int w = metrics.width ( mText );
208-
int h = metrics.height() ;
209-
208+
int h = metrics.height() - metrics.descent();
209+
210+
/*
210211
int buf = 0;
211212
212213
if ( mBox ) {
213214
buf = (int) ( size / 10 * mComposition->scale() + 2 ); // 2 is for line width
214215
}
215216
216-
QRect r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
217+
QRectF r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
218+
*/
219+
220+
QRectF r;
221+
222+
if(mBox){
223+
//what happens if we haven't called paint() first?
224+
r.setRect(-mBoxBuffer, -h, w + (2 * mBoxBuffer), h + (2 * mBoxBuffer));
225+
}
226+
else{
227+
r.setRect(0, -h, w, h);
228+
}
217229

218230
return r;
231+
219232
}
220233

221-
Q3PointArray QgsComposerLabel::areaPoints() const
234+
QPolygonF QgsComposerLabel::areaPoints() const
222235
{
223236
std::cout << "QgsComposerLabel::areaPoints" << std::endl;
224-
QRect r = boundingRect();
237+
QRectF r = boundingRect();
225238

226-
Q3PointArray pa(4);
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() );
239+
QPolygonF pa;
240+
pa << QPointF( r.x(), r.y() );
241+
pa << QPointF( r.x()+r.width(), r.y() );
242+
pa << QPointF( r.x()+r.width(), r.y()+r.height() );
243+
pa << QPointF( r.x(), r.y()+r.height() );
231244

232245
return pa ;
233246
}
@@ -241,20 +254,18 @@ void QgsComposerLabel::setOptions ( void )
241254

242255
void QgsComposerLabel::on_mTextLineEdit_returnPressed()
243256
{
244-
QRect r = boundingRect();
257+
QRectF r = boundingRect();
245258
mText = mTextLineEdit->text();
246-
Q3CanvasPolygonalItem::invalidate();
247-
Q3CanvasPolygonalItem::canvas()->setChanged(r);
248-
Q3CanvasPolygonalItem::update();
249-
Q3CanvasPolygonalItem::canvas()->update();
259+
QAbstractGraphicsShapeItem::prepareGeometryChange();
260+
QAbstractGraphicsShapeItem::update();
250261
writeSettings();
251262
}
252263

253264
void QgsComposerLabel::setSelected ( bool s )
254265
{
255266
std::cout << "QgsComposerLabel::setSelected" << std::endl;
256267
mSelected = s;
257-
Q3CanvasPolygonalItem::update(); // show highlight
268+
QAbstractGraphicsShapeItem::update(); // show highlight
258269

259270
std::cout << "mSelected = " << mSelected << std::endl;
260271
}
@@ -277,8 +288,8 @@ bool QgsComposerLabel::writeSettings ( void )
277288

278289
QgsProject::instance()->writeEntry( "Compositions", path+"text", mText );
279290

280-
QgsProject::instance()->writeEntry( "Compositions", path+"x", mComposition->toMM((int)Q3CanvasPolygonalItem::x()) );
281-
QgsProject::instance()->writeEntry( "Compositions", path+"y", mComposition->toMM((int)Q3CanvasPolygonalItem::y()) );
291+
QgsProject::instance()->writeEntry( "Compositions", path+"x", mComposition->toMM((int)QAbstractGraphicsShapeItem::x()) );
292+
QgsProject::instance()->writeEntry( "Compositions", path+"y", mComposition->toMM((int)QAbstractGraphicsShapeItem::y()) );
282293

283294
QgsProject::instance()->writeEntry( "Compositions", path+"font/size", mFont.pointSize() );
284295
QgsProject::instance()->writeEntry( "Compositions", path+"font/family", mFont.family() );
@@ -302,9 +313,8 @@ bool QgsComposerLabel::readSettings ( void )
302313
mText = QgsProject::instance()->readEntry("Compositions", path+"text", "???", &ok);
303314

304315
int x = mComposition->fromMM( QgsProject::instance()->readDoubleEntry( "Compositions", path+"x", 0, &ok) );
305-
Q3CanvasPolygonalItem::setX( x );
306316
int y = mComposition->fromMM(QgsProject::instance()->readDoubleEntry( "Compositions", path+"y", 0, &ok) );
307-
Q3CanvasPolygonalItem::setY( y );
317+
QAbstractGraphicsShapeItem::setPos(x,y);
308318

309319
mFont.setFamily ( QgsProject::instance()->readEntry("Compositions", path+"font/family", "", &ok) );
310320
mFont.setPointSize ( QgsProject::instance()->readNumEntry("Compositions", path+"font/size", 10, &ok) );
@@ -314,7 +324,7 @@ bool QgsComposerLabel::readSettings ( void )
314324

315325
mBox = QgsProject::instance()->readBoolEntry("Compositions", path+"box", false, &ok);
316326

317-
Q3CanvasPolygonalItem::update();
327+
QAbstractGraphicsShapeItem::update();
318328

319329
return true;
320330
}

‎src/app/composer/qgscomposerlabel.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
#include "ui_qgscomposerlabelbase.h"
2121
#include "qgscomposeritem.h"
22-
#include <Q3CanvasPolygonalItem>
22+
#include <QAbstractGraphicsShapeItem>
2323
#include <QFont>
2424
#include <QPen>
25-
#include <Q3PointArray>
25+
#include <QPolygon>
2626
#include <QRect>
2727
#include <QString>
2828

@@ -35,7 +35,7 @@ class QDomDocument;
3535
*/
3636
// NOTE: QgsComposerLabelBase must be first, otherwise does not compile
3737
//class QgsComposerLabel : public QgsComposerLabelBase, public QCanvasRectangle, public QgsComposerItem
38-
class QgsComposerLabel : public QWidget, private Ui::QgsComposerLabelBase, public Q3CanvasPolygonalItem, public QgsComposerItem
38+
class QgsComposerLabel : public QWidget, private Ui::QgsComposerLabelBase, public QAbstractGraphicsShapeItem, public QgsComposerItem
3939
{
4040
Q_OBJECT
4141

@@ -63,13 +63,13 @@ class QgsComposerLabel : public QWidget, private Ui::QgsComposerLabelBase, publi
6363
bool writeXML( QDomNode & node, QDomDocument & document, bool temp = false );
6464
bool readXML( QDomNode & node );
6565

66-
QRect boundingRect ( void ) const;
66+
QRectF boundingRect ( void ) const; //errors about overriding things?
6767

68-
/** \brief Reimplementation of QCanvasItem::draw - draw on canvas */
69-
void draw ( QPainter & painter );
68+
/** \brief Reimplementation of QGraphicsItem::paint() - draw on canvas */
69+
void paint ( QPainter*, const QStyleOptionGraphicsItem*, QWidget* );
7070

71-
void drawShape(QPainter&);
72-
Q3PointArray areaPoints() const;
71+
// void drawShape(QPainter&, const QStyleOptionGraphicsItem*, QWidget*);
72+
QPolygonF areaPoints() const;
7373

7474
/** \brief Set values in GUI to current values */
7575
void setOptions ( void );

0 commit comments

Comments
 (0)
Please sign in to comment.