46
46
#include < QString>
47
47
#include < QSettings>
48
48
#include < QSvgRenderer>
49
- #include < QPrinter>
50
49
51
50
// other includes
52
51
#include < cmath>
57
56
58
57
QgsQuickPrint::QgsQuickPrint ()
59
58
{
59
+ mPageSize = QPrinter::A4;
60
60
}
61
61
62
62
QgsQuickPrint::~QgsQuickPrint ()
@@ -106,6 +106,10 @@ void QgsQuickPrint::setMapBackgroundColor(QColor theColor)
106
106
{
107
107
mMapBackgroundColour = theColor;
108
108
}
109
+ void QgsQuickPrint::setPageSize (QPrinter::PageSize theSize)
110
+ {
111
+ mPageSize = theSize;
112
+ }
109
113
110
114
void QgsQuickPrint::printMap ()
111
115
{
@@ -123,18 +127,21 @@ void QgsQuickPrint::printMap()
123
127
mOutputFileName += " .pdf" ;
124
128
}
125
129
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
129
135
//
130
136
// Try to force the printer resolution to 300dpi
131
137
// to get past platform specific defaults in printer
132
138
// resolution...
133
139
//
134
140
int myPrintResolutionDpi = 300 ;
135
- int myScreenResolutionDpi = 72 ; // try to get programmatically
136
141
myPrinter.setResolution ( myPrintResolutionDpi );
137
142
myPrinter.setOutputFormat ( QPrinter::PdfFormat );
143
+ qDebug ( " Printing to page size" + pageSizeToString (mPageSize ).toLocal8Bit ());
144
+ myPrinter.setPageSize ( mPageSize );
138
145
myPrinter.setOutputFileName ( mOutputFileName );
139
146
myPrinter.setOrientation ( QPrinter::Landscape );
140
147
myPrinter.setDocName ( " quickprint Report" );
@@ -621,7 +628,13 @@ void QgsQuickPrint::printMap()
621
628
//
622
629
// Draw the scale bar
623
630
//
631
+ myOriginY += myLogoYDim/2 ;
632
+ myPrintPainter.setViewport (myOriginX,
633
+ myOriginY,
634
+ myOriginalViewport.width (),
635
+ myOriginalViewport.height ());
624
636
renderPrintScaleBar (&myPrintPainter, mpMapRender, myLogoXDim);
637
+ myPrintPainter.setViewport (myOriginalViewport);
625
638
626
639
//
627
640
// Finish up
@@ -754,19 +767,16 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
754
767
int theMaximumWidth)
755
768
{
756
769
// hard coding some options for now
757
- int myStyleIndex = 1 ; // tick up
758
770
bool mySnappingFlag = true ;
759
771
QColor mColour = Qt::black;
760
- int myPlacementIndex = 3 ; // br
761
772
// 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 ;
770
780
int myBufferSize=1 ; // softcode this later
771
781
QColor myBackColor = Qt::white; // used for text
772
782
QColor myForeColor = Qt::black; // used for text
@@ -778,24 +788,16 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
778
788
// projections) and that just confuses the rest of the code in this
779
789
// function, so force to a positive number.
780
790
double myMuppDouble = std::abs (thepMapRender->mupp ());
781
-
791
+ //
782
792
// Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze
783
793
int myLayerCount=thepMapRender->layerSet ().count ();
784
794
if (!myLayerCount || !myMuppDouble) return ;
785
795
786
-
787
796
// 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;
797
798
myActualSize = myScaleBarWidth * myMuppDouble;
798
799
800
+
799
801
// Work out the exponent for the number - e.g, 1234 will give 3,
800
802
// and .001234 will give -3
801
803
double myPowerOf10 = floor (log10 (myActualSize));
@@ -884,32 +886,9 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
884
886
// Calculate total width of scale bar and label
885
887
double myTotalScaleBarWidth = myScaleBarWidth + myFontWidth;
886
888
887
- // determine the origin of scale bar depending on placement selected
889
+ // determine the origin of scale bar (bottom right)
888
890
int myOriginX=myXMargin;
889
891
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
- }
913
892
914
893
// Set pen to draw with
915
894
QPen myForegroundPen ( mColour , 2 );
@@ -918,120 +897,15 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
918
897
// Cast myScaleBarWidth to int for drawing
919
898
int myScaleBarWidthInt = (int ) myScaleBarWidth;
920
899
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
+ );
1035
909
1036
910
//
1037
911
// Do drawing of scale bar text
@@ -1098,15 +972,15 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
1098
972
for (int j = 0 -myBufferSize; j <= myBufferSize; j++)
1099
973
{
1100
974
thepPainter->drawText ( i + (myOriginX+myScaleBarWidthInt+myTextOffsetX),
1101
- j + ( myOriginY+ myMajorTickSize) + myTextOffsetY,
975
+ j + myOriginY + myMajorTickSize + myFontHeight + myTextOffsetY,
1102
976
myScaleBarUnitLabel);
1103
977
}
1104
978
}
1105
979
// then the text itself
1106
980
thepPainter->setPen ( myForeColor );
1107
981
thepPainter->drawText (
1108
982
myOriginX + myScaleBarWidthInt + myTextOffsetX,
1109
- myOriginY + myMajorTickSize + myTextOffsetY,
983
+ myOriginY + myMajorTickSize + myFontHeight + myTextOffsetY,
1110
984
myScaleBarUnitLabel
1111
985
);
1112
986
}
@@ -1151,5 +1025,75 @@ QStringList QgsQuickPrint::wordWrap(QString theString,
1151
1025
// qDebug(myList.join("\n").toLocal8Bit());
1152
1026
return myList;
1153
1027
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
+
1154
1063
}
1155
1064
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