mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
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@7644 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
parent
89c145ab57
commit
00e26fd64e
@ -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 );
|
||||
font.setStyleStrategy ( QFont::StyleStrategy(QFont::PreferOutline | QFont::PreferAntialias) );
|
||||
font.setPointSizeF ( size );
|
||||
|
||||
QFontMetricsF metrics ( font );
|
||||
|
||||
// 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();
|
||||
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 );
|
||||
|
||||
@ -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
|
||||
@ -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() );
|
||||
|
||||
@ -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;
|
||||
@ -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);
|
||||
@ -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();
|
||||
|
@ -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();
|
||||
|
@ -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() );
|
||||
|
@ -1,7 +1,4 @@
|
||||
<ui version="4.0" >
|
||||
<author></author>
|
||||
<comment></comment>
|
||||
<exportmacro></exportmacro>
|
||||
<class>QgsComposerLabelBase</class>
|
||||
<widget class="QWidget" name="QgsComposerLabelBase" >
|
||||
<property name="geometry" >
|
||||
@ -9,7 +6,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>215</width>
|
||||
<height>151</height>
|
||||
<height>341</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy" >
|
||||
@ -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>
|
||||
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user