4646#include < QString>
4747#include < QSettings>
4848#include < QSvgRenderer>
49- #include < QPrinter>
5049
5150// other includes
5251#include < cmath>
5756
5857QgsQuickPrint::QgsQuickPrint ()
5958{
59+ mPageSize = QPrinter::A4;
6060}
6161
6262QgsQuickPrint::~QgsQuickPrint ()
@@ -106,6 +106,10 @@ void QgsQuickPrint::setMapBackgroundColor(QColor theColor)
106106{
107107 mMapBackgroundColour = theColor;
108108}
109+ void QgsQuickPrint::setPageSize (QPrinter::PageSize theSize)
110+ {
111+ mPageSize = theSize;
112+ }
109113
110114void QgsQuickPrint::printMap ()
111115{
@@ -123,18 +127,21 @@ void QgsQuickPrint::printMap()
123127 mOutputFileName += " .pdf" ;
124128 }
125129
126- QPrinter myPrinter ( QPrinter::HighResolution ); // 1200dpi for ps ( & pdf I think )
127- myPrinter.setPageSize ( QPrinter::Letter );
128- // myPrinter.setPageSize ( QPrinter::A4 );
130+ // Initialising the printer this way lets us find out what
131+ // the screen resolution is which we store and then
132+ // reset the resolution of the printer after that...
133+ QPrinter myPrinter ( QPrinter::ScreenResolution );
134+ int myScreenResolutionDpi = myPrinter.resolution (); // try to get programmatically
129135 //
130136 // Try to force the printer resolution to 300dpi
131137 // to get past platform specific defaults in printer
132138 // resolution...
133139 //
134140 int myPrintResolutionDpi = 300 ;
135- int myScreenResolutionDpi = 72 ; // try to get programmatically
136141 myPrinter.setResolution ( myPrintResolutionDpi );
137142 myPrinter.setOutputFormat ( QPrinter::PdfFormat );
143+ qDebug ( " Printing to page size" + pageSizeToString (mPageSize ).toLocal8Bit ());
144+ myPrinter.setPageSize ( mPageSize );
138145 myPrinter.setOutputFileName ( mOutputFileName );
139146 myPrinter.setOrientation ( QPrinter::Landscape );
140147 myPrinter.setDocName ( " quickprint Report" );
@@ -621,7 +628,13 @@ void QgsQuickPrint::printMap()
621628 //
622629 // Draw the scale bar
623630 //
631+ myOriginY += myLogoYDim/2 ;
632+ myPrintPainter.setViewport (myOriginX,
633+ myOriginY,
634+ myOriginalViewport.width (),
635+ myOriginalViewport.height ());
624636 renderPrintScaleBar (&myPrintPainter, mpMapRender, myLogoXDim);
637+ myPrintPainter.setViewport (myOriginalViewport);
625638
626639 //
627640 // Finish up
@@ -754,19 +767,16 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
754767 int theMaximumWidth)
755768{
756769 // hard coding some options for now
757- int myStyleIndex = 1 ; // tick up
758770 bool mySnappingFlag = true ;
759771 QColor mColour = Qt::black;
760- int myPlacementIndex = 3 ; // br
761772 // Hard coded sizes
762- int myMajorTickSize=20 ;
763- int myTextOffsetX=30 ;
764- int myTextOffsetY=30 ;
765- int myXMargin=260 ;
766- int myYMargin=180 ;
767- int myCanvasWidth = thepMapRender->width ();
768- int myPreferredSize = theMaximumWidth;
769- double myActualSize=myPreferredSize;
773+ int myMajorTickSize=10 ;
774+ int myTextOffsetX=0 ;
775+ int myTextOffsetY=5 ;
776+ int myXMargin=20 ;
777+ int myYMargin=20 ;
778+ int myPreferredSize = theMaximumWidth - (myXMargin *2 );
779+ double myActualSize = 0 ;
770780 int myBufferSize=1 ; // softcode this later
771781 QColor myBackColor = Qt::white; // used for text
772782 QColor myForeColor = Qt::black; // used for text
@@ -778,24 +788,16 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
778788 // projections) and that just confuses the rest of the code in this
779789 // function, so force to a positive number.
780790 double myMuppDouble = std::abs (thepMapRender->mupp ());
781-
791+ //
782792 // Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze
783793 int myLayerCount=thepMapRender->layerSet ().count ();
784794 if (!myLayerCount || !myMuppDouble) return ;
785795
786-
787796 // Calculate size of scale bar for preferred number of map units
788- double myScaleBarWidth = myPreferredSize / myMuppDouble;
789-
790- // If scale bar is very small reset to 1/4 of the canvas wide
791- if (myScaleBarWidth < 30 )
792- {
793- myScaleBarWidth = myCanvasWidth / 4 ; // pixels
794- myActualSize = myScaleBarWidth * myMuppDouble; // map units
795- };
796-
797+ double myScaleBarWidth = myPreferredSize;
797798 myActualSize = myScaleBarWidth * myMuppDouble;
798799
800+
799801 // Work out the exponent for the number - e.g, 1234 will give 3,
800802 // and .001234 will give -3
801803 double myPowerOf10 = floor (log10 (myActualSize));
@@ -884,32 +886,9 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
884886 // Calculate total width of scale bar and label
885887 double myTotalScaleBarWidth = myScaleBarWidth + myFontWidth;
886888
887- // determine the origin of scale bar depending on placement selected
889+ // determine the origin of scale bar (bottom right)
888890 int myOriginX=myXMargin;
889891 int myOriginY=myYMargin;
890- switch (myPlacementIndex)
891- {
892- case 0 : // Bottom Left
893- myOriginX = myXMargin;
894- myOriginY = thepPainter->device ()->height () - myYMargin + myTextOffsetY;
895- break ;
896- case 1 : // Top Left
897- myOriginX = myXMargin;
898- myOriginY = myYMargin;
899- break ;
900- case 2 : // Top Right
901- myOriginX = thepPainter->device ()->width () - ((int ) myTotalScaleBarWidth) - myXMargin;
902- myOriginY = myXMargin;
903- break ;
904- case 3 : // Bottom Right
905- myOriginX = thepPainter->device ()->width () -
906- ((int ) myTotalScaleBarWidth) -
907- (myXMargin * 2 );
908- myOriginY = thepPainter->device ()->height () - myYMargin;
909- break ;
910- default :
911- QgsDebugMsg ( " Unable to determine where to put scale bar so defaulting to top left" );
912- }
913892
914893 // Set pen to draw with
915894 QPen myForegroundPen ( mColour , 2 );
@@ -918,120 +897,15 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
918897 // Cast myScaleBarWidth to int for drawing
919898 int myScaleBarWidthInt = (int ) myScaleBarWidth;
920899
921- // Create array of vertices for scale bar depending on style
922- switch (myStyleIndex)
923- {
924- case 0 : // Tick Down
925- {
926- QPolygon myTickDownArray (4 );
927- // draw a buffer first so bar shows up on dark images
928- thepPainter->setPen ( myBackgroundPen );
929- myTickDownArray.putPoints (0 ,4 ,
930- myOriginX , (myOriginY + myMajorTickSize) ,
931- myOriginX , myOriginY ,
932- (myScaleBarWidthInt + myOriginX), myOriginY ,
933- (myScaleBarWidthInt + myOriginX), (myOriginY + myMajorTickSize)
934- );
935- thepPainter->drawPolyline (myTickDownArray);
936- // now draw the bar itself in user selected color
937- thepPainter->setPen ( myForegroundPen );
938- myTickDownArray.putPoints (0 ,4 ,
939- myOriginX , (myOriginY + myMajorTickSize) ,
940- myOriginX , myOriginY ,
941- (myScaleBarWidthInt + myOriginX), myOriginY ,
942- (myScaleBarWidthInt + myOriginX), (myOriginY + myMajorTickSize)
943- );
944- thepPainter->drawPolyline (myTickDownArray);
945- break ;
946- }
947- case 1 : // tick up
948- {
949- QPolygon myTickUpArray (4 );
950- // draw a buffer first so bar shows up on dark images
951- thepPainter->setPen ( myBackgroundPen );
952- myTickUpArray.putPoints (0 ,4 ,
953- myOriginX , myOriginY ,
954- myOriginX , myOriginY + myMajorTickSize ,
955- (myScaleBarWidthInt + myOriginX), myOriginY + myMajorTickSize ,
956- (myScaleBarWidthInt + myOriginX), myOriginY
957- );
958- thepPainter->drawPolyline (myTickUpArray);
959- // now draw the bar itself in user selected color
960- thepPainter->setPen ( myForegroundPen );
961- myTickUpArray.putPoints (0 ,4 ,
962- myOriginX , myOriginY ,
963- myOriginX , myOriginY + myMajorTickSize ,
964- (myScaleBarWidthInt + myOriginX), myOriginY + myMajorTickSize ,
965- (myScaleBarWidthInt + myOriginX), myOriginY
966- );
967- thepPainter->drawPolyline (myTickUpArray);
968- break ;
969- }
970- case 2 : // Bar
971- {
972- QPolygon myBarArray (2 );
973- // draw a buffer first so bar shows up on dark images
974- thepPainter->setPen ( myBackgroundPen );
975- myBarArray.putPoints (0 ,2 ,
976- myOriginX,
977- (myOriginY + (myMajorTickSize/2 )),
978- (myScaleBarWidthInt + myOriginX), (myOriginY + (myMajorTickSize/2 ))
979- );
980- thepPainter->drawPolyline (myBarArray);
981- // now draw the bar itself in user selected color
982- thepPainter->setPen ( myForegroundPen );
983- myBarArray.putPoints (0 ,2 ,
984- myOriginX , (myOriginY + (myMajorTickSize/2 )),
985- (myScaleBarWidthInt + myOriginX), (myOriginY + (myMajorTickSize/2 ))
986- );
987- thepPainter->drawPolyline (myBarArray);
988- break ;
989- }
990- case 3 : // box
991- {
992- // Want square corners for a box
993- myBackgroundPen.setJoinStyle ( Qt::MiterJoin );
994- myForegroundPen.setJoinStyle ( Qt::MiterJoin );
995- QPolygon myBoxArray (5 );
996- // draw a buffer first so bar shows up on dark images
997- thepPainter->setPen ( myBackgroundPen );
998- myBoxArray.putPoints (0 ,5 ,
999- myOriginX , myOriginY,
1000- (myScaleBarWidthInt + myOriginX), myOriginY,
1001- (myScaleBarWidthInt + myOriginX), (myOriginY+myMajorTickSize),
1002- myOriginX , (myOriginY+myMajorTickSize),
1003- myOriginX , myOriginY
1004- );
1005- thepPainter->drawPolyline (myBoxArray);
1006- // now draw the bar itself in user selected color
1007- thepPainter->setPen ( myForegroundPen );
1008- thepPainter->setBrush ( QBrush ( mColour , Qt::SolidPattern) );
1009- int midPointX = myScaleBarWidthInt/2 + myOriginX;
1010- myBoxArray.putPoints (0 ,5 ,
1011- myOriginX , myOriginY,
1012- midPointX, myOriginY,
1013- midPointX, (myOriginY+myMajorTickSize),
1014- myOriginX , (myOriginY+myMajorTickSize),
1015- myOriginX , myOriginY
1016- );
1017- thepPainter->drawPolygon (myBoxArray);
1018-
1019- thepPainter->setBrush ( Qt::NoBrush );
1020- myBoxArray.putPoints (0 ,5 ,
1021- midPointX , myOriginY,
1022- (myScaleBarWidthInt + myOriginX), myOriginY,
1023- (myScaleBarWidthInt + myOriginX), (myOriginY+myMajorTickSize),
1024- midPointX , (myOriginY+myMajorTickSize),
1025- midPointX , myOriginY
1026- );
1027- thepPainter->drawPolygon (myBoxArray);
1028- break ;
1029- }
1030- default :
1031- std::cerr << " Unknown style\n " ;
1032- }
1033-
1034- // Do actual drawing of scale bar
900+ // now draw the bar itself in user selected color
901+ // thepPainter->setPen( myBackgroundPen );
902+ thepPainter->setPen ( myForegroundPen );
903+ thepPainter->drawRect (
904+ myOriginX,
905+ myOriginY,
906+ myOriginX + myScaleBarWidthInt,
907+ myOriginY + myMajorTickSize
908+ );
1035909
1036910 //
1037911 // Do drawing of scale bar text
@@ -1098,15 +972,15 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
1098972 for (int j = 0 -myBufferSize; j <= myBufferSize; j++)
1099973 {
1100974 thepPainter->drawText ( i + (myOriginX+myScaleBarWidthInt+myTextOffsetX),
1101- j + ( myOriginY+ myMajorTickSize) + myTextOffsetY,
975+ j + myOriginY + myMajorTickSize + myFontHeight + myTextOffsetY,
1102976 myScaleBarUnitLabel);
1103977 }
1104978 }
1105979 // then the text itself
1106980 thepPainter->setPen ( myForeColor );
1107981 thepPainter->drawText (
1108982 myOriginX + myScaleBarWidthInt + myTextOffsetX,
1109- myOriginY + myMajorTickSize + myTextOffsetY,
983+ myOriginY + myMajorTickSize + myFontHeight + myTextOffsetY,
1110984 myScaleBarUnitLabel
1111985 );
1112986}
@@ -1151,5 +1025,75 @@ QStringList QgsQuickPrint::wordWrap(QString theString,
11511025 // qDebug(myList.join("\n").toLocal8Bit());
11521026 return myList;
11531027
1028+ }
1029+ QString QgsQuickPrint::pageSizeToString (QPrinter::PageSize theSize)
1030+ {
1031+ if (theSize==QPrinter::A0) return " QPrinter::A0" ;
1032+ if (theSize==QPrinter::A1) return " QPrinter::A1" ;
1033+ if (theSize==QPrinter::A2) return " QPrinter::A2" ;
1034+ if (theSize==QPrinter::A3) return " QPrinter::A3" ;
1035+ if (theSize==QPrinter::A4) return " QPrinter::A4" ;
1036+ if (theSize==QPrinter::A5) return " QPrinter::A5" ;
1037+ if (theSize==QPrinter::A6) return " QPrinter::A6" ;
1038+ if (theSize==QPrinter::A7) return " QPrinter::A7" ;
1039+ if (theSize==QPrinter::A8) return " QPrinter::A8" ;
1040+ if (theSize==QPrinter::A9) return " QPrinter::A9" ;
1041+ if (theSize==QPrinter::B0) return " QPrinter::B0" ;
1042+ if (theSize==QPrinter::B1) return " QPrinter::B1" ;
1043+ if (theSize==QPrinter::B10) return " QPrinter::B10" ;
1044+ if (theSize==QPrinter::B2) return " QPrinter::B2" ;
1045+ if (theSize==QPrinter::B3) return " QPrinter::B3" ;
1046+ if (theSize==QPrinter::B4) return " QPrinter::B4" ;
1047+ if (theSize==QPrinter::B5) return " QPrinter::B5" ;
1048+ if (theSize==QPrinter::B6) return " QPrinter::B6" ;
1049+ if (theSize==QPrinter::B7) return " QPrinter::B7" ;
1050+ if (theSize==QPrinter::B8) return " QPrinter::B8" ;
1051+ if (theSize==QPrinter::B9) return " QPrinter::B9" ;
1052+ if (theSize==QPrinter::C5E) return " QPrinter::C5E" ;
1053+ if (theSize==QPrinter::Comm10E) return " QPrinter::Comm10E" ;
1054+ if (theSize==QPrinter::DLE) return " QPrinter::DLE" ;
1055+ if (theSize==QPrinter::Executive) return " QPrinter::Executive" ;
1056+ if (theSize==QPrinter::Folio) return " QPrinter::Folio" ;
1057+ if (theSize==QPrinter::Ledger) return " QPrinter::Ledger" ;
1058+ if (theSize==QPrinter::Legal) return " QPrinter::Legal" ;
1059+ if (theSize==QPrinter::Letter) return " QPrinter::Letter" ;
1060+ // falback
1061+ return " QPrinter::A4" ;
1062+
11541063}
11551064
1065+ QPrinter::PageSize QgsQuickPrint::stringToPageSize (QString theSize)
1066+ {
1067+ if (theSize==" QPrinter::A0" ) return QPrinter::A0;
1068+ if (theSize==" QPrinter::A1" ) return QPrinter::A1;
1069+ if (theSize==" QPrinter::A2" ) return QPrinter::A2;
1070+ if (theSize==" QPrinter::A3" ) return QPrinter::A3;
1071+ if (theSize==" QPrinter::A4" ) return QPrinter::A4;
1072+ if (theSize==" QPrinter::A5" ) return QPrinter::A5;
1073+ if (theSize==" QPrinter::A6" ) return QPrinter::A6;
1074+ if (theSize==" QPrinter::A7" ) return QPrinter::A7;
1075+ if (theSize==" QPrinter::A8" ) return QPrinter::A8;
1076+ if (theSize==" QPrinter::A9" ) return QPrinter::A9;
1077+ if (theSize==" QPrinter::B0" ) return QPrinter::B0;
1078+ if (theSize==" QPrinter::B1" ) return QPrinter::B1;
1079+ if (theSize==" QPrinter::B10" ) return QPrinter::B10;
1080+ if (theSize==" QPrinter::B2" ) return QPrinter::B2;
1081+ if (theSize==" QPrinter::B3" ) return QPrinter::B3;
1082+ if (theSize==" QPrinter::B4" ) return QPrinter::B4;
1083+ if (theSize==" QPrinter::B5" ) return QPrinter::B5;
1084+ if (theSize==" QPrinter::B6" ) return QPrinter::B6;
1085+ if (theSize==" QPrinter::B7" ) return QPrinter::B7;
1086+ if (theSize==" QPrinter::B8" ) return QPrinter::B8;
1087+ if (theSize==" QPrinter::B9" ) return QPrinter::B9;
1088+ if (theSize==" QPrinter::C5E" ) return QPrinter::C5E;
1089+ if (theSize==" QPrinter::Comm10E" ) return QPrinter::Comm10E;
1090+ if (theSize==" QPrinter::DLE" ) return QPrinter::DLE;
1091+ if (theSize==" QPrinter::Executive" ) return QPrinter::Executive;
1092+ if (theSize==" QPrinter::Folio" ) return QPrinter::Folio;
1093+ if (theSize==" QPrinter::Ledger" ) return QPrinter::Ledger;
1094+ if (theSize==" QPrinter::Legal" ) return QPrinter::Legal;
1095+ if (theSize==" QPrinter::Letter" ) return QPrinter::Letter;
1096+ // falback
1097+ return QPrinter::A4;
1098+
1099+ }
0 commit comments