~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/composer/qgscomposerlabel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                         qgscomposerlabel.cpp
3
 
                             -------------------
4
 
    begin                : January 2005
5
 
    copyright            : (C) 2005 by Radim Blazek
6
 
    email                : blazek@itc.it
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
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.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
#include "qgscomposerlabel.h"
18
 
 
19
 
#include "qgsproject.h"
20
 
#include <QFontDialog>
21
 
#include <QPainter>
22
 
#include <iostream>
23
 
 
24
 
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id, 
25
 
                                                    int x, int y, QString text, int fontSize )
26
 
    : QWidget(composition), Q3CanvasPolygonalItem(0), mBox(false)
27
 
{
28
 
    setupUi(this);
29
 
 
30
 
    std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
31
 
 
32
 
    mComposition = composition;
33
 
    mId  = id;
34
 
 
35
 
    //mText = text;
36
 
    mText = "Quantum GIS";
37
 
 
38
 
    // Font and pen 
39
 
    mFont.setPointSize ( fontSize );
40
 
    // Could make this user variable in the future
41
 
    mPen.setWidthF (0.5);
42
 
 
43
 
    Q3CanvasPolygonalItem::setX(x);
44
 
    Q3CanvasPolygonalItem::setY(y);
45
 
 
46
 
    mSelected = false;
47
 
 
48
 
    setOptions();
49
 
 
50
 
    // Add to canvas
51
 
    setCanvas(mComposition->canvas());
52
 
    Q3CanvasPolygonalItem::setZ(100);
53
 
    setActive(true);
54
 
    Q3CanvasPolygonalItem::show();
55
 
    Q3CanvasPolygonalItem::update(); // ?
56
 
 
57
 
    writeSettings();
58
 
}
59
 
 
60
 
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition, int id ) 
61
 
    : Q3CanvasPolygonalItem(0)
62
 
{
63
 
    std::cout << "QgsComposerLabel::QgsComposerLabel()" << std::endl;
64
 
 
65
 
    setupUi(this);
66
 
 
67
 
    mComposition = composition;
68
 
    mId  = id;
69
 
    mSelected = false;
70
 
 
71
 
    readSettings();
72
 
    
73
 
    setOptions();
74
 
 
75
 
    // Add to canvas
76
 
    setCanvas(mComposition->canvas());
77
 
    Q3CanvasPolygonalItem::setZ(100);
78
 
    setActive(true);
79
 
    Q3CanvasPolygonalItem::show();
80
 
    Q3CanvasPolygonalItem::update(); // ?
81
 
 
82
 
}
83
 
 
84
 
QgsComposerLabel::~QgsComposerLabel()
85
 
{
86
 
    std::cout << "QgsComposerLabel::~QgsComposerLabel" << std::endl;
87
 
    Q3CanvasItem::hide();
88
 
}
89
 
 
90
 
void QgsComposerLabel::drawShape ( QPainter & painter )
91
 
{
92
 
    std::cout << "QgsComposerLabel::drawShape" << std::endl;
93
 
    draw ( painter );
94
 
}
95
 
 
96
 
void QgsComposerLabel::draw ( QPainter & painter )
97
 
{
98
 
    std::cout << "QgsComposerLabel::render" << std::endl;
99
 
 
100
 
    float size =  25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
101
 
    mBoxBuffer = (int) ( size / 10 * mComposition->scale() );
102
 
 
103
 
    QFont font ( mFont );
104
 
    font.setPointSizeFloat ( size );
105
 
    QFontMetrics metrics ( font );
106
 
 
107
 
    // Not sure about Style Strategy, QFont::PreferMatch ?
108
 
    //font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
109
 
 
110
 
    painter.setPen ( mPen );
111
 
    painter.setFont ( font );
112
 
    
113
 
    int x = (int) Q3CanvasPolygonalItem::x();
114
 
    int y = (int) Q3CanvasPolygonalItem::y();
115
 
    
116
 
    int w = metrics.width ( mText );
117
 
    int h = metrics.height() ;
118
 
 
119
 
    QRect r ( (int)(x - w/2), (int) (y - h/2), w, h );
120
 
    
121
 
    QRect boxRect;
122
 
    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
 
    }
129
 
    painter.setPen ( mPen );
130
 
    
131
 
    // The width is not sufficient in postscript
132
 
    QRect tr = r;
133
 
    tr.setWidth ( r.width() );
134
 
 
135
 
    if ( plotStyle() == QgsComposition::Postscript ) 
136
 
    {
137
 
        // This metrics.ascent() is empirical
138
 
        size = metrics.ascent() * 72.0 / mComposition->resolution(); 
139
 
        font.setPointSizeF ( size );
140
 
        painter.setFont ( font );
141
 
    } 
142
 
    painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
143
 
 
144
 
    // Show selected / Highlight
145
 
    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();
155
 
        
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 );
160
 
    }
161
 
}
162
 
 
163
 
void QgsComposerLabel::on_mFontButton_clicked() 
164
 
{
165
 
    bool result;
166
 
 
167
 
    QRect r = boundingRect();
168
 
 
169
 
    mFont = QFontDialog::getFont(&result, mFont, this );
170
 
 
171
 
    if ( result ) {
172
 
        Q3CanvasPolygonalItem::invalidate();
173
 
        Q3CanvasPolygonalItem::canvas()->setChanged(r);
174
 
        Q3CanvasPolygonalItem::update();
175
 
        Q3CanvasPolygonalItem::canvas()->update();
176
 
    }
177
 
    writeSettings();
178
 
}
179
 
 
180
 
void QgsComposerLabel::on_mBoxCheckBox_clicked()
181
 
