Skip to content

Commit

Permalink
histogram: qwt6 and file export fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Jul 15, 2012
1 parent ecaade2 commit d5e8745
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
90 changes: 68 additions & 22 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -72,6 +72,10 @@
#include <qwt_plot_picker.h>
#include <qwt_picker_machine.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_layout.h>
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
#include <qwt_plot_renderer.h>
#endif

QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanvas* theCanvas, QWidget *parent, Qt::WFlags fl )
: QDialog( parent, fl ),
Expand Down Expand Up @@ -1323,7 +1327,9 @@ void QgsRasterLayerProperties::refreshHistogram()
return;
}

#if !defined(QWT_VERSION) || QWT_VERSION<0x060000
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mpPlot->detachItems();
#else
mpPlot->clear();
#endif
//ensure all children get removed
Expand All @@ -1334,6 +1340,7 @@ void QgsRasterLayerProperties::refreshHistogram()
mpPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr( "Pixel Value" ) );
mpPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr( "Frequency" ) );
mpPlot->setAxisAutoScale( QwtPlot::yLeft );

// x axis scale only set after computing global min/max across bands (see below)
// add a grid
QwtPlotGrid * myGrid = new QwtPlotGrid();
Expand Down Expand Up @@ -1523,24 +1530,25 @@ void QgsRasterLayerProperties::refreshHistogram()
if ( ! mHistoPicker )
{
mHistoPicker = new QwtPlotPicker( mpPlot->canvas() );
#if (QWT_VERSION>=0x060000)
mHistoPicker->setStateMachine(new QwtPickerDragPointMachine);
#else
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
#endif
// mHistoPicker->setTrackerMode( QwtPicker::ActiveOnly );
mHistoPicker->setTrackerMode( QwtPicker::AlwaysOff );
mHistoPicker->setRubberBand( QwtPicker::VLineRubberBand );
mHistoPicker->setEnabled( false );
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelected( const QwtDoublePoint & ) ) );
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoPicker->setStateMachine( new QwtPickerDragPointMachine );
connect( mHistoPicker, SIGNAL( selected( const QPointF & ) ), this, SLOT( histoPickerSelected( const QPointF & ) ) );
#else
mHistoPicker->setSelectionFlags( QwtPicker::PointSelection | QwtPicker::DragSelection );
connect( mHistoPicker, SIGNAL( selected( const QwtDoublePoint & ) ), this, SLOT( histoPickerSelectedQwt5( const QwtDoublePoint & ) ) );
#endif
}

// plot zoomer
if ( ! mHistoZoomer )
{
mHistoZoomer = new QwtPlotZoomer( mpPlot->canvas() );
#if (QWT_VERSION>=0x060000)
mHistoZoomer->setStateMachine(new QwtPickerDragRectMachine);
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
mHistoZoomer->setStateMachine( new QwtPickerDragRectMachine );
#else
mHistoZoomer->setSelectionFlags( QwtPicker::RectSelection | QwtPicker::DragSelection );
#endif
Expand All @@ -1563,29 +1571,64 @@ void QgsRasterLayerProperties::on_mSaveAsImageButton_clicked()
return;
}

QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
QFileInfo myInfo( myFileNameAndFilter.first );
if ( QFileInfo( myFileNameAndFilter.first ).baseName() != "" )
{
histoSaveAsImage( myFileNameAndFilter.first );
}
}

