15
15
* *
16
16
***************************************************************************/
17
17
#include " qgscomposerlabel.h"
18
-
19
18
#include " qgsproject.h"
19
+
20
+ #include < QGraphicsScene>
21
+ #include < QAbstractGraphicsShapeItem>
22
+ #include < QPolygonF>
20
23
#include < QFontDialog>
21
24
#include < QPainter>
25
+
22
26
#include < iostream>
23
27
24
28
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
25
29
int x, int y, QString text, int fontSize )
26
- : QWidget(composition), Q3CanvasPolygonalItem (0 ), mBox(false )
30
+ : QWidget(composition), QAbstractGraphicsShapeItem (0 ), mBox(false )
27
31
{
28
32
setupUi (this );
29
33
@@ -37,28 +41,29 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id,
37
41
38
42
// Font and pen
39
43
mFont .setPointSize ( fontSize );
44
+
40
45
// Could make this user variable in the future
41
46
mPen .setWidthF (0.5 );
42
47
43
- Q3CanvasPolygonalItem::setX (x);
44
- Q3CanvasPolygonalItem::setY (y);
48
+ QAbstractGraphicsShapeItem::setPos (x, y);
45
49
46
50
mSelected = false ;
47
51
48
52
setOptions ();
49
53
50
54
// 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 ();
56
61
57
62
writeSettings ();
58
63
}
59
64
60
65
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
61
- : Q3CanvasPolygonalItem (0 )
66
+ : QAbstractGraphicsShapeItem (0 )
62
67
{
63
68
std::cout << " QgsComposerLabel::QgsComposerLabel()" << std::endl;
64
69
@@ -73,161 +78,169 @@ QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id )
73
78
setOptions ();
74
79
75
80
// 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 ();
81
86
82
87
}
83
88
84
89
QgsComposerLabel::~QgsComposerLabel ()
85
90
{
86
91
std::cout << " QgsComposerLabel::~QgsComposerLabel" << std::endl;
87
- Q3CanvasItem ::hide ();
92
+ QGraphicsItem ::hide ();
88
93
}
89
-
90
- void QgsComposerLabel::drawShape ( QPainter & painter )
94
+ /*
95
+ void QgsComposerLabel::drawShape ( QPainter & painter, const QStyleOptionGraphicsItem* item, QWidget* pWidget )
91
96
{
92
97
std::cout << "QgsComposerLabel::drawShape" << std::endl;
93
- draw ( painter );
98
+ paint ( painter, item, pWidget );
94
99
}
100
+ */
95
101
96
- void QgsComposerLabel::draw ( QPainter & painter )
102
+ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
97
103
{
98
- std::cout << " QgsComposerLabel::render " << std::endl;
104
+ std::cout << " QgsComposerLabel::paint " << std::endl;
99
105
100
106
float size = 25.4 * mComposition ->scale () * mFont .pointSizeFloat () / 72 ;
101
107
mBoxBuffer = (int ) ( size / 10 * mComposition ->scale () );
108
+ mBoxBuffer = 1 ;
109
+
102
110
103
111
QFont font ( mFont );
104
112
font.setPointSizeFloat ( size );
105
- QFontMetrics metrics ( font );
113
+ QFontMetricsF metrics ( font );
106
114
107
115
// Not sure about Style Strategy, QFont::PreferMatch ?
108
116
// font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
109
117
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 );
115
120
116
- int w = metrics.width ( mText );
117
- int h = metrics.height () ;
121
+ double w = metrics.width ( mText );
122
+ double h = metrics.height () - metrics. descent () ;
118
123
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
120
125
121
- QRect boxRect;
126
+ QRectF boxRect;
122
127
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 );
128
134
}
129
- painter. setPen ( mPen );
135
+ painter-> setPen ( mPen );
130
136
131
137
// The width is not sufficient in postscript
132
- QRect tr = r;
138
+ QRectF tr = r;
133
139
tr.setWidth ( r.width () );
134
140
135
141
if ( plotStyle () == QgsComposition::Postscript )
136
142
{
137
143
// This metrics.ascent() is empirical
138
144
size = metrics.ascent () * 72.0 / mComposition ->resolution ();
139
145
font.setPointSizeF ( size );
140
- painter.setFont ( font );
146
+ painter->setFont ( font );
147
+ std::cout << " label using PS render size" << std::endl;
141
148
}
142
- painter. drawText ( x-w/ 2 ,( int )(y+metrics. height ()/ 2 -metrics. descent ()), mText );
149
+ painter-> drawText ( 0 , 0 , mText );
143
150
144
151
// Show selected / Highlight
145
152
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 ();
155
163
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 ) );
160
168
}
161
169
}
162
170
163
171
void QgsComposerLabel::on_mFontButton_clicked ()
164
172
{
165
173
bool result;
166
174
167
- QRect r = boundingRect ();
175
+ QRectF r = boundingRect ();
168
176
169
177
mFont = QFontDialog::getFont (&result, mFont , this );
170
178
171
179
if ( result ) {
172
- Q3CanvasPolygonalItem::invalidate ();
173
- Q3CanvasPolygonalItem::canvas ()->setChanged (r);
174
- Q3CanvasPolygonalItem::update ();
175
- Q3CanvasPolygonalItem::canvas ()->update ();
180
+ QAbstractGraphicsShapeItem::prepareGeometryChange ();
181
+ QAbstractGraphicsShapeItem::update ();
176
182
}
177
183
writeSettings ();
178
184
}
179
185
180
186
void QgsComposerLabel::on_mBoxCheckBox_clicked ()
181
187
{
182
- QRect r = boundingRect ();
188
+ QRectF r = boundingRect ();
183
189
184
190
mBox = mBoxCheckBox ->isChecked ();
185
191
186
- Q3CanvasPolygonalItem::invalidate ();
187
- Q3CanvasPolygonalItem::canvas ()->setChanged (r);
188
- Q3CanvasPolygonalItem::update ();
189
- Q3CanvasPolygonalItem::canvas ()->update ();
190
-
192
+ QAbstractGraphicsShapeItem::prepareGeometryChange ();
193
+ QAbstractGraphicsShapeItem::update ();
191
194
writeSettings ();
192
195
}
193
196
194
- QRect QgsComposerLabel::boundingRect ( void ) const
197
+ QRectF QgsComposerLabel::boundingRect ( void ) const
195
198
{
196
199
// Recalculate sizes according to current font size
197
200
198
- float size = 25.4 * mComposition ->scale () * mFont .pointSize () / 72 ;
199
-
201
+ float size = 25.4 * mComposition ->scale () * mFont .pointSizeFloat () / 72 ;
202
+
200
203
QFont font ( mFont );
201
204
font.setPointSizeFloat ( size );
202
-
203
205
QFontMetrics metrics ( font );
204
-
205
- int x = (int ) Q3CanvasPolygonalItem::x ();
206
- int y = (int ) Q3CanvasPolygonalItem::y ();
206
+
207
207
int w = metrics.width ( mText );
208
- int h = metrics.height () ;
209
-
208
+ int h = metrics.height () - metrics.descent ();
209
+
210
+ /*
210
211
int buf = 0;
211
212
212
213
if ( mBox ) {
213
214
buf = (int) ( size / 10 * mComposition->scale() + 2 ); // 2 is for line width
214
215
}
215
216
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
+ }
217
229
218
230
return r;
231
+
219
232
}
220
233
221
- Q3PointArray QgsComposerLabel::areaPoints () const
234
+ QPolygonF QgsComposerLabel::areaPoints () const
222
235
{
223
236
std::cout << " QgsComposerLabel::areaPoints" << std::endl;
224
- QRect r = boundingRect ();
237
+ QRectF r = boundingRect ();
225
238
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 () );
231
244
232
245
return pa ;
233
246
}
@@ -241,20 +254,18 @@ void QgsComposerLabel::setOptions ( void )
241
254
242
255
void QgsComposerLabel::on_mTextLineEdit_returnPressed ()
243
256
{
244
- QRect r = boundingRect ();
257
+ QRectF r = boundingRect ();
245
258
mText = mTextLineEdit ->text ();
246
- Q3CanvasPolygonalItem::invalidate ();
247
- Q3CanvasPolygonalItem::canvas ()->setChanged (r);
248
- Q3CanvasPolygonalItem::update ();
249
- Q3CanvasPolygonalItem::canvas ()->update ();
259
+ QAbstractGraphicsShapeItem::prepareGeometryChange ();
260
+ QAbstractGraphicsShapeItem::update ();
250
261
writeSettings ();
251
262
}
252
263
253
264
void QgsComposerLabel::setSelected ( bool s )
254
265
{
255
266
std::cout << " QgsComposerLabel::setSelected" << std::endl;
256
267
mSelected = s;
257
- Q3CanvasPolygonalItem ::update (); // show highlight
268
+ QAbstractGraphicsShapeItem ::update (); // show highlight
258
269
259
270
std::cout << " mSelected = " << mSelected << std::endl;
260
271
}
@@ -277,8 +288,8 @@ bool QgsComposerLabel::writeSettings ( void )
277
288
278
289
QgsProject::instance ()->writeEntry ( " Compositions" , path+" text" , mText );
279
290
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 ()) );
282
293
283
294
QgsProject::instance ()->writeEntry ( " Compositions" , path+" font/size" , mFont .pointSize () );
284
295
QgsProject::instance ()->writeEntry ( " Compositions" , path+" font/family" , mFont .family () );
@@ -302,9 +313,8 @@ bool QgsComposerLabel::readSettings ( void )
302
313
mText = QgsProject::instance ()->readEntry (" Compositions" , path+" text" , " ???" , &ok);
303
314
304
315
int x = mComposition ->fromMM ( QgsProject::instance ()->readDoubleEntry ( " Compositions" , path+" x" , 0 , &ok) );
305
- Q3CanvasPolygonalItem::setX ( x );
306
316
int y = mComposition ->fromMM (QgsProject::instance ()->readDoubleEntry ( " Compositions" , path+" y" , 0 , &ok) );
307
- Q3CanvasPolygonalItem::setY ( y );
317
+ QAbstractGraphicsShapeItem::setPos (x,y );
308
318
309
319
mFont .setFamily ( QgsProject::instance ()->readEntry (" Compositions" , path+" font/family" , " " , &ok) );
310
320
mFont .setPointSize ( QgsProject::instance ()->readNumEntry (" Compositions" , path+" font/size" , 10 , &ok) );
@@ -314,7 +324,7 @@ bool QgsComposerLabel::readSettings ( void )
314
324
315
325
mBox = QgsProject::instance ()->readBoolEntry (" Compositions" , path+" box" , false , &ok);
316
326
317
- Q3CanvasPolygonalItem ::update ();
327
+ QAbstractGraphicsShapeItem ::update ();
318
328
319
329
return true ;
320
330
}
0 commit comments