Skip to content

Commit 828bb2e

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@7072 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 5324e89 commit 828bb2e

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 );

‎src/app/composer/qgscomposermap.cpp

Lines changed: 188 additions & 181 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposermap.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "ui_qgscomposermapbase.h"
2121
#include "qgscomposeritem.h"
2222
#include "qgsrect.h"
23-
#include <Q3CanvasRectangle>
23+
#include <QGraphicsRectItem>
2424
#include <QPixmap>
2525

2626
class QgsComposition;
@@ -34,7 +34,7 @@ class QPainter;
3434
* \brief Object representing map window.
3535
*/
3636
// NOTE: QgsComposerMapBase must be first, otherwise does not compile
37-
class QgsComposerMap : public QWidget, private Ui::QgsComposerMapBase, public Q3CanvasRectangle, public QgsComposerItem
37+
class QgsComposerMap : public QWidget, private Ui::QgsComposerMapBase, public QGraphicsRectItem, public QgsComposerItem
3838
{
3939
Q_OBJECT
4040

@@ -74,8 +74,8 @@ class QgsComposerMap : public QWidget, private Ui::QgsComposerMapBase, public Q3
7474
/** \brief Draw to paint device */
7575
void draw(QPainter *painter, QgsRect &extent, QgsMapToPixel *transform);
7676

77-
/** \brief Reimplementation of QCanvasItem::draw - draw on canvas */
78-
void draw ( QPainter & painter );
77+
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
78+
void paint (QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget);
7979

8080
/** \brief Set extent requested by user */
8181
void setUserExtent ( QgsRect const & rect);

‎src/app/composer/qgscomposerpicture.cpp

Lines changed: 138 additions & 163 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposerpicture.h

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

2020
#include "ui_qgscomposerpicturebase.h"
2121
#include "qgscomposeritem.h"
22-
#include <Q3CanvasPolygonalItem>
22+
#include <QAbstractGraphicsShapeItem>
2323
#include <QPen>
24-
#include <Q3Picture>
25-
#include <Q3PointArray>
24+
#include <QPicture>
25+
#include <QPolygonF>
2626
#include <QRect>
2727

2828
class QgsComposition;
@@ -36,8 +36,7 @@ class QPainter;
3636
// NOTE: QgsComposerPictureBase must be first, otherwise does not compile
3737
// public QCanvasRectangle,
3838
class QgsComposerPicture : public QWidget, private Ui::QgsComposerPictureBase,
39-
public Q3CanvasPolygonalItem,
40-
public QgsComposerItem
39+
public QAbstractGraphicsShapeItem, public QgsComposerItem
4140
{
4241
Q_OBJECT
4342

@@ -68,17 +67,12 @@ class QgsComposerPicture : public QWidget, private Ui::QgsComposerPictureBase,
6867

6968

7069
// Reimplement QCanvasItem::boundingRect
71-
QRect boundingRect ( void ) const;
70+
QRectF boundingRect ( void ) const;
7271

73-
Q3PointArray areaPoints() const;
72+
QPolygonF areaPoints() const;
7473

75-
// Reimplemented
76-
void moveBy( double x, double y);
77-
78-
/** \brief Reimplementation of QCanvasItem::draw - draw on canvas */
79-
void draw ( QPainter & painter );
80-
81-
void drawShape( QPainter & painter );
74+
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
75+
void paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
8276

8377
/** \brief Set values in GUI to current values */
8478
void setOptions ( void );
@@ -88,7 +82,7 @@ class QgsComposerPicture : public QWidget, private Ui::QgsComposerPictureBase,
8882

8983
// Set box, picture will be inside box, used when placed by mouse.
9084
// Coordinates do not need to be oriented
91-
void setBox ( int x1, int y1, int x2, int y2 );
85+
void setSize(double width, double height );
9286

9387
// Picture dialog, returns file name or empty string
9488
static QString pictureDialog ( void );
@@ -117,7 +111,7 @@ public slots:
117111
QString mPicturePath;
118112

119113
// Picture
120-
Q3Picture mPicture;
114+
QPicture mPicture;
121115

122116
bool mPictureValid;
123117

@@ -127,8 +121,8 @@ public slots:
127121
// Coordinates of upper left picture corner
128122
int mX, mY;
129123

130-
// Picture width and height in paper
131-
int mWidth, mHeight;
124+
// Picture width and height in scene units
125+
double mWidth, mHeight;
132126

133127
// Scale, number of canvas units / image unit
134128

@@ -147,13 +141,13 @@ public slots:
147141
/** \brief Calculate size according to current settings */
148142
void recalculate ( void );
149143

150-
Q3PointArray mAreaPoints;
144+
QPolygonF mAreaPoints;
151145

152146
// Called when picture file is changed
153147
void pictureChanged ( void );
154148

155149
// Current bounding box
156-
QRect mBoundingRect;
150+
QRectF mBoundingRect;
157151

158152
// Adjust size so that picture fits to current box
159153
void adjustPictureSize();

‎src/app/composer/qgscomposerscalebar.cpp

Lines changed: 404 additions & 379 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposerscalebar.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
#include "ui_qgscomposerscalebarbase.h"
2020
#include "qgscomposeritem.h"
21-
#include <Q3CanvasPolygonalItem>
21+
#include <QAbstractGraphicsShapeItem>
2222
#include <Q3PointArray>
2323
#include <QRect>
24+
#include <QPen>
2425

2526
class QgsMapCanvas;
2627
class QgsComposition;
@@ -33,10 +34,9 @@ class QPen;
3334

3435
/** \class QgsComposerScalebar
3536
* \brief Object representing map window.
36-
* x,y is center of upper side of the bar, the center position depends on scalebar style
3737
*/
3838
// NOTE: QgsComposerScalebarBase must be first, otherwise does not compile
39-
class QgsComposerScalebar : public QWidget, private Ui::QgsComposerScalebarBase, public Q3CanvasPolygonalItem, public QgsComposerItem
39+
class QgsComposerScalebar : public QWidget, private Ui::QgsComposerScalebarBase, public QAbstractGraphicsShapeItem, public QgsComposerItem
4040
{
4141
Q_OBJECT
4242

@@ -66,19 +66,19 @@ class QgsComposerScalebar : public QWidget, private Ui::QgsComposerScalebarBase,
6666
bool writeXML( QDomNode & node, QDomDocument & document, bool temp = false );
6767
bool readXML( QDomNode & node );
6868

69-
QRect boundingRect ( void ) const;
69+
QRectF boundingRect ( void ) const;
7070

7171
/** \brief Draw to paint device, internal use
7272
* \param painter painter or 0
7373
* \return bounding box
7474
*/
75-
QRect render (QPainter *painter);
75+
QRectF render (QPainter *painter);
7676

77-
/** \brief Reimplementation of QCanvasItem::draw - draw on canvas */
78-
void draw ( QPainter & painter );
77+
/** \brief Reimplementation of QGraphicsItem::paint - draw on canvas */
78+
void paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget);
7979

80-
void drawShape(QPainter&);
81-
Q3PointArray areaPoints() const;
80+
//void drawShape(QPainter&);
81+
QPolygonF areaPoints() const;
8282

8383
/** \brief Calculate size according to current settings */
8484
void recalculate ( void );
@@ -87,7 +87,7 @@ class QgsComposerScalebar : public QWidget, private Ui::QgsComposerScalebarBase,
8787
void setOptions ( void );
8888

8989
// Move to position
90-
void moveBy ( double x, double y );
90+
// void moveBy ( double x, double y );
9191

9292
public slots:
9393
// Open font dialog
@@ -122,7 +122,7 @@ public slots:
122122
std::vector<int> mMaps;
123123

124124
// Current bounding box
125-
QRect mBoundingRect;
125+
QRectF mBoundingRect;
126126

127127
// Number of map units in scalebar unit
128128
double mMapUnitsPerUnit;
@@ -134,7 +134,7 @@ public slots:
134134
QFont mFont;
135135

136136
// Pen
137-
QPen mPen;
137+
QPen mPen;
138138

139139
// Brush
140140
QBrush mBrush;
@@ -146,7 +146,7 @@ public slots:
146146
double mSegmentLength;
147147

148148
// Height of scalebar box in canvas units (box style only)
149-
int mHeight;
149+
double mHeight;
150150

151151
// Margin
152152
int mMargin;

‎src/app/composer/qgscomposervectorlegend.cpp

Lines changed: 147 additions & 141 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposervectorlegend.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343

4444
#include "ui_qgscomposervectorlegendbase.h"
4545
#include "qgscomposeritem.h"
46-
#include <Q3CanvasPolygonalItem>
46+
47+
#include <QAbstractGraphicsShapeItem>
48+
#include <QPen>
4749
#include <QPixmap>
4850
#include <map>
4951

@@ -63,7 +65,7 @@ class QRect;
6365
// NOTE: QgsComposerVectorLegendBase must be first, otherwise does not compile
6466
class QgsComposerVectorLegend : public QWidget,
6567
private Ui::QgsComposerVectorLegendBase,
66-
public Q3CanvasRectangle,
68+
public QGraphicsRectItem,
6769
public QgsComposerItem
6870
{
6971
Q_OBJECT
@@ -105,10 +107,10 @@ class QgsComposerVectorLegend : public QWidget,
105107
* \param painter painter or 0
106108
* \return bounding box
107109
*/
108-
QRect render (QPainter *painter);
110+
QRectF render (QPainter *painter);
109111

110-
/** \brief Reimplementation of QCanvasItem::draw - draw on canvas */
111-
void draw ( QPainter & painter );
112+
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
113+
void paint ( QPainter*, const QStyleOptionGraphicsItem*, QWidget*);
112114

113115
/** \brief Calculate size according to current settings */
114116
void recalculate ( void );

‎src/app/composer/qgscomposerview.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,35 @@
2929

3030
// Note: |WRepaintNoErase|WResizeNoErase|WStaticContents doeen't make it faster
3131
QgsComposerView::QgsComposerView( QgsComposer *composer, QWidget* parent, const char* name, Qt::WFlags f) :
32-
Q3CanvasView(parent,name,f|Qt::WNoAutoErase|Qt::WResizeNoErase|Qt::WStaticContents)
32+
QGraphicsView(parent)
33+
//,name,f|Qt::WNoAutoErase|Qt::WResizeNoErase|Qt::WStaticContents
3334
{
3435
mComposer = composer;
3536

3637
// TODO: nothing doe work -> necessary to call setFocus ()
3738
setEnabled ( true );
3839
setFocusPolicy ( Qt::StrongFocus );
3940
setFocusProxy ( 0 );
41+
42+
setResizeAnchor ( QGraphicsView::AnchorViewCenter );
43+
4044
}
4145

42-
void QgsComposerView::contentsMousePressEvent(QMouseEvent* e)
46+
void QgsComposerView::mousePressEvent(QMouseEvent* e)
4347
{
4448
// TODO: find how to get focus without setFocus
4549
setFocus ();
46-
mComposer->composition()->contentsMousePressEvent(e);
50+
mComposer->composition()->mousePressEvent(e);
4751
}
4852

49-
void QgsComposerView::contentsMouseReleaseEvent(QMouseEvent* e)
53+
void QgsComposerView::mouseReleaseEvent(QMouseEvent* e)
5054
{
51-
mComposer->composition()->contentsMouseReleaseEvent(e);
55+
mComposer->composition()->mouseReleaseEvent(e);
5256
}
5357

54-
void QgsComposerView::contentsMouseMoveEvent(QMouseEvent* e)
58+
void QgsComposerView::mouseMoveEvent(QMouseEvent* e)
5559
{
56-
mComposer->composition()->contentsMouseMoveEvent(e);
60+
mComposer->composition()->mouseMoveEvent(e);
5761
}
5862

5963
void QgsComposerView::keyPressEvent ( QKeyEvent * e )
@@ -63,5 +67,13 @@ void QgsComposerView::keyPressEvent ( QKeyEvent * e )
6367

6468
void QgsComposerView::resizeEvent ( QResizeEvent * )
6569
{
66-
mComposer->zoomFull();
70+
/* BUG: When QT adds scrollbars because we're zooming in, it causes a resizeEvent.
71+
* If we call zoomFull(), we reset the view size, which keeps us from zooming in.
72+
* Really, we should do something like re-center the window.
73+
*/
74+
//mComposer->zoomFull();
75+
std::cout << "resize anchor: " << resizeAnchor() << std::endl;
6776
}
77+
78+
//TODO: add mouse wheel event forwarding
79+

‎src/app/composer/qgscomposerview.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@
1818
#ifndef QGSCOMPOSERVIEW_H
1919
#define QGSCOMPOSERVIEW_H
2020

21-
#include <Q3Canvas>
22-
#include <Q3CanvasView>
21+
22+
#include <QGraphicsScene>
23+
#include <QGraphicsView>
2324

2425
class QgsComposer;
2526
class QKeyEvent;
2627
class QMouseEvent;
2728

28-
class QgsComposerView: public Q3CanvasView
29+
class QgsComposerView: public QGraphicsView
2930
{
3031
Q_OBJECT
3132

3233
public:
3334
QgsComposerView (QgsComposer *composer, QWidget* parent=0, const char* name=0, Qt::WFlags f=0);
3435

3536
protected:
36-
void contentsMousePressEvent(QMouseEvent*);
37-
void contentsMouseReleaseEvent(QMouseEvent*);
38-
void contentsMouseMoveEvent(QMouseEvent*);
37+
void mousePressEvent(QMouseEvent*);
38+
void mouseReleaseEvent(QMouseEvent*);
39+
void mouseMoveEvent(QMouseEvent*);
3940

4041
void keyPressEvent ( QKeyEvent * e );
4142
void resizeEvent ( QResizeEvent * );

‎src/app/composer/qgscomposition.cpp

Lines changed: 178 additions & 172 deletions
Large diffs are not rendered by default.

‎src/app/composer/qgscomposition.h

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ class QgsComposerMap;
2727
class QgsComposerView;
2828
class QgsMapCanvas;
2929

30-
class Q3Canvas;
31-
class Q3CanvasItem;
32-
class Q3CanvasRectangle;
30+
class QGraphicsScene;
31+
class QGraphicsItem;
32+
class QGraphicsRectItem;
33+
3334
class QDomDocument;
3435
class QDomNode;
3536
class QKeyEvent;
@@ -148,16 +149,16 @@ class QgsComposition: public QWidget, private Ui::QgsCompositionBase
148149
void setActive ( bool active );
149150

150151
/** \brief returns pointer to canvas */
151-
Q3Canvas *canvas(void);
152+
QGraphicsScene *canvas(void);
152153

153-
/** \brief recieves contentsMousePressEvent from view */
154-
void contentsMousePressEvent(QMouseEvent*);
154+
/** \brief recieves mousePressEvent from view */
155+
void mousePressEvent(QMouseEvent*);
155156

156-
/** \brief recieves contentsMouseReleaseEvent from view */
157-
void contentsMouseReleaseEvent(QMouseEvent*);
157+
/** \brief recieves mouseReleaseEvent from view */
158+
void mouseReleaseEvent(QMouseEvent*);
158159

159-
/** \brief recieves contentsMouseMoveEvent from view */
160-
void contentsMouseMoveEvent(QMouseEvent*);
160+
/** \brief recieves mouseMoveEvent from view */
161+
void mouseMoveEvent(QMouseEvent*);
161162

162163
/** \brief recieves keyPressEvent from view */
163164
void keyPressEvent ( QKeyEvent * e );
@@ -188,8 +189,8 @@ class QgsComposition: public QWidget, private Ui::QgsCompositionBase
188189
/** \brief Return number screen pixels / canvas point */
189190
double viewScale( void );
190191

191-
/** \brief Selection box size in _canvas_ units */
192-
int selectionBoxSize ( void );
192+
/** \brief Selection box size in _scene_ units */
193+
double selectionBoxSize ( void );
193194

194195
/** \brief Selection box pen and brush */
195196
QPen selectionPen ( void );
@@ -268,23 +269,19 @@ public slots:
268269
QgsComposer *mComposer;
269270

270271
/** \brief Canvas. One per composition, created by QgsComposition */
271-
Q3Canvas *mCanvas;
272+
QGraphicsScene *mCanvas;
272273

273274
/** \brief Pointer to canvas view */
274275
QgsComposerView *mView;
275276

276277
/** \brief List of all composer items */
277278
std::list<QgsComposerItem*> mItems;
278279

279-
/** \brief Last position of the mouse in mView */
280-
double mLastX;
281-
double mLastY;
282-
283280
/** \brief Selected item, 0 if no item is selected */
284-
Q3CanvasItem* mSelectedItem;
281+
QGraphicsItem* mSelectedItem;
285282

286283
/** \brief Item representing the paper */
287-
Q3CanvasRectangle *mPaperItem;
284+
QGraphicsRectItem *mPaperItem;
288285

289286
/** \brief Plot style */
290287
PlotStyle mPlotStyle;
@@ -296,10 +293,10 @@ public slots:
296293
int mToolStep;
297294

298295
/** \brief Temporary rectangle item used as rectangle drawn by mouse */
299-
Q3CanvasRectangle *mRectangleItem;
300-
296+
QGraphicsRectItem *mRectangleItem;
297+
301298
/** \brief Temporary item used as pointer to new objecs which must be drawn */
302-
Q3CanvasItem *mNewCanvasItem;
299+
QGraphicsItem *mNewCanvasItem;
303300

304301
/** \brief Resolution in DPI */
305302
int mResolution;
@@ -310,14 +307,16 @@ public slots:
310307
/** \brief id for next new item */
311308
int mNextItemId;
312309

310+
QGraphicsRectItem *sizeBox;
311+
313312
/** \brief Create canvas */
314313
void createCanvas(void);
315314

316315
/** \brief Resize canvas to current paper size */
317316
void resizeCanvas(void);
318317

319-
/** \brief first point, set with MousePressEvent */
320-
QPoint mLastPoint;
318+
/** \brief Grab point, set with MousePressEvent */
319+
QPointF mGrabPoint;
321320
};
322321

323322
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.