Skip to content

Commit

Permalink
Fix for ticket #181 (multi-line text labels on map composer)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
g_j_m committed Nov 23, 2007
1 parent fd1a41a commit f509d5f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 67 deletions.
58 changes: 29 additions & 29 deletions src/app/composer/qgscomposerlabel.cpp
Expand Up @@ -91,44 +91,39 @@ QgsComposerLabel::~QgsComposerLabel()
}

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

float size = 25.4 * mComposition->scale() * mFont.pointSizeFloat() / 72;
float size = 25.4 * mComposition->scale() * mFont.pointSizeF() / 72;

//mBoxBuffer = size / 10 * mComposition->scale();
mBoxBuffer = 1;
mBoxBuffer = 0;

QFont font ( mFont );
font.setPointSizeFloat ( size );
QFontMetricsF metrics ( font );
font.setStyleStrategy ( QFont::StyleStrategy(QFont::PreferOutline | QFont::PreferAntialias) );
font.setPointSizeF ( size );

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

double w = metrics.width ( mText );
double h = metrics.height() - metrics.descent();
QFontMetricsF metrics ( font );
QSizeF textSize = metrics.size(0, mText);
qreal w = textSize.width() + WIDTH_EXTENSION;
qreal h = textSize.height();

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

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


/*This code doesn't do anything...?
// The width is not sufficient in postscript
QRectF tr = r;
tr.setWidth ( r.width() );
*/

font.setPointSizeFloat ( size * FONT_WORKAROUND_SCALE ); //Hack to work around Qt font bug
font.setPointSizeF ( size * FONT_WORKAROUND_SCALE );
painter->setFont ( font );
painter->setPen ( mPen );

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

//Hack to work around the Qt font bug

painter->save();
painter->scale(1./FONT_WORKAROUND_SCALE, 1./FONT_WORKAROUND_SCALE);

painter->drawText(0, 0, mText);

QRectF scaledBox(r.x() * FONT_WORKAROUND_SCALE,
r.y() * FONT_WORKAROUND_SCALE,
(r.width() + WIDTH_EXTENSION) * FONT_WORKAROUND_SCALE,
r.height() * FONT_WORKAROUND_SCALE);
painter->drawText(scaledBox, Qt::AlignLeft|Qt::AlignVCenter, mText);
painter->restore(); //undo our scaling of painter - End of the font workaround

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

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

QFont font ( mFont );
font.setPointSizeFloat ( size );
font.setPointSizeF ( size );
QFontMetricsF metrics ( font );

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

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

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

void QgsComposerLabel::setOptions ( void )
{
mTextLineEdit->setText ( mText );
mTextEdit->setText ( mText );
mBoxCheckBox->setChecked ( mBox );

}

void QgsComposerLabel::on_mTextLineEdit_returnPressed()
void QgsComposerLabel::on_mTextEdit_textChanged()
{
QRectF r = boundingRect();
mText = mTextLineEdit->text();
mText = mTextEdit->text();
QAbstractGraphicsShapeItem::prepareGeometryChange();
QAbstractGraphicsShapeItem::update();
writeSettings();
Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposerlabel.h
Expand Up @@ -77,7 +77,7 @@ public slots:
// Open font dialog
void on_mFontButton_clicked();

void on_mTextLineEdit_returnPressed();
void on_mTextEdit_textChanged();

// Box settings changed
void on_mBoxCheckBox_clicked();
Expand Down
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposition.cpp
Expand Up @@ -751,7 +751,7 @@ void QgsComposition::setTool ( Tool tool )
if ( mNewCanvasItem ) delete mNewCanvasItem;

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

mNewCanvasItem = dynamic_cast <QGraphicsItem *> (lab);
mComposer->showItemOptions ( lab->options() );
Expand Down
63 changes: 27 additions & 36 deletions src/ui/qgscomposerlabelbase.ui
@@ -1,15 +1,12 @@
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>QgsComposerLabelBase</class>
<widget class="QWidget" name="QgsComposerLabelBase" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>215</width>
<height>151</height>
<height>341</height>
</rect>
</property>
<property name="sizePolicy" >
Expand All @@ -23,37 +20,21 @@
<property name="windowTitle" >
<string>Label Options</string>
</property>
<layout class="QGridLayout" >
<layout class="QVBoxLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="3" column="0" >
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType" >
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<size>
<width>16</width>
<height>23</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" >
<widget class="QCheckBox" name="mBoxCheckBox" >
<property name="text" >
<string>Box</string>
<item>
<widget class="QTextEdit" name="mTextEdit" >
<property name="lineWrapMode" >
<enum>QTextEdit::NoWrap</enum>
</property>
</widget>
</item>
<item row="1" column="0" >
<item>
<widget class="QPushButton" name="mFontButton" >
<property name="sizePolicy" >
<sizepolicy>
Expand All @@ -68,24 +49,34 @@
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLineEdit" name="mTextLineEdit" >
<property name="sizePolicy" >
<sizepolicy>
<hsizetype>5</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item>
<widget class="QCheckBox" name="mBoxCheckBox" >
<property name="text" >
<string>Box</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType" >
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<size>
<width>197</width>
<height>71</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11" />
<pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
<tabstops>
<tabstop>mTextLineEdit</tabstop>
<tabstop>mFontButton</tabstop>
<tabstop>mBoxCheckBox</tabstop>
</tabstops>
Expand Down

0 comments on commit f509d5f

Please sign in to comment.