Skip to content

Commit 89e2a97

Browse files
author
timlinux
committedJan 21, 2008
Tidy ups for quickprint:
- let the quickprint be based on either qgsmapcanvas or qgsmaprender - word wrapping for legend entries - added setMapBackgroundColor to public api git-svn-id: http://svn.osgeo.org/qgis/trunk@8013 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0cef849 commit 89e2a97

File tree

2 files changed

+138
-80
lines changed

2 files changed

+138
-80
lines changed
 

‎src/gui/qgsquickprint.cpp

Lines changed: 127 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ void QgsQuickPrint::setOutputPdf(QString theFileName)
9595
}
9696
void QgsQuickPrint::setMapCanvas(QgsMapCanvas * thepMapCanvas)
9797
{
98-
mpMapCanvas=thepMapCanvas;
98+
mpMapRender=thepMapCanvas->mapRender();
99+
mMapBackgroundColour = thepMapCanvas->canvasColor();
100+
}
101+
void QgsQuickPrint::setMapRender(QgsMapRender * thepMapRender)
102+
{
103+
mpMapRender=thepMapRender;
104+
}
105+
void QgsQuickPrint::setMapBackgroundColor(QColor theColor)
106+
{
107+
mMapBackgroundColour = theColor;
99108
}
100109

101110
void QgsQuickPrint::printMap()
@@ -104,7 +113,7 @@ void QgsQuickPrint::printMap()
104113
{
105114
return;
106115
}
107-
if ( mpMapCanvas == NULL )
116+
if ( mpMapRender == NULL )
108117
{
109118
return;
110119
}
@@ -222,7 +231,6 @@ void QgsQuickPrint::printMap()
222231

223232
// Background colour for pixmaps
224233
QColor myLegendBackgroundColour = Qt::white;
225-
QColor myMapBackgroundColour = mpMapCanvas->canvasColor();
226234
//QColor myMapBackgroundColour = "#98dbf9"; // nice blue colour
227235

228236

