Skip to content

Commit f509d5f

Browse files
author
g_j_m
committed
Fix for ticket #181 (multi-line text labels on map composer)
Removed some Qt3 usages Made the default text label font size a bit smaller Note: There is a problem with the last character in the text being clipped at some font sizes... git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7644 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent fd1a41a commit f509d5f

File tree

4 files changed

+58
-67
lines changed

4 files changed

+58
-67
lines changed

src/app/composer/qgscomposerlabel.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,39 @@ QgsComposerLabel::~QgsComposerLabel()
9191
}
9292

9393
#define FONT_WORKAROUND_SCALE 10
94+
// This partially resolves a problem with the bounding rectangle for the text
95+
// being too short for most font sizes.
96+
#define WIDTH_EXTENSION 1.0
9497
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
9598
{
9699
//std::cout << "QgsComposerLabel::paint" << std::endl;
97100

98-
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
101+
float size = 25.4 * mComposition->scale() * mFont.pointSizeF() / 72;
102+
99103
//mBoxBuffer = size / 10 * mComposition->scale();
100-
mBoxBuffer = 1;
104+
mBoxBuffer = 0;
101105

102106
QFont font ( mFont );
103-
font.setPointSizeFloat ( size );
104-
QFontMetricsF metrics ( font );
107+
font.setStyleStrategy ( QFont::StyleStrategy(QFont::PreferOutline | QFont::PreferAntialias) );
108+
font.setPointSizeF ( size );
105109

106-
// Not sure about Style Strategy, QFont::PreferMatch ?
107-
//font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias ) );
108-
109-
double w = metrics.width ( mText );
110-
double h = metrics.height() - metrics.descent();
110+
QFontMetricsF metrics ( font );
111+
QSizeF textSize = metrics.size(0, mText);
112+
qreal w = textSize.width() + WIDTH_EXTENSION;
113+
qreal h = textSize.height();
111114

112115
QRectF r (0, -h, w, h); //used as the rectangle to draw the selection boxes on the corners of if there is no box
113116

114117
QRectF boxRect;
115118
if ( mBox ) {
116-
//I don't know why the top coordinate is -h rather than -(h+mBoxBuffer), but it seems to work better.
117-
boxRect.setRect(-mBoxBuffer, -h, w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
119+
boxRect.setRect(-mBoxBuffer, -(h+mBoxBuffer), w + (mBoxBuffer * 2)+1, h + (mBoxBuffer * 2));
118120
QBrush brush ( QColor(255,255,255) );
119121
painter->setBrush ( brush );
120122
painter->setPen(QPen(QColor(0, 0, 0), .2));
121123
painter->drawRect ( boxRect );
122124
}
123125

124-
125-
/*This code doesn't do anything...?
126-
// The width is not sufficient in postscript
127-
QRectF tr = r;
128-
tr.setWidth ( r.width() );
129-
*/
130-
131-
font.setPointSizeFloat ( size * FONT_WORKAROUND_SCALE ); //Hack to work around Qt font bug
126+
font.setPointSizeF ( size * FONT_WORKAROUND_SCALE );
132127
painter->setFont ( font );
133128
painter->setPen ( mPen );
134129

@@ -142,11 +137,14 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
142137
}
143138

144139
//Hack to work around the Qt font bug
140+
145141
painter->save();
146142
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);
147-
148-
painter->drawText(0, 0, mText);
149-
143+
QRectF scaledBox(r.x() * FONT_WORKAROUND_SCALE,
144+
r.y() * FONT_WORKAROUND_SCALE,
145+
(r.width() + WIDTH_EXTENSION) * FONT_WORKAROUND_SCALE,
146+
r.height() * FONT_WORKAROUND_SCALE);
147+
painter->drawText(scaledBox, Qt::AlignLeft|Qt::AlignVCenter, mText);
150148
painter->restore(); //undo our scaling of painter - End of the font workaround
151149

152150
// Show selected / Highlight
@@ -157,6 +155,7 @@ void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem
157155
} else {
158156
hr = r;
159157
}
158+
hr.setWidth(hr.width() + WIDTH_EXTENSION);
160159
painter->setPen( mComposition->selectionPen() );
161160
painter->setBrush( mComposition->selectionBrush() );
162161

@@ -202,11 +201,12 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
202201
float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
203202

204203
QFont font ( mFont );
205-
font.setPointSizeFloat ( size );
204+
font.setPointSizeF ( size );
206205
QFontMetricsF metrics ( font );
207206

208-
double w = metrics.width ( mText );
209-
double h = metrics.height() - metrics.descent();
207+
QSizeF textSize = metrics.size(0, mText);
208+
double w = textSize.width() + WIDTH_EXTENSION;
209+
double h = textSize.height();
210210