void QgsRasterLayerProperties::histoSaveAsImage( const QString& theFilename )
{
// make sure dir. exists
QDir myDir( QFileInfo( theFilename ).dir() );
if ( ! myDir.exists() )
{
QgsDebugMsg( QString( "Error, directory %1 non-existent (theFilename = %2)" ).arg( myDir.absolutePath() ).arg( theFilename ) );
return;
}

// prepare the pixmap
QPixmap myPixmap( 600, 600 );
QRect myQRect( 5, 5, 590, 590 ); // leave a 5px border on all sides
myPixmap.fill( Qt::white ); // Qt::transparent ?

#if (QWT_VERSION<0x060000)
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
QwtPlotRenderer myRenderer;
myRenderer.setDiscardFlags( QwtPlotRenderer::DiscardBackground |
QwtPlotRenderer::DiscardCanvasBackground );
myRenderer.setLayoutFlags( QwtPlotRenderer::FrameWithScales );

QPainter myPainter;
myPainter.begin( &myPixmap );
myRenderer.render( mpPlot, &myPainter, myQRect );
myPainter.end();
#else
QwtPlotPrintFilter myFilter;
int myOptions = QwtPlotPrintFilter::PrintAll;
myOptions &= ~QwtPlotPrintFilter::PrintBackground;
myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
myFilter.setOptions( myOptions );

mpPlot->print( myPixmap, myFilter );
#else
QPainter painter;
painter.begin( &myPixmap );
mpPlot->drawCanvas( &painter );
painter.end();
QPainter myPainter;
myPainter.begin( &myPixmap );
mpPlot->print( &myPainter, myQRect, myFilter );
myPainter.end();

// "fix" for bug in qwt5 - legend and plot shifts a bit
// can't see how to avoid this without picking qwt5 apart...
refreshHistogram();
refreshHistogram();
#endif

// save pixmap to file
myPixmap.save( theFilename );

#if defined(QWT_VERSION) && QWT_VERSION<0x060000
#endif
QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
if ( myFileNameAndFilter.first != "" )
{
myPixmap.save( myFileNameAndFilter.first );
}
}

void QgsRasterLayerProperties::on_pbnImportTransparentPixelValues_clicked()
{
int myLineCounter = 0;
Expand Down Expand Up @@ -2242,7 +2285,11 @@ void QgsRasterLayerProperties::histoPickerSelected( const QPointF & pos )
}
if ( QApplication::overrideCursor() )
QApplication::restoreOverrideCursor();
}

void QgsRasterLayerProperties::histoPickerSelectedQwt5( const QwtDoublePoint & pos )
{
histoPickerSelected( QPointF( pos.x(), pos.y() ) );
}

void QgsRasterLayerProperties::updateHistoMarkers( )
Expand Down Expand Up @@ -2287,7 +2334,6 @@ void QgsRasterLayerProperties::updateHistoMarkers( )
mHistoMarkerMax->show();

mpPlot->replot();

}


Expand Down
11 changes: 11 additions & 0 deletions src/app/qgsrasterlayerproperties.h
Expand Up @@ -39,6 +39,11 @@ class QwtPlotZoomer;
*@author Tim Sutton
*/

// fix for qwt5/qwt6 QwtDoublePoint vs. QPointF
#if defined(QWT_VERSION) && QWT_VERSION>=0x060000
typedef QPointF QwtDoublePoint;
#endif

class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPropertiesBase
{
Q_OBJECT
Expand All @@ -54,6 +59,9 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
/** synchronize state with associated raster layer */
void sync();

/** Save the histogram as an image to disk */
void histoSaveAsImage( const QString& theFilename );

public slots:
//TODO: Verify that these all need to be public
/** \brief Applies the settings made in the dialog without closing the box */
Expand Down Expand Up @@ -115,6 +123,8 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
void on_btnHistoMax_toggled();
/** Called when a selection has been made using the plot picker. */
void histoPickerSelected( const QPointF & );
/** Called when a selection has been made using the plot picker (for qwt5 only). */
void histoPickerSelectedQwt5( const QwtDoublePoint & );
/** Various actions that are stored in btnHistoActions. */
void histoActionTriggered( QAction* );
/** Draw the min/max markers on the histogram plot. */
Expand Down Expand Up @@ -178,6 +188,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
QgsMapToolEmitPoint* mPixelSelectorTool;

// histogram

QwtPlotPicker* mHistoPicker;
QwtPlotZoomer* mHistoZoomer;
QwtPlotMarker* mHistoMarkerMin;
Expand Down

0 comments on commit d5e8745

Please sign in to comment.