@@ -314,20 +322,20 @@ void QgsQuickPrint::printMap()
314322
int myMapDimensionX = (myDrawableWidth / 100) * myMapHeightPercent;
315323
int myMapDimensionY = (myDrawableHeight / 100) * myMapWidthPercent;
316324
QPixmap myMapPixmap ( myMapDimensionX,myMapDimensionY );
317-
myMapPixmap.fill ( myMapBackgroundColour );
325+
myMapPixmap.fill ( mMapBackgroundColour );
318326
QPainter myMapPainter;
319327
myMapPainter.begin( &myMapPixmap );
320-
mpMapCanvas->mapRender()->setOutputSize(
328+
mpMapRender->setOutputSize(
321329
QSize ( myMapDimensionX, myMapDimensionY ), myPrinter.resolution() );
322330
scalePointSymbols(mySymbolScalingAmount, ScaleUp);
323331
scaleTextLabels(mySymbolScalingAmount, ScaleUp);
324-
mpMapCanvas->mapRender()->render( &myMapPainter );
332+
mpMapRender->render( &myMapPainter );
325333
//maprender has no accessor for output size so
326334
//we couldnt store the size before starting the print render
327335
//so that it can be restored properly so what follows here is a
328336
//best guess approach
329-
mpMapCanvas->mapRender()->setOutputSize(
330-
QSize ( mpMapCanvas->width(), mpMapCanvas->height() ), myScreenResolutionDpi );
337+
mpMapRender->setOutputSize(
338+
QSize ( mpMapRender->width(), mpMapRender->height() ), myScreenResolutionDpi );
331339
myMapPainter.end();
332340
//draw the map pixmap onto our pdf print device
333341
myOriginX = myPrinter.pageRect().left() + myHorizontalSpacing;
@@ -344,6 +352,8 @@ void QgsQuickPrint::printMap()
344352
//myPrintPainter.setFont(myLegendFont);
345353
int myLegendDimensionX = (myDrawableWidth / 100) * myLegendWidthPercent;
346354
int myLegendDimensionY = (myDrawableHeight / 100) * myLegendHeightPercent;
355+
356+
347357
// Create a viewport to make coordinate conversions easier
348358
// The viewport has the same dimensions as the page(otherwise items
349359
// drawn into it will appear squashed), but a different origin.
@@ -353,15 +363,22 @@ void QgsQuickPrint::printMap()
353363
myOriginY,
354364
myOriginalViewport.width(),
355365
myOriginalViewport.height());
366+
//draw a rectangale around the legend frame
367+
//@TODO make this user settable
368+
if (0==1) //put some real logic here
369+
{
370+
myPrintPainter.drawRect( 0, 0, myLegendDimensionX, myLegendDimensionY );
371+
}
372+
//get font metric and other vars needed
356373
QFontMetrics myLegendFontMetrics( myLegendFont, &myPrinter );
357374
int myLegendFontHeight = myLegendFontMetrics.height();
358375
int myLegendXPos = 0;
359376
int myLegendYPos = 0;
360-
int myLegendSpacer = myLegendFontHeight; //for vertical and horizontal spacing
377+
int myLegendSpacer = myLegendFontHeight/2; //for vertical and horizontal spacing
361378
int myLegendVerticalSpacer = myLegendFontHeight/3; //for vertical between rows
362379
int myIconWidth = myLegendFontHeight;
363380
myPrintPainter.setFont( myLegendFont );
364-
QStringList myLayerSet = mpMapCanvas->mapRender()->layerSet();
381+
QStringList myLayerSet = mpMapRender->layerSet();
365382
QStringListIterator myLayerIterator ( myLayerSet );
366383
while ( myLayerIterator.hasNext() )
367384
{
@@ -389,7 +406,7 @@ void QgsQuickPrint::printMap()
389406
QgsSymbol * mypSymbol = mySymbolList.at(0);
390407
myPrintPainter.setPen( mypSymbol->pen() );
391408
myPrintPainter.setBrush( mypSymbol->brush() );
392-
myLegendXPos = myLegendSpacer;
409+
myLegendXPos = 0 ;
393410
if ( mypSymbol->type() == QGis::Point )
394411
{
395412
QImage myImage;
@@ -410,46 +427,50 @@ void QgsQuickPrint::printMap()
410427
}
411428
myLegendXPos += myIconWidth + myLegendSpacer;
412429
myPrintPainter.setPen( Qt::black );
413-
// check if the text will overflow the space we have
414430
int myMaximumLabelWidth = myLegendDimensionX - myLegendXPos;
415-
if (myLayerNameWidth > myMaximumLabelWidth)
416-
{
417-
int myRowCount = (myLayerNameWidth / myMaximumLabelWidth) + 1;
418-
QRect myLegendItemRect (myLegendXPos,
419-
myLegendYPos,
420-
myLayerNameWidth,
421-
myLegendFontHeight * myRowCount);
422-
QTextOption myTextOption( Qt::AlignCenter );
423-
myTextOption.setWrapMode (
424-
QTextOption::WrapAtWordBoundaryOrAnywhere );
425-
myPrintPainter.drawText(
426-
myLegendItemRect,
427-
myLayerName,
428-
myTextOption
429-
);
430-
myLegendYPos += myLegendVerticalSpacer + (myLegendFontHeight * myRowCount);
431-
}
432-
else //fits ok on a single line
431+
QStringList myWrappedLayerNameList = wordWrap(myLayerName,
432+
myLegendFontMetrics,
433+
myLegendDimensionX - myIconWidth);
434+
//
435+
// Loop through wrapped legend label lines
436+
//
437+
QStringListIterator myLineWrapIterator(myWrappedLayerNameList);
438+
while (myLineWrapIterator.hasNext())
433439
{
440+
QString myLine = myLineWrapIterator.next();
434441
QRect myLegendItemRect (myLegendXPos,
435442
myLegendYPos,
436-
myLayerNameWidth,
443+
myLegendDimensionX - myIconWidth,
437444
myLegendFontHeight);
438-
myPrintPainter.drawText( myLegendItemRect, Qt::AlignCenter, myLayerName );
445+
myPrintPainter.drawText( myLegendItemRect, Qt::AlignLeft, myLine );
439446
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
440447
}
441448
}
442449
else //class breaks
443450
{
444451
// draw in the layer name first, after we loop for the class breaks
445-
myLegendXPos = myIconWidth + myLegendSpacer;
446-
QRect myLegendItemRect (myLegendXPos,
447-
myLegendYPos,
448-
myLayerNameWidth,
449-
myLegendFontHeight);
450-
myPrintPainter.setPen( Qt::black );
451-
myPrintPainter.drawText( myLegendItemRect, Qt::AlignCenter, myLayerName );
452-
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
452+
QStringList myWrappedLayerNameList = wordWrap(myLayerName,
453+
myLegendFontMetrics,
454+
myLegendDimensionX - myIconWidth);
455+
//
456+
// Loop through wrapped legend label lines
457+
//
458+
QStringListIterator myLineWrapIterator(myWrappedLayerNameList);
459+
while (myLineWrapIterator.hasNext())
460+
{
461+
QString myLine = myLineWrapIterator.next();
462+
myLegendXPos = myIconWidth;
463+
QRect myLegendItemRect (myLegendXPos,
464+
myLegendYPos,
465+
myLegendFontMetrics.width(myLine),
466+
myLegendFontHeight);
467+
myPrintPainter.setPen( Qt::black );
468+
myPrintPainter.drawText( myLegendItemRect, Qt::AlignLeft, myLine );
469+
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
470+
}
471+
//
472+
// Lop through the class breaks
473+
//
453474
QListIterator<QgsSymbol *> myIterator ( mySymbolList );
454475
while ( myIterator.hasNext() )
455476
{
@@ -500,41 +521,26 @@ void QgsQuickPrint::printMap()
500521
myLegendXPos += myIconWidth + myLegendSpacer;
501522
int myLabelWidth = myLegendFontMetrics.width(myLabel);
502523
myPrintPainter.setPen( Qt::black );
503-
// check if the text will overflow the space we have
504-
int myMaximumLabelWidth = myLegendDimensionX - myLegendXPos;
505-
if (myLabelWidth > myMaximumLabelWidth)
506-
{
507-
int myRowCount = (myLabelWidth / myMaximumLabelWidth) + 1;
508-
QRect myLegendItemRect (myLegendXPos,
509-
myLegendYPos,
510-
myLabelWidth,
511-
myLegendFontHeight * myRowCount);
512-
QTextOption myTextOption( Qt::AlignLeft );
513-
myTextOption.setWrapMode (
514-
QTextOption::WordWrap);
515-
//QTextOption::WrapAtWordBoundaryOrAnywhere );
516-
myPrintPainter.drawText(
517-
myLegendItemRect,
518-
myLabel,
519-
myTextOption
520-
);
521-
/*
522-
myPrintPainter.drawText( myLegendItemRect,
523-
Qt::AlignLeft | Qt::TextWordWrap,
524-
myLabel );
525-
myPrintPainter.drawRect( myLegendItemRect ),
526-
*/
527-
myLegendYPos += myLegendVerticalSpacer + (myLegendFontHeight * myRowCount);
528-
}
529-
else //fits ok on a single line
524+
//
525+
526+
QStringList myWrappedLayerNameList = wordWrap(myLabel,
527+
myLegendFontMetrics,
528+
myLegendDimensionX - myIconWidth);
529+
//
530+
// Loop through wrapped legend label lines
531+
//
532+
QStringListIterator myLineWrapIterator(myWrappedLayerNameList);
533+
while (myLineWrapIterator.hasNext())
530534
{
535+
QString myLine = myLineWrapIterator.next();
536+
// check if the text will overflow the space we have
531537
QRect myLegendItemRect (myLegendXPos,
532538
myLegendYPos,
533-
myLabelWidth,
539+
myLegendDimensionX - myIconWidth,
534540
myLegendFontHeight);
535-
myPrintPainter.drawText( myLegendItemRect, Qt::AlignCenter, myLabel );
541+
myPrintPainter.drawText( myLegendItemRect, Qt::AlignLeft, myLine );
536542
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
537-
}
543+
} //wordwrap loop
538544
} //symbol loop
539545
} //class breaks
540546
} //if vectorlayer
@@ -615,7 +621,7 @@ void QgsQuickPrint::printMap()
615621
//
616622
// Draw the scale bar
617623
//
618-
renderPrintScaleBar(&myPrintPainter, mpMapCanvas, myLogoXDim);
624+
renderPrintScaleBar(&myPrintPainter, mpMapRender, myLogoXDim);
619625

