@@ -95,7 +95,16 @@ void QgsQuickPrint::setOutputPdf(QString theFileName)
95
95
}
96
96
void QgsQuickPrint::setMapCanvas (QgsMapCanvas * thepMapCanvas)
97
97
{
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;
99
108
}
100
109
101
110
void QgsQuickPrint::printMap ()
@@ -104,7 +113,7 @@ void QgsQuickPrint::printMap()
104
113
{
105
114
return ;
106
115
}
107
- if ( mpMapCanvas == NULL )
116
+ if ( mpMapRender == NULL )
108
117
{
109
118
return ;
110
119
}
@@ -222,7 +231,6 @@ void QgsQuickPrint::printMap()
222
231
223
232
// Background colour for pixmaps
224
233
QColor myLegendBackgroundColour = Qt::white;
225
- QColor myMapBackgroundColour = mpMapCanvas->canvasColor ();
226
234
// QColor myMapBackgroundColour = "#98dbf9"; // nice blue colour
227
235
228
236
@@ -314,20 +322,20 @@ void QgsQuickPrint::printMap()
314
322
int myMapDimensionX = (myDrawableWidth / 100 ) * myMapHeightPercent;
315
323
int myMapDimensionY = (myDrawableHeight / 100 ) * myMapWidthPercent;
316
324
QPixmap myMapPixmap ( myMapDimensionX,myMapDimensionY );
317
- myMapPixmap.fill ( myMapBackgroundColour );
325
+ myMapPixmap.fill ( mMapBackgroundColour );
318
326
QPainter myMapPainter;
319
327
myMapPainter.begin ( &myMapPixmap );
320
- mpMapCanvas-> mapRender () ->setOutputSize (
328
+ mpMapRender ->setOutputSize (
321
329
QSize ( myMapDimensionX, myMapDimensionY ), myPrinter.resolution () );
322
330
scalePointSymbols (mySymbolScalingAmount, ScaleUp);
323
331
scaleTextLabels (mySymbolScalingAmount, ScaleUp);
324
- mpMapCanvas-> mapRender () ->render ( &myMapPainter );
332
+ mpMapRender ->render ( &myMapPainter );
325
333
// maprender has no accessor for output size so
326
334
// we couldnt store the size before starting the print render
327
335
// so that it can be restored properly so what follows here is a
328
336
// best guess approach
329
- mpMapCanvas-> mapRender () ->setOutputSize (
330
- QSize ( mpMapCanvas ->width (), mpMapCanvas ->height () ), myScreenResolutionDpi );
337
+ mpMapRender ->setOutputSize (
338
+ QSize ( mpMapRender ->width (), mpMapRender ->height () ), myScreenResolutionDpi );
331
339
myMapPainter.end ();
332
340
// draw the map pixmap onto our pdf print device
333
341
myOriginX = myPrinter.pageRect ().left () + myHorizontalSpacing;
@@ -344,6 +352,8 @@ void QgsQuickPrint::printMap()
344
352
// myPrintPainter.setFont(myLegendFont);
345
353
int myLegendDimensionX = (myDrawableWidth / 100 ) * myLegendWidthPercent;
346
354
int myLegendDimensionY = (myDrawableHeight / 100 ) * myLegendHeightPercent;
355
+
356
+
347
357
// Create a viewport to make coordinate conversions easier
348
358
// The viewport has the same dimensions as the page(otherwise items
349
359
// drawn into it will appear squashed), but a different origin.
@@ -353,15 +363,22 @@ void QgsQuickPrint::printMap()
353
363
myOriginY,
354
364
myOriginalViewport.width (),
355
365
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
356
373
QFontMetrics myLegendFontMetrics ( myLegendFont, &myPrinter );
357
374
int myLegendFontHeight = myLegendFontMetrics.height ();
358
375
int myLegendXPos = 0 ;
359
376
int myLegendYPos = 0 ;
360
- int myLegendSpacer = myLegendFontHeight; // for vertical and horizontal spacing
377
+ int myLegendSpacer = myLegendFontHeight/ 2 ; // for vertical and horizontal spacing
361
378
int myLegendVerticalSpacer = myLegendFontHeight/3 ; // for vertical between rows
362
379
int myIconWidth = myLegendFontHeight;
363
380
myPrintPainter.setFont ( myLegendFont );
364
- QStringList myLayerSet = mpMapCanvas-> mapRender () ->layerSet ();
381
+ QStringList myLayerSet = mpMapRender ->layerSet ();
365
382
QStringListIterator myLayerIterator ( myLayerSet );
366
383
while ( myLayerIterator.hasNext () )
367
384
{
@@ -389,7 +406,7 @@ void QgsQuickPrint::printMap()
389
406
QgsSymbol * mypSymbol = mySymbolList.at (0 );
390
407
myPrintPainter.setPen ( mypSymbol->pen () );
391
408
myPrintPainter.setBrush ( mypSymbol->brush () );
392
- myLegendXPos = myLegendSpacer ;
409
+ myLegendXPos = 0 ;
393
410
if ( mypSymbol->type () == QGis::Point )
394
411
{
395
412
QImage myImage;
@@ -410,46 +427,50 @@ void QgsQuickPrint::printMap()
410
427
}
411
428
myLegendXPos += myIconWidth + myLegendSpacer;
412
429
myPrintPainter.setPen ( Qt::black );
413
- // check if the text will overflow the space we have
414
430
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 ())
433
439
{
440
+ QString myLine = myLineWrapIterator.next ();
434
441
QRect myLegendItemRect (myLegendXPos,
435
442
myLegendYPos,
436
- myLayerNameWidth ,
443
+ myLegendDimensionX - myIconWidth ,
437
444
myLegendFontHeight);
438
- myPrintPainter.drawText ( myLegendItemRect, Qt::AlignCenter, myLayerName );
445
+ myPrintPainter.drawText ( myLegendItemRect, Qt::AlignLeft, myLine );
439
446
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
440
447
}
441
448
}
442
449
else // class breaks
443
450
{
444
451
// 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
+ //
453
474
QListIterator<QgsSymbol *> myIterator ( mySymbolList );
454
475
while ( myIterator.hasNext () )
455
476
{
@@ -500,41 +521,26 @@ void QgsQuickPrint::printMap()
500
521
myLegendXPos += myIconWidth + myLegendSpacer;
501
522
int myLabelWidth = myLegendFontMetrics.width (myLabel);
502
523
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 ())
530
534
{
535
+ QString myLine = myLineWrapIterator.next ();
536
+ // check if the text will overflow the space we have
531
537
QRect myLegendItemRect (myLegendXPos,
532
538
myLegendYPos,
533
- myLabelWidth ,
539
+ myLegendDimensionX - myIconWidth ,
534
540
myLegendFontHeight);
535
- myPrintPainter.drawText ( myLegendItemRect, Qt::AlignCenter, myLabel );
541
+ myPrintPainter.drawText ( myLegendItemRect, Qt::AlignLeft, myLine );
536
542
myLegendYPos += myLegendVerticalSpacer + myLegendFontHeight;
537
- }
543
+ } // wordwrap loop
538
544
} // symbol loop
539
545
} // class breaks
540
546
} // if vectorlayer
@@ -615,7 +621,7 @@ void QgsQuickPrint::printMap()
615
621
//
616
622
// Draw the scale bar
617
623
//
618
- renderPrintScaleBar (&myPrintPainter, mpMapCanvas , myLogoXDim);
624
+ renderPrintScaleBar (&myPrintPainter, mpMapRender , myLogoXDim);
619
625
620
626
//
621
627
// Finish up
@@ -643,7 +649,7 @@ void QgsQuickPrint::scaleTextLabels( int theScaleFactor, SymbolScalingType theDi
643
649
QgsDebugMsg (" QgsQuickPrintGui::scaleTextLabels invalid scale factor" );
644
650
return ;
645
651
}
646
- QStringList myLayerSet = mpMapCanvas-> mapRender () ->layerSet ();
652
+ QStringList myLayerSet = mpMapRender ->layerSet ();
647
653
QStringListIterator myLayerIterator ( myLayerSet );
648
654
while ( myLayerIterator.hasNext () )
649
655
{
@@ -683,7 +689,7 @@ void QgsQuickPrint::scalePointSymbols( int theScaleFactor, SymbolScalingType the
683
689
QgsDebugMsg (" QgsQuickPrintGui::scalePointSymbolsForPrint invalid scale factor" );
684
690
return ;
685
691
}
686
- QStringList myLayerSet = mpMapCanvas-> mapRender () ->layerSet ();
692
+ QStringList myLayerSet = mpMapRender ->layerSet ();
687
693
QStringListIterator myLayerIterator ( myLayerSet );
688
694
while ( myLayerIterator.hasNext () )
689
695
{
@@ -744,7 +750,7 @@ void QgsQuickPrint::scalePointSymbols( int theScaleFactor, SymbolScalingType the
744
750
745
751
746
752
void QgsQuickPrint::renderPrintScaleBar (QPainter * thepPainter,
747
- QgsMapCanvas * thepMapCanvas ,
753
+ QgsMapRender * thepMapRender ,
748
754
int theMaximumWidth)
749
755
{
750
756
// hard coding some options for now
@@ -758,7 +764,7 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
758
764
int myTextOffsetY=30 ;
759
765
int myXMargin=260 ;
760
766
int myYMargin=180 ;
761
- int myCanvasWidth = thepMapCanvas ->width ();
767
+ int myCanvasWidth = thepMapRender ->width ();
762
768
int myPreferredSize = theMaximumWidth;
763
769
double myActualSize=myPreferredSize;
764
770
int myBufferSize=1 ; // softcode this later
@@ -771,10 +777,10 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
771
777
// Get map units per pixel. This can be negative at times (to do with
772
778
// projections) and that just confuses the rest of the code in this
773
779
// function, so force to a positive number.
774
- double myMuppDouble = std::abs (thepMapCanvas ->mupp ());
780
+ double myMuppDouble = std::abs (thepMapRender ->mupp ());
775
781
776
782
// 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 ();
778
784
if (!myLayerCount || !myMuppDouble) return ;
779
785
780
786
@@ -803,7 +809,7 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
803
809
}
804
810
805
811
// Get type of map units and set scale bar unit label text
806
- QGis::units myMapUnits=thepMapCanvas ->mapUnits ();
812
+ QGis::units myMapUnits=thepMapRender ->mapUnits ();
807
813
QString myScaleBarUnitLabel;
808
814
switch (myMapUnits)
809
815
{
@@ -1104,3 +1110,46 @@ void QgsQuickPrint::renderPrintScaleBar(QPainter * thepPainter,
1104
1110
myScaleBarUnitLabel
1105
1111
);
1106
1112
}
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
+
0 commit comments