{
182
 
    QRect r = boundingRect();
183
 
    
184
 
    mBox = mBoxCheckBox->isChecked();
185
 
 
186
 
    Q3CanvasPolygonalItem::invalidate();
187
 
    Q3CanvasPolygonalItem::canvas()->setChanged(r);
188
 
    Q3CanvasPolygonalItem::update();
189
 
    Q3CanvasPolygonalItem::canvas()->update();
190
 
 
191
 
    writeSettings();
192
 
}
193
 
 
194
 
QRect QgsComposerLabel::boundingRect ( void ) const
195
 
{
196
 
    // Recalculate sizes according to current font size
197
 
    
198
 
    float size = 25.4 * mComposition->scale() * mFont.pointSize() / 72;
199
 
    
200
 
    QFont font ( mFont );
201
 
    font.setPointSizeFloat ( size );
202
 
    
203
 
    QFontMetrics metrics ( font );
204
 
    
205
 
    int x = (int) Q3CanvasPolygonalItem::x();
206
 
    int y = (int) Q3CanvasPolygonalItem::y();
207
 
    int w = metrics.width ( mText );
208
 
    int h = metrics.height() ;
209
 
    
210
 
    int buf = 0;
211
 
    
212
 
    if ( mBox ) {
213
 
        buf = (int) ( size / 10 * mComposition->scale() + 2 ); // 2 is for line width
214
 
    }
215
 
    
216
 
    QRect r ( (int)(x - w/2 - 1.5*buf), (int) (y - h/2 - buf), (int)(w+3*buf), h+2*buf );
217
 
 
218
 
    return r;
219
 
}
220
 
 
221
 
Q3PointArray QgsComposerLabel::areaPoints() const
222
 
{
223
 
    std::cout << "QgsComposerLabel::areaPoints" << std::endl;
224
 
    QRect r = boundingRect();
225
 
 
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() );
231
 
 
232
 
    return pa ;
233
 
}
234
 
 
235
 
void QgsComposerLabel::setOptions ( void )
236
 
237
 
    mTextLineEdit->setText ( mText );
238
 
    mBoxCheckBox->setChecked ( mBox );
239
 
    
240
 
}
241
 
 
242
 
void QgsComposerLabel::on_mTextLineEdit_returnPressed()
243
 
244
 
    QRect r = boundingRect();
245
 
    mText = mTextLineEdit->text();
246
 
    Q3CanvasPolygonalItem::invalidate();
247
 
    Q3CanvasPolygonalItem::canvas()->setChanged(r);
248
 
    Q3CanvasPolygonalItem::update();
249
 
    Q3CanvasPolygonalItem::canvas()->update();
250
 
    writeSettings();
251
 
}
252
 
 
253
 
void QgsComposerLabel::setSelected (  bool s ) 
254
 
{
255
 
    std::cout << "QgsComposerLabel::setSelected" << std::endl;
256
 
    mSelected = s;
257
 
    Q3CanvasPolygonalItem::update(); // show highlight
258
 
            
259
 
    std::cout << "mSelected = " << mSelected << std::endl;
260
 
}    
261
 
 
262
 
bool QgsComposerLabel::selected( void )
263
 
{
264
 
    return mSelected;
265
 
}
266
 
 
267
 
QWidget *QgsComposerLabel::options ( void )
268
 
{
269
 
    setOptions ();
270
 
    return ( dynamic_cast <QWidget *> (this) );
271
 
}
272
 
 
273
 
bool QgsComposerLabel::writeSettings ( void )  
274
 
{
275
 
    QString path;
276
 
    path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId ); 
277
 
    
278
 
    QgsProject::instance()->writeEntry( "Compositions", path+"text", mText );
279
 
 
280
 
    QgsProject::instance()->writeEntry( "Compositions", path+"x", mComposition->toMM((int)Q3CanvasPolygonalItem::x()) );
281
 
    QgsProject::instance()->writeEntry( "Compositions", path+"y", mComposition->toMM((int)Q3CanvasPolygonalItem::y()) );
282
 
 
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() );
288
 
 
289
 
    QgsProject::instance()->writeEntry( "Compositions", path+"box", mBox );
290
 
    
291
 
    return true; 
292
 
}
293
 
 
294
 
bool QgsComposerLabel::readSettings ( void )
295
 
{
296
 
    std::cout << "QgsComposerLabel::readSettings mId = " << mId << std::endl;
297
 
    bool ok;
298
 
 
299
 
    QString path;
300
 
    path.sprintf("/composition_%d/label_%d/", mComposition->id(), mId );
301
 
 
302
 
    mText = QgsProject::instance()->readEntry("Compositions", path+"text", "???", &ok);
303
 
 
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 );
308
 
 
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) );
314
 
 
315
 
    mBox = QgsProject::instance()->readBoolEntry("Compositions", path+"box", false, &ok);
316
 
 
317
 
    Q3CanvasPolygonalItem::update();
318
 
 
319
 
    return true;
320
 
}
321
 
 
322
 
bool QgsComposerLabel::removeSettings ( void )
323
 
{
324
 
    QString path;
325
 
    path.sprintf("/composition_%d/label_%d", mComposition->id(), mId );
326
 
    return QgsProject::instance()->removeEntry ( "Compositions", path );
327
 
}
328
 
 
329
 
bool QgsComposerLabel::writeXML( QDomNode & node, QDomDocument & document, bool temp )
330
 
{
331
 
    return true;
332
 
}
333
 
 
334
 
bool QgsComposerLabel::readXML( QDomNode & node )
335
 
{
336
 
    return true;
337
 
}