620626
//
621627
// Finish up
@@ -643,7 +649,7 @@ void QgsQuickPrint::scaleTextLabels( int theScaleFactor, SymbolScalingType theDi
643649
QgsDebugMsg ("QgsQuickPrintGui::scaleTextLabels invalid scale factor");
644650
return;
645651
}
646-
QStringList myLayerSet = mpMapCanvas->mapRender()->layerSet();
652+
QStringList myLayerSet = mpMapRender->layerSet();
647653
QStringListIterator myLayerIterator ( myLayerSet );
648654
while ( myLayerIterator.hasNext() )
649655
{
@@ -683,7 +689,7 @@ void QgsQuickPrint::scalePointSymbols( int theScaleFactor, SymbolScalingType the
683689
QgsDebugMsg ("QgsQuickPrintGui::scalePointSymbolsForPrint invalid scale factor");
684690
return;
685691
}
686-
QStringList myLayerSet = mpMapCanvas->mapRender()->layerSet();
692+
QStringList myLayerSet = mpMapRender->layerSet();
687693
QStringListIterator myLayerIterator ( myLayerSet );
688694
while ( myLayerIterator.hasNext() )
689695
{
@@ -744,7 +750,7 @@ void QgsQuickPrint::scalePointSymbols( int theScaleFactor, SymbolScalingType the
744750

745751

746752
void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
747-
QgsMapCanvas * thepMapCanvas,
753+
QgsMapRender * thepMapRender,
748754
int theMaximumWidth)
749755
{
750756
//hard coding some options for now
@@ -758,7 +764,7 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
758764
int myTextOffsetY=30;
759765
int myXMargin=260;
760766
int myYMargin=180;
761-
int myCanvasWidth = thepMapCanvas->width();
767+
int myCanvasWidth = thepMapRender->width();
762768
int myPreferredSize = theMaximumWidth;
763769
double myActualSize=myPreferredSize;
764770
int myBufferSize=1; //softcode this later
@@ -771,10 +777,10 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
771777
//Get map units per pixel. This can be negative at times (to do with
772778
//projections) and that just confuses the rest of the code in this
773779
//function, so force to a positive number.
774-
double myMuppDouble = std::abs(thepMapCanvas->mupp());
780+
double myMuppDouble = std::abs(thepMapRender->mupp());
775781

776782
// Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze
777-
int myLayerCount=thepMapCanvas->layerCount();
783+
int myLayerCount=thepMapRender->layerSet().count();
778784
if (!myLayerCount || !myMuppDouble) return;
779785

780786

@@ -803,7 +809,7 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
803809
}
804810

805811
//Get type of map units and set scale bar unit label text
806-
QGis::units myMapUnits=thepMapCanvas->mapUnits();
812+
QGis::units myMapUnits=thepMapRender->mapUnits();
807813
QString myScaleBarUnitLabel;
808814
switch (myMapUnits)
809815
{
@@ -1104,3 +1110,46 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
11041110
myScaleBarUnitLabel
11051111
);
11061112
}
1113+
1114+
QStringList QgsQuickPrint::wordWrap(QString theString,
1115+
QFontMetrics theMetrics,
1116+
int theWidth)
1117+
{
1118+
//iterate the string
1119+
QStringList myList;
1120+
QString myCumulativeLine="";
1121+
QString myStringToPreviousSpace="";
1122+
int myPreviousSpacePos=0;
1123+
for (int i=0; i < theString.count(); ++i)
1124+
{
1125+
QChar myChar = theString.at(i);
1126+
if (myChar == QChar(' '))
1127+
{
1128+
myStringToPreviousSpace = myCumulativeLine;
1129+
myPreviousSpacePos=i;
1130+
}
1131+
myCumulativeLine += myChar;
1132+
if (theMetrics.width(myCumulativeLine) >= theWidth)
1133+
{
1134+
//time to wrap
1135+
//@todo deal with long strings that have no spaces
1136+
//forcing a break at current pos...
1137+
myList << myStringToPreviousSpace.trimmed();
1138+
i = myPreviousSpacePos;
1139+
myStringToPreviousSpace = "";
1140+
myCumulativeLine = "";
1141+
}
1142+
}//end of i loop
1143+
//add whatever is left in the string to the list
1144+
if (!myCumulativeLine.trimmed().isEmpty())
1145+
{
1146+
myList << myCumulativeLine.trimmed();
1147+
}
1148+
1149+
//qDebug("Wrapped legend entry:");
1150+
//qDebug(theString);
1151+
//qDebug(myList.join("\n").toLocal8Bit());
1152+
return myList;
1153+
1154+
}
1155+

‎src/gui/qgsquickprint.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
//QT4 includes
2323
#include <QObject>
24+
#include <QColor>
2425

2526
//QGIS includes
2627
#include <qgsmaprender.h>
@@ -51,11 +52,18 @@ public slots:
5152
void setLogo1(QString theFileName);
5253
void setLogo2(QString theFileName);
5354
void setOutputPdf(QString theFileName);
55+
//! This is just a convenience function to get the
56+
//map render from the mapcanvas
5457
void setMapCanvas(QgsMapCanvas * thepMapCanvas);
58+
void setMapRender(QgsMapRender * thepMapRender);
59+
void setMapBackgroundColor(QColor theColor);
5560
private:
5661
void renderPrintScaleBar(QPainter * thepPainter,
57-
QgsMapCanvas * thepMapCanvas,
62+
QgsMapRender * thepMapRender,
5863
int theMaximumWidth);
64+
QStringList wordWrap(QString theString,
65+
QFontMetrics theMetrics,
66+
int theWidth);
5967
/**
6068
* Scale symbols in all layers by the specified amount.
6169
* Typically used for printing. Each symbol in
@@ -89,14 +97,15 @@ public slots:
8997
*/
9098
void scaleTextLabels( int theScaleFactor, SymbolScalingType theDirection);
9199

92-
QgsMapCanvas * mpMapCanvas;
100+
QgsMapRender * mpMapRender;
93101
QString mTitleText;
94102
QString mNameText;
95103
QString mCopyrightText;
96104
QString mNorthArrowFile;
97105
QString mLogo1File;
98106
QString mLogo2File;
99107
QString mOutputFileName;
108+
QColor mMapBackgroundColour;
100109
};
101110

102111
#endif //QGSQUICKPRINT_H

0 commit comments

Comments
 (0)
Please sign in to comment.