72
72
#include < qwt_plot_picker.h>
73
73
#include < qwt_picker_machine.h>
74
74
#include < qwt_plot_zoomer.h>
75
+ #include < qwt_plot_layout.h>
76
+ #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
77
+ #include < qwt_plot_renderer.h>
78
+ #endif
75
79
76
80
QgsRasterLayerProperties::QgsRasterLayerProperties ( QgsMapLayer* lyr, QgsMapCanvas* theCanvas, QWidget *parent, Qt::WFlags fl )
77
81
: QDialog( parent, fl ),
@@ -1323,7 +1327,9 @@ void QgsRasterLayerProperties::refreshHistogram()
1323
1327
return ;
1324
1328
}
1325
1329
1326
- #if !defined(QWT_VERSION) || QWT_VERSION<0x060000
1330
+ #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
1331
+ mpPlot->detachItems ();
1332
+ #else
1327
1333
mpPlot->clear ();
1328
1334
#endif
1329
1335
// ensure all children get removed
@@ -1334,6 +1340,7 @@ void QgsRasterLayerProperties::refreshHistogram()
1334
1340
mpPlot->setAxisTitle ( QwtPlot::xBottom, QObject::tr ( " Pixel Value" ) );
1335
1341
mpPlot->setAxisTitle ( QwtPlot::yLeft, QObject::tr ( " Frequency" ) );
1336
1342
mpPlot->setAxisAutoScale ( QwtPlot::yLeft );
1343
+
1337
1344
// x axis scale only set after computing global min/max across bands (see below)
1338
1345
// add a grid
1339
1346
QwtPlotGrid * myGrid = new QwtPlotGrid ();
@@ -1523,24 +1530,25 @@ void QgsRasterLayerProperties::refreshHistogram()
1523
1530
if ( ! mHistoPicker )
1524
1531
{
1525
1532
mHistoPicker = new QwtPlotPicker ( mpPlot->canvas () );
1526
- #if (QWT_VERSION>=0x060000)
1527
- mHistoPicker ->setStateMachine (new QwtPickerDragPointMachine);
1528
- #else
1529
- mHistoPicker ->setSelectionFlags ( QwtPicker::PointSelection | QwtPicker::DragSelection );
1530
- #endif
1531
1533
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
1532
1534
mHistoPicker ->setTrackerMode ( QwtPicker::AlwaysOff );
1533
1535
mHistoPicker ->setRubberBand ( QwtPicker::VLineRubberBand );
1534
1536
mHistoPicker ->setEnabled ( false );
1535
- connect ( mHistoPicker , SIGNAL ( selected ( const QwtDoublePoint & ) ), this , SLOT ( histoPickerSelected ( const QwtDoublePoint & ) ) );
1537
+ #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
1538
+ mHistoPicker ->setStateMachine ( new QwtPickerDragPointMachine );
1539
+ connect ( mHistoPicker , SIGNAL ( selected ( const QPointF & ) ), this , SLOT ( histoPickerSelected ( const QPointF & ) ) );
1540
+ #else
1541
+ mHistoPicker ->setSelectionFlags ( QwtPicker::PointSelection | QwtPicker::DragSelection );
1542
+ connect ( mHistoPicker , SIGNAL ( selected ( const QwtDoublePoint & ) ), this , SLOT ( histoPickerSelectedQwt5 ( const QwtDoublePoint & ) ) );
1543
+ #endif
1536
1544
}
1537
1545
1538
1546
// plot zoomer
1539
1547
if ( ! mHistoZoomer )
1540
1548
{
1541
1549
mHistoZoomer = new QwtPlotZoomer ( mpPlot->canvas () );
1542
- #if (QWT_VERSION>=0x060000)
1543
- mHistoZoomer ->setStateMachine (new QwtPickerDragRectMachine);
1550
+ #if defined (QWT_VERSION) && QWT_VERSION >=0x060000
1551
+ mHistoZoomer ->setStateMachine ( new QwtPickerDragRectMachine );
1544
1552
#else
1545
1553
mHistoZoomer ->setSelectionFlags ( QwtPicker::RectSelection | QwtPicker::DragSelection );
1546
1554
#endif
@@ -1563,29 +1571,64 @@ void QgsRasterLayerProperties::on_mSaveAsImageButton_clicked()
1563
1571
return ;
1564
1572
}
1565
1573
1574
+ QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName ( this , tr ( " Choose a file name to save the map image as" ) );
1575
+ QFileInfo myInfo ( myFileNameAndFilter.first );
1576
+ if ( QFileInfo ( myFileNameAndFilter.first ).baseName () != " " )
1577
+ {
1578
+ histoSaveAsImage ( myFileNameAndFilter.first );
1579
+ }
1580
+ }
1581
+
1582
+ void QgsRasterLayerProperties::histoSaveAsImage ( const QString& theFilename )
1583
+ {
1584
+ // make sure dir. exists
1585
+ QDir myDir ( QFileInfo ( theFilename ).dir () );
1586
+ if ( ! myDir.exists () )
1587
+ {
1588
+ QgsDebugMsg ( QString ( " Error, directory %1 non-existent (theFilename = %2)" ).arg ( myDir.absolutePath () ).arg ( theFilename ) );
1589
+ return ;
1590
+ }
1591
+
1592
+ // prepare the pixmap
1566
1593
QPixmap myPixmap ( 600 , 600 );
1594
+ QRect myQRect ( 5 , 5 , 590 , 590 ); // leave a 5px border on all sides
1567
1595
myPixmap.fill ( Qt::white ); // Qt::transparent ?
1568
1596
1569
- #if (QWT_VERSION<0x060000)
1597
+ #if defined(QWT_VERSION) && QWT_VERSION>=0x060000
1598
+ QwtPlotRenderer myRenderer;
1599
+ myRenderer.setDiscardFlags ( QwtPlotRenderer::DiscardBackground |
1600
+ QwtPlotRenderer::DiscardCanvasBackground );
1601
+ myRenderer.setLayoutFlags ( QwtPlotRenderer::FrameWithScales );
1602
+
1603
+ QPainter myPainter;
1604
+ myPainter.begin ( &myPixmap );
1605
+ myRenderer.render ( mpPlot, &myPainter, myQRect );
1606
+ myPainter.end ();
1607
+ #else
1570
1608
QwtPlotPrintFilter myFilter;
1571
1609
int myOptions = QwtPlotPrintFilter::PrintAll;
1572
1610
myOptions &= ~QwtPlotPrintFilter::PrintBackground;
1573
1611
myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
1574
1612
myFilter.setOptions ( myOptions );
1575
1613
1576
- mpPlot->print ( myPixmap, myFilter );
1577
- #else
1578
- QPainter painter;
1579
- painter.begin ( &myPixmap );
1580
- mpPlot->drawCanvas ( &painter );
1581
- painter.end ();
1614
+ QPainter myPainter;
1615
+ myPainter.begin ( &myPixmap );
1616
+ mpPlot->print ( &myPainter, myQRect, myFilter );
1617
+ myPainter.end ();
1618
+
1619
+ // "fix" for bug in qwt5 - legend and plot shifts a bit
1620
+ // can't see how to avoid this without picking qwt5 apart...
1621
+ refreshHistogram ();
1622
+ refreshHistogram ();
1623
+ #endif
1624
+
1625
+ // save pixmap to file
1626
+ myPixmap.save ( theFilename );
1627
+
1628
+ #if defined(QWT_VERSION) && QWT_VERSION<0x060000
1582
1629
#endif
1583
- QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName ( this , tr ( " Choose a file name to save the map image as" ) );
1584
- if ( myFileNameAndFilter.first != " " )
1585
- {
1586
- myPixmap.save ( myFileNameAndFilter.first );
1587
- }
1588
1630
}
1631
+
1589
1632
void QgsRasterLayerProperties::on_pbnImportTransparentPixelValues_clicked ()
1590
1633
{
1591
1634
int myLineCounter = 0 ;
@@ -2242,7 +2285,11 @@ void QgsRasterLayerProperties::histoPickerSelected( const QPointF & pos )
2242
2285
}
2243
2286
if ( QApplication::overrideCursor () )
2244
2287
QApplication::restoreOverrideCursor ();
2288
+ }
2245
2289
2290
+ void QgsRasterLayerProperties::histoPickerSelectedQwt5 ( const QwtDoublePoint & pos )
2291
+ {
2292
+ histoPickerSelected ( QPointF ( pos.x (), pos.y () ) );
2246
2293
}
2247
2294
2248
2295
void QgsRasterLayerProperties::updateHistoMarkers ( )
@@ -2287,7 +2334,6 @@ void QgsRasterLayerProperties::updateHistoMarkers( )
2287
2334
mHistoMarkerMax ->show ();
2288
2335
2289
2336
mpPlot->replot ();
2290
-
2291
2337
}
2292
2338
2293
2339
0 commit comments