27 #include <QCoreApplication>
29 #include <QDomElement>
40 , mHtmlUnitsToMM( 1.0 )
41 , mHtmlLoaded( false )
44 , mFontColor( QColor( 0, 0, 0 ) )
45 , mHAlignment( Qt::AlignLeft )
46 , mVAlignment( Qt::AlignTop )
47 , mExpressionFeature( 0 )
48 , mExpressionLayer( 0 )
52 mHtmlUnitsToMM = htmlUnitsToMM();
56 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
57 if ( !defaultFontString.isEmpty() )
59 mFont.setFamily( defaultFontString );
63 mFont.setPointSizeF( 10 );
90 Q_UNUSED( itemStyle );
105 painter->setRenderHint( QPainter::Antialiasing,
true );
107 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
108 double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
109 double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
110 QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );
116 painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
117 QWebPage *webPage =
new QWebPage();
123 timeoutTimer.setSingleShot(
true );
126 QPalette palette = webPage->palette();
127 palette.setBrush( QPalette::Base, Qt::transparent );
128 webPage->setPalette( palette );
131 webPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) );
132 webPage->mainFrame()->setZoomFactor( 10.0 );
133 webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
134 webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
142 webPage->settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
146 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
147 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
153 connect( webPage, SIGNAL( loadFinished(
bool ) ), SLOT( loadingHtmlFinished(
bool ) ) );
155 webPage->mainFrame()->setHtml( textToDraw );
163 timeoutTimer.start( 20000 );
167 webPage->mainFrame()->render( painter );
171 painter->setFont( mFont );
175 QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
188 void QgsComposerLabel::loadingHtmlFinished(
bool result )
194 double QgsComposerLabel::htmlUnitsToMM()
219 if ( state == mHtmlState )
235 mExpressionFeature = feature;
236 mExpressionLayer = layer;
237 mSubstitutions = substitutions;
279 replaceDateText( displayText );
280 QMap<QString, QVariant> subs = mSubstitutions;
285 void QgsComposerLabel::replaceDateText( QString& text )
const
287 QString constant =
"$CURRENT_DATE";
288 int currentDatePos = text.indexOf( constant );
289 if ( currentDatePos != -1 )
293 int openingBracketPos = text.indexOf(
"(", currentDatePos );
294 int closingBracketPos = text.indexOf(
")", openingBracketPos + 1 );
295 if ( openingBracketPos != -1 &&
296 closingBracketPos != -1 &&
297 ( closingBracketPos - openingBracketPos ) > 1 &&
298 openingBracketPos == currentDatePos + constant.size() )
300 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
301 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
305 text.replace(
"$CURRENT_DATE", QDate::currentDate().toString() );
319 prepareGeometryChange();
325 prepareGeometryChange();
331 prepareGeometryChange();
339 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
341 double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
342 double height = fontHeight + 2 * mMarginY + 2 * penWidth;
347 itemShiftAdjustSize( width, height, xShift, yShift );
350 QRectF evaluatedRect =
evalItemRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
368 QDomElement composerLabelElem = doc.createElement(
"ComposerLabel" );
370 composerLabelElem.setAttribute(
"htmlState", mHtmlState );
372 composerLabelElem.setAttribute(
"labelText", mText );
373 composerLabelElem.setAttribute(
"marginX", QString::number( mMarginX ) );
374 composerLabelElem.setAttribute(
"marginY", QString::number( mMarginY ) );
375 composerLabelElem.setAttribute(
"halign", mHAlignment );
376 composerLabelElem.setAttribute(
"valign", mVAlignment );
379 QDomElement labelFontElem = doc.createElement(
"LabelFont" );
380 labelFontElem.setAttribute(
"description", mFont.toString() );
381 composerLabelElem.appendChild( labelFontElem );
384 QDomElement fontColorElem = doc.createElement(
"FontColor" );
385 fontColorElem.setAttribute(
"red", mFontColor.red() );
386 fontColorElem.setAttribute(
"green", mFontColor.green() );
387 fontColorElem.setAttribute(
"blue", mFontColor.blue() );
388 composerLabelElem.appendChild( fontColorElem );
390 elem.appendChild( composerLabelElem );
391 return _writeXML( composerLabelElem, doc );
398 if ( itemElem.isNull() )
406 mText = itemElem.attribute(
"labelText" );
409 mHtmlState = itemElem.attribute(
"htmlState" ).toInt();
412 bool marginXOk =
false;
413 bool marginYOk =
false;
414 mMarginX = itemElem.attribute(
"marginX" ).toDouble( &marginXOk );
415 mMarginY = itemElem.attribute(
"marginY" ).toDouble( &marginYOk );
416 if ( !marginXOk || !marginYOk )
419 double margin = itemElem.attribute(
"margin",
"1.0" ).toDouble();
425 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"halign" ).toInt() );
428 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"valign" ).toInt() );
431 QDomNodeList labelFontList = itemElem.elementsByTagName(
"LabelFont" );
432 if ( labelFontList.size() > 0 )
434 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
435 mFont.fromString( labelFontElem.attribute(
"description" ) );
439 QDomNodeList fontColorList = itemElem.elementsByTagName(
"FontColor" );
440 if ( fontColorList.size() > 0 )
442 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
443 int red = fontColorElem.attribute(
"red",
"0" ).toInt();
444 int green = fontColorElem.attribute(
"green",
"0" ).toInt();
445 int blue = fontColorElem.attribute(
"blue",
"0" ).toInt();
446 mFontColor = QColor( red, green, blue );
450 mFontColor = QColor( 0, 0, 0 );
454 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
455 if ( composerItemList.size() > 0 )
457 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
460 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
463 setItemRotation( composerItemElem.attribute(
"rotation",
"0" ).toDouble() );
474 if ( !
id().isEmpty() )
481 return tr(
"<HTML label>" );
485 QString text = mText;
486 if ( text.isEmpty() )
488 return tr(
"<label>" );
490 if ( text.length() > 25 )
492 return QString(
tr(
"%1..." ) ).arg( text.left( 25 ).simplified() );
496 return text.simplified();
502 QRectF rectangle = rect();
503 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
504 rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
508 rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
512 rectangle.adjust( 0, mMarginY, 0, -mMarginY );
521 prepareGeometryChange();
527 prepareGeometryChange();
530 void QgsComposerLabel::itemShiftAdjustSize(
double newWidth,
double newHeight,
double& xShift,
double& yShift )
const
533 double currentWidth = rect().width();
534 double currentHeight = rect().height();
540 if ( mHAlignment == Qt::AlignHCenter )
542 xShift = - ( newWidth - currentWidth ) / 2.0;
544 else if ( mHAlignment == Qt::AlignRight )
546 xShift = - ( newWidth - currentWidth );
548 if ( mVAlignment == Qt::AlignVCenter )
550 yShift = -( newHeight - currentHeight ) / 2.0;
552 else if ( mVAlignment == Qt::AlignBottom )
554 yShift = - ( newHeight - currentHeight );
559 if ( mHAlignment == Qt::AlignHCenter )
561 yShift = -( newHeight - currentHeight ) / 2.0;
563 else if ( mHAlignment == Qt::AlignRight )
565 yShift = -( newHeight - currentHeight );
567 if ( mVAlignment == Qt::AlignTop )
569 xShift = -( newWidth - currentWidth );
571 else if ( mVAlignment == Qt::AlignVCenter )
573 xShift = -( newWidth - currentWidth / 2.0 );
578 if ( mHAlignment == Qt::AlignHCenter )
580 xShift = -( newWidth - currentWidth ) / 2.0;
582 else if ( mHAlignment == Qt::AlignLeft )
584 xShift = -( newWidth - currentWidth );
586 if ( mVAlignment == Qt::AlignVCenter )
588 yShift = ( newHeight - currentHeight ) / 2.0;
590 else if ( mVAlignment == Qt::AlignTop )
592 yShift = ( newHeight - currentHeight );
597 if ( mHAlignment == Qt::AlignHCenter )
599 yShift = -( newHeight - currentHeight ) / 2.0;
601 else if ( mHAlignment == Qt::AlignLeft )
603 yShift = -( newHeight - currentHeight );
605 if ( mVAlignment == Qt::AlignBottom )
607 xShift = -( newWidth - currentWidth );
609 else if ( mVAlignment == Qt::AlignVCenter )
611 xShift = -( newWidth - currentWidth / 2.0 );