Skip to content

Commit d5e8745

Browse files
committed
histogram: qwt6 and file export fixes
1 parent ecaade2 commit d5e8745

File tree

2 files changed

+79
-22
lines changed

2 files changed

+79
-22
lines changed

src/app/qgsrasterlayerproperties.cpp

+68-22
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
#include <qwt_plot_picker.h>
7373
#include <qwt_picker_machine.h>
7474
#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
7579

7680
QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanvas* theCanvas, QWidget *parent, Qt::WFlags fl )
7781
: QDialog( parent, fl ),
@@ -1323,7 +1327,9 @@ void QgsRasterLayerProperties::refreshHistogram()
13231327
return;
13241328
}
13251329

1326-
#if !defined(QWT_VERSION) || QWT_VERSION<0x060000
1330+
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
1331+
mpPlot->detachItems();
1332+
#else
13271333
mpPlot->clear();
13281334
#endif
13291335
//ensure all children get removed
@@ -1334,6 +1340,7 @@ void QgsRasterLayerProperties::refreshHistogram()
13341340
mpPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr( "Pixel Value" ) );
13351341
mpPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr( "Frequency" ) );
13361342
mpPlot->setAxisAutoScale( QwtPlot::yLeft );
1343+
13371344
// x axis scale only set after computing global min/max across bands (see below)
13381345
// add a grid
13391346
QwtPlotGrid * myGrid = new QwtPlotGrid();
@@ -1523,24 +1530,25 @@ void QgsRasterLayerProperties::refreshHistogram()
15231530
if ( ! mHistoPicker )
15241531
{
15251532
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
15311533
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
15321534
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
15331535
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
15341536
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
15361544
}
15371545

15381546
// plot zoomer
15391547
if ( ! mHistoZoomer )
15401548
{
15411549
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 );
15441552
#else
15451553
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
15461554
#endif
@@ -1563,29 +1571,64 @@ void QgsRasterLayerProperties::on_mSaveAsImageButton_clicked()
15631571
return;
15641572
}
15651573

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
15661593
QPixmap myPixmap( 600, 600 );
1594+
QRect myQRect( 5, 5, 590, 590 ); // leave a 5px border on all sides
15671595
myPixmap.fill( Qt::white ); // Qt::transparent ?
15681596

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
15701608
QwtPlotPrintFilter myFilter;
15711609
int myOptions = QwtPlotPrintFilter::PrintAll;
15721610
myOptions &= ~QwtPlotPrintFilter::PrintBackground;
15731611
myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
15741612
myFilter.setOptions( myOptions );
15751613

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
15821629
#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-
}
15881630
}
1631+
15891632
void QgsRasterLayerProperties::on_pbnImportTransparentPixelValues_clicked()
15901633
{
15911634
int myLineCounter = 0;
@@ -2242,7 +2285,11 @@ void QgsRasterLayerProperties::histoPickerSelected( const QPointF & pos )
22422285
}
22432286
if ( QApplication::overrideCursor() )
22442287
QApplication::restoreOverrideCursor();
2288+
}
22452289

2290+
void QgsRasterLayerProperties::histoPickerSelectedQwt5( const QwtDoublePoint & pos )
2291+
{
2292+
histoPickerSelected( QPointF( pos.x(), pos.y() ) );
22462293
}
22472294

22482295
void QgsRasterLayerProperties::updateHistoMarkers( )
@@ -2287,7 +2334,6 @@ void QgsRasterLayerProperties::updateHistoMarkers( )
22872334
mHistoMarkerMax->show();
22882335

22892336
mpPlot->replot();
2290-
22912337
}
22922338

22932339

src/app/qgsrasterlayerproperties.h

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class QwtPlotZoomer;
3939
*@author Tim Sutton
4040
*/
4141

42+
// fix for qwt5/qwt6 QwtDoublePoint vs. QPointF
43+
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
44+
typedef QPointF QwtDoublePoint;
45+
#endif
46+
4247
class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPropertiesBase
4348
{
4449
Q_OBJECT
@@ -54,6 +59,9 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
5459
/** synchronize state with associated raster layer */
5560
void sync();
5661

62+
/** Save the histogram as an image to disk */
63+
void histoSaveAsImage( const QString& theFilename );
64+
5765
public slots:
5866
//TODO: Verify that these all need to be public
5967
/** \brief Applies the settings made in the dialog without closing the box */
@@ -115,6 +123,8 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
115123
void on_btnHistoMax_toggled();
116124
/** Called when a selection has been made using the plot picker. */
117125
void histoPickerSelected( const QPointF & );
126+
/** Called when a selection has been made using the plot picker (for qwt5 only). */
127+
void histoPickerSelectedQwt5( const QwtDoublePoint & );
118128
/** Various actions that are stored in btnHistoActions. */
119129
void histoActionTriggered( QAction* );
120130
/** Draw the min/max markers on the histogram plot. */
@@ -178,6 +188,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
178188
QgsMapToolEmitPoint* mPixelSelectorTool;
179189

180190
// histogram
191+
181192
QwtPlotPicker* mHistoPicker;
182193
QwtPlotZoomer* mHistoZoomer;
183194
QwtPlotMarker* mHistoMarkerMin;

0 commit comments

Comments
 (0)