211211
/*
212212
int buf = 0;
@@ -222,7 +222,7 @@ QRectF QgsComposerLabel::boundingRect ( void ) const
222222

223223
if(mBox){
224224
//what happens if we haven't called paint() first?
225-
r.setRect(-mBoxBuffer, -h, w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
225+
r.setRect(-mBoxBuffer, -(h+mBoxBuffer), w + (mBoxBuffer * 2), h + (mBoxBuffer * 2));
226226
}
227227
else{
228228
r.setRect(0, -h, w, h);
@@ -248,15 +248,15 @@ QPolygonF QgsComposerLabel::areaPoints() const
248248

249249
void QgsComposerLabel::setOptions ( void )
250250
{
251-
mTextLineEdit->setText ( mText );
251+
mTextEdit->setText ( mText );
252252
mBoxCheckBox->setChecked ( mBox );
253253

254254
}
255255

256-
void QgsComposerLabel::on_mTextLineEdit_returnPressed()
256+
void QgsComposerLabel::on_mTextEdit_textChanged()
257257
{
258258
QRectF r = boundingRect();
259-
mText = mTextLineEdit->text();
259+
mText = mTextEdit->text();
260260
QAbstractGraphicsShapeItem::prepareGeometryChange();
261261
QAbstractGraphicsShapeItem::update();
262262
writeSettings();

src/app/composer/qgscomposerlabel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public slots:
7777
// Open font dialog
7878
void on_mFontButton_clicked();
7979

80-
void on_mTextLineEdit_returnPressed();
80+
void on_mTextEdit_textChanged();
8181

8282
// Box settings changed
8383
void on_mBoxCheckBox_clicked();

src/app/composer/qgscomposition.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ void QgsComposition::setTool ( Tool tool )
751751
if ( mNewCanvasItem ) delete mNewCanvasItem;
752752

753753
// Create new object outside the visible area
754-
QgsComposerLabel *lab = new QgsComposerLabel ( this, mNextItemId++, -1000, -1000, tr("Label"), (int) (mPaperHeight/40));
754+
QgsComposerLabel *lab = new QgsComposerLabel ( this, mNextItemId++, -1000, -1000, tr("Label"), (int) (mPaperHeight/20));
755755

756756
mNewCanvasItem = dynamic_cast <QGraphicsItem *> (lab);
757757
mComposer->showItemOptions ( lab->options() );

src/ui/qgscomposerlabelbase.ui

+27-36
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<ui version="4.0" >
2-
<author></author>
3-
<comment></comment>
4-
<exportmacro></exportmacro>
52
<class>QgsComposerLabelBase</class>
63
<widget class="QWidget" name="QgsComposerLabelBase" >
74
<property name="geometry" >
85
<rect>
96
<x>0</x>
107
<y>0</y>
118
<width>215</width>
12-
<height>151</height>
9+
<height>341</height>
1310
</rect>
1411
</property>
1512
<property name="sizePolicy" >
@@ -23,37 +20,21 @@
2320
<property name="windowTitle" >
2421
<string>Label Options</string>
2522
</property>
26-
<layout class="QGridLayout" >
23+
<layout class="QVBoxLayout" >
2724
<property name="margin" >
2825
<number>9</number>
2926
</property>
3027
<property name="spacing" >
3128
<number>6</number>
3229
</property>
33-
<item row="3" column="0" >
34-
<spacer>
35-
<property name="orientation" >
36-
<enum>Qt::Vertical</enum>
37-
</property>
38-
<property name="sizeType" >
39-
<enum>QSizePolicy::Expanding</enum>
40-
</property>
41-
<property name="sizeHint" >
42-
<size>
43-
<width>16</width>
44-
<height>23</height>
45-
</size>
46-
</property>
47-
</spacer>
48-
</item>
49-
<item row="2" column="0" >
50-
<widget class="QCheckBox" name="mBoxCheckBox" >
51-
<property name="text" >
52-
<string>Box</string>
30+
<item>
31+
<widget class="QTextEdit" name="mTextEdit" >
32+
<property name="lineWrapMode" >
33+
<enum>QTextEdit::NoWrap</enum>
5334
</property>
5435
</widget>
5536
</item>
56-
<item row="1" column="0" >
37+
<item>
5738
<widget class="QPushButton" name="mFontButton" >
5839
<property name="sizePolicy" >
5940
<sizepolicy>
@@ -68,24 +49,34 @@
6849
</property>
6950
</widget>
7051
</item>
71-
<item row="0" column="0" >
72-
<widget class="QLineEdit" name="mTextLineEdit" >
73-
<property name="sizePolicy" >
74-
<sizepolicy>
75-
<hsizetype>5</hsizetype>
76-
<vsizetype>0</vsizetype>
77-
<horstretch>0</horstretch>
78-
<verstretch>0</verstretch>
79-
</sizepolicy>
52+
<item>
53+
<widget class="QCheckBox" name="mBoxCheckBox" >
54+
<property name="text" >
55+
<string>Box</string>
8056
</property>
8157
</widget>
8258
</item>
59+
<item>
60+
<spacer>
61+
<property name="orientation" >
62+
<enum>Qt::Vertical</enum>
63+
</property>
64+
<property name="sizeType" >
65+
<enum>QSizePolicy::Expanding</enum>
66+
</property>
67+
<property name="sizeHint" >
68+
<size>
69+
<width>197</width>
70+
<height>71</height>
71+
</size>
72+
</property>
73+
</spacer>
74+
</item>
8375
</layout>
8476
</widget>
8577
<layoutdefault spacing="6" margin="11" />
8678
<pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
8779
<tabstops>
88-
<tabstop>mTextLineEdit</tabstop>
8980
<tabstop>mFontButton</tabstop>
9081
<tabstop>mBoxCheckBox</tabstop>
9182
</tabstops>

0 commit comments

Comments
 (0)