Skip to content
Permalink
Browse files

histogram: qwt6 and file export fixes

  • Loading branch information
etiennesky committed Jul 15, 2012
1 parent ecaade2 commit d5e87457bf919e0f702ea6f592778309de62de8e
Showing with 79 additions and 22 deletions.
  1. +68 −22 src/app/qgsrasterlayerproperties.cpp
  2. +11 −0 src/app/qgsrasterlayerproperties.h
@@ -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 ),
@@ -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
@@ -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();
@@ -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
@@ -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;
@@ -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( )
@@ -2287,7 +2334,6 @@ void QgsRasterLayerProperties::updateHistoMarkers( )
mHistoMarkerMax->show();

mpPlot->replot();

}


@@ -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
@@ -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 */
@@ -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. */
@@ -178,6 +188,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
QgsMapToolEmitPoint* mPixelSelectorTool;

// histogram

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

0 comments on commit d5e8745

Please sign in to comment.
You can’t perform that action